ソースを参照

Menu background now chosen randomly

Thomas Buck 10年前
コミット
28c15c890e

+ 6
- 0
ChangeLog.md ファイルの表示

2
 
2
 
3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4
 
4
 
5
+    [ 20141230 ]
6
+    * Added random number generation utility
7
+    * Added file name searching utilities
8
+    * Finally made file name comparisons case-insensitive
9
+    * Splash screen now chosen randomly from all PCX images in pak folder
10
+
5
     [ 20141229 ]
11
     [ 20141229 ]
6
     * Removed utils/png, utils/tga image readers
12
     * Removed utils/png, utils/tga image readers
7
     * Included current stb_image, stb_image_write and stb_textedit
13
     * Included current stb_image, stb_image_write and stb_textedit

+ 2
- 2
include/TextureManager.h ファイルの表示

94
                        TextureStorage s = TextureStorage::GAME,
94
                        TextureStorage s = TextureStorage::GAME,
95
                        int slot = -1, bool filter = true);
95
                        int slot = -1, bool filter = true);
96
 
96
 
97
-    int loadImage(const char* filename, TextureStorage s = TextureStorage::GAME, int slot = -1);
97
+    int loadImage(std::string filename, TextureStorage s = TextureStorage::GAME, int slot = -1);
98
 
98
 
99
     void addTile(TextureTile* t);
99
     void addTile(TextureTile* t);
100
     int numTiles();
100
     int numTiles();
107
 
107
 
108
   private:
108
   private:
109
     std::vector<unsigned int>& getIds(TextureStorage s);
109
     std::vector<unsigned int>& getIds(TextureStorage s);
110
-    int loadPCX(const char* filename, TextureStorage s, int slot);
110
+    int loadPCX(std::string filename, TextureStorage s, int slot);
111
 
111
 
112
     std::vector<unsigned int> mTextureIdsGame;
112
     std::vector<unsigned int> mTextureIdsGame;
113
     std::vector<unsigned int> mTextureIdsSystem;
113
     std::vector<unsigned int> mTextureIdsSystem;

+ 0
- 28
include/utils/File.h ファイルの表示

1
-/*!
2
- * \file include/utils/File.h
3
- * \brief Recursive file-system walking utilities
4
- *
5
- * \author xythobuz
6
- */
7
-
8
-#ifndef _UTILS_FILE_H_
9
-#define _UTILS_FILE_H_
10
-
11
-#include <string>
12
-
13
-class Folder;
14
-
15
-class File {
16
-  public:
17
-    File(std::string file);
18
-
19
-    std::string& getName();
20
-    std::string& getPath();
21
-
22
-  private:
23
-    std::string name;
24
-    std::string path;
25
-};
26
-
27
-#endif
28
-

+ 16
- 4
include/utils/Folder.h ファイルの表示

9
 #define _UTILS_FOLDER_H_
9
 #define _UTILS_FOLDER_H_
10
 
10
 
11
 #include "Exception.h"
11
 #include "Exception.h"
12
-#include "utils/File.h"
13
 
12
 
14
 #include <functional>
13
 #include <functional>
15
 #include <memory>
14
 #include <memory>
16
 #include <string>
15
 #include <string>
17
 #include <vector>
16
 #include <vector>
18
 
17
 
