Skip to content

Commit dd9251d

Browse files
committed
Use std::filesystem for paths
Signed-off-by: Artem Senichev <artemsen@gmail.com>
1 parent a55be22 commit dd9251d

5 files changed

Lines changed: 24 additions & 20 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ List of supported distributives can be found on the [Repology page](https://repo
1515
## Build
1616

1717
To build the project you will need:
18-
- C++ compiler with support for the C++11 standard;
18+
- C++ compiler with support for the C++20 standard;
1919
- Meson build system;
2020
- SDL3 library.
2121

meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ project(
55
'cpp',
66
default_options: [
77
'warning_level=3',
8-
'cpp_std=c++11',
8+
'cpp_std=c++20',
99
'buildtype=release',
1010
],
1111
license: 'MIT',

src/sound.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include <SDL3/SDL.h>
1010

11-
#include <string>
11+
#include <cstring>
1212

1313
// The only supported audio format
1414
constexpr const SDL_AudioSpec sound_spec = { SDL_AUDIO_S16, 2, 44100 };
@@ -41,8 +41,8 @@ bool Sound::initialize()
4141
// load wave files
4242
if (!load(APP_DATADIR)) {
4343
// try portable variant
44-
std::string path(SDL_GetBasePath());
45-
path += "data";
44+
std::filesystem::path path(SDL_GetBasePath());
45+
path /= "data";
4646
if (!load(path.c_str())) {
4747
return false;
4848
}
@@ -59,32 +59,32 @@ void Sound::play(Sound::Type type)
5959
}
6060
}
6161

62-
bool Sound::load(const char* dir)
62+
bool Sound::load(const std::filesystem::path& dir)
6363
{
64-
memset(waves, 0, sizeof(waves));
65-
6664
for (size_t i = 0; i < sizeof(waves) / sizeof(waves[0]); ++i) {
6765
SDL_AudioSpec spec;
68-
std::string file = dir;
66+
std::filesystem::path file = dir;
6967
switch (i) {
7068
case Clatz:
71-
file += "clatz.wav";
69+
file /= "clatz.wav";
7270
break;
7371
case Complete:
74-
file += "complete.wav";
72+
file /= "complete.wav";
7573
break;
7674
}
77-
if (!SDL_LoadWAV(file.c_str(), &spec, &waves[i].data, &waves[i].size)) {
75+
const std::string wav_path = file.string();
76+
if (!SDL_LoadWAV(wav_path.c_str(), &spec, &waves[i].data,
77+
&waves[i].size)) {
7878
SDL_LogError(SDL_LOG_CATEGORY_AUDIO, "Error loading wav %s: %s",
79-
file.c_str(), SDL_GetError());
79+
wav_path.c_str(), SDL_GetError());
8080
break;
8181
}
8282
if (spec.format != sound_spec.format ||
8383
spec.channels != sound_spec.channels ||
8484
spec.freq != sound_spec.freq) {
8585
SDL_LogError(SDL_LOG_CATEGORY_AUDIO,
8686
"Error loading wav %s: unsupported format",
87-
file.c_str());
87+
wav_path.c_str());
8888
break;
8989
}
9090
}
@@ -93,7 +93,8 @@ bool Sound::load(const char* dir)
9393
SDL_free(waves[Clatz].data);
9494
SDL_free(waves[Complete].data);
9595
memset(waves, 0, sizeof(waves));
96+
return false;
9697
}
9798

98-
return waves[Clatz].data && waves[Complete].data;
99+
return true;
99100
}

src/sound.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
#include <SDL3/SDL.h>
88

9+
#include <filesystem>
10+
911
/** Sound subsystem. */
1012
class Sound {
1113
public:
@@ -37,13 +39,13 @@ class Sound {
3739
* @param dir path to directory with sound files (wav)
3840
* @return false if load failed
3941
*/
40-
bool load(const char* dir);
42+
bool load(const std::filesystem::path& dir);
4143

4244
struct Wave {
4345
Uint8* data; ///< Plain wave data
4446
Uint32 size; ///< Size of wave data
4547
};
46-
Wave waves[2]; ///< Wav data instances
48+
Wave waves[2] = {}; ///< Wav data instances
4749

4850
SDL_AudioStream* stream; ///< Output audio stream
4951
};

src/state.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <SDL3/SDL.h>
88

99
#include <cstring>
10+
#include <filesystem>
1011
#include <vector>
1112

1213
#include "level.hpp"
@@ -35,10 +36,10 @@ class IniFile {
3536
{
3637
char* dir = SDL_GetPrefPath(nullptr, app_name);
3738
if (dir) {
38-
std::string path = dir;
39-
path += state_file;
39+
std::filesystem::path path(dir);
40+
path /= state_file;
4041
SDL_free(dir);
41-
io = SDL_IOFromFile(path.c_str(), readonly ? "rb" : "wb");
42+
io = SDL_IOFromFile(path.string().c_str(), readonly ? "rb" : "wb");
4243
}
4344
}
4445

0 commit comments

Comments
 (0)