Ver código fonte

Started implementing new level file loader

Thomas Buck 9 anos atrás
pai
commit
3156a0bad8

+ 3
- 0
ChangeLog.md Ver arquivo

@@ -2,6 +2,9 @@
2 2
 
3 3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4 4
 
5
+    [ 20140812 ]
6
+    * Started implementing new level file loader, currently TR2 only
7
+
5 8
     [ 20140811 ]
6 9
     * New Font API, allows drawing strings with line-wrapping
7 10
     * Added ability to Menu to show info dialogs

+ 3
- 1
TODO.md Ver arquivo

@@ -2,7 +2,7 @@
2 2
 
3 3
 ## General
4 4
 
5
-* Move to newer OpenGL (GLES or 3.x or 4.x?)
5
+* Move to newer OpenGL (GL ES or 2.1 or 3.x or 4.x?)
6 6
 * Endian dependence ugly, shouldn't dereference to different types?
7 7
     * TombRaider.h/cpp structs aren't aligned... unportable to some big endian & other archs?!
8 8
     * Maybe replace loader with [VT](http://icculus.org/vt/), also used by OpenTomb.
@@ -20,6 +20,8 @@
20 20
 
21 21
 ## Future Features
22 22
 
23
+* Depend on libcdio, use it to read original CDs or CUE/TOC/ISO images
24
+* Depend on imgui for nicer GUIs?
23 25
 * Add ability to play the FMVs. Format? VLC can play them!
24 26
 * Cut TGA image reader, currently only used for menu background?!
25 27
     * Need useful, always available image writer alternative for screenshots then

+ 1
- 1
include/Mesh.h Ver arquivo

@@ -119,7 +119,7 @@ public:
119 119
 
120 120
     void setVertex(unsigned int index, float x, float y, float z);
121 121
 
122
-#ifdef NOT_IMPLEMENTED
122
+#if 0
123 123
     void sortFacesByTexture();
124 124
 
125 125
     void addFace(int textureIndex, int textureIndexB, unsigned int flags,

+ 1
- 1
include/Script.h Ver arquivo

@@ -41,7 +41,7 @@ public:
41 41
         OP_PSX_DEMO       = 8,  //!< Does not compile. PSX?
42 42
         OP_END            = 9,  //!< Closes script sequence
43 43
         OP_TRACK          = 10, //!< Play soundtrack (precedes level opcode)
44
-        OP_SUNSET         = 11, //!< Unknown, nothing changes in TR2.
44
+        OP_SUNSET         = 11, //!< Unknown, nothing changes in TR2. Start in Motorboat?
45 45
         OP_LOAD_PIC       = 12, //!< Does not compile. PSX? Used in TR3.
46 46
         OP_DEADLY_WATER   = 13, //!< Unknown, nothing changes in TR2.
47 47
         OP_REMOVE_WEAPONS = 14, //!< Start level without weapons

+ 39
- 0
include/loader/Loader.h Ver arquivo

@@ -0,0 +1,39 @@
1
+/*!
2
+ * \file include/loader/Loader.h
3
+ * \brief Level file loader
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _LOADER_LOADER_H_
9
+#define _LOADER_LOADER_H_
10
+
11
+#include <string>
12
+#include "utils/binary.h"
13
+
14
+class Loader {
15
+public:
16
+
17
+    typedef enum {
18
+        TR_UNKNOWN = 0,
19
+        TR_1 = 1,
20
+        TR_2 = 2,
21
+        TR_3 = 3,
22
+        TR_4 = 4,
23
+        TR_5 = 5
24
+    } LoaderVersion;
25
+
26
+    static LoaderVersion checkFile(std::string f);
27
+
28
+    static Loader *createLoader(std::string f);
29
+
30
+    virtual ~Loader();
31
+
32
+    virtual int load(std::string f) = 0;
33
+
34
+protected:
35
+    BinaryFile file;
36
+};
37
+
38
+#endif
39
+

+ 37
- 0
include/loader/LoaderTR2.h Ver arquivo

@@ -0,0 +1,37 @@
1
+/*!
2
+ * \file include/loader/LoaderTR2.h
3
+ * \brief TR2 level file loader
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _LOADER_LOADER_TR2_H_
9
+#define _LOADER_LOADER_TR2_H_
10
+
11
+#include <cstdint>
12
+
13
+#include "loader/Loader.h"
14
+
15
+class LoaderTR2 : public Loader {
16
+public:
17
+    LoaderTR2();
18
+    virtual ~LoaderTR2();
19
+
20
+    virtual int load(std::string f);
21
+
22
+
23
+private:
24
+
25
+    void loadPaletteTextiles();
26
+
27
+    void loadRooms();
28
+
29
+    uint32_t *palette; //!< 256 * 4 bytes, RGBA, A unused
30
+    uint32_t numTextiles;
31
+    uint16_t **textiles; //!< numTextiles, 256 * 256 * 2 bytes, ARGB (MSB + 3x5bit)
32
+
33
+    uint16_t numRooms;
34
+};
35
+
36
+#endif
37
+

+ 2
- 0
src/CMakeLists.txt Ver arquivo

@@ -214,6 +214,7 @@ set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${OpenRaider_CXX_FLAGS}
214 214
 
215 215
 # Add subdirectories
216 216
 add_subdirectory ("deps")
217
+add_subdirectory ("loader")
217 218
 add_subdirectory ("math")
218 219
 add_subdirectory ("utils")
219 220
 
@@ -222,6 +223,7 @@ add_library (OpenRaider_all OBJECT ${SRCS})
222 223
 add_executable (OpenRaider MACOSX_BUNDLE ${RESRCS}
223 224
     $<TARGET_OBJECTS:OpenRaider_all> $<TARGET_OBJECTS:OpenRaider_deps>
224 225
     $<TARGET_OBJECTS:OpenRaider_math> $<TARGET_OBJECTS:OpenRaider_utils>
226
+    $<TARGET_OBJECTS:OpenRaider_loader>
225 227
 )
226 228
 
227 229
 #################################################################

+ 9
- 6
src/MenuFolder.cpp Ver arquivo

@@ -8,8 +8,8 @@
8 8
 #include "global.h"
9 9
 #include "Console.h"
10 10
 #include "Font.h"
11
+#include "loader/Loader.h"
11 12
 #include "OpenRaider.h"
12
-#include "TombRaider.h"
13 13
 #include "Window.h"
14 14
 #include "MenuFolder.h"
15 15
 
@@ -53,10 +53,10 @@ int MenuFolder::initialize(Folder *folder, bool filter) {
53 53
             }
54 54
 
55 55
             // Check maps for validity
56
-            int error = TombRaider::checkMime(f.getPath().c_str());
57
-            if (error != 0) {
56
+            Loader::LoaderVersion version = Loader::checkFile(f.getPath());
57
+            if (version == Loader::TR_UNKNOWN) {
58 58
                 getConsole() << "Error: pak file '" << f.getName().c_str()
59
-                    << "' " << ((error == -1) ? "not found" : "invalid") << Console::endl;
59
+                    << "' invalid" << Console::endl;
60 60
                 return true; // delete file from list
61 61
             }
62 62
 
@@ -115,10 +115,13 @@ void MenuFolder::loadOrOpen() {
115 115
     } else {
116 116
         std::string tmp = "load ";
117 117
         tmp += mapFolder->getFile((unsigned long)mCursor - 1 - mapFolder->folderCount()).getPath();
118
-        if (getOpenRaider().command(tmp.c_str()) == 0) {
118
+        int error = getOpenRaider().command(tmp.c_str());
119
+        if (error == 0) {
119 120
             setVisible(false);
120 121
         } else {
121
-            showDialog("Error loading map!", "OK", "");
122
+            std::ostringstream err;
123
+            err << "Error loading map: " << error << "!";
124
+            showDialog(err.str(), "OK", "");
122 125
         }
123 126
     }
124 127
 }

+ 7
- 0
src/loader/CMakeLists.txt Ver arquivo

@@ -0,0 +1,7 @@
1
+# Source files
2
+set (LOADER_SRCS ${LOADER_SRCS} "Loader.cpp")
3
+set (LOADER_SRCS ${LOADER_SRCS} "LoaderTR2.cpp")
4
+
5
+# Add library
6
+add_library (OpenRaider_loader OBJECT ${LOADER_SRCS})
7
+

+ 55
- 0
src/loader/Loader.cpp Ver arquivo

@@ -0,0 +1,55 @@
1
+/*!
2
+ * \file src/loader/Loader.cpp
3
+ * \brief Level file loader
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#include "global.h"
9
+#include "loader/Loader.h"
10
+#include "loader/LoaderTR2.h"
11
+
12
+Loader::LoaderVersion Loader::checkFile(std::string f) {
13
+    BinaryFile file;
14
+    if (file.open(f.c_str()) != 0)
15
+        return TR_UNKNOWN;
16
+
17
+    uint32_t start = file.readU32();
18
+    switch (start) {
19
+        case 0x00000020:
20
+            return TR_1;
21
+
22
+        case 0x0000002D:
23
+            return TR_2;
24
+
25
+        case 0xFF080038:
26
+        case 0xFF180038:
27
+            return TR_3;
28
+
29
+        case 0xFFFFFFF0: // bogus
30
+        case 0x00345254: // "TR4\0"
31
+            return TR_4;
32
+    }
33
+
34
+    return TR_UNKNOWN;
35
+}
36
+
37
+Loader *Loader::createLoader(std::string f) {
38
+    LoaderVersion v = checkFile(f);
39
+    switch (v) {
40
+        case TR_1:
41
+        case TR_3:
42
+        case TR_4:
43
+        case TR_5:
44
+        case TR_UNKNOWN:
45
+            return NULL;
46
+
47
+        case TR_2:
48
+            return new LoaderTR2();
49
+    }
50
+}
51
+
52
+Loader::~Loader() {
53
+
54
+}
55
+

+ 84
- 0
src/loader/LoaderTR2.cpp Ver arquivo

@@ -0,0 +1,84 @@
1
+/*!
2
+ * \file src/loader/LoaderTR2.cpp
3
+ * \brief TR2 level file loader
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#include "loader/LoaderTR2.h"
9
+
10
+LoaderTR2::LoaderTR2() {
11
+    palette = new uint32_t[256];
12
+    numTextiles = 0;
13
+    textiles = nullptr;
14
+}
15
+
16
+LoaderTR2::~LoaderTR2() {
17
+    delete [] palette;
18
+    palette = nullptr;
19
+
20
+    if (textiles != nullptr) {
21
+        for (unsigned int i = 0; i < numTextiles; i++)
22
+            delete [] textiles[i];
23
+        delete [] textiles;
24
+    }
25
+}
26
+
27
+int LoaderTR2::load(std::string f) {
28
+    if (file.open(f.c_str()) != 0) {
29
+        return 1; // Could not open file
30
+    }
31
+
32
+    if (file.readU32() != 0x2D) {
33
+        return 2; // Not a TR2 level?!
34
+    }
35
+
36
+    loadPaletteTextiles();
37
+
38
+    file.seek(file.tell() + 4); // Unused value?
39
+
40
+    loadRooms();
41
+
42
+
43
+
44
+    return 0;
45
+}
46
+
47
+void LoaderTR2::loadPaletteTextiles() {
48
+    file.seek(file.tell() + 768); // Skip 8bit palette, 256 * 3 bytes
49
+
50
+    // Read the 16bit palette, 256 * 4 bytes, RGBA, A unused
51
+    for (unsigned int i = 0; i < 256; i++)
52
+        palette[i] = file.readU32();
53
+
54
+    numTextiles = file.readU32();
55
+
56
+    file.seek(file.tell() + (numTextiles * 256 * 256)); // Skip 8bit textiles
57
+
58
+    // Read the 16bit textiles, numTextiles * 256 * 256 * 2 bytes
59
+    textiles = new uint16_t *[numTextiles];
60
+    for (unsigned int i = 0; i < numTextiles; i++) {
61
+        textiles[i] = new uint16_t[256 * 256];
62
+        for (unsigned int j = 0; j < (256 * 256); j++) {
63
+            textiles[i][j] = file.readU16();
64
+        }
65
+    }
66
+}
67
+
68
+void LoaderTR2::loadRooms() {
69
+    numRooms = file.readU16();
70
+
71
+    for (unsigned int i = 0; i < numRooms; i++) {
72
+        // Room Header
73
+        int32_t xOffset = file.read32();
74
+        int32_t zOffset = file.read32();
75
+        int32_t yBottom = file.read32(); // lowest point == largest y value
76
+        int32_t yTop = file.read32(); // highest point == smallest y value
77
+
78
+        // Number of data words (2 bytes) to follow
79
+        uint32_t dataToFollow = file.readU32();
80
+
81
+
82
+    }
83
+}
84
+

Carregando…
Cancelar
Salvar