18
+class File {
19
+  public:
20
+    File(std::string file);
21
+
22
+    std::string& getName() { return name; }
23
+    std::string& getPath() { return path; }
24
+
25
+  private:
26
+    std::string name;
27
+    std::string path;
28
+};
29
+
19
 class Folder {
30
 class Folder {
20
   public:
31
   public:
21
     Folder(std::string folder, bool listDotFiles = false);
32
     Folder(std::string folder, bool listDotFiles = false);
22
 
33
 
23
-    std::string& getName();
24
-    std::string& getPath();
34
+    std::string& getName() { return name; }
35
+    std::string& getPath() { return path; }
25
 
36
 
26
     unsigned long fileCount();
37
     unsigned long fileCount();
27
     File& getFile(unsigned long i);
38
     File& getFile(unsigned long i);
32
     Folder getParent();
43
     Folder getParent();
33
 
44
 
34
     void executeRemoveFiles(std::function<bool (File& f)> func);
45
     void executeRemoveFiles(std::function<bool (File& f)> func);
46
+    void findFilesEndingWith(std::vector<File>& found, std::string end, bool casesensitive = false);
35
 
47
 
36
     // Accessing a folder recursively
48
     // Accessing a folder recursively
37
     // This treats all files in all subfolders as if they were in this folder
49
     // This treats all files in all subfolders as if they were in this folder
39
     void executeRemoveRecursiveFiles(std::function<bool (File& f)> func);
51
     void executeRemoveRecursiveFiles(std::function<bool (File& f)> func);
40
     std::string getRecursiveFileName(unsigned long i);
52
     std::string getRecursiveFileName(unsigned long i);
41
     File& getRecursiveFile(unsigned long i);
53
     File& getRecursiveFile(unsigned long i);
54
+    void findRecursiveFilesEndingWith(std::vector<File>& found, std::string end, bool casesensitive = false);
42
 
55
 
43
   private:
56
   private:
44
-
45
     void createFolderItems();
57
     void createFolderItems();
46
     int readFolderItems(std::vector<std::string>& foundFiles, std::vector<std::string>& foundFolders);
58
     int readFolderItems(std::vector<std::string>& foundFiles, std::vector<std::string>& foundFolders);
47
 
59
 

+ 14
- 0
include/utils/random.h ファイルの表示

1
+/*!
2
+ * \file include/utils/random.h
3
+ * \brief Random number generation
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _UTILS_RANDOM_H_
9
+#define _UTILS_RANDOM_H_
10
+
11
+int randomInteger(int max, int min = 0);
12
+
13
+#endif
14
+

+ 1
- 1
include/utils/strings.h ファイルの表示

14
 
14
 
15
 std::string expandHomeDirectory(std::string s);
15
 std::string expandHomeDirectory(std::string s);
16
 
16
 
17
-bool stringEndsWith(std::string s, std::string suffix);
17
+bool stringEndsWith(std::string s, std::string suffix, bool casesensitive = false);
18
 
18
 
19
 #endif
19
 #endif
20
 
20
 

+ 20
- 14
src/TextureManager.cpp ファイルの表示

16
 #include "global.h"
16
 #include "global.h"
17
 #include "Log.h"
17
 #include "Log.h"
18
 #include "RunTime.h"
18
 #include "RunTime.h"
19
+#include "utils/Folder.h"
19
 #include "utils/pcx.h"
20
 #include "utils/pcx.h"
20
 #include "utils/pixel.h"
21
 #include "utils/pixel.h"
22
+#include "utils/random.h"
21
 #include "utils/strings.h"
23
 #include "utils/strings.h"
22
 #include "TextureManager.h"
24
 #include "TextureManager.h"
23
 
25
 
145
         return -1;
147
         return -1;
146
     }
148
     }
147
 
149
 
148
-    //! \fixme Temporary?
149
-    std::string filename = RunTime::getPakDir() + "/tr2/TITLE.PCX";
150
-    if (loadImage(filename.c_str(), TextureStorage::SYSTEM, TEXTURE_SPLASH) < 0) {
151
-        filename = RunTime::getDataDir() + "/splash.tga";
152
-        if (loadImage(filename.c_str(), TextureStorage::SYSTEM, TEXTURE_SPLASH) < 0) {
150
+    Folder f(RunTime::getPakDir());
151
+    std::vector<File> files;
152
+    f.findRecursiveFilesEndingWith(files, ".pcx");
153
+    if (files.size() == 0) {
154
+        if (loadImage(RunTime::getDataDir() + "/splash.tga", TextureStorage::SYSTEM, TEXTURE_SPLASH) < 0) {
153
             return -2;
155
             return -2;
154
         }
156
         }
157
+    } else {
158
+        int i = randomInteger(files.size() - 1);
159
+        if (loadImage(files.at(i).getPath(), TextureStorage::SYSTEM, TEXTURE_SPLASH) < 0) {
160
+            if (loadImage(RunTime::getDataDir() + "/splash.tga", TextureStorage::SYSTEM, TEXTURE_SPLASH) < 0) {
161
+                return -3;
162
+            }
163
+        }
155
     }
164
     }
156
 
165
 
157
     return 0;
166
     return 0;
237
     glBindTexture(GL_TEXTURE_2D, getIds(s).at(n));
246
     glBindTexture(GL_TEXTURE_2D, getIds(s).at(n));
238
 }
247
 }
239
 
248
 
240
-int TextureManager::loadImage(const char* filename, TextureStorage s, int slot) {
249
+int TextureManager::loadImage(std::string filename, TextureStorage s, int slot) {
241
     //! \todo case insensitive compare
250
     //! \todo case insensitive compare
242
 
251
 
243
-    if (stringEndsWith(filename, ".pcx") || stringEndsWith(filename, ".PCX")) {
252
+    if (stringEndsWith(filename, ".pcx")) {
244
         return loadPCX(filename, s, slot);
253
         return loadPCX(filename, s, slot);
245
     } else {
254
     } else {
246
         int x, y, n;
255
         int x, y, n;
247
-        unsigned char* data = stbi_load(filename, &x, &y, &n, 0);
256
+        unsigned char* data = stbi_load(filename.c_str(), &x, &y, &n, 0);
248
         if (data) {
257
         if (data) {
249
             if ((n < 3) || (n > 4)) {
258
             if ((n < 3) || (n > 4)) {
250
                 getLog() << "Image \"" << filename << "\" has unsupported format ("
259
                 getLog() << "Image \"" << filename << "\" has unsupported format ("
263
     }
272
     }
264
 }
273
 }
265
 
274
 
266
-int TextureManager::loadPCX(const char* filename, TextureStorage s, int slot) {
267
-    assert(filename != nullptr);
268
-    assert(filename[0] != '\0');
269
-
270
-    int error = pcxCheck(filename);
275
+int TextureManager::loadPCX(std::string filename, TextureStorage s, int slot) {
276
+    int error = pcxCheck(filename.c_str());
271
     if (!error) {
277
     if (!error) {
272
         unsigned char* image;
278
         unsigned char* image;
273
         unsigned int w, h, bpp;
279
         unsigned int w, h, bpp;
274
         ColorMode c;
280
         ColorMode c;
275
 
281
 
276
-        error = pcxLoad(filename, &image, &w, &h, &c, &bpp);
282
+        error = pcxLoad(filename.c_str(), &image, &w, &h, &c, &bpp);
277
         if (!error) {
283
         if (!error) {
278
             unsigned char* image2 = scaleBuffer(image, &w, &h, bpp);
284
             unsigned char* image2 = scaleBuffer(image, &w, &h, bpp);
279
             if (image2) {
285
             if (image2) {

+ 1
- 1
src/utils/CMakeLists.txt ファイルの表示

1
 # Source files
1
 # Source files
2
 set (UTIL_SRCS ${UTIL_SRCS} "binary.cpp" "../../include/utils/binary.h")
2
 set (UTIL_SRCS ${UTIL_SRCS} "binary.cpp" "../../include/utils/binary.h")
3
-set (UTIL_SRCS ${UTIL_SRCS} "File.cpp" "../../include/utils/File.h")
4
 set (UTIL_SRCS ${UTIL_SRCS} "filesystem.cpp" "../../include/utils/filesystem.h")
3
 set (UTIL_SRCS ${UTIL_SRCS} "filesystem.cpp" "../../include/utils/filesystem.h")
5
 set (UTIL_SRCS ${UTIL_SRCS} "Folder.cpp" "../../include/utils/Folder.h")
4
 set (UTIL_SRCS ${UTIL_SRCS} "Folder.cpp" "../../include/utils/Folder.h")
6
 set (UTIL_SRCS ${UTIL_SRCS} "FolderRecursive.cpp")
5
 set (UTIL_SRCS ${UTIL_SRCS} "FolderRecursive.cpp")
7
 set (UTIL_SRCS ${UTIL_SRCS} "pcx.cpp" "../../include/utils/pcx.h")
6
 set (UTIL_SRCS ${UTIL_SRCS} "pcx.cpp" "../../include/utils/pcx.h")
8
 set (UTIL_SRCS ${UTIL_SRCS} "pixel.cpp" "../../include/utils/pixel.h")
7
 set (UTIL_SRCS ${UTIL_SRCS} "pixel.cpp" "../../include/utils/pixel.h")
8
+set (UTIL_SRCS ${UTIL_SRCS} "random.cpp" "../../include/utils/random.h")
9
 set (UTIL_SRCS ${UTIL_SRCS} "strings.cpp" "../../include/utils/strings.h")
9
 set (UTIL_SRCS ${UTIL_SRCS} "strings.cpp" "../../include/utils/strings.h")
10
 set (UTIL_SRCS ${UTIL_SRCS} "time.cpp" "../../include/utils/time.h")
10
 set (UTIL_SRCS ${UTIL_SRCS} "time.cpp" "../../include/utils/time.h")
11
 
11
 

+ 0
- 27
src/utils/File.cpp ファイルの表示

1
-/*!
2
- * \file src/utils/File.cpp
3
- * \brief Recursive file-system walking utilities
4
- *
5
- * \author xythobuz
6
- */
7
-
8
-#include <algorithm>
9
-
10
-#include "global.h"
11
-#include "utils/File.h"
12
-#include "utils/Folder.h"
13
-
14
-File::File(std::string file) : path(file) {
15
-    size_t pos = file.rfind('/', file.length() - 2);
16
-    name = file.substr(pos + 1);
17
-    std::transform(name.begin(), name.end(), name.begin(), ::tolower);
18
-}
19
-
20
-std::string& File::getName() {
21
-    return name;
22
-}
23
-
24
-std::string& File::getPath() {
25
-    return path;
26
-}
27
-

+ 17
- 9
src/utils/Folder.cpp ファイルの表示

12
 
12
 
13
 #include "global.h"
13
 #include "global.h"
14
 #include "utils/filesystem.h"
14
 #include "utils/filesystem.h"
15
-#include "utils/File.h"
15
+#include "utils/strings.h"
16
 #include "utils/Folder.h"
16
 #include "utils/Folder.h"
17
 
17
 
18
+File::File(std::string file) : path(file) {
19
+    size_t pos = file.rfind('/', file.length() - 2);
20
+    name = file.substr(pos + 1);
21
+    std::transform(name.begin(), name.end(), name.begin(), ::tolower);
22
+}
23
+
24
+// ----------------------------------------------------------------------------
25
+
18
 #if defined(HAVE_DIRENT_H) && defined(HAVE_OPENDIR) && defined(HAVE_READDIR_R) && defined(HAVE_CLOSEDIR) && defined(HAVE_DT_DIR)
26
 #if defined(HAVE_DIRENT_H) && defined(HAVE_OPENDIR) && defined(HAVE_READDIR_R) && defined(HAVE_CLOSEDIR) && defined(HAVE_DT_DIR)
19
 #include <dirent.h>
27
 #include <dirent.h>
20
 #define USE_DIRENT
28
 #define USE_DIRENT
70
     listDot = listDotFiles;
78
     listDot = listDotFiles;
71
 }
79
 }
