Skip to content

Commit 989dbd2

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

8 files changed

Lines changed: 65 additions & 63 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/game.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ void Game::save(State& state) const
159159
state.level_height = level.height;
160160
state.level_wrap = level.wrap;
161161
state.level_pipes = level.save();
162-
state.skin = skin.name;
162+
state.skin = skin.name();
163163
state.sound = sound.enable;
164164
}
165165

@@ -335,8 +335,8 @@ void Game::draw_settings()
335335
render.draw_text(skin_label, font_sz,
336336
layout.skinprev->x + layout.skinprev->w,
337337
layout.skinprev->y);
338-
width = render.text_width(skin.name.c_str(), font_sz);
339-
render.draw_text(skin.name.c_str(), font_sz,
338+
width = render.text_width(skin.name().c_str(), font_sz);
339+
render.draw_text(skin.name().c_str(), font_sz,
340340
layout.window.w / 2 - width / 2,
341341
layout.skinprev->y + layout.skinprev->h * 1.2);
342342
render.draw(Render::ButtonPrev, layout.skinprev);

src/skin.cpp

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,26 @@ SDL_Surface* Skin::initialize(const std::string& name)
5252

5353
if (!search(APP_DATADIR)) {
5454
// try portable variant
55-
const char* app_dir = SDL_GetBasePath();
56-
if (app_dir) {
57-
std::string path = app_dir;
58-
path += "data";
59-
search(path);
60-
}
55+
std::filesystem::path path(SDL_GetBasePath());
56+
path /= "data";
57+
search(path);
6158
}
6259

6360
// search for specified skin
64-
for (size_t i = 0; i < available.size(); ++i) {
65-
const std::string& path = available[i];
66-
if (name == get_name(path)) {
61+
for (size_t i = 0; i < skins.size(); ++i) {
62+
if (name == skins[i].stem()) {
6763
image = load(i);
6864
break;
6965
}
7066
}
71-
// fallback: load first available
72-
for (size_t i = 0; !image && i < available.size(); ++i) {
73-
image = load(i);
67+
if (!image) {
68+
// fallback: load first available
69+
for (size_t i = 0; i < skins.size(); ++i) {
70+
image = load(i);
71+
if (image) {
72+
break;
73+
}
74+
}
7475
}
7576

7677
return image;
@@ -80,11 +81,11 @@ SDL_Surface* Skin::prev()
8081
{
8182
SDL_Surface* image = nullptr;
8283

83-
for (ssize_t i = current - 1; !image && i >= 0; --i) {
84+
for (ssize_t i = skin_index - 1; !image && i >= 0; --i) {
8485
image = load(i);
8586
}
86-
for (ssize_t i = available.size() - 1;
87-
!image && i > static_cast<ssize_t>(current); --i) {
87+
for (ssize_t i = skins.size() - 1;
88+
!image && i > static_cast<ssize_t>(skin_index); --i) {
8889
image = load(i);
8990
}
9091

@@ -95,10 +96,10 @@ SDL_Surface* Skin::next()
9596
{
9697
SDL_Surface* image = nullptr;
9798

98-
for (size_t i = current + 1; !image && i < available.size(); ++i) {
99+
for (size_t i = skin_index + 1; !image && i < skins.size(); ++i) {
99100
image = load(i);
100101
}
101-
for (size_t i = 0; !image && i < current; ++i) {
102+
for (size_t i = 0; !image && i < skin_index; ++i) {
102103
image = load(i);
103104
}
104105

@@ -107,30 +108,28 @@ SDL_Surface* Skin::next()
107108

108109
SDL_Surface* Skin::load(size_t index)
109110
{
110-
const std::string& path = available[index];
111-
SDL_Surface* image = load_png(path.c_str());
111+
const std::filesystem::path& path = skins[index];
112+
SDL_Surface* image = load_png(path.string().c_str());
112113
if (image) {
113-
name = get_name(path);
114-
current = index;
114+
skin_name = path.stem();
115+
skin_index = index;
115116
}
116117
return image;
117118
}
118119

119-
bool Skin::search(const std::string& path)
120+
bool Skin::search(const std::filesystem::path& dir)
120121
{
121122
int count;
122-
char** files = SDL_GlobDirectory(path.c_str(), "*.png", 0, &count);
123+
char** files = SDL_GlobDirectory(dir.string().c_str(), "*.png", 0, &count);
123124
for (int i = 0; i < count; ++i) {
124-
const std::string skin_path = path + files[i];
125-
available.push_back(skin_path);
125+
std::filesystem::path path = dir / files[i];
126+
skins.push_back(path);
126127
}
127128
SDL_free(files);
128129
return count > 0;
129130
}
130131

131-
std::string Skin::get_name(const std::string& path) const
132+
const std::string& Skin::name() const
132133
{
133-
const size_t dir_end = path.find_last_of("\\/") + 1;
134-
const size_t ext_start = path.rfind('.');
135-
return path.substr(dir_end, ext_start - dir_end);
134+
return skin_name;
136135
}

src/skin.hpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <SDL3/SDL.h>
88

9+
#include <filesystem>
910
#include <string>
1011
#include <vector>
1112

@@ -31,15 +32,19 @@ class Skin {
3132
*/
3233
SDL_Surface* next();
3334

34-
std::string name; ///< Skin name
35+
/**
36+
* Get current skin name.
37+
* @return skin name
38+
*/
39+
const std::string& name() const;
3540

3641
private:
3742
/**
3843
* Search skin files in the specified directory.
3944
* @param dir path to directory with skin files (png)
4045
* @return true if at least one skin was found
4146
*/
42-
bool search(const std::string& dir);
47+
bool search(const std::filesystem::path& dir);
4348

4449
/**
4550
* Load skin.
@@ -48,13 +53,7 @@ class Skin {
4853
*/
4954
SDL_Surface* load(size_t index);
5055

51-
/**
52-
* Get skin name from file path.
53-
* @param path path to the skin file
54-
* @return skin name (file name without dir and extension)
55-
*/
56-
std::string get_name(const std::string& path) const;
57-
58-
std::vector<std::string> available; ///< List of available skins
59-
size_t current; ///< Index of the current skin
56+
std::vector<std::filesystem::path> skins; ///< List of available skins
57+
size_t skin_index; ///< Index of the current skin
58+
std::string skin_name; ///< Name of the current skin
6059
};

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)