Przeglądaj źródła

Menu background now chosen randomly

Thomas Buck 10 lat temu
rodzic
commit
28c15c890e

+ 6
- 0
ChangeLog.md Wyświetl plik

@@ -2,6 +2,12 @@
2 2
 
3 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 11
     [ 20141229 ]
6 12
     * Removed utils/png, utils/tga image readers
7 13
     * Included current stb_image, stb_image_write and stb_textedit

+ 2
- 2
include/TextureManager.h Wyświetl plik

@@ -94,7 +94,7 @@ class TextureManager {
94 94
                        TextureStorage s = TextureStorage::GAME,
95 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 99
     void addTile(TextureTile* t);
100 100
     int numTiles();
@@ -107,7 +107,7 @@ class TextureManager {
107 107
 
108 108
   private:
109 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 112
     std::vector<unsigned int> mTextureIdsGame;
113 113
     std::vector<unsigned int> mTextureIdsSystem;

+ 0
- 28
include/utils/File.h Wyświetl plik

@@ -1,28 +0,0 @@
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 Wyświetl plik

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

+ 14
- 0
include/utils/random.h Wyświetl plik

@@ -0,0 +1,14 @@
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 Wyświetl plik

@@ -14,7 +14,7 @@ std::string findAndReplace(std::string s, std::string find, std::string replace)
14 14
 
15 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 19
 #endif
20 20
 

+ 20
- 14
src/TextureManager.cpp Wyświetl plik

@@ -16,8 +16,10 @@
16 16
 #include "global.h"
17 17
 #include "Log.h"
18 18
 #include "RunTime.h"
19
+#include "utils/Folder.h"
19 20
 #include "utils/pcx.h"
20 21
 #include "utils/pixel.h"
22
+#include "utils/random.h"
21 23
 #include "utils/strings.h"
22 24
 #include "TextureManager.h"
23 25
 
@@ -145,13 +147,20 @@ int TextureManager::initializeSplash() {
145 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 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 166
     return 0;
@@ -237,14 +246,14 @@ void TextureManager::bindTextureId(unsigned int n, TextureStorage s, unsigned in
237 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 250
     //! \todo case insensitive compare
242 251
 
243
-    if (stringEndsWith(filename, ".pcx") || stringEndsWith(filename, ".PCX")) {
252
+    if (stringEndsWith(filename, ".pcx")) {
244 253
         return loadPCX(filename, s, slot);
245 254
     } else {
246 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 257
         if (data) {
249 258
             if ((n < 3) || (n > 4)) {
250 259
                 getLog() << "Image \"" << filename << "\" has unsupported format ("
@@ -263,17 +272,14 @@ int TextureManager::loadImage(const char* filename, TextureStorage s, int slot)
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 277
     if (!error) {
272 278
         unsigned char* image;
273 279
         unsigned int w, h, bpp;
274 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 283
         if (!error) {
278 284
             unsigned char* image2 = scaleBuffer(image, &w, &h, bpp);
279 285
             if (image2) {

+ 1
- 1
src/utils/CMakeLists.txt Wyświetl plik

@@ -1,11 +1,11 @@
1 1
 # Source files
2 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 3
 set (UTIL_SRCS ${UTIL_SRCS} "filesystem.cpp" "../../include/utils/filesystem.h")
5 4
 set (UTIL_SRCS ${UTIL_SRCS} "Folder.cpp" "../../include/utils/Folder.h")
6 5
 set (UTIL_SRCS ${UTIL_SRCS} "FolderRecursive.cpp")
7 6
 set (UTIL_SRCS ${UTIL_SRCS} "pcx.cpp" "../../include/utils/pcx.h")
8 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 9
 set (UTIL_SRCS ${UTIL_SRCS} "strings.cpp" "../../include/utils/strings.h")
10 10
 set (UTIL_SRCS ${UTIL_SRCS} "time.cpp" "../../include/utils/time.h")
11 11
 

+ 0
- 27
src/utils/File.cpp Wyświetl plik

@@ -1,27 +0,0 @@
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 Wyświetl plik

@@ -12,9 +12,17 @@
12 12
 
13 13
 #include "global.h"
14 14
 #include "utils/filesystem.h"
15
-#include "utils/File.h"
15
+#include "utils/strings.h"
16 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 26
 #if defined(HAVE_DIRENT_H) && defined(HAVE_OPENDIR) && defined(HAVE_READDIR_R) && defined(HAVE_CLOSEDIR) && defined(HAVE_DT_DIR)
19 27
 #include <dirent.h>
20 28
 #define USE_DIRENT
@@ -70,14 +78,6 @@ Folder::Folder(std::string folder, bool listDotFiles) {
70 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 81
 unsigned long Folder::fileCount() {
82 82
     createFolderItems();
83 83
     return files.size();
@@ -220,3 +220,11 @@ int Folder::readFolderItems(std::vector<std::string>& foundFiles,
220 220
 
221 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 Wyświetl plik

@@ -6,7 +6,7 @@
6 6
  */
7 7
 
8 8
 #include "global.h"
9
-#include "utils/File.h"
9
+#include "utils/strings.h"
10 10
 #include "utils/Folder.h"
11 11
 
12 12
 unsigned long Folder::countRecursiveFiles() {
@@ -63,3 +63,12 @@ File& Folder::getRecursiveFile(unsigned long i) {
63 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 Wyświetl plik

@@ -0,0 +1,40 @@
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 Wyświetl plik

@@ -5,6 +5,8 @@
5 5
  * \author xythobuz
6 6
  */
7 7
 
8
+#include <algorithm>
9
+
8 10
 #include "global.h"
9 11
 #include "utils/filesystem.h"
10 12
 #include "utils/strings.h"
@@ -27,7 +29,12 @@ std::string expandHomeDirectory(std::string s) {
27 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 38
     if (s.length() >= suffix.length()) {
32 39
         std::string end = s.substr(s.length() - suffix.length());
33 40
         return (end == suffix);

Ładowanie…
Anuluj
Zapisz