72
 
80
 
73
-std::string& Folder::getName() {
74
-    return name;
75
-}
76
-
77
-std::string& Folder::getPath() {
78
-    return path;
79
-}
80
-
81
 unsigned long Folder::fileCount() {
81
 unsigned long Folder::fileCount() {
82
     createFolderItems();
82
     createFolderItems();
83
     return files.size();
83
     return files.size();
220
 
220
 
221
 #endif
221
 #endif
222
 
222
 
223
+void Folder::findFilesEndingWith(std::vector<File>& found, std::string end, bool casesensitive) {
224
+    createFolderItems();
225
+    for (auto& f : files) {
226
+        if (stringEndsWith(f.getName(), end, casesensitive))
227
+            found.push_back(f);
228
+    }
229
+}
230
+

+ 10
- 1
src/utils/FolderRecursive.cpp ファイルの表示

6
  */
6
  */
7
 
7
 
8
 #include "global.h"
8
 #include "global.h"
9
-#include "utils/File.h"
9
+#include "utils/strings.h"
10
 #include "utils/Folder.h"
10
 #include "utils/Folder.h"
11
 
11
 
12
 unsigned long Folder::countRecursiveFiles() {
12
 unsigned long Folder::countRecursiveFiles() {
63
     return files.at(0);
63
     return files.at(0);
64
 }
64
 }
65
 
65
 
66
+void Folder::findRecursiveFilesEndingWith(std::vector<File>& found, std::string end, bool casesensitive) {
67
+    createFolderItems();
68
+    for (unsigned long i = 0; i < countRecursiveFiles(); i++) {
69
+        if (stringEndsWith(getRecursiveFile(i).getName(), end, casesensitive)) {
70
+            found.push_back(getRecursiveFile(i));
71
+        }
72
+    }
73
+}
74
+

+ 40
- 0
src/utils/random.cpp ファイルの表示

1
+/*!
2
+ * \file src/utils/random.cpp
3
+ * \brief Random number generation
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#include <chrono>
9
+#include <map>
10
+#include <random>
11
+#include <utility>
12
+
13
+#include "global.h"
14
+#include "utils/random.h"
15
+
16
+static std::map<int, std::uniform_int_distribution<int>> distributions;
17
+static std::default_random_engine engine;
18
+static bool engineIsSeeded = false;
19
+
20
+int randomInteger(int max, int min) {
21
+    if (max == min)
22
+        return max;
23
+
24
+    assert(max > min);
25
+
26
+    if (!engineIsSeeded) {
27
+        engine.seed(std::chrono::system_clock::now().time_since_epoch().count());
28
+        engineIsSeeded = true;
29
+    }
30
+
31
+    int range = max - min;
32
+    auto elem = distributions.find(range);
33
+    if (elem == distributions.end()) {
34
+        distributions[range] = std::uniform_int_distribution<int>(0, range);
35
+        return distributions[range](engine) + min;
36
+    } else {
37
+        return std::get<1>(*elem)(engine) + min;
38
+    }
39
+}
40
+

+ 8
- 1
src/utils/strings.cpp ファイルの表示

5
  * \author xythobuz
5
  * \author xythobuz
6
  */
6
  */
7
 
7
 
8
+#include <algorithm>
9
+
8
 #include "global.h"
10
 #include "global.h"
9
 #include "utils/filesystem.h"
11
 #include "utils/filesystem.h"
10
 #include "utils/strings.h"
12
 #include "utils/strings.h"
27
     return s;
29
     return s;
28
 }
30
 }
29
 
31
 
30
-bool stringEndsWith(std::string s, std::string suffix) {
32
+bool stringEndsWith(std::string s, std::string suffix, bool casesensitive) {
33
+    if (!casesensitive) {
34
+        std::transform(s.begin(), s.end(), s.begin(), ::tolower);
35
+        std::transform(suffix.begin(), suffix.end(), suffix.begin(), ::tolower);
36
+    }
37
+
31
     if (s.length() >= suffix.length()) {
38
     if (s.length() >= suffix.length()) {
32
         std::string end = s.substr(s.length() - suffix.length());
39
         std::string end = s.substr(s.length() - suffix.length());
33
         return (end == suffix);
40
         return (end == suffix);

読み込み中…
キャンセル
保存