Browse Source

Sprite support in LoaderTR2 and UI

Thomas Buck 9 years ago
parent
commit
42a23d13d0
13 changed files with 332 additions and 82 deletions
  1. 5
    0
      ChangeLog.md
  2. 6
    1
      include/Render.h
  3. 19
    6
      include/Sprite.h
  4. 5
    5
      include/World.h
  5. 6
    6
      src/Game.cpp
  6. 31
    2
      src/Render.cpp
  7. 1
    1
      src/SoundManager.cpp
  8. 69
    34
      src/Sprite.cpp
  9. 75
    3
      src/UI.cpp
  10. 10
    10
      src/World.cpp
  11. 100
    13
      src/loader/LoaderTR2.cpp
  12. 4
    0
      src/system/SoundAL.cpp
  13. 1
    1
      src/utils/binary.cpp

+ 5
- 0
ChangeLog.md View File

@@ -2,6 +2,11 @@
2 2
 
3 3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4 4
 
5
+    [ 20141202 ]
6
+    * No longer crashing when LoaderTR2 does not find an SFX file
7
+    * LoaderTR2 now loads level-wide Sprites (no Room Sprites yet)
8
+    * Added Sprite Viewer Debug UI
9
+
5 10
     [ 20141201 ]
6 11
     * Moved Font, Sound, Window modules into system subfolder
7 12
     * Rewrote OpenAL sound implementation, separated sourcing and buffering

+ 6
- 1
include/Render.h View File

