Quellcode durchsuchen

TextureManager using vector, now also global service

Thomas Buck vor 10 Jahren
Ursprung
Commit
8e09359af3
12 geänderte Dateien mit 135 neuen und 256 gelöschten Zeilen
  1. 2
    0
      ChangeLog.md
  2. 0
    21
      include/Render.h
  3. 7
    47
      include/TextureManager.h
  4. 3
    0
      include/utils/pixel.h
  5. 8
    4
      src/Game.cpp
  6. 9
    1
      src/OpenRaider.cpp
  7. 6
    40
      src/Render.cpp
  8. 2
    3
      src/Room.cpp
  9. 14
    22
      src/StaticMesh.cpp
  10. 62
    118
      src/TextureManager.cpp
  11. 6
    0
      src/main.cpp
  12. 16
    0
      src/utils/pixel.cpp

+ 2
- 0
ChangeLog.md Datei anzeigen

@@ -7,6 +7,8 @@
7 7
     * Modified utils/png to allow compilation with different integer sizes
8 8
       Thanks to Christian for his help and his Linux machine :)
9 9
     * Removed TextureManagers fUseMipmaps flag, now always used
10
+    * TextureManager now using vector for GL texture IDs
11
+    * TextureManager now global object, no longer part of Render
10 12
 
11 13
     [ 20140623 ]
12 14
     * Use unsigned char instead of float for colors

+ 0
- 21
include/Render.h Datei anzeigen

@@ -12,7 +12,6 @@
12 12
 #include <vector>
13 13
 
14 14
 #include "Room.h"
15
-#include "TextureManager.h"
16 15
 #include "ViewVolume.h"
17 16
 
18 17
 /*!
@@ -69,25 +68,6 @@ public:
69 68
     int getMode();
70 69
 
71 70
     /*!
72
-     * \brief Loads textures in a certain id slot
73
-     * \param image Image to load
74
-     * \param width width of image
75
-     * \param height height of image
76
-     * \param id id for texture
77
-     * \sa Texture::loadBufferSlot()
78
-     */
79
-    void loadTexture(unsigned char *image,
80
-                          unsigned int width, unsigned int height,
81
-                          unsigned int id);
82
-
83
-    /*!
84
-     * \brief Sets up textures for OpenRaider
85
-     * \param textureDir Is valid and exists with textures
86
-     * \returns number of loaded textures
87
-     */
88
-    int initTextures(char *textureDir);
89
-
90
-    /*!
91 71
      * Removes current world/entity/etc geometry
92 72
      */
93 73
     void ClearWorld();
@@ -140,7 +120,6 @@ public:
140 120
 
141 121
     //! \fixme should be private
142 122
     ViewVolume mViewVolume; //!< View Volume for frustum culling
143
-    TextureManager mTexture; //!< Texture subsystem
144 123
 
145 124
 private:
146 125
 

+ 7
- 47
include/TextureManager.h Datei anzeigen

@@ -9,6 +9,8 @@
9 9
 #ifndef _TEXTURE_MANAGER_H
10 10
 #define _TEXTURE_MANAGER_H
11 11
 
12
+#include <vector>
13
+
12 14
 /*!
13 15
  * \brief Texture registry
14 16
  */
@@ -43,16 +45,7 @@ public:
43 45
      */
44 46
     void reset();
45 47
 
46
-    /*!
47
-     * \brief Generates a texture buffer with (width * height * 4) bytes.
48
-     * \param rgba 32bpp RGBA color to fill into buffer
49
-     * \param width width of newly allocated buffer, power of 2, pref same as height
50
-     * \param height height of newly allocated buffer, power of 2, pref same as width
51
-     * \returns newly allocated texture buffer filled with specified color
52
-     */
53
-    static unsigned char *generateColorTexture(const unsigned char rgba[4],
54
-                                                unsigned int width,
55
-                                                unsigned int height);
48
+    int initialize();
56 49
 
