Browse Source

TR3 level loader, assert macros

Thomas Buck 9 years ago
parent
commit
dfbc01cc42

+ 1
- 0
ChangeLog.md View File

@@ -6,6 +6,7 @@
6 6
     * The menu can be navigated using a controller
7 7
     * Vertical camera rotation is now also clamped when using a controller
8 8
     * Worked on rendering SkeletalModels, not yet working correctly
9
+    * Implemented level loader for TR3, but tries to access invalid textiles
9 10
 
10 11
     [ 20140211 ]
11 12
     * Updated imgui to version 1.32

+ 8
- 2
include/RoomData.h View File

@@ -35,7 +35,8 @@ class BoundingBox {
35 35
     }
36 36
 
37 37
     glm::vec3 getCorner(int i) {
38
-        assert((i >= 0) && (i < 8));
38
+        assertGreaterThanEqual(i, 0);
39
+        assertLessThan(i, 8);
39 40
         return corner[i];
40 41
     }
41 42
 
@@ -81,7 +82,12 @@ class Portal {
81 82
     }
82 83
     int getAdjoiningRoom() { return adjoiningRoom; }
83 84
     glm::vec3 getNormal() { return normal; }
84
-    glm::vec3 getVertex(int i) { assert((i >= 0) && (i < 4)); return vert[i]; }
85
+
86
+    glm::vec3 getVertex(int i) {
87
+        assertGreaterThanEqual(i, 0);
88
+        assertLessThan(i, 4);
89
+        return vert[i];
90
+    }
85 91
 
86 92
   private:
87 93
     int adjoiningRoom;

+ 41
- 1
include/global.h View File

@@ -115,7 +115,7 @@ template<typename T, typename U>
115 115
     std::cout << std::endl << "assertion failed:" << std::endl;
116 116
     std::cout << "\t" << exp << std::endl;
117 117
     if (print)
118
-        std::cout << "\t (" << a << " " << str << " " << b << ")" << std::endl;
118
+        std::cout << "\t(" << a << " " << str << " " << b << ")" << std::endl;
119 119
     std::cout << "in " << file << ":" << line << std::endl << std::endl;
120 120
 
121 121
     for (int i = 0; i < frames; i++)
@@ -150,11 +150,47 @@ template<typename T, typename U>
150 150
                                   __FILE__, __LINE__, true, "=="); \
151 151
 }
152 152
 
153
+#define assertLessThan(x, y) { \
154
+    auto assertEvalTemp = x; \
155
+    auto assertEvalTemp2 = y; \
156
+    if (assertEvalTemp >= assertEvalTemp2) \
157
+        assertEqualImplementation(#x " < " #y, assertEvalTemp, assertEvalTemp2, \
158
+                                  __FILE__, __LINE__, true, ">="); \
159
+}
160
+
161
+#define assertLessThanEqual(x, y) { \
162
+    auto assertEvalTemp = x; \
163
+    auto assertEvalTemp2 = y; \
164
+    if (assertEvalTemp > assertEvalTemp2) \
165
+        assertEqualImplementation(#x " <= " #y, assertEvalTemp, assertEvalTemp2, \
166
+                                  __FILE__, __LINE__, true, ">"); \
167
+}
168
+
169
+#define assertGreaterThan(x, y) { \
170
+    auto assertEvalTemp = x; \
171
+    auto assertEvalTemp2 = y; \
172
+    if (assertEvalTemp <= assertEvalTemp2) \
173
+        assertEqualImplementation(#x " > " #y, assertEvalTemp, assertEvalTemp2, \
174
+                                  __FILE__, __LINE__, true, "<="); \
175
+}
176
+
177
+#define assertGreaterThanEqual(x, y) { \
178
+    auto assertEvalTemp = x; \
179
+    auto assertEvalTemp2 = y; \
180
+    if (assertEvalTemp < assertEvalTemp2) \
181
+        assertEqualImplementation(#x " >= " #y, assertEvalTemp, assertEvalTemp2, \
182
+                                  __FILE__, __LINE__, true, "<"); \
183
+}
184
+
153 185
 #else // NODEBUG
154 186
 
155 187
 #define assert(x)
156 188
 #define assertEqual(x, y)
157 189
 #define assertNotEqual(x, y)
190
+#define assertLessThan(x, y)
191
+#define assertLessThanEqual(x, y)
192
+#define assertGreaterThan(x, y)
193
+#define assertGreaterThanEqual(x, y)
158 194
 
159 195
 #endif // NODEBUG
160 196
 
@@ -164,6 +200,10 @@ template<typename T, typename U>
164 200
 #include <cassert>
165 201
 #define assertEqual(x, y) assert((x) == (y))
166 202
 #define assertNotEqual(x, y) assert((x) != (y))
203
+#define assertLessThan(x, y) assert((x) < (y))
204
+#define assertLessThanEqual(x, y) assert((x) <= (y))
205
+#define assertGreaterThan(x, y) assert((x) > (y))
206
+#define assertGreaterThanEqual(x, y) assert((x) >= (y))
167 207
 
168 208
 #endif // EXECINFO
169 209
 

+ 0
- 4
include/loader/Loader.h View File

@@ -16,7 +16,6 @@
16 16
 
