@@ -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
108109SDL_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}
0 commit comments