@@ -123,6 +123,9 @@ class Render {
123 123
     void debugDisplayTextile(int texture = -1,
124 124
                              float x = 0.0f, float y = 0.0f, float w = 64.0f, float h = 64.0f);
125 125
 
126
+    void debugDisplaySprite(int sprite = -1, int offset = 0,
127
+                            float x = 0.0f, float y = 0.0f, float w = 128.0f, float h = 128.0f);
128
+
126 129
   private:
127 130
 
128 131
     void drawTexture(float x, float y, float w, float h,
@@ -130,6 +133,8 @@ class Render {
130 133
 
131 134
     void drawTextile(float x, float y, float w, float h, unsigned int textile);
132 135
 
136
+    void drawSprite(float x, float y, float w, float h, unsigned int sprite, unsigned int offset);
137
+
133 138
     static void lightRoom(Room& room);
134 139
 
135 140
     void drawLoadScreen();
@@ -170,7 +175,7 @@ class Render {
170 175
     int mSkyMesh;                         //!< Skymesh model id
171 176
     bool mSkyMeshRotation;                //!< Should Skymesh be rotated?
172 177
 
173
-    int debugTexture, debugTextile;
178
+    int debugTexture, debugTextile, debugSprite, debugSpriteOffset;
174 179
     TextureManager::TextureStorage debugTextureStorage;
175 180
     float debugX, debugY, debugW, debugH;
176 181
 };

+ 19
- 6
include/Sprite.h View File

@@ -15,23 +15,33 @@
15 15
 
16 16
 class Sprite {
17 17
   public:
18
-    Sprite(struct vertex_t vert, uint16_t tex);
18
+    Sprite(uint16_t tile, uint8_t x, uint8_t y, uint16_t width, uint16_t height);
19 19
 
20
-    // Old API
21 20
     Sprite(TombRaider& tr, unsigned int room, unsigned int index);
22
-    Sprite(TombRaider& tr, unsigned int item, unsigned int sequence, unsigned int index);
21
+
22
+    Sprite(TombRaider& tr, unsigned int item, unsigned int sequence, unsigned int index) :
23
+        Sprite((tr.Sprite() + ((tr.SpriteSequence() + sequence)->offset) + index)->tile,
24
+        (tr.Sprite() + ((tr.SpriteSequence() + sequence)->offset) + index)->x,
25
+        (tr.Sprite() + ((tr.SpriteSequence() + sequence)->offset) + index)->y,
26
+        (tr.Sprite() + ((tr.SpriteSequence() + sequence)->offset) + index)->width,
27
+        (tr.Sprite() + ((tr.SpriteSequence() + sequence)->offset) + index)->height) { }
28
+
23 29
     void display();
30
+    void display(float x, float y, float w, float h);
24 31
 
25 32
   private:
26 33
     float vertex[4][3];
27 34
     float texel[4][2];
28
-    float pos[3];
29
-    float radius; //!< \fixme yeah, I know (I don't? --xythobuz)
35
+    //float pos[3];
36
+    //float radius; //!< \fixme yeah, I know (I don't? --xythobuz)
30 37
     int texture;
31 38
 };
32 39
 
33 40
 class SpriteSequence {
34 41
   public:
42
+    SpriteSequence(int32_t objectID);
43
+    void add(Sprite s);
44
+
35 45
     SpriteSequence(TombRaider& tr, unsigned int item, unsigned int sequence);
36 46
     ~SpriteSequence();
37 47
 
@@ -39,7 +49,10 @@ class SpriteSequence {
39 49
     Sprite& get(unsigned long index);
40 50
 
41 51
   private:
42
-    std::vector<Sprite*> sprites;
52
+    int32_t id;
53
+    std::vector<Sprite> sprites;
54
+
55
+    std::vector<Sprite*> oldsprites;
43 56
 };
44 57
 
45 58
 #endif

+ 5
- 5
include/World.h View File

@@ -34,23 +34,23 @@ class World {
34 34
      */
35 35
     void destroy();
36 36
 
37
-    void addRoom(Room& room);
37
+    void addRoom(Room* room);
38 38
     unsigned long sizeRoom();
39 39
     Room& getRoom(unsigned long index);
40 40
 
41
-    void addSprite(SpriteSequence& sprite);
41
+    void addSprite(SpriteSequence* sprite);
42 42
     unsigned long sizeSprite();
43 43
     SpriteSequence& getSprite(unsigned long index);
44 44
 
45
-    void addEntity(Entity& entity);
45
+    void addEntity(Entity* entity);
46 46
     unsigned long sizeEntity();
47 47
     Entity& getEntity(unsigned long index);
48 48
 
49
-    void addSkeletalModel(SkeletalModel& model);
49
+    void addSkeletalModel(SkeletalModel* model);
50 50
     unsigned long sizeSkeletalModel();
51 51
     SkeletalModel& getSkeletalModel(unsigned long index);
52 52
 
53
-    void addStaticMesh(StaticMesh& model);
53
+    void addStaticMesh(StaticMesh* model);
54 54
     unsigned long sizeStaticMesh();
55 55
     StaticMesh& getStaticMesh(unsigned long index);
56 56
 

+ 6
- 6
src/Game.cpp View File

@@ -189,7 +189,7 @@ void Game::processSprites() {
189 189
 
190 190
         for (int j = 0; j < mTombRaider.NumSpriteSequences(); j++) {
191 191
             if (mTombRaider.SpriteSequence()[j].object_id == mTombRaider.Item()[i].object_id)
192
-                getWorld().addSprite(*new SpriteSequence(mTombRaider, i, j));
192
+                getWorld().addSprite(new SpriteSequence(mTombRaider, i, j));
193 193
         }
194 194
     }
195 195
 
@@ -198,14 +198,14 @@ void Game::processSprites() {
198 198
 
199 199
 void Game::processRooms() {
200 200
     for (int index = 0; index < mTombRaider.NumRooms(); index++)
201
-        getWorld().addRoom(*new Room(mTombRaider, index));
201
+        getWorld().addRoom(new Room(mTombRaider, index));
202 202
 
203 203
     getLog() << "Found " << mTombRaider.NumRooms() << " rooms." << Log::endl;
204 204
 }
205 205
 
206 206
 void Game::processModels() {
207 207
     for (int index = 0; index < mTombRaider.getMeshCount(); index++)
208
-        getWorld().addStaticMesh(*new StaticMesh(mTombRaider, index));
208
+        getWorld().addStaticMesh(new StaticMesh(mTombRaider, index));
209 209
 
210 210
     getLog() << "Found " << mTombRaider.getMeshCount() << " meshes." << Log::endl;
211 211
 }
@@ -386,14 +386,14 @@ void Game::processMoveable(int index, int i, int object_id) {
386 386
 
387 387
     // Create a new SkeletalModel, if needed
388 388
     if (!cached)
389
-        getWorld().addSkeletalModel(*new SkeletalModel(mTombRaider, index, object_id));
389
+        getWorld().addSkeletalModel(new SkeletalModel(mTombRaider, index, object_id));
390 390
 
391 391
     // Create a new Entity, using the cached or the new SkeletalModel
392
-    Entity& entity = *new Entity(mTombRaider, index, i, model);
392
+    Entity* entity = new Entity(mTombRaider, index, i, model);
393 393
     getWorld().addEntity(entity);
394 394
 
395 395
     // Store reference to Lara
396
-    if (entity.getObjectId() == 0)
396
+    if (entity->getObjectId() == 0)
397 397
         mLara = getWorld().sizeEntity() - 1;
398 398
 
399 399
     // Store reference to the SkyMesh

+ 31
- 2
src/Render.cpp View File

@@ -31,7 +31,9 @@ Render::Render() {
31 31
 
32 32
     debugTexture = -1;
33 33
     debugTextile = -1;
34
+    debugSprite = -1;
34 35
     debugTextureStorage = TextureManager::TextureStorage::GAME;
36
+    debugSpriteOffset = 0;
35 37
     debugX = 0.0f;
36 38
     debugY = 0.0f;
37 39
     debugW = 256.0f;
@@ -342,6 +344,10 @@ void Render::display() {
342 344
             getWindow().glEnter2D();
343 345
             drawTextile(debugX, debugY, debugW, debugH, debugTextile);
344 346
             getWindow().glExit2D();
347
+        } else if (debugSprite >= 0) {
348
+            getWindow().glEnter2D();
349
+            drawSprite(debugX, debugY, debugW, debugH, debugSprite, debugSpriteOffset);
350
+            getWindow().glExit2D();
345 351
         }
346 352
     }
347 353
 
@@ -361,6 +367,8 @@ void Render::drawLoadScreen() {
361 367
         drawTexture(debugX, debugY, debugW, debugH, debugTexture, debugTextureStorage);
362 368
     else if (debugTextile >= 0)
363 369
         drawTextile(debugX, debugY, debugW, debugH, debugTextile);
370
+    else if (debugSprite >= 0)
371
+        drawSprite(debugX, debugY, debugW, debugH, debugSprite, debugSpriteOffset);
364 372
 
365 373
     getWindow().glExit2D();
366 374
 
@@ -532,14 +540,35 @@ void Render::debugDisplayTextile(int texture, float x, float y, float w, float h
532 540
 }
533 541
 
534 542
 void Render::drawTextile(float x, float y, float w, float h, unsigned int textile) {
535
-    float z = 0.0f;
543
+    glColor3ubv(WHITE);
544
+
545
+    if (mFlags & Render::fGL_Lights)
546
+        glDisable(GL_LIGHTING);
547
+
548
+    getTextureManager().getTile(textile).display(x, y, w, h, 0.0f);
549
+
550
+    if (mFlags & Render::fGL_Lights)
551
+        glEnable(GL_LIGHTING);
552
+}
553
+
554
+void Render::debugDisplaySprite(int sprite, int offset, float x, float y, float w, float h) {
555
+    debugSprite = sprite;
556
+    debugSpriteOffset = offset;
557
+    debugX = x;
558
+    debugY = y;
559
+    debugW = w;
560
+    debugH = h;
561
+    debugTexture = -1;
562
+    debugTextile = -1;
563
+}
536 564
 
565
+void Render::drawSprite(float x, float y, float w, float h, unsigned int sprite, unsigned int offset) {
537 566
     glColor3ubv(WHITE);
538 567
 
539 568
     if (mFlags & Render::fGL_Lights)
540 569
         glDisable(GL_LIGHTING);
541 570
 
542
-    getTextureManager().getTile(textile).display(x, y, w, h, z);
571
+    getWorld().getSprite(sprite).get(offset).display(x, y, w, h);
543 572
 
544 573
     if (mFlags & Render::fGL_Lights)
545 574
         glEnable(GL_LIGHTING);

+ 1
- 1
src/SoundManager.cpp View File

@@ -36,7 +36,7 @@ int SoundManager::prepareSources() {
36 36
     for (int i = 0; i < soundMap.size(); i++) {
37 37
         float vol;
38 38
         int index = getIndex(i, &vol);
39
-        if (index >= 0) {
39
+        if ((index >= 0) && (index < Sound::numBuffers())) {
40 40
             int ret = Sound::addSource(index, vol, true, false);
41 41
             assert(ret >= 0);
42 42
         }

+ 69
- 34
src/Sprite.cpp View File

@@ -12,47 +12,61 @@
12 12
 #include "TextureManager.h"
13 13
 #include "Sprite.h"
14 14
 
15
+SpriteSequence::SpriteSequence(int32_t objectID) {
16
+    id = objectID;
17
+}
18
+
19
+void SpriteSequence::add(Sprite s) {
20
+    sprites.push_back(s);
21
+}
22
+
23
+// -----------------------
24
+
15 25
 SpriteSequence::SpriteSequence(TombRaider& tr, unsigned int item, unsigned int sequence) {
16 26
     for (int i = 0; i < (-tr.SpriteSequence()[sequence].negative_length); i++)
17
-        sprites.push_back(new Sprite(tr, item, sequence, i));
27
+        oldsprites.push_back(new Sprite(tr, item, sequence, i));
18 28
 }
19 29
 
20 30
 SpriteSequence::~SpriteSequence() {
21
-    for (unsigned int i = 0; i < sprites.size(); i++)
22
-        delete sprites.at(i);
31
+    for (unsigned int i = 0; i < oldsprites.size(); i++)
32
+        delete oldsprites.at(i);
23 33
 }
24 34
 
25 35
 unsigned long SpriteSequence::size() {
26
-    return sprites.size();
36
+    if (oldsprites.size() > 0)
37
+        return oldsprites.size();
38
+    else
39
+        return sprites.size();
27 40
 }
28 41
 
29 42
 Sprite& SpriteSequence::get(unsigned long index) {
30
-    assert(index < sprites.size());
31
-    return *sprites.at(index);
43
+    if (oldsprites.size() > 0) {
44
+        assert(index < oldsprites.size());
45
+        return *oldsprites.at(index);
46
+    } else {
47
+        assert(index < sprites.size());
48
+        return sprites.at(index);
49
+    }
32 50
 }
33 51
 
34
-Sprite::Sprite(TombRaider& tr, unsigned int item, unsigned int sequence, unsigned int index) {
35
-    tr2_item_t* i = tr.Item();
36
-    tr2_sprite_texture_t* spriteTextures = tr.Sprite();
37
-    tr2_sprite_sequence_t* spriteSequence = tr.SpriteSequence();
52
+// ----------------------------------------------------------------------------
38 53
 
39
-    //! \fixme index was unused?!
40
-    tr2_sprite_texture_t* sprite = &spriteTextures[spriteSequence[sequence].offset + index];
54
+Sprite::Sprite(uint16_t tile, uint8_t x, uint8_t y, uint16_t width, uint16_t height) {
55
+    /*!
56
+     * \fixme TODO Can't do translation/visibility-check when rendering here.
57
+     * We don't know xyz of this Sprite because it could be used by
58
+     * different items at other coordinates in the world.
59
+     * Do translation/visibility-check at item/room level.
60
+     */
41 61
 
42
-    int width = sprite->width >> 8;
43
-    int height = sprite->height >> 8;
44
-    int x = sprite->x;
45
-    int y = sprite->y;
46
-    float scale = 4.0f;
47
-    float texelScale = 256.0f;
62
+    const float scale = 4.0f;
63
+    const float texelScale = 256.0f;
64
+
65
+    width >>= 8;
66
+    height >>= 8;
48 67
     int width2 = (int)(width * scale);
49 68
     int height2 = (int)(height * scale);
50 69
 
51
-    // For vising use
52
-    pos[0] = i[item].x;
53
-    pos[1] = i[item].y;
54
-    pos[2] = i[item].z;
55
-
56 70
     vertex[0][0] = -width2 / 2.0f;
57 71
     vertex[1][0] = -width2 / 2.0f;
58 72
     vertex[2][0] = width2 / 2.0f;
@@ -80,16 +94,19 @@ Sprite::Sprite(TombRaider& tr, unsigned int item, unsigned int sequence, unsigne
80 94
     texel[0][0] = (float)(x) / texelScale;
81 95
     texel[0][1] = (float)(y + height) / texelScale;
82 96
 
83
-    texture = sprite->tile;
84
-    radius = width2 / 2.0f;
97
+    texture = tile;
98
+    //radius = width2 / 2.0f;
85 99
 }
86 100
 
101
+// -----------------------
102
+
87 103
 Sprite::Sprite(TombRaider& tr, unsigned int room, unsigned int index) {
88 104
     float spriteVertices[12];
89 105
     float spriteTexCoords[8];
106
+    float posUnused[3];
90 107
 
91 108
     tr.getRoomSprite(room, index,
92
-                     10.0f, &texture, pos, spriteVertices, spriteTexCoords);
109
+                     10.0f, &texture, posUnused, spriteVertices, spriteTexCoords);
93 110
 
94 111
     for (unsigned int j = 0; j < 12; j++)
95 112
         vertex[j / 3][j % 3] = spriteVertices[j];
@@ -97,19 +114,15 @@ Sprite::Sprite(TombRaider& tr, unsigned int room, unsigned int index) {
97 114
     for (unsigned int j = 0; j < 8; j++)
98 115
         texel[j / 2][j % 2] = spriteTexCoords[j];
99 116
 
100
-    radius = 0.0f;
101
-}
102
-
103
-Sprite::Sprite(struct vertex_t vert, uint16_t tex) {
104
-
117
+    //radius = 0.0f;
105 118
 }
106 119
 
107 120
 void Sprite::display() {
108
-    if (!getRender().isVisible(pos[0], pos[1], pos[2], radius))
109
-        return;
121
+    //if (!getRender().isVisible(pos[0], pos[1], pos[2], radius))
122
+    //    return;
110 123
 
111 124
     glPushMatrix();
112
-    glTranslated(pos[0], pos[1], pos[2]);
125
+    //glTranslated(pos[0], pos[1], pos[2]);
113 126
 
114 127
     // Sprites must always face camera, because they have no depth =)
115 128
     glRotated(OR_RAD_TO_DEG(getCamera().getRadianYaw()), 0, 1, 0);
@@ -160,3 +173,25 @@ void Sprite::display() {
160 173
     glPopMatrix();
161 174
 }
162 175
 
176
+void Sprite::display(float x, float y, float w, float h) {
177
+    float z = 0.0f;
178
+
179
+    getTextureManager().bindTextureId(texture);
180
+
181
+    glBegin(GL_QUADS);
182
+
183
+    glTexCoord2fv(texel[1]);
184
+    glVertex3f(x, y, z);
185
+
186
+    glTexCoord2fv(texel[2]);
187
+    glVertex3f(x + w, y, z);
188
+
189
+    glTexCoord2fv(texel[3]);
190
+    glVertex3f(x + w, y + h, z);
191
+
192
+    glTexCoord2fv(texel[0]);
193
+    glVertex3f(x, y + h, z);
194
+
195
+    glEnd();
196
+}
197
+

+ 75
- 3
src/UI.cpp View File

@@ -17,6 +17,7 @@
17 17
 #include "RunTime.h"
18 18
 #include "SoundManager.h"
19 19
 #include "TextureManager.h"
20
+#include "World.h"
20 21
 #include "commands/Command.h"
21 22
 #include "system/Sound.h"
22 23
 #include "system/Window.h"
@@ -308,9 +309,12 @@ void UI::display() {
308 309
             }
309 310
         }
310 311
 
312
+        ImGui::Separator();
313
+
311 314
         static bool visibleTex = false;
312 315
         static bool visibleTile = false;
313 316
         static bool visibleAnim = false;
317
+        static bool visibleSprite = false;
314 318
         if (ImGui::CollapsingHeader("Texture Viewer")) {
315 319
             static bool game = getGame().isLoaded();
316 320
             static int index = 0;
@@ -348,6 +352,7 @@ void UI::display() {
348 352
                 visibleTex = true;
349 353
                 visibleTile = false;
350 354
                 visibleAnim = false;
355
+                visibleSprite = false;
351 356
             }
352 357
             ImGui::SameLine();
353 358
             if (ImGui::Button("Clear##texclear")) {
@@ -389,6 +394,7 @@ void UI::display() {
389 394
                     visibleTile = true;
390 395
                     visibleTex = false;
391 396
                     visibleAnim = false;
397
+                    visibleSprite = false;
392 398
                 }
393 399
                 ImGui::SameLine();
394 400
                 if (ImGui::Button("Clear##tileclear")) {
@@ -435,11 +441,11 @@ void UI::display() {
435 441
                     tile = getTextureManager().getFirstTileAnimation(index);
436 442
                 }
437 443
                 ImGui::SameLine();
438
-                static int fr = 0;
439 444
                 if (ImGui::Button("Show##animshow")) {
440 445
                     visibleAnim = true;
441 446
                     visibleTex = false;
442 447
                     visibleTile = false;
448
+                    visibleSprite = false;
443 449
                 }
444 450
                 ImGui::SameLine();
445 451
                 if (ImGui::Button("Clear##animclear")) {
@@ -447,6 +453,7 @@ void UI::display() {
447 453
                     visibleAnim = false;
448 454
                 }
449 455
                 if (visibleAnim) {
456
+                    static int fr = 0;
450 457
                     if (fr > 0) {
451 458
                         fr--;
452 459
                     } else {
@@ -464,7 +471,7 @@ void UI::display() {
464 471
             }
465 472
         }
466 473
 
467
-        if (ImGui::CollapsingHeader("SoundManager Player")) {
474
+        if (ImGui::CollapsingHeader("Sound Map Player")) {
468 475
             if (!Sound::getEnabled()) {
469 476
                 ImGui::Text("Please enable Sound first!");
470 477
                 if (ImGui::Button("Enable Sound!")) {
@@ -500,7 +507,72 @@ void UI::display() {
500 507
             }
501 508
         }
502 509
 
503
-        if (ImGui::CollapsingHeader("ImGui UI Help")) {
510
+        if (ImGui::CollapsingHeader("Sprite Sequence Viewer")) {
511
+            if (getWorld().sizeSprite() <= 0) {
512
+                ImGui::Text("Please load a level containing sprites!");
513
+            } else {
514
+                static int index = 0;
515
+                static int sprite = 0;
516
+                ImGui::PushItemWidth(ImGui::GetWindowWidth() * 0.5f);
517
+                if (ImGui::SliderInt("##spriteslide", &index, 0, getWorld().sizeSprite() - 1))
518
+                    sprite = 0;
519
+                ImGui::PopItemWidth();
520
+                ImGui::SameLine();
521
+                if (ImGui::Button("+##spriteplus", ImVec2(0, 0), true)) {
522
+                    if (index < (getWorld().sizeSprite() - 1))
523
+                        index++;
524
+                    else
525
+                        index = 0;
526
+                    sprite = 0;
527
+                }
528
+                ImGui::SameLine();
529
+                if (ImGui::Button("-##spriteminus", ImVec2(0, 0), true)) {
530
+                    if (index > 0)
531
+                        index--;
532
+                    else
533
+                        index = getWorld().sizeSprite() - 1;
534
+                    sprite = 0;
535
+                }
536
+                ImGui::SameLine();
537
+                if (ImGui::Button("Show##spriteshow")) {
538
+                    visibleSprite = true;
539
+                    visibleTex = false;
540
+                    visibleTile = false;
541
+                    visibleAnim = false;
542
+                    sprite = 0;
543
+                }
544
+                ImGui::SameLine();
545
+                if (ImGui::Button("Clear##spriteclear")) {
546
+                    getRender().debugDisplaySprite();
547
+                    visibleSprite = false;
548
+                }
549
+                if (visibleSprite) {
550
+                    static int fr = 0;
551
+                    if (fr > 0) {
552
+                        fr--;
553
+                    } else {
554
+                        getRender().debugDisplaySprite(index, sprite,
555
+                                                       ImGui::GetWindowPos().x - (ImGui::GetWindowWidth() / 2),
556
+                                                       ImGui::GetWindowPos().y,
557
+                                                       (ImGui::GetWindowWidth() / 2), (ImGui::GetWindowWidth() / 2));
558
+                        fr = getRunTime().getFPS() / 10;
559
+                        if (sprite < (getWorld().getSprite(index).size() - 1))
560
+                            sprite++;
561
+                        else
562
+                            sprite = 0;
563
+                    }
564
+
565
+                    ImGui::Text("Sprite %d/%d", sprite + 1, getWorld().getSprite(index).size());
566
+                }
567
+            }
568
+        }
569
+
570
+        ImGui::Separator();
571
+
572
+        if (ImGui::CollapsingHeader("ImGui/Debug UI Help")) {
573
+            ImGui::TextWrapped("DebugViewer Textures/Textiles/Sprites will be drawn on"
574
+                               " the left side and scale with the size of this window!");
575
+            ImGui::Separator();
504 576
             ImGui::ShowUserGuide();
505 577
         }
506 578
     }

+ 10
- 10
src/World.cpp View File

@@ -15,8 +15,8 @@ World::~World() {
15 15
     destroy();
16 16
 }
17 17
 
18
-void World::addRoom(Room& room) {
19
-    mRooms.push_back(std::unique_ptr<Room>(&room));
18
+void World::addRoom(Room* room) {
19
+    mRooms.emplace_back(std::unique_ptr<Room>(room));
20 20
 }
21 21
 
22 22
 unsigned long World::sizeRoom() {
@@ -28,8 +28,8 @@ Room& World::getRoom(unsigned long index) {
28 28
     return *mRooms.at(index);
29 29
 }
30 30
 
31
-void World::addSprite(SpriteSequence& sprite) {
32
-    mSprites.push_back(std::unique_ptr<SpriteSequence>(&sprite));
31
+void World::addSprite(SpriteSequence* sprite) {
32
+    mSprites.emplace_back(std::unique_ptr<SpriteSequence>(sprite));
33 33
 }
34 34
 
35 35
 unsigned long World::sizeSprite() {
@@ -41,8 +41,8 @@ SpriteSequence& World::getSprite(unsigned long index) {
41 41
     return *mSprites.at(index);
42 42
 }
43 43
 
44
-void World::addEntity(Entity& entity) {
45
-    mEntities.push_back(std::unique_ptr<Entity>(&entity));
44
+void World::addEntity(Entity* entity) {
45
+    mEntities.emplace_back(std::unique_ptr<Entity>(entity));
46 46
 }
47 47
 
48 48
 unsigned long World::sizeEntity() {
@@ -54,8 +54,8 @@ Entity& World::getEntity(unsigned long index) {
54 54
     return *mEntities.at(index);
55 55
 }
56 56
 
57
-void World::addSkeletalModel(SkeletalModel& model) {
58
-    mModels.push_back(std::unique_ptr<SkeletalModel>(&model));
57
+void World::addSkeletalModel(SkeletalModel* model) {
58
+    mModels.emplace_back(std::unique_ptr<SkeletalModel>(model));
59 59
 }
60 60
 
61 61
 unsigned long World::sizeSkeletalModel() {
@@ -67,8 +67,8 @@ SkeletalModel& World::getSkeletalModel(unsigned long index) {
67 67
     return *mModels.at(index);
68 68
 }
69 69
 
70
-void World::addStaticMesh(StaticMesh& model) {
71
-    mMeshes.push_back(std::unique_ptr<StaticMesh>(&model));
70
+void World::addStaticMesh(StaticMesh* model) {
71
+    mMeshes.emplace_back(std::unique_ptr<StaticMesh>(model));
72 72
 }
73 73
 
74 74
 unsigned long World::sizeStaticMesh() {

+ 100
- 13
src/loader/LoaderTR2.cpp View File

@@ -15,6 +15,7 @@
15 15
 #include "Room.h"
16 16
 #include "SoundManager.h"
17 17
 #include "TextureManager.h"
18
+#include "World.h"
18 19
 #include "system/Sound.h"
19 20
 #include "utils/pixel.h"
20 21
 #include "loader/LoaderTR2.h"
@@ -235,9 +236,9 @@ void LoaderTR2::loadRooms() {
235 236
         uint16_t numSprites = file.readU16();
236 237
         for (unsigned int s = 0; s < numSprites; s++) {
237 238
             uint16_t vertex = file.readU16(); // Index into vertex list
238
-            uint16_t texture = file.readU16(); // Index into object-texture list
239
+            uint16_t sprite = file.readU16(); // Index into sprite list
239 240
 
240
-            room->addSprite(new Sprite(vertices.at(vertex), texture));
241
+            // TODO store sprites somewhere
241 242
         }
242 243
 
243 244
         uint16_t numPortals = file.readU16();
@@ -336,6 +337,8 @@ void LoaderTR2::loadRooms() {
336 337
         int16_t alternateRoom = file.read16();
337 338
 
338 339
         uint16_t flags = file.readU16();
340
+
341
+        getWorld().addRoom(room);
339 342
     }
340 343
 }
341 344
 
@@ -353,18 +356,21 @@ void LoaderTR2::loadFloorData() {
353 356
 
354 357
 void LoaderTR2::loadSprites() {
355 358
     uint32_t numSpriteTextures = file.readU32();
359
+    std::vector<Sprite> sprites;
356 360
     for (unsigned int s = 0; s < numSpriteTextures; s++) {
357 361
         uint16_t tile = file.readU16();
358 362
         uint8_t x = file.readU8();
359 363
         uint8_t y = file.readU8();
360 364
         uint16_t width = file.readU16(); // Actually (width * 256) + 255
361 365
         uint16_t height = file.readU16(); // Actually (height * 256) + 255
366
+
367
+        // Required for what?
362 368
         int16_t leftSide = file.read16();
363 369
         int16_t topSide = file.read16();
364 370
         int16_t rightSide = file.read16();
365 371
         int16_t bottomSide = file.read16();
366 372
 
367
-        // TODO store sprite textures somewhere
373
+        sprites.emplace_back(tile, x, y, width, height);
368 374
     }
369 375
 
370 376
     uint32_t numSpriteSequences = file.readU32();
@@ -373,7 +379,15 @@ void LoaderTR2::loadSprites() {
373 379
         int16_t negativeLength = file.read16(); // Negative sprite count
374 380
         int16_t offset = file.read16(); // Where sequence starts in sprite texture list
375 381
 
376
-        // TODO store sprite sequences somewhere
382
+        assert(negativeLength < 0);
383
+        assert(offset >= 0);
384
+        assert((offset + (negativeLength * -1)) <= numSpriteTextures);
385
+
386
+        SpriteSequence* ss = new SpriteSequence(objectID);
387
+        for (int i = 0; i < (negativeLength * -1); i++) {
388
+            ss->add(sprites.at(offset + i));
389
+        }
390
+        getWorld().addSprite(ss);
377 391
     }
378 392
 }
379 393
 
@@ -399,10 +413,85 @@ void LoaderTR2::loadMeshes() {
399 413
 
400 414
     for (unsigned int i = 0; i < numMeshPointers; i++) {
401 415
         uint32_t meshPointer = file.readU32();
402
-        char* tmpPtr = reinterpret_cast<char*>(&buffer[meshPointer]);
403
-        BinaryMemory mem(tmpPtr, numMeshData - meshPointer);
404 416
 
405
-        // TODO interpret the buffered mesh data
417
+        char* tmpPtr = reinterpret_cast<char*>(&buffer[meshPointer / 2]);
418
+        BinaryMemory mem(tmpPtr, (numMeshData * 2) - meshPointer);
419
+
420
+        int16_t mx = mem.read16();
421
+        int16_t my = mem.read16();
422
+        int16_t mz = mem.read16();
423
+
424
+        int32_t collisionSize = mem.read32();
425
+
426
+        uint16_t numVertices = mem.readU16();
427
+        for (int v = 0; v < numVertices; v++) {
428
+            int16_t x = mem.read16();
429
+            int16_t y = mem.read16();
430
+            int16_t z = mem.read16();
431
+
432
+        }
433
+
434
+        int16_t numNormals = mem.read16();
435
+        if (numNormals > 0) {
436
+            // External vertex lighting is used, with the lighting calculated
437
+            // from the rooms ambient and point-source lighting values. The
438
+            // latter appears to use a simple Lambert law for directionality:
439
+            // intensity is proportional to
440
+            //      max((normal direction).(direction to source), 0)
441
+            for (int n = 0; n < numNormals; n++) {
442
+                int16_t x = mem.read16();
443
+                int16_t y = mem.read16();
444
+                int16_t z = mem.read16();
445
+
446
+            }
447
+        } else if (numNormals < 0) {
448
+            // Internal vertex lighting is used,
449
+            // using the data included with the mesh
450
+            for (int l = 0; l < (numNormals * -1); l++) {
451
+                int16_t light = mem.read16();
452
+
453
+            }
454
+        }
455
+
456
+        int16_t numTexturedRectangles = mem.read16();
457
+        for (int r = 0; r < numTexturedRectangles; r++) {
458
+            uint16_t vertex1 = mem.readU16();
459
+            uint16_t vertex2 = mem.readU16();
460
+            uint16_t vertex3 = mem.readU16();
461
+            uint16_t vertex4 = mem.readU16();
462
+            uint16_t texture = mem.readU16();
463
+
464
+        }
465
+
466
+        int16_t numTexturedTriangles = mem.read16();
467
+        for (int t = 0; t < numTexturedTriangles; t++) {
468
+            uint16_t vertex1 = mem.readU16();
469
+            uint16_t vertex2 = mem.readU16();
470
+            uint16_t vertex3 = mem.readU16();
471
+            uint16_t texture = mem.readU16();
472
+
473
+        }
474
+
475
+        int16_t numColoredRectangles = mem.read16();
476
+        for (int r = 0; r < numColoredRectangles; r++) {
477
+            uint16_t vertex1 = mem.readU16();
478
+            uint16_t vertex2 = mem.readU16();
479
+            uint16_t vertex3 = mem.readU16();
480
+            uint16_t vertex4 = mem.readU16();
481
+            uint16_t texture = mem.readU16();
482
+
483
+        }
484
+
485
+        int16_t numColoredTriangles = mem.read16();
486
+        for (int t = 0; t < numColoredTriangles; t++) {
487
+            uint16_t vertex1 = mem.readU16();
488
+            uint16_t vertex2 = mem.readU16();
489
+            uint16_t vertex3 = mem.readU16();
490
+            uint16_t texture = mem.readU16();
491
+
492
+        }
493
+
494
+        // TODO store mesh data somewhere
406 495
     }
407 496
 }
408 497
 
@@ -509,12 +598,10 @@ void LoaderTR2::loadMoveables() {
509 598
 
510 599
 
511 600
         // Offset of mesh origin from the parent mesh origin
512
-
513
-        /* Where the hell does this come from?!
514
-        int32_t x = file.read32();
515
-        int32_t y = file.read32();
516
-        int32_t z = file.read32();
517
-        */
601
+        //int32_t x = file.read32();
602
+        //int32_t y = file.read32();
603
+        //int32_t z = file.read32();
604
+        // Does not appear to be true...?
518 605
 
519 606
         // TODO store mesh trees somewhere
520 607
     }

+ 4
- 0
src/system/SoundAL.cpp View File

@@ -78,12 +78,16 @@ void SoundAL::clear() {
78 78
 
79 79
     alGetError();
80 80
     alDeleteBuffers(buffers.size(), &buffers[0]);
81
+    buffers.clear();
81 82
     if (alGetError() != AL_NO_ERROR) {
82 83
         getLog() << "SoundAL: Error while deleting buffers!" << Log::endl;
83 84
     }
84 85
 
85 86
     for (int i = 0; i < 3; i++)
86 87
         lastPosition[i] = 0.0f;
88
+
89
+    float orientation[6] = { 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f };
90
+    listenAt(lastPosition, orientation);
87 91
 }
88 92
 
89 93
 int SoundAL::numBuffers() {

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

@@ -192,7 +192,7 @@ void BinaryMemory::read(char* d, int c) {
192 192
     if ((offset + c) > max) {
193 193
         std::ostringstream ss;
194 194
         ss << "BinaryMemory read out of bounds ("
195
-           << offset << " + " << c << " > " << max;
195
+           << offset << " + " << c << " > " << max << ")";
196 196
         throw new Exception(ss.str());
197 197
     }
198 198
 

Loading…
Cancel
Save