57 50
     /*!
58 51
      * \brief Get number of textures in use
@@ -88,19 +81,6 @@ public:
88 81
      * \param height height of image
89 82
      * \param mode mode of image
90 83
      * \param bpp bits per pixel of image
91
-     * \returns texture ID or < 0 on error
92
-     */
93
-    int loadBuffer(unsigned char *image,
94
-                    unsigned int width, unsigned int height,
95
-                    ColorMode mode, unsigned int bpp);
96
-
97
-    /*!
98
-     * \brief Loads Buffer as texture
99
-     * \param image pixmap matching other params
100
-     * \param width width of image
101
-     * \param height height of image
102
-     * \param mode mode of image
103
-     * \param bpp bits per pixel of image
104 84
      * \param slot slot (ID) of image
105 85
      * \returns texture ID or < 0 on error
106 86
      */
@@ -110,16 +90,6 @@ public:
110 90
                         unsigned int slot);
111 91
 
112 92
     /*!
113
-     * \brief Generates and loads a solid color texture.
114
-     * \param rgba color for new texture
115
-     * \param width width of new texture
116
-     * \param height height of new texture
117
-     * \returns texture ID or -1 on error
118
-     */
119
-    int loadColorTexture(const unsigned char rgba[4],
120
-                            unsigned int width, unsigned int height);
121
-
122
-    /*!
123 93
      * \brief Loads TGA file as texture
124 94
      * \param filename Existing TGA file
125 95
      * \returns ID of new texture or -1 on error
@@ -134,24 +104,14 @@ public:
134 104
      */
135 105
     void setFlag(TextureFlag flag);
136 106
 
137
-    /*!
138
-     * \brief Sets up GL texturing.
139
-     *
140
-     * Must be called as first setup step!
141
-     * \param n maximum number of textures you wish to allow
142
-     */
143
-    void setMaxTextureCount(unsigned int n);
144
-
145
-    void useMultiTexture(float u, float v);
146
-
147 107
     void useMultiTexture(float aU, float aV, float bU, float bV);
148 108
 
149 109
 private:
150
-    unsigned int *mTextureIds;  //!< GL texture list
151
-    unsigned int mTextureCount; //!< Texture counter
152
-    unsigned int mTextureLimit; //!< The texture limit
153
-    unsigned int mFlags;        //!< Class options
110
+    std::vector<unsigned int> mTextureIds;
111
+    unsigned int mFlags;
154 112
 };
155 113
 
114
+TextureManager &getTextureManager();
115
+
156 116
 #endif
157 117
 

+ 3
- 0
include/utils/pixel.h Datei anzeigen

@@ -8,6 +8,9 @@
8 8
 #ifndef _UTILS_PIXEL_H_
9 9
 #define _UTILS_PIXEL_H_
10 10
 
11
+unsigned char *generateColorTexture(const unsigned char *rgba, unsigned int width,
12
+        unsigned int height, unsigned int bpp);
13
+
11 14
 void bgr2rgb24(unsigned char *image, unsigned int w, unsigned int h);
12 15
 void bgra2rgba32(unsigned char *image, unsigned int w, unsigned int h);
13 16
 void argb2rgba32(unsigned char *image, unsigned int w, unsigned int h);

+ 8
- 4
src/Game.cpp Datei anzeigen

@@ -15,6 +15,7 @@
15 15
 #include "OpenRaider.h"
16 16
 #include "Sound.h"
17 17
 #include "StaticMesh.h"
18
+#include "TextureManager.h"
18 19
 #include "utils/strings.h"
19 20
 
20 21
 #include "games/TombRaider1.h"