17 17
 class Loader {
18 18
   public:
19
-
20 19
     typedef enum {
21 20
         TR_UNKNOWN = 0,
22 21
         TR_1 = 1,
@@ -27,11 +26,8 @@ class Loader {
27 26
     } LoaderVersion;
28 27
 
29 28
     static LoaderVersion checkFile(std::string f);
30
-
31 29
     static std::unique_ptr<Loader> createLoader(std::string f);
32 30
 
33
-    virtual ~Loader();
34
-
35 31
     virtual int load(std::string f) = 0;
36 32
 
37 33
   protected:

+ 23
- 24
include/loader/LoaderTR2.h View File

@@ -15,32 +15,31 @@
15 15
 
16 16
 class LoaderTR2 : public Loader {
17 17
   public:
18
-    LoaderTR2();
19
-    virtual ~LoaderTR2();
20
-
21 18
     virtual int load(std::string f);
22 19
 
23
-  private:
24
-    void loadPaletteTextiles();
25
-    void loadRooms();
26
-    void loadFloorData();
27
-    void loadMeshes();
28
-    void loadMoveables();
29
-    void loadStaticMeshes();
30
-    void loadTextures();
31
-    void loadSprites();
32
-    void loadCameras();
33
-    void loadSoundSources();
34
-    void loadBoxesOverlapsZones();
35
-    void loadAnimatedTextures();
36
-    void loadItems();
37
-    void loadCinematicFrames();
38
-    void loadDemoData();
39
-    void loadSoundMap();
40
-    void loadSoundDetails();
41
-    void loadSampleIndices();
42
-
43
-    void loadExternalSoundFile(std::string f);
20
+  protected:
21
+    virtual void loadPaletteTextiles();
22
+    virtual void loadRoomLights();
23
+    virtual void loadRoomDataEnd(int16_t& alternateRoom, unsigned int& roomFlags);
24
+    virtual void loadRooms();
25
+    virtual void loadFloorData();
26
+    virtual void loadMeshes();
27
+    virtual void loadMoveables();
28
+    virtual void loadStaticMeshes();
29
+    virtual void loadTextures();
30
+    virtual void loadSprites();
31
+    virtual void loadCameras();
32
+    virtual void loadSoundSources();
33
+    virtual void loadBoxesOverlapsZones();
34
+    virtual void loadAnimatedTextures();
35
+    virtual void loadItems();
36
+    virtual void loadCinematicFrames();
37
+    virtual void loadDemoData();
38
+    virtual void loadSoundMap();
39
+    virtual void loadSoundDetails();
40
+    virtual void loadSampleIndices();
41
+
42
+    virtual void loadExternalSoundFile(std::string f);
44 43
 
45 44
     std::array<uint32_t, 256> palette;
46 45
 };

+ 5
- 7
include/loader/LoaderTR3.h View File

@@ -8,17 +8,15 @@
8 8
 #ifndef _LOADER_LOADER_TR3_H_
9 9
 #define _LOADER_LOADER_TR3_H_
10 10
 
11
-#include "loader/Loader.h"
11
+#include "loader/LoaderTR2.h"
12 12
 
13
-class LoaderTR3 : public Loader {
13
+class LoaderTR3 : public LoaderTR2 {
14 14
   public:
15
-    LoaderTR3();
16
-    virtual ~LoaderTR3();
17
-
18 15
     virtual int load(std::string f);
19 16
 
20
-  private:
21
-
17
+  protected:
18
+    virtual void loadRoomLights();
19
+    virtual void loadRoomDataEnd(int16_t& alternateRoom, unsigned int& roomFlags);
22 20
 };
23 21
 
24 22
 #endif

+ 1
- 1
src/Console.cpp View File

@@ -110,7 +110,7 @@ void Console::display() {
110 110
         for (unsigned long i = 0; i < Log::size(); i++) {
111 111
             auto& entry = Log::getEntry(i);
112 112
 
113
-            assert(entry.level < LOG_COUNT);
113
+            assertLessThan(entry.level, LOG_COUNT);
114 114
             if (!visibleLogs[entry.level]) {
115 115
                 continue;
116 116
             }

+ 2
- 2
src/Entity.cpp View File

@@ -50,8 +50,8 @@ void Entity::display(glm::mat4 VP) {
50 50
             }
51 51
         }
52 52
 
53
-        assert(cache > -1);
54
-        assert(cacheType > -1);
53
+        assertGreaterThan(cache, -1);
54
+        assertGreaterThan(cacheType, -1);
55 55
     }
56 56
 
57 57
     glm::mat4 translate = glm::translate(glm::mat4(1.0f), glm::vec3(pos.x, -pos.y, pos.z));

+ 5
- 5
src/Game.cpp View File

@@ -81,7 +81,7 @@ int Game::loadLevel(const char* level) {
81 81
 }
82 82
 
83 83
 void Game::handleAction(ActionEvents action, bool isFinished) {
84
-    assert(action < ActionEventCount);
84
+    assertLessThan(action, ActionEventCount);
85 85
 
86 86
     if (!mLoaded)
87 87
         return;
@@ -112,14 +112,14 @@ void Game::handleControllerAxis(float value, KeyboardButton axis) {
112 112
 }
113 113
 
114 114
 Entity& Game::getLara() {
115
-    assert(mLara >= 0);
116
-    assert(mLara < (int)getWorld().sizeEntity());
115
+    assertGreaterThanEqual(mLara, 0);
116
+    assertLessThan(mLara, getWorld().sizeEntity());
117 117
     return getWorld().getEntity(mLara);
118 118
 }
119 119
 
120 120
 void Game::setLara(long lara) {
121
-    assert(lara >= 0);
122
-    assert(lara < getWorld().sizeEntity());
121
+    assertGreaterThanEqual(lara, 0);
122
+    assertLessThan(lara, getWorld().sizeEntity());
123 123
     mLara = lara;
124 124
 }
125 125
 

+ 2
- 2
src/Log.cpp View File

@@ -12,8 +12,8 @@ LogLevel Log::logs[LOG_COUNT] = { 0, 1, 2, 3, 4 };
12 12
 std::vector<LogEntry> Log::wholeLog;
13 13
 
14 14
 LogLevel& Log::get(int level) {
15
-    assert(level >= 0);
16
-    assert(level < LOG_COUNT);
15
+    assertGreaterThanEqual(level, 0);
16
+    assertLessThan(level, LOG_COUNT);
17 17
     return logs[level];
18 18
 }
19 19
 

+ 6
- 6
src/Menu.cpp View File

@@ -14,12 +14,12 @@
14 14
 void Menu::showDialog(std::string msg, std::string btn1, std::string btn2,
15 15
                       std::function<int (bool state)> callback) {
16 16
     // Only show one dialog at a time
17
-    assert(dialogText.length() == 0);
18
-    assert(dialogButton1.length() == 0);
19
-    assert(dialogButton2.length() == 0);
17
+    assertEqual(dialogText.length(), 0);
18
+    assertEqual(dialogButton1.length(), 0);
19
+    assertEqual(dialogButton2.length(), 0);
20 20
 
21
-    assert(msg.length() > 0);
22
-    assert(btn1.length() > 0);
21
+    assertGreaterThan(msg.length(), 0);
22
+    assertGreaterThan(btn1.length(), 0);
23 23
 
24 24
     dialogText = msg;
25 25
     dialogButton1 = btn1;
@@ -93,7 +93,7 @@ void Menu::displayDialog() {
93 93
             w0 = wMax;
94 94
         unsigned int h0 =  Font::heightText(1.0f, w0, dialogText) + 10;
95 95
 
96
-        assert(dialogButton1.length() > 0);
96
+        assertGreaterThan(dialogButton1.length(), 0);
97 97
         unsigned int w1 = Font::widthText(1.0f, dialogButton1) + 20;
98 98
         if (w1 > wMax)
99 99
             w1 = wMax;

+ 5
- 5
src/Mesh.cpp View File

@@ -78,9 +78,9 @@ void Mesh::prepare() {
78 78
         vertIndex += (indicesBuff.at(i) == 0) ? 4 : 3;
79 79
     }
80 80
 
81
-    assert((ind.size() % 3) == 0);
82
-    assert(vert.size() == tex.size());
83
-    assert(vert.size() == uvBuff.size());
81
+    assertEqual(ind.size() % 3, 0);
82
+    assertEqual(vert.size(), tex.size());
83
+    assertEqual(vert.size(), uvBuff.size());
84 84
 
85 85
     indicesBuff = std::move(ind);
86 86
     vertices.bufferData(vert);
@@ -112,8 +112,8 @@ void Mesh::prepare() {
112 112
         vertIndex += (indicesColorBuff.at(i) == 0) ? 4 : 3;
113 113
     }
114 114
 
115
-    assert((indCol.size() % 3) == 0);
116
-    assert(vertCol.size() == cols.size());
115
+    assertEqual(indCol.size() % 3, 0);
116
+    assertEqual(vertCol.size(), cols.size());
117 117
 
118 118
     indicesColor.bufferData(indCol);
119 119
     verticesColor.bufferData(vertCol);

+ 1
- 1
src/Room.cpp View File

@@ -46,7 +46,7 @@ void Room::display(glm::mat4 VP) {
46 46
 }
47 47
 
48 48
 bool Room::isWall(unsigned long sector) {
49
-    assert(sector < sectors.size());
49
+    assertLessThan(sector, sectors.size());
50 50
 
51 51
     //! \fixme is (sector > 0) correct??
52 52
     return ((sector > 0) && sectors.at(sector)->isWall());

+ 1
- 1
src/RoomData.cpp View File

@@ -76,7 +76,7 @@ void StaticModel::display(glm::mat4 VP) {
76 76
                 cache = i;
77 77
             }
78 78
         }
79
-        assert(cache >= 0);
79
+        assertGreaterThanEqual(cache, 0);
80 80
     }
81 81
 
82 82
     getWorld().getStaticMesh(cache).display(VP * model);

+ 3
- 3
src/RoomMesh.cpp View File

@@ -59,9 +59,9 @@ void RoomMesh::prepare() {
59 59
         vertIndex += (indicesBuff.at(i) == 0) ? 4 : 3;
60 60
     }
61 61
 
62
-    assert((ind.size() % 3) == 0);
63
-    assert(vert.size() == tex.size());
64
-    assert(vert.size() == uvBuff.size());
62
+    assertEqual(ind.size() % 3, 0);
63
+    assertEqual(vert.size(), tex.size());
64
+    assertEqual(vert.size(), uvBuff.size());
65 65
 
66 66
     indicesBuff = std::move(ind);
67 67
     vertices.bufferData(vert);

+ 2
- 2
src/RunTime.cpp View File

@@ -54,12 +54,12 @@ void RunTime::initialize() {
54 54
 }
55 55
 
56 56
 KeyboardButton RunTime::getKeyBinding(ActionEvents event) {
57
-    assert(event < ActionEventCount);
57
+    assertLessThan(event, ActionEventCount);
58 58
     return keyBindings[event];
59 59
 }
60 60
 
61 61
 void RunTime::setKeyBinding(ActionEvents event, KeyboardButton button) {
62
-    assert(event < ActionEventCount);
62
+    assertLessThan(event, ActionEventCount);
63 63
     keyBindings[event] = button;
64 64
 }
65 65
 

+ 16
- 16
src/Script.cpp View File

@@ -228,12 +228,12 @@ unsigned int Script::levelCount() {
228 228
 }
229 229
 
230 230
 std::string Script::getLevelName(unsigned int i) {
231
-    assert(i < numLevels);
231
+    assertLessThan(i, numLevels);
232 232
     return levelNames.at(i);
233 233
 }
234 234
 
235 235
 std::string Script::getLevelFilename(unsigned int i) {
236
-    assert(i < numLevels);
236
+    assertLessThan(i, numLevels);
237 237
     return levelFilenames.at(i);
238 238
 }
239 239
 
@@ -242,7 +242,7 @@ unsigned int Script::pictureCount() {
242 242
 }
243 243
 
244 244
 std::string Script::getPictureFilename(unsigned int i) {
245
-    assert(i < numPictures);
245
+    assertLessThan(i, numPictures);
246 246
     return pictureFilenames.at(i);
247 247
 }
248 248
 
@@ -251,7 +251,7 @@ unsigned int Script::cutsceneCount() {
251 251
 }
252 252
 
253 253
 std::string Script::getCutsceneFilename(unsigned int i) {
254
-    assert(i < numCutscenes);
254
+    assertLessThan(i, numCutscenes);
255 255
     return cutsceneFilenames.at(i);
256 256
 }
257 257
 
@@ -260,7 +260,7 @@ unsigned int Script::titleCount() {
260 260
 }
261 261
 
262 262
 std::string Script::getTitleFilename(unsigned int i) {
263
-    assert(i < numTitles);
263
+    assertLessThan(i, numTitles);
264 264
     return titleFilenames.at(i);
265 265
 }
266 266
 
@@ -269,7 +269,7 @@ unsigned int Script::videoCount() {
269 269
 }
270 270
 
271 271
 std::string Script::getVideoFilename(unsigned int i) {
272
-    assert(i < numFMVs);
272
+    assertLessThan(i, numFMVs);
273 273
     return fmvFilenames.at(i);
274 274
 }
275 275
 
@@ -278,7 +278,7 @@ unsigned int Script::gameStringCount() {
278 278
 }
279 279
 
280 280
 std::string Script::getGameString(unsigned int i) {
281
-    assert(i < numGameStrings);
281
+    assertLessThan(i, numGameStrings);
282 282
     return gameStrings.at(i);
283 283
 }
284 284
 
@@ -287,35 +287,35 @@ unsigned int Script::pcStringCount() {
287 287
 }
288 288
 
289 289
 std::string Script::getPCString(unsigned int i) {
290
-    assert(i < numPCStrings);
290
+    assertLessThan(i, numPCStrings);
291 291
     return pcStrings.at(i);
292 292
 }
293 293
 
294 294
 std::string Script::getPuzzleString(unsigned int i, unsigned int j) {
295
-    assert(i < 4);
296
-    assert(j < numLevels);
295
+    assertLessThan(i, 4);
296
+    assertLessThan(j, numLevels);
297 297
     return puzzles.at(i).at(j);
298 298
 }
299 299
 
300 300
 std::string Script::getPickupString(unsigned int i, unsigned int j) {
301
-    assert(i < 2);
302
-    assert(j < numLevels);
301
+    assertLessThan(i, 2);
302
+    assertLessThan(j, numLevels);
303 303
     return pickups.at(i).at(j);
304 304
 }
305 305
 
306 306
 std::string Script::getKeyString(unsigned int i, unsigned int j) {
307
-    assert(i < 4);
308
-    assert(j < numLevels);
307
+    assertLessThan(i, 4);
308
+    assertLessThan(j, numLevels);
309 309
     return keys.at(i).at(j);
310 310
 }
311 311
 
312 312
 void Script::registerScriptHandler(ScriptOpCode op, std::function<int (bool, uint16_t)> func) {
313
-    assert(op < OP_UNKNOWN);
313
+    assertLessThan(op, OP_UNKNOWN);
314 314
     scriptHandlers[op] = func;
315 315
 }
316 316
 
317 317
 int Script::runScript(unsigned int level) {
318
-    assert(level < (numLevels + 1));
318
+    assertLessThan(level, (numLevels + 1));
319 319
     std::vector<uint16_t> s = script.at(level);
320 320
 
321 321
     for (unsigned int i = 0; i < s.size(); i++) {

+ 8
- 8
src/SkeletalModel.cpp View File

@@ -29,7 +29,7 @@ unsigned long BoneFrame::size() {
29 29
 }
30 30
 
31 31
 BoneTag& BoneFrame::get(unsigned long i) {
32
-    assert(i < tag.size());
32
+    assertLessThan(i, tag.size());
33 33
     return *tag.at(i);
34 34
 }
35 35
 
@@ -49,7 +49,7 @@ unsigned long AnimationFrame::size() {
49 49
 }
50 50
 
51 51
 BoneFrame& AnimationFrame::get(unsigned long i) {
52
-    assert(i < frame.size());
52
+    assertLessThan(i, frame.size());
53 53
     return *frame.at(i);
54 54
 }
55 55
 
@@ -64,19 +64,19 @@ class MatrixStack {
64 64
     MatrixStack(glm::mat4 start) : startVal(start) { stack.push_back(startVal); }
65 65
 
66 66
     void push() {
67
-        //assert(stack.size() > 0);
67
+        //assertGreaterThan(stack.size(), 0);
68 68
         if (stack.size() > 0)
69 69
             stack.push_back(stack.at(stack.size() - 1));
70 70
     }
71 71
 
72 72
     void pop() {
73
-        //assert(stack.size() > 0);
73
+        //assertGreaterThan(stack.size(), 0);
74 74
         if (stack.size() > 0)
75 75
             stack.pop_back();
76 76
     }
77 77
 
78 78
     glm::mat4 get() {
79
-        //assert(stack.size() > 0);
79
+        //assertGreaterThan(stack.size(), 0);
80 80
         if (stack.size() > 0)
81 81
             return stack.at(stack.size() - 1);
82 82
         return startVal;
@@ -101,8 +101,8 @@ SkeletalModel::~SkeletalModel() {
101 101
 #endif
102 102
 
103 103
 void SkeletalModel::display(glm::mat4 MVP, int aframe, int bframe) {
104
-    assert(aframe < size());
105
-    assert(bframe < get(aframe).size());
104
+    assertLessThan(aframe, size());
105
+    assertLessThan(bframe, get(aframe).size());
106 106
 
107 107
     AnimationFrame& anim = get(aframe);
108 108
     BoneFrame& boneframe = anim.get(bframe);
@@ -173,7 +173,7 @@ unsigned long SkeletalModel::size() {
173 173
 }
174 174
 
175 175
 AnimationFrame& SkeletalModel::get(unsigned long i) {
176
-    assert(i < animation.size());
176
+    assertLessThan(i, animation.size());
177 177
     return *animation.at(i);
178 178
 }
179 179
 

+ 3
- 3
src/SoundManager.cpp View File

@@ -30,10 +30,10 @@ int SoundManager::prepareSources() {
30 30
         float vol;
31 31
         int index = getIndex(soundSources.at(i).id, &vol);
32 32
         int ret = Sound::addSource(index, vol, false, true);
33
-        assert(ret == i);
33
+        assertEqual(ret, i);
34 34
         float pos[3] = { soundSources.at(i).x, soundSources.at(i).y, soundSources.at(i).z };
35 35
         ret = Sound::sourceAt(i, pos);
36
-        assert(ret == 0);
36
+        assertEqual(ret, 0);
37 37
         Sound::play(i, false);
38 38
     }
39 39
 
@@ -42,7 +42,7 @@ int SoundManager::prepareSources() {
42 42
         int index = getIndex(i, &vol);
43 43
         if ((index >= 0) && (index < Sound::numBuffers())) {
44 44
             int ret = Sound::addSource(index, vol, true, false);
45
-            assert(ret >= 0);
45
+            assertGreaterThanEqual(ret, 0);
46 46
         }
47 47
     }
48 48
 }

+ 2
- 2
src/Sprite.cpp View File

@@ -52,8 +52,8 @@ void Sprite::display(glm::mat4 MVP) {
52 52
 // ----------------------------------------------------------------------------
53 53
 
54 54
 void SpriteSequence::display(glm::mat4 MVP, int index) {
55
-    assert(index >= 0);
56
-    assert(index < length);
55
+    assertGreaterThanEqual(index, 0);
56
+    assertLessThan(index, length);
57 57
     getWorld().getSprite(start + index).display(MVP);
58 58
 }
59 59
 

+ 12
- 12
src/TextureManager.cpp View File

@@ -56,8 +56,8 @@ std::vector<BufferManager> TextureManager::gameBuffers;
56 56
 std::vector<BufferManager> TextureManager::systemBuffers;
57 57
 
58 58
 int TextureManager::initialize() {
59
-    assert(mTextureIdsGame.size() == 0);
60
-    assert(mTextureIdsSystem.size() == 0);
59
+    assertEqual(mTextureIdsGame.size(), 0);
60
+    assertEqual(mTextureIdsSystem.size(), 0);
61 61
 
62 62
     while (mTextureIdsSystem.size() < 2) {
63 63
         unsigned int id;
@@ -132,8 +132,8 @@ int TextureManager::loadBufferSlot(unsigned char* image,
132 132
                                    unsigned int width, unsigned int height,
133 133
                                    ColorMode mode, unsigned int bpp,
134 134
                                    TextureStorage s, int slot, bool filter) {
135
-    assert(width > 0);
136
-    assert(height > 0);
135
+    assertGreaterThan(width, 0);
136
+    assertGreaterThan(height, 0);
137 137
     assert((mode == ColorMode::RGB)
138 138
            || (mode == ColorMode::BGR)
139 139
            || (mode == ColorMode::ARGB)
@@ -201,15 +201,15 @@ int TextureManager::numTextures(TextureStorage s) {
201 201
 }
202 202
 
203 203
 void TextureManager::bindTextureId(unsigned int n, TextureStorage s, unsigned int unit) {
204
-    assert(n < getIds(s).size());
205
-    assert(unit < 80); //! \todo Query GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS
204
+    assertLessThan(n, getIds(s).size());
205
+    assertLessThan(unit, 80); //! \todo Query GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS
206 206
 
207 207
     glActiveTexture(GL_TEXTURE0 + unit);
208 208
     glBindTexture(GL_TEXTURE_2D, getIds(s).at(n));
209 209
 }
210 210
 
211 211
 int TextureManager::bindTexture(unsigned int n, TextureStorage s) {
212
-    assert(n < getIds(s).size());
212
+    assertLessThan(n, getIds(s).size());
213 213
 
214 214
     if ((n < getUnits(s).size()) && (getUnits(s).at(n) >= 0)) {
215 215
         bindTextureId(n, s, getUnits(s).at(n));
@@ -233,8 +233,8 @@ int TextureManager::numTiles() {
233 233
 }
234 234
 
235 235
 TextureTile& TextureManager::getTile(int index) {
236
-    assert(index >= 0);
237
-    assert(index < tiles.size());
236
+    assertGreaterThanEqual(index, 0);
237
+    assertLessThan(index, tiles.size());
238 238
     return *tiles.at(index);
239 239
 }
240 240
 
@@ -250,13 +250,13 @@ int TextureManager::numAnimatedTiles() {
250 250
 }
251 251
 
252 252
 int TextureManager::getFirstTileAnimation(int index) {
253
-    assert(index < animations.size());
254
-    assert(animations.at(index).size() > 0);
253
+    assertLessThan(index, animations.size());
254
+    assertGreaterThan(animations.at(index).size(), 0);
255 255
     return animations.at(index).at(0);
256 256
 }
257 257
 
258 258
 int TextureManager::getNextTileAnimation(int index, int tile) {
259
-    assert(index < animations.size());
259
+    assertLessThan(index, animations.size());
260 260
     for (int i = 0; i < animations.at(index).size(); i++) {
261 261
         if (animations.at(index).at(i) == tile) {
262 262
             if (i < (animations.at(index).size() - 1))

+ 7
- 7
src/World.cpp View File

@@ -31,7 +31,7 @@ unsigned long World::sizeRoom() {
31 31
 }
32 32
 
33 33
 Room& World::getRoom(unsigned long index) {
34
-    assert(index < mRooms.size());
34
+    assertLessThan(index, mRooms.size());
35 35
     return *mRooms.at(index);
36 36
 }
37 37
 
@@ -44,7 +44,7 @@ unsigned long World::sizeSprite() {
44 44
 }
45 45
 
46 46
 Sprite& World::getSprite(unsigned long index) {
47
-    assert(index < mSprites.size());
47
+    assertLessThan(index, mSprites.size());
48 48
     return *mSprites.at(index);
49 49
 }
50 50
 
@@ -57,7 +57,7 @@ unsigned long World::sizeSpriteSequence() {
57 57
 }
58 58
 
59 59
 SpriteSequence& World::getSpriteSequence(unsigned long index) {
60
-    assert(index < mSpriteSequences.size());
60
+    assertLessThan(index, mSpriteSequences.size());
61 61
     return *mSpriteSequences.at(index);
62 62
 }
63 63
 
@@ -70,7 +70,7 @@ unsigned long World::sizeEntity() {
70 70
 }
71 71
 
72 72
 Entity& World::getEntity(unsigned long index) {
73
-    assert(index < mEntities.size());
73
+    assertLessThan(index, mEntities.size());
74 74
     return *mEntities.at(index);
75 75
 }
76 76
 
@@ -83,7 +83,7 @@ unsigned long World::sizeSkeletalModel() {
83 83
 }
84 84
 
85 85
 SkeletalModel& World::getSkeletalModel(unsigned long index) {
86
-    assert(index < mModels.size());
86
+    assertLessThan(index, mModels.size());
87 87
     return *mModels.at(index);
88 88
 }
89 89
 
@@ -96,7 +96,7 @@ unsigned long World::sizeStaticMesh() {
96 96
 }
97 97
 
98 98
 StaticMesh& World::getStaticMesh(unsigned long index) {
99
-    assert(index < mStaticMeshes.size());
99
+    assertLessThan(index, mStaticMeshes.size());
100 100
     return *mStaticMeshes.at(index);
101 101
 }
102 102
 
@@ -109,7 +109,7 @@ unsigned long World::sizeMesh() {
109 109
 }
110 110
 
111 111
 Mesh& World::getMesh(unsigned long index) {
112
-    assert(index < mMeshes.size());
112
+    assertLessThan(index, mMeshes.size());
113 113
     return *mMeshes.at(index);
114 114
 }
115 115
 

+ 0
- 2
src/commands/Command.cpp View File

@@ -36,8 +36,6 @@ void Command::fillCommandList() {
36 36
 }
37 37
 
38 38
 int Command::command(std::string c) {
39
-    assert(commands.size() > 0);
40
-
41 39
     // Remove comment, if any
42 40
     size_t comment = c.find_first_of('#');
43 41
     if (comment != std::string::npos)

+ 4
- 5
src/loader/Loader.cpp View File

@@ -8,6 +8,7 @@
8 8
 #include "global.h"
9 9
 #include "loader/Loader.h"
10 10
 #include "loader/LoaderTR2.h"
11
+#include "loader/LoaderTR3.h"
11 12
 
12 13
 Loader::LoaderVersion Loader::checkFile(std::string f) {
13 14
     BinaryFile file;
@@ -38,7 +39,6 @@ std::unique_ptr<Loader> Loader::createLoader(std::string f) {
38 39
     LoaderVersion v = checkFile(f);
39 40
     switch (v) {
40 41
         case TR_1:
41
-        case TR_3:
42 42
         case TR_4:
43 43
         case TR_5:
44 44
         case TR_UNKNOWN:
@@ -46,10 +46,9 @@ std::unique_ptr<Loader> Loader::createLoader(std::string f) {
46 46
 
47 47
         case TR_2:
48 48
             return std::unique_ptr<Loader>(new LoaderTR2());
49
-    }
50
-}
51
-
52
-Loader::~Loader() {
53 49
 
50
+        case TR_3:
51
+            return std::unique_ptr<Loader>(new LoaderTR3());
52
+    }
54 53
 }
55 54
 

+ 49
- 36
src/loader/LoaderTR2.cpp View File

@@ -21,12 +21,6 @@
21 21
 
22 22
 #include <glm/gtc/matrix_transform.hpp>
23 23
 
24
-LoaderTR2::LoaderTR2() {
25
-}
26
-
27
-LoaderTR2::~LoaderTR2() {
28
-}
29
-
30 24
 int LoaderTR2::load(std::string f) {
31 25
     if (file.open(f) != 0) {
32 26
         return 1; // Could not open file
@@ -91,7 +85,7 @@ void LoaderTR2::loadPaletteTextiles() {
91 85
         int r = TextureManager::loadBufferSlot(img, 256, 256,
92 86
                                                ColorMode::ARGB, 32,
93 87
                                                TextureStorage::GAME, i);
94
-        assert(r >= 0); //! \fixme properly handle error when texture could not be loaded!
88
+        assertGreaterThanEqual(r, 0); //! \fixme properly handle error when texture could not be loaded!
95 89
         delete [] img;
96 90
     }
97 91
 
@@ -106,9 +100,14 @@ void LoaderTR2::loadTextures() {
106 100
     for (unsigned int o = 0; o < numObjectTextures; o++) {
107 101
         // 0 means that a texture is all-opaque, and that transparency
108 102
         // information is ignored.
103
+        //
109 104
         // 1 means that transparency information is used. In 8-bit color,
110 105
         // index 0 is the transparent color, while in 16-bit color, the
111 106
         // top bit (0x8000) is the alpha channel (1 = opaque, 0 = transparent)
107
+        //
108
+        // 2 (TR3 only) means that the opacity (alpha) is equal to the intensity;
109
+        // the brighter the color, the more opaque it is. The intensity is probably
110
+        // calculated as the maximum of the individual color values.
112 111
         uint16_t attribute = file.readU16();
113 112
 
114 113
         // Index into the textile list
@@ -179,6 +178,38 @@ void LoaderTR2::loadAnimatedTextures() {
179 178
 
180 179
 // ---- Rooms ----
181 180
 
181
+void LoaderTR2::loadRoomLights() {
182
+    int16_t intensity1 = file.read16();
183
+    int16_t intensity2 = file.read16();
184
+    int16_t lightMode = file.read16();
185
+
186
+    uint16_t numLights = file.readU16();
187
+    for (unsigned int l = 0; l < numLights; l++) {
188
+        // Position of light, in world coordinates
189
+        int32_t x = file.read32();
190
+        int32_t y = file.read32();
191
+        int32_t z = file.read32();
192
+
193
+        uint16_t intensity1 = file.readU16();
194
+        uint16_t intensity2 = file.readU16(); // Almost always equal to intensity1
195
+
196
+        uint32_t fade1 = file.readU32(); // Falloff value?
197
+        uint32_t fade2 = file.readU32(); // Falloff value?
198
+
199
+        // TODO store light somewhere
200
+    }
201
+}
202
+
203
+void LoaderTR2::loadRoomDataEnd(int16_t& alternateRoom, unsigned int& roomFlags) {
204
+    alternateRoom = file.read16();
205
+
206
+    uint16_t flags = file.readU16();
207
+    roomFlags = 0;
208
+    if (flags & 0x0001) {
209
+        roomFlags |= RoomFlagUnderWater;
210
+    }
211
+}
212
+
182 213
 void LoaderTR2::loadRooms() {
183 214
     uint16_t numRooms = file.readU16();
184 215
     for (unsigned int i = 0; i < numRooms; i++) {
@@ -332,6 +363,10 @@ void LoaderTR2::loadRooms() {
332 363
             uint8_t roomAbove = file.readU8(); // 0xFF if none
333 364
             int8_t ceiling = file.read8(); // Absolute height of ceiling (/ 256)
334 365
 
366
+            // In TR3 indexBox is more complicated. Only bits 4-14 are the 'real' index.
367
+            // Bits 0-3 are most likely some kind of flag (footstep sound?).
368
+            // There is a special value of the 'real' index, 2047 or 0x7FF.
369
+
335 370
             bool wall = false;
336 371
             if ((((uint8_t)floor) == 0x81) || (((uint8_t)ceiling) == 0x81)) {
337 372
                 wall = true;
@@ -341,25 +376,7 @@ void LoaderTR2::loadRooms() {
341 376
             // TODO store sectors
342 377
         }
343 378
 
344
-        int16_t intensity1 = file.read16();
345
-        int16_t intensity2 = file.read16();
346
-        int16_t lightMode = file.read16();
347
-
348
-        uint16_t numLights = file.readU16();
349
-        for (unsigned int l = 0; l < numLights; l++) {
350
-            // Position of light, in world coordinates
351
-            int32_t x = file.read32();
352
-            int32_t y = file.read32();
353
-            int32_t z = file.read32();
354
-
355
-            uint16_t intensity1 = file.readU16();
356
-            uint16_t intensity2 = file.readU16(); // Almost always equal to intensity1
357
-
358
-            uint32_t fade1 = file.readU32(); // Falloff value?
359
-            uint32_t fade2 = file.readU32(); // Falloff value?
360
-
361
-            // TODO store light somewhere
362
-        }
379
+        loadRoomLights();
363 380
 
364 381
         uint16_t numStaticMeshes = file.readU16();
365 382
         std::vector<StaticModel*> staticModels;
@@ -385,13 +402,9 @@ void LoaderTR2::loadRooms() {
385 402
                                                    objectID));
386 403
         }
387 404
 
388
-        int16_t alternateRoom = file.read16();
389
-
390
-        uint16_t flags = file.readU16();
405
+        int16_t alternateRoom = -1;
391 406
         unsigned int roomFlags = 0;
392
-        if (flags & 0x0001) {
393
-            roomFlags |= RoomFlagUnderWater;
394
-        }
407
+        loadRoomDataEnd(alternateRoom, roomFlags);
395 408
 
396 409
         BoundingBox* boundingbox = new BoundingBox(bbox[0], bbox[1]);
397 410
         RoomMesh* mesh = new RoomMesh(vertices, rectangles, triangles);
@@ -463,9 +476,9 @@ void LoaderTR2::loadSprites() {
463 476
         int16_t negativeLength = file.read16(); // Negative sprite count
464 477
         int16_t offset = file.read16(); // Where sequence starts in sprite texture list
465 478
 
466
-        assert(negativeLength < 0);
467
-        assert(offset >= 0);
468
-        assert((offset + (negativeLength * -1)) <= numSpriteTextures);
479
+        assertLessThan(negativeLength, 0);
480
+        assertGreaterThanEqual(offset, 0);
481
+        assertLessThanEqual(offset + (negativeLength * -1), numSpriteTextures);
469 482
 
470 483
         SpriteSequence* ss = new SpriteSequence(objectID, offset, (negativeLength * -1));
471 484
         getWorld().addSpriteSequence(ss);
@@ -1106,7 +1119,7 @@ void LoaderTR2::loadExternalSoundFile(std::string f) {
1106 1119
             buff[i] = sfx.readU8();
1107 1120
 
1108 1121
         int ret = Sound::loadBuffer(buff, riffSize + 8);
1109
-        assert(ret >= 0);
1122
+        assertGreaterThanEqual(ret, 0);
1110 1123
 
1111 1124
         riffCount++;
1112 1125
     }

+ 65
- 7
src/loader/LoaderTR3.cpp View File

@@ -8,19 +8,77 @@
8 8
 #include "global.h"
9 9
 #include "loader/LoaderTR3.h"
10 10
 
11
-LoaderTR3::LoaderTR3() {
12
-}
11
+int LoaderTR3::load(std::string f) {
12
+    if (file.open(f) != 0) {
13
+        return 1; // Could not open file
14
+    }
15
+
16
+    uint32_t version = file.readU32();
17
+    if ((version != 0xFF080038) && (version != 0xFF180038)) {
18
+        return 2; // Not a TR3 level?!
19
+    }
20
+
21
+    loadPaletteTextiles();
22
+
23
+    file.seek(file.tell() + 4); // Unused value?
24
+
25
+    loadRooms();
26
+    loadFloorData();
27
+    loadMeshes();
28
+    loadMoveables();
29
+    loadStaticMeshes();
30
+    loadSprites();
31
+    loadCameras();
32
+    loadSoundSources();
33
+    loadBoxesOverlapsZones();
34
+    loadAnimatedTextures();
35
+    loadTextures();
36
+    loadItems();
37
+
38
+    file.seek(file.tell() + 8192); // Skip Light map, only for 8bit coloring
13 39
 
14
-LoaderTR3::~LoaderTR3() {
40
+    loadCinematicFrames();
41
+    loadDemoData();
42
+    loadSoundMap();
43
+    loadSoundDetails();
44
+    loadSampleIndices();
45
+
46
+    loadExternalSoundFile(f);
47
+
48
+    // TODO load TR3 PC version CDAUDIO.WAD file
49
+    // TODO load TR3 Mac version CDAudio.db and WAV files
50
+
51
+    return 0;
15 52
 }
16 53
 
17
-int LoaderTR3::load(std::string f) {
18
-    if (file.open(f.c_str()) != 0) {
19
-        return 1; // Could not open file
54
+void LoaderTR3::loadRoomLights() {
55
+    int16_t intensity1 = file.read16();
56
+    int16_t intensity2 = file.read16();
57
+
58
+    uint16_t numLights = file.readU16();
59
+    for (unsigned int l = 0; l < numLights; l++) {
60
+        // Position of light, in world coordinates
61
+        int32_t x = file.read32();
62
+        int32_t y = file.read32();
63
+        int32_t z = file.read32();
64
+
65
+        uint16_t intensity1 = file.readU16();
66
+        uint16_t intensity2 = file.readU16(); // Almost always equal to intensity1
67
+
68
+        uint32_t fade1 = file.readU32(); // Falloff value?
69
+        uint32_t fade2 = file.readU32(); // Falloff value?
70
+
71
+        // TODO store light somewhere
20 72
     }
73
+}
21 74
 
75
+void LoaderTR3::loadRoomDataEnd(int16_t& alternateRoom, unsigned int& roomFlags) {
76
+    LoaderTR2::loadRoomDataEnd(alternateRoom, roomFlags);
22 77
 
78
+    uint8_t r = file.readU8();
79
+    uint8_t g = file.readU8();
80
+    uint8_t b = file.readU8();
23 81
 
24
-    return 1; // stub
82
+    // TODO store room-light color (?) somewhere
25 83
 }
26 84
 

+ 1
- 1
src/utils/random.cpp View File

@@ -21,7 +21,7 @@ int randomInteger(int max, int min) {
21 21
     if (max == min)
22 22
         return max;
23 23
 
24
-    assert(max > min);
24
+    assertGreaterThan(max, min);
25 25
 
26 26
     if (!engineIsSeeded) {
27 27
         engine.seed(std::chrono::system_clock::now().time_since_epoch().count());

Loading…
Cancel
Save