@@ -45,9 +46,10 @@ unsigned int Game::getTextureOffset() {
45 46
 
46 47
 int Game::initialize() {
47 48
     // Enable Renderer
48
-    mTextureStart = getRender().initTextures(getOpenRaider().mDataDir);
49 49
     getRender().setMode(Render::modeLoadScreen);
50 50
 
51
+    mTextureStart = getTextureManager().getTextureCount();
52
+
51 53
     return 0;
52 54
 }
53 55
 
@@ -256,7 +258,8 @@ void Game::processTextures()
256 258
         mTombRaider.Texture(i, &image, &bumpmap);
257 259
 
258 260
         // Overwrite any previous level textures on load
259
-        getRender().loadTexture(image, 256, 256, (mTextureStart - 1) + i);
261
+        getTextureManager().loadBufferSlot(image, 256, 256,
262
+                TextureManager::RGBA, 32, (mTextureStart - 1) + i);
260 263
 
261 264
 #ifdef MULTITEXTURE
262 265
         gMapTex2Bump[(mTextureStart - 1) + i] = -1;
@@ -268,8 +271,9 @@ void Game::processTextures()
268 271
             gMapTex2Bump[(mTextureStart - 1) + i] = (mTextureStart - 1) + i +
269 272
                     mTombRaider.NumTextures();
270 273
 #endif
271
-            getRender().loadTexture(bumpmap, 256, 256, (mTextureStart - 1) + i +
272
-                    mTombRaider.NumTextures());
274
+            getTextureManager().loadBufferSlot(bumpmap, 256, 256,
275
+                    TextureManager::RGBA, 32,
276
+                    (mTextureStart - 1) + i + mTombRaider.NumTextures());
273 277
         }
274 278
 
275 279
         if (image)

+ 9
- 1
src/OpenRaider.cpp Datei anzeigen

@@ -14,6 +14,7 @@
14 14
 #include "math/math.h"
15 15
 #include "Menu.h"
16 16
 #include "Sound.h"
17
+#include "TextureManager.h"
17 18
 #include "TombRaider.h"
18 19
 #include "utils/strings.h"
19 20
 #include "utils/time.h"
@@ -75,11 +76,18 @@ int OpenRaider::initialize() {
75 76
         return -4;
76 77
     }
77 78
 
79
+    // Initialize Texture Manager
80
+    error = getTextureManager().initialize();
81
+    if (error != 0) {
82
+        printf("Could not initialize Textures (%d)!\n", error);
83
+        return -5;
84
+    }
85
+
78 86
     // Initialize game engine
79 87
     error = getGame().initialize();
80 88
     if (error != 0) {
81 89
         printf("Could not initialize Game (%d)!\n", error);
82
-        return -5;
90
+        return -6;
83 91
     }
84 92
 
85 93
 #ifdef DEBUG

+ 6
- 40
src/Render.cpp Datei anzeigen

@@ -16,6 +16,7 @@
16 16
 #include "Game.h"
17 17
 #include "OpenRaider.h"
18 18
 #include "Render.h"
19
+#include "TextureManager.h"
19 20
 #include "utils/strings.h"
20 21
 #include "utils/tga.h"
21 22
 #include "Window.h"
@@ -77,44 +78,6 @@ unsigned int Render::getFlags() {
77 78
 }
78 79
 
79 80
 
80
-void Render::loadTexture(unsigned char *image,
81
-        unsigned int width, unsigned int height,
82
-        unsigned int id)
83
-{
84
-    glColor3ubv(WHITE);
85
-    mTexture.loadBufferSlot(image, width, height, TextureManager::RGBA, 32, id);
86
-}
87
-
88
-
89
-int Render::initTextures(char *textureDir) {
90
-    char *filename;
91
-    unsigned int numTextures = 0;
92
-
93
-    mTexture.reset();
94
-    mTexture.setMaxTextureCount(128);  /* TR never needs more than 32 iirc
95
-                                          However, color texturegen is a lot */
96
-
97
-    if (mTexture.loadColorTexture(WHITE, 32, 32) > -1)
98
-        numTextures++;
99
-
100
-    // Temporary
101
-    filename = bufferString("%s/tr2/TITLE.PCX", getOpenRaider().mPakDir);
102
-    if (mTexture.loadPCX(filename) > -1) {
103
-        numTextures++;
104
-        delete [] filename;
105
-    } else {
106
-        delete [] filename;
107
-        //! \fixme Error Checking. Return negative error code, check in calling place too
108
-        filename = bufferString("%s/%s", textureDir, "splash.tga");
109
-        if (mTexture.loadTGA(filename) > -1)
110
-            numTextures++;
111
-        delete [] filename;
112
-    }
113
-
114
-    return numTextures;
115
-}
116
-
117
-
118 81
 // Texture must be set to WHITE solid color texture
119 82
 void renderTrace(int color, vec3_t start, vec3_t end)
120 83
 {
@@ -508,7 +471,7 @@ void Render::drawLoadScreen()
508 471
     float x = 0.0f, y = 0.0f, z = -160.0f;
509 472
     float w = getWindow().getWidth(), h = getWindow().getHeight();
510 473
 
511
-    if (mTexture.getTextureCount() <= 0)
474
+    if (getTextureManager().getTextureCount() <= 0)
512 475
         return;
513 476
 
514 477
     // Mongoose 2002.01.01, Rendered while game is loading...
@@ -525,7 +488,10 @@ void Render::drawLoadScreen()
525 488
     glTranslatef(0.0f, 0.0f, -2000.0f);
526 489
     glRotatef(180.0f, 1.0f, 0.0f, 0.0f);
527 490
 
528
-    mTexture.bindTextureId(1); //! \fixme store texture id somewhere
491
+    if (getTextureManager().getTextureCount() < 2)
492
+        getTextureManager().bindTextureId(0); //! \fixme store texture id somewhere
493
+    else
494
+        getTextureManager().bindTextureId(1);
529 495
 
530 496
     glBegin(GL_TRIANGLE_STRIP);
531 497
     glTexCoord2f(1.0, 1.0);

+ 2
- 3
src/Room.cpp Datei anzeigen

@@ -12,6 +12,7 @@
12 12
 #include "Game.h"
13 13
 #include "Render.h"
14 14
 #include "Room.h"
15
+#include "TextureManager.h"
15 16
 
16 17
 #ifdef MULTITEXTURE
17 18
 #include <map>
@@ -457,7 +458,7 @@ void Room::display(bool alpha) {
457 458
     glPushMatrix();
458 459
     //LightingSetup();
459 460
 
460
-    getRender().mTexture.bindTextureId(0); // WHITE texture
461
+    getTextureManager().bindTextureId(0); // \fixme WHITE texture
461 462
 
462 463
     if ((!alpha) && getRender().getMode() == Render::modeWireframe) {
463 464
         glLineWidth(2.0);
@@ -508,8 +509,6 @@ void Room::display(bool alpha) {
508 509
 
509 510
     glPopMatrix();
510 511
 
511
-    //getRender().mTexture.bindTextureId(0);
512
-
513 512
     // Draw other room meshes and sprites
514 513
     if (alpha || (getRender().getMode() == Render::modeWireframe)
515 514
             || (getRender().getMode() == Render::modeSolid)) {

+ 14
- 22
src/StaticMesh.cpp Datei anzeigen

@@ -8,12 +8,10 @@
8 8
 #include "global.h"
9 9
 #include "Game.h"
10 10
 #include "Render.h"
11
+#include "TextureManager.h"
12
+#include "utils/pixel.h"
11 13
 #include "StaticMesh.h"
12 14
 
13
-#ifdef EXPERIMENTAL
14
-std::vector<unsigned int> gColorTextureHACK;
15
-#endif
16
-
17 15
 TexturedTriangle::TexturedTriangle(int i[3], vec_t s[6], int tex, unsigned short trans) {
18 16
     index[0] = i[0];
19 17
     index[1] = i[1];
@@ -95,10 +93,14 @@ void TexturedTriangle::display(vec_t *vertices, vec_t *colors, vec_t *normals) {
95 93
 }
96 94
 
97 95
 #ifdef EXPERIMENTAL
96
+
97
+#include <map>
98
+std::map<unsigned int, unsigned int> gColorTextureHACK;
99
+
98 100
 int setupTextureColor(float *colorf) {
99 101
     unsigned char color[4];
100 102
     unsigned int colorI;
101
-    int texture;
103
+    unsigned int texture;
102 104
 
103 105
     color[0] = (unsigned char)(colorf[0]*255.0f);
104 106
     color[1] = (unsigned char)(colorf[1]*255.0f);
@@ -110,23 +112,13 @@ int setupTextureColor(float *colorf) {
110 112
     ((unsigned char *)(&colorI))[1] = color[2];
111 113
     ((unsigned char *)(&colorI))[0] = color[3];
112 114
 
113
-    bool found = false;
114
-    unsigned int foundIndex = 0;
115
-    for (foundIndex = 0; foundIndex < gColorTextureHACK.size(); foundIndex++) {
116
-        if (gColorTextureHACK[foundIndex] == colorI) {
117
-            found = true;
118
-            break;
119
-        }
120
-    }
121
-
122
-    if (!found) {
123
-        gColorTextureHACK.push_back(colorI);
124
-        texture = getGame().getTextureOffset() + gColorTextureHACK.size();
125
-
126
-        getRender().loadTexture(TextureManager::generateColorTexture(color, 32, 32),
127
-                32, 32, texture);
128
-    } else {
129
-        texture = getGame().getTextureOffset() + foundIndex;
115
+    try {
116
+        texture = gColorTextureHACK.at(colorI);
117
+    } catch (...) {
118
+        unsigned char *image = generateColorTexture(color, 32, 32, 32);
119
+        texture = getTextureManager().loadBufferSlot(image, 32, 32,
120
+                TextureManager::RGBA, 32, getTextureManager().getTextureCount());
121
+        delete [] image;
130 122
     }
131 123
 
132 124
     return texture;

+ 62
- 118
src/TextureManager.cpp Datei anzeigen

@@ -12,6 +12,7 @@
12 12
 #include <stdarg.h>
13 13
 
14 14
 #include "global.h"
15
+#include "OpenRaider.h"
15 16
 #include "utils/pcx.h"
16 17
 #include "utils/pixel.h"
17 18
 #include "utils/strings.h"
@@ -19,45 +20,37 @@
19 20
 #include "TextureManager.h"
20 21
 
21 22
 TextureManager::TextureManager() {
22
-    mTextureIds = NULL;
23 23
     mFlags = 0;
24
-    mTextureCount = 0;
25
-    mTextureLimit = 0;
26 24
 }
27 25
 
28 26
 TextureManager::~TextureManager() {
29 27
     reset();
30 28
 }
31 29
 
32
-unsigned char *TextureManager::generateColorTexture(const unsigned char rgba[4],
33
-        unsigned int width, unsigned int height) {
34
-    assert(rgba != NULL);
35
-    assert(width > 0);
36
-    assert(height > 0);
37
-
38
-    unsigned char *image = new unsigned char[height * width * 4];
39
-
40
-    for (unsigned int i = 0; i < (width * height); i++) {
41
-        image[i * 4] = rgba[0];
42
-        image[(i * 4) + 1] = rgba[1];
43
-        image[(i * 4) + 2] = rgba[2];
44
-        image[(i * 4) + 3] = rgba[3];
30
+void TextureManager::reset() {
31
+    while (mTextureIds.size() > 0) {
32
+        unsigned int id = mTextureIds.at(mTextureIds.size() - 1);
33
+        glDeleteTextures(1, &id);
34
+        mTextureIds.pop_back();
45 35
     }
46
-
47
-    return image;
48 36
 }
49 37
 
50
-int TextureManager::loadColorTexture(const unsigned char rgba[4],
51
-        unsigned int width, unsigned int height) {
52
-    assert(rgba != NULL);
53
-    assert(width > 0);
54
-    assert(height > 0);
55
-
56
-    unsigned char *image = generateColorTexture(rgba, width, height);
57
-    int id = loadBuffer(image, width, height, RGBA, 32);
38
+int TextureManager::initialize() {
39
+    unsigned char *image = generateColorTexture(WHITE, 32, 32, 32);
40
+    loadBufferSlot(image, 32, 32, RGBA, 32, mTextureIds.size());
58 41
     delete [] image;
59 42
 
60
-    return id;
43
+    //! \fixme Temporary
44
+    char *filename = bufferString("%s/tr2/TITLE.PCX", getOpenRaider().mPakDir);
45
+    if (loadPCX(filename) < 0) {
46
+        delete [] filename;
47
+        //! \fixme Error Checking. Return negative error code, check in calling place too
48
+        filename = bufferString("%s/%s", getOpenRaider().mDataDir, "splash.tga");
49
+        loadTGA(filename);
50
+    }
51
+    delete [] filename;
52
+
53
+    return (mTextureIds.size() == 0) ? -1 : 0;
61 54
 }
62 55
 
63 56
 void TextureManager::setFlag(TextureFlag flag) {
@@ -68,15 +61,8 @@ void TextureManager::clearFlag(TextureFlag flag) {
68 61
     mFlags &= ~flag;
69 62
 }
70 63
 
71
-void TextureManager::reset() {
72
-    if (mTextureIds) {
73
-        glDeleteTextures(mTextureLimit, mTextureIds);
74
-        delete [] mTextureIds;
75
-    }
76
-
77
-    mTextureIds = NULL;
78
-    mTextureCount = 0;
79
-    mTextureLimit = 0;
64
+int TextureManager::getTextureCount() {
65
+    return mTextureIds.size();
80 66
 }
81 67
 
82 68
 void TextureManager::disableMultiTexture() {
@@ -94,128 +80,94 @@ void TextureManager::useMultiTexture(float aU, float aV, float bU, float bV) {
94 80
     glMultiTexCoord2fARB(GL_TEXTURE1_ARB, bU, bV);
95 81
 }
96 82
 
97
-void TextureManager::useMultiTexture(float u, float v) {
98
-    useMultiTexture(u, v, u, v);
99
-}
100
-
101 83
 void TextureManager::bindMultiTexture(int texture0, int texture1) {
102
-    assert(mTextureIds != NULL);
103 84
     assert(texture0 >= 0);
104 85
     assert(texture1 >= 0);
105
-    assert((unsigned int)texture0 <= mTextureCount);
106
-    assert((unsigned int)texture1 <= mTextureCount);
86
+    assert((unsigned int)texture0 < mTextureIds.size());
87
+    assert((unsigned int)texture1 < mTextureIds.size());
107 88
 
108 89
     mFlags |= fUseMultiTexture;
109 90
 
110 91
     glActiveTextureARB(GL_TEXTURE0_ARB);
111 92
     glEnable(GL_TEXTURE_2D);
112
-    glBindTexture(GL_TEXTURE_2D, mTextureIds[texture0]);
93
+    glBindTexture(GL_TEXTURE_2D, mTextureIds.at(texture0));
113 94
 
114 95
     glActiveTextureARB(GL_TEXTURE1_ARB);
115 96
     glEnable(GL_TEXTURE_2D);
116
-    glBindTexture(GL_TEXTURE_2D, mTextureIds[texture1]);
117
-}
118
-
119
-void TextureManager::setMaxTextureCount(unsigned int n) {
120
-    mTextureLimit = n;
121
-    mTextureIds = new unsigned int[n];
122
-    glGenTextures(n, mTextureIds);
123
-}
124
-
125
-int TextureManager::getTextureCount() {
126
-    return mTextureCount - 1;
97
+    glBindTexture(GL_TEXTURE_2D, mTextureIds.at(texture1));
127 98
 }
128 99
 
129
-int TextureManager::loadBuffer(unsigned char *image,
100
+int TextureManager::loadBufferSlot(unsigned char *image,
130 101
         unsigned int width, unsigned int height,
131
-        ColorMode mode, unsigned int bpp) {
132
-    int id;
133
-
102
+        ColorMode mode, unsigned int bpp,
103
+        unsigned int slot) {
134 104
     assert(image != NULL);
135 105
     assert(width > 0);
136 106
     assert(height > 0);
137
-    assert((bpp == 8) || (bpp == 24) || (bpp == 32));
107
+    assert((mode == GREYSCALE) || (mode == RGB)
108
+            || (mode == BGR) || (mode == ARGB)
109
+            || (mode == RGBA) || (mode ==  BGRA));
110
+
111
+    switch (mode) {
112
+        case GREYSCALE:
113
+            assert(bpp == 8);
114
+            break;
138 115
 
139
-    id = loadBufferSlot(image, width, height, mode, bpp, mTextureCount++);
116
+        case RGB:
117
+        case BGR:
118
+            assert(bpp == 24);
119
+            break;
140 120
 
141
-    if (id < 0)
142
-        return id;
121
+        case ARGB:
122
+        case RGBA:
123
+        case BGRA:
124
+            assert(bpp == 32);
125
+            break;
126
+    }
143 127
 
144
-    return id;
145
-}
128
+    while (mTextureIds.size() <= slot) {
129
+        unsigned int id;
130
+        glGenTextures(1, &id);
131
+        mTextureIds.push_back(id);
132
+    }
146 133
 
147
-int TextureManager::loadBufferSlot(unsigned char *image,
148
-        unsigned int width, unsigned int height,
149
-        ColorMode mode, unsigned int bpp,
150
-        unsigned int slot) {
151 134
     unsigned char bytes;
152 135
     unsigned int glcMode;
153 136
 
154
-    assert(mTextureIds != NULL);
155
-    assert(slot < mTextureLimit);
156
-    assert(image != NULL);
157
-    assert(width > 0);
158
-    assert(height > 0);
159
-    assert((bpp == 8) || (bpp == 24) || (bpp == 32));
160
-
161 137
     switch (mode) {
162 138
         case GREYSCALE:
163
-            if (bpp != 8) {
164
-                printf("TextureManager::Load ERROR Unsupported GREYSCALE, %i bpp\n", bpp);
165
-                return -1;
166
-            }
167 139
             bytes = 1;
168 140
             glcMode = GL_LUMINANCE;
169 141
             break;
170 142
 
171 143
         case RGB:
172
-            if (bpp != 24) {
173
-                printf("TextureManager::Load ERROR Unsupported RGB, %i bpp\n", bpp);
174
-                return -1;
175
-            }
176 144
             bytes = 3;
177 145
             glcMode = GL_RGB;
178 146
             break;
179 147
 
180 148
         case ARGB:
181
-            if (bpp == 32) {
182
-                argb2rgba32(image, width, height);
183
-            } else {
184
-                printf("TextureManager::Load ERROR Unsupported ARGB, %i bpp\n", bpp);
185
-                return -1;
186
-            }
149
+            argb2rgba32(image, width, height);
187 150
             bytes = 4;
188 151
             glcMode = GL_RGBA;
189 152
             break;
190 153
 
191 154
         case RGBA:
192
-            if (bpp != 32) {
193
-                printf("TextureManager::Load ERROR Unsupported RGBA, %i bpp\n", bpp);
194
-                return -1;
195
-            }
196 155
             bytes = 4;
197 156
             glcMode = GL_RGBA;
198 157
             break;
199 158
 
200 159
         case BGR:
201
-            if (bpp != 24) {
202
-                printf("TextureManager::Load ERROR Unsupported BGR, %i bpp\n", bpp);
203
-                return -1;
204
-            }
205 160
             bytes = 3;
206 161
             glcMode = GL_BGR_EXT;
207 162
             break;
208 163
 
209 164
         case BGRA:
210
-            if (bpp != 32) {
211
-                printf("TextureManager::Load ERROR Unsupported BGRA, %i bpp\n", bpp);
212
-                return -1;
213
-            }
214 165
             bytes = 4;
215 166
             glcMode = GL_BGRA_EXT;
216 167
             break;
217 168
     }
218 169
 
170
+    glColor3ubv(WHITE);
219 171
     glClearColor(0.0, 0.0, 0.0, 0.0);
220 172
     glEnable(GL_DEPTH_TEST);
221 173
     glShadeModel(GL_SMOOTH);
@@ -241,13 +193,12 @@ int TextureManager::loadBufferSlot(unsigned char *image,
241 193
 }
242 194
 
243 195
 void TextureManager::bindTextureId(unsigned int n) {
244
-    assert(mTextureIds != NULL);
245
-    assert(n <= mTextureCount);
196
+    assert(n < mTextureIds.size());
246 197
 
247 198
     glEnable(GL_TEXTURE_2D);
248 199
     //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
249 200
 
250
-    glBindTexture(GL_TEXTURE_2D, mTextureIds[n]);
201
+    glBindTexture(GL_TEXTURE_2D, mTextureIds.at(n));
251 202
 }
252 203
 
253 204
 int TextureManager::loadPCX(const char *filename) {
@@ -266,7 +217,7 @@ int TextureManager::loadPCX(const char *filename) {
266 217
             delete [] image;
267 218
             image = image2;
268 219
         }
269
-        id = loadBuffer(image, w, h, c, bpp);
220
+        id = loadBufferSlot(image, w, h, c, bpp, mTextureIds.size());
270 221
         delete [] image;
271 222
     }
272 223
 
@@ -277,8 +228,7 @@ int TextureManager::loadTGA(const char *filename) {
277 228
     assert(filename != NULL);
278 229
     assert(filename[0] != '\0');
279 230
 
280
-    unsigned char *image = NULL;
281
-    unsigned char *image2 = NULL;
231
+    unsigned char *image;
282 232
     unsigned int w, h;
283 233
     char type;
284 234
     int id = -1;
@@ -286,26 +236,20 @@ int TextureManager::loadTGA(const char *filename) {
286 236
     if (!tgaCheck(filename)) {
287 237
         tgaLoad(filename, &image, &w, &h, &type);
288 238
 
289
-        image2 = scaleBuffer(image, &w, &h, (type == 2) ? 32 : 24);
290
-
239
+        unsigned char *image2 = scaleBuffer(image, &w, &h, (type == 2) ? 32 : 24);
291 240
         if (image2) {
292 241
             delete [] image;
293 242
             image = image2;
294 243
         }
295
-
296 244
         if (image) {
297
-            id = loadBuffer(image, w, h,
245
+            id = loadBufferSlot(image, w, h,
298 246
                     (type == 2) ? RGBA : RGB,
299
-                    (type == 2) ? 32 : 24);
300
-
247
+                    (type == 2) ? 32 : 24,
248
+                    mTextureIds.size());
301 249
             delete [] image;
302 250
         }
303 251
     }
304 252
 
305
-    if (id == -1) {
306
-        printf("TextureManager::loadTGA> ERROR: Failed to load '%s'\n", filename);
307
-    }
308
-
309 253
     return id;
310 254
 }
311 255
 

+ 6
- 0
src/main.cpp Datei anzeigen

@@ -17,6 +17,7 @@
17 17
 #include "Menu.h"
18 18
 #include "OpenRaider.h"
19 19
 #include "Render.h"
20
+#include "TextureManager.h"
20 21
 #include "World.h"
21 22
 #include "utils/strings.h"
22 23
 #include "utils/time.h"
@@ -40,6 +41,7 @@ Game gGame;
40 41
 Menu gMenu;
41 42
 OpenRaider gOpenRaider;
42 43
 Render gRender;
44
+TextureManager gTextureManager;
43 45
 World gWorld;
44 46
 
45 47
 #ifdef USING_AL
@@ -84,6 +86,10 @@ Sound &getSound() {
84 86
     return gSound;
85 87
 }
86 88
 
89
+TextureManager &getTextureManager() {
90
+    return gTextureManager;
91
+}
92
+
87 93
 Window &getWindow() {
88 94
     return gWindow;
89 95
 }

+ 16
- 0
src/utils/pixel.cpp Datei anzeigen

@@ -8,6 +8,22 @@
8 8
 #include "global.h"
9 9
 #include "utils/pixel.h"
10 10
 
11
+unsigned char *generateColorTexture(const unsigned char *rgba, unsigned int width,
12
+        unsigned int height, unsigned int bpp) {
13
+    assert(rgba != NULL);
14
+    assert(width > 0);
15
+    assert(height > 0);
16
+    assert((bpp % 8) == 0);
17
+
18
+    unsigned char *image = new unsigned char[height * width * (bpp / 8)];
19
+    for (unsigned int i = 0; i < (width * height); i++) {
20
+        for (unsigned int a = 0; a < (bpp / 8); a++) {
21
+            image[(i * (bpp / 8)) + a] = rgba[a];
22
+        }
23
+    }
24
+    return image;
25
+}
26
+
11 27
 void bgr2rgb24(unsigned char *image, unsigned int w, unsigned int h) {
12 28
     assert(image != nullptr);
13 29
     assert(w > 0);

Laden…
Abbrechen
Speichern