Browse Source

TextureManager using vector, now also global service

Thomas Buck 11 years ago
parent
commit
8e09359af3
12 changed files with 135 additions and 256 deletions
  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 View File

7
     * Modified utils/png to allow compilation with different integer sizes
7
     * Modified utils/png to allow compilation with different integer sizes
8
       Thanks to Christian for his help and his Linux machine :)
8
       Thanks to Christian for his help and his Linux machine :)
9
     * Removed TextureManagers fUseMipmaps flag, now always used
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
     [ 20140623 ]
13
     [ 20140623 ]
12
     * Use unsigned char instead of float for colors
14
     * Use unsigned char instead of float for colors

+ 0
- 21
include/Render.h View File

12
 #include <vector>
12
 #include <vector>
13
 
13
 
14
 #include "Room.h"
14
 #include "Room.h"
15
-#include "TextureManager.h"
16
 #include "ViewVolume.h"
15
 #include "ViewVolume.h"
17
 
16
 
18
 /*!
17
 /*!
69
     int getMode();
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
      * Removes current world/entity/etc geometry
71
      * Removes current world/entity/etc geometry
92
      */
72
      */
93
     void ClearWorld();
73
     void ClearWorld();
140
 
120
 
141
     //! \fixme should be private
121
     //! \fixme should be private
142
     ViewVolume mViewVolume; //!< View Volume for frustum culling
122
     ViewVolume mViewVolume; //!< View Volume for frustum culling
143
-    TextureManager mTexture; //!< Texture subsystem
144
 
123
 
145
 private:
124
 private:
146
 
125
 

+ 7
- 47
include/TextureManager.h View File

9
 #ifndef _TEXTURE_MANAGER_H
9
 #ifndef _TEXTURE_MANAGER_H
10
 #define _TEXTURE_MANAGER_H
10
 #define _TEXTURE_MANAGER_H
11
 
11
 
12
+#include <vector>
13
+
12
 /*!
14
 /*!
13
  * \brief Texture registry
15
  * \brief Texture registry
14
  */
16
  */
43
      */
45
      */
44
     void reset();
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
      * \brief Get number of textures in use
51
      * \brief Get number of textures in use
88
      * \param height height of image
81
      * \param height height of image
89
      * \param mode mode of image
82
      * \param mode mode of image
90
      * \param bpp bits per pixel of image
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
      * \param slot slot (ID) of image
84
      * \param slot slot (ID) of image
105
      * \returns texture ID or < 0 on error
85
      * \returns texture ID or < 0 on error
106
      */
86
      */
110
                         unsigned int slot);
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
      * \brief Loads TGA file as texture
93
      * \brief Loads TGA file as texture
124
      * \param filename Existing TGA file
94
      * \param filename Existing TGA file
125
      * \returns ID of new texture or -1 on error
95
      * \returns ID of new texture or -1 on error
134
      */
104
      */
135
     void setFlag(TextureFlag flag);
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
     void useMultiTexture(float aU, float aV, float bU, float bV);
107
     void useMultiTexture(float aU, float aV, float bU, float bV);
148
 
108
 
149
 private:
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
 #endif
116
 #endif
157
 
117
 

+ 3
- 0
include/utils/pixel.h View File

8
 #ifndef _UTILS_PIXEL_H_
8
 #ifndef _UTILS_PIXEL_H_
9
 #define _UTILS_PIXEL_H_
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
 void bgr2rgb24(unsigned char *image, unsigned int w, unsigned int h);
14
 void bgr2rgb24(unsigned char *image, unsigned int w, unsigned int h);
12
 void bgra2rgba32(unsigned char *image, unsigned int w, unsigned int h);
15
 void bgra2rgba32(unsigned char *image, unsigned int w, unsigned int h);
13
 void argb2rgba32(unsigned char *image, unsigned int w, unsigned int h);
16
 void argb2rgba32(unsigned char *image, unsigned int w, unsigned int h);

+ 8
- 4
src/Game.cpp View File

15
 #include "OpenRaider.h"
15
 #include "OpenRaider.h"
16
 #include "Sound.h"
16
 #include "Sound.h"
17
 #include "StaticMesh.h"
17
 #include "StaticMesh.h"
18
+#include "TextureManager.h"
18
 #include "utils/strings.h"
19
 #include "utils/strings.h"
19
 
20
 
20
 #include "games/TombRaider1.h"
21
 #include "games/TombRaider1.h"
45
 
46
 
46
 int Game::initialize() {
47
 int Game::initialize() {
47
     // Enable Renderer
48
     // Enable Renderer
48
-    mTextureStart = getRender().initTextures(getOpenRaider().mDataDir);
49
     getRender().setMode(Render::modeLoadScreen);
49
     getRender().setMode(Render::modeLoadScreen);
50
 
50
 
51
+    mTextureStart = getTextureManager().getTextureCount();
52
+
51
     return 0;
53
     return 0;
52
 }
54
 }
53
 
55
 
256
         mTombRaider.Texture(i, &image, &bumpmap);
258
         mTombRaider.Texture(i, &image, &bumpmap);
257
 
259
 
258
         // Overwrite any previous level textures on load
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
 #ifdef MULTITEXTURE
264
 #ifdef MULTITEXTURE
262
         gMapTex2Bump[(mTextureStart - 1) + i] = -1;
265
         gMapTex2Bump[(mTextureStart - 1) + i] = -1;
268
             gMapTex2Bump[(mTextureStart - 1) + i] = (mTextureStart - 1) + i +
271
             gMapTex2Bump[(mTextureStart - 1) + i] = (mTextureStart - 1) + i +
269
                     mTombRaider.NumTextures();
272
                     mTombRaider.NumTextures();
270
 #endif
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
         if (image)
279
         if (image)

+ 9
- 1
src/OpenRaider.cpp View File

14
 #include "math/math.h"
14
 #include "math/math.h"
15
 #include "Menu.h"
15
 #include "Menu.h"
16
 #include "Sound.h"
16
 #include "Sound.h"
17
+#include "TextureManager.h"
17
 #include "TombRaider.h"
18
 #include "TombRaider.h"
18
 #include "utils/strings.h"
19
 #include "utils/strings.h"
19
 #include "utils/time.h"
20
 #include "utils/time.h"
75
         return -4;
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
     // Initialize game engine
86
     // Initialize game engine
79
     error = getGame().initialize();
87
     error = getGame().initialize();
80
     if (error != 0) {
88
     if (error != 0) {
81
         printf("Could not initialize Game (%d)!\n", error);
89
         printf("Could not initialize Game (%d)!\n", error);
82
-        return -5;
90
+        return -6;
83
     }
91
     }
84
 
92
 
85
 #ifdef DEBUG
93
 #ifdef DEBUG

+ 6
- 40
src/Render.cpp View File

16
 #include "Game.h"
16
 #include "Game.h"
17
 #include "OpenRaider.h"
17
 #include "OpenRaider.h"
18
 #include "Render.h"
18
 #include "Render.h"
19
+#include "TextureManager.h"
19
 #include "utils/strings.h"
20
 #include "utils/strings.h"
20
 #include "utils/tga.h"
21
 #include "utils/tga.h"
21
 #include "Window.h"
22
 #include "Window.h"
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
 // Texture must be set to WHITE solid color texture
81
 // Texture must be set to WHITE solid color texture
119
 void renderTrace(int color, vec3_t start, vec3_t end)
82
 void renderTrace(int color, vec3_t start, vec3_t end)
120
 {
83
 {
508
     float x = 0.0f, y = 0.0f, z = -160.0f;
471
     float x = 0.0f, y = 0.0f, z = -160.0f;
509
     float w = getWindow().getWidth(), h = getWindow().getHeight();
472
     float w = getWindow().getWidth(), h = getWindow().getHeight();
510
 
473
 
511
-    if (mTexture.getTextureCount() <= 0)
474
+    if (getTextureManager().getTextureCount() <= 0)
512
         return;
475
         return;
513
 
476
 
514
     // Mongoose 2002.01.01, Rendered while game is loading...
477
     // Mongoose 2002.01.01, Rendered while game is loading...
525
     glTranslatef(0.0f, 0.0f, -2000.0f);
488
     glTranslatef(0.0f, 0.0f, -2000.0f);
526
     glRotatef(180.0f, 1.0f, 0.0f, 0.0f);
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
     glBegin(GL_TRIANGLE_STRIP);
496
     glBegin(GL_TRIANGLE_STRIP);
531
     glTexCoord2f(1.0, 1.0);
497
     glTexCoord2f(1.0, 1.0);

+ 2
- 3
src/Room.cpp View File

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

+ 14
- 22
src/StaticMesh.cpp View File

8
 #include "global.h"
8
 #include "global.h"
9
 #include "Game.h"
9
 #include "Game.h"
10
 #include "Render.h"
10
 #include "Render.h"
11
+#include "TextureManager.h"
12
+#include "utils/pixel.h"
11
 #include "StaticMesh.h"
13
 #include "StaticMesh.h"
12
 
14
 
13
-#ifdef EXPERIMENTAL
14
-std::vector<unsigned int> gColorTextureHACK;
15
-#endif
16
-
17
 TexturedTriangle::TexturedTriangle(int i[3], vec_t s[6], int tex, unsigned short trans) {
15
 TexturedTriangle::TexturedTriangle(int i[3], vec_t s[6], int tex, unsigned short trans) {
18
     index[0] = i[0];
16
     index[0] = i[0];
19
     index[1] = i[1];
17
     index[1] = i[1];
95
 }
93
 }
96
 
94
 
97
 #ifdef EXPERIMENTAL
95
 #ifdef EXPERIMENTAL
96
+
97
+#include <map>
98
+std::map<unsigned int, unsigned int> gColorTextureHACK;
99
+
98
 int setupTextureColor(float *colorf) {
100
 int setupTextureColor(float *colorf) {
99
     unsigned char color[4];
101
     unsigned char color[4];
100
     unsigned int colorI;
102
     unsigned int colorI;
101
-    int texture;
103
+    unsigned int texture;
102
 
104
 
103
     color[0] = (unsigned char)(colorf[0]*255.0f);
105
     color[0] = (unsigned char)(colorf[0]*255.0f);
104
     color[1] = (unsigned char)(colorf[1]*255.0f);
106
     color[1] = (unsigned char)(colorf[1]*255.0f);
110
     ((unsigned char *)(&colorI))[1] = color[2];
112
     ((unsigned char *)(&colorI))[1] = color[2];
111
     ((unsigned char *)(&colorI))[0] = color[3];
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
     return texture;
124
     return texture;

+ 62
- 118
src/TextureManager.cpp View File

12
 #include <stdarg.h>
12
 #include <stdarg.h>
13
 
13
 
14
 #include "global.h"
14
 #include "global.h"
15
+#include "OpenRaider.h"
15
 #include "utils/pcx.h"
16
 #include "utils/pcx.h"
16
 #include "utils/pixel.h"
17
 #include "utils/pixel.h"
17
 #include "utils/strings.h"
18
 #include "utils/strings.h"
19
 #include "TextureManager.h"
20
 #include "TextureManager.h"
20
 
21
 
21
 TextureManager::TextureManager() {
22
 TextureManager::TextureManager() {
22
-    mTextureIds = NULL;
23
     mFlags = 0;
23
     mFlags = 0;
24
-    mTextureCount = 0;
25
-    mTextureLimit = 0;
26
 }
24
 }
27
 
25
 
28
 TextureManager::~TextureManager() {
26
 TextureManager::~TextureManager() {
29
     reset();
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
     delete [] image;
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
 void TextureManager::setFlag(TextureFlag flag) {
56
 void TextureManager::setFlag(TextureFlag flag) {
68
     mFlags &= ~flag;
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
 void TextureManager::disableMultiTexture() {
68
 void TextureManager::disableMultiTexture() {
94
     glMultiTexCoord2fARB(GL_TEXTURE1_ARB, bU, bV);
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
 void TextureManager::bindMultiTexture(int texture0, int texture1) {
83
 void TextureManager::bindMultiTexture(int texture0, int texture1) {
102
-    assert(mTextureIds != NULL);
103
     assert(texture0 >= 0);
84
     assert(texture0 >= 0);
104
     assert(texture1 >= 0);
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
     mFlags |= fUseMultiTexture;
89
     mFlags |= fUseMultiTexture;
109
 
90
 
110
     glActiveTextureARB(GL_TEXTURE0_ARB);
91
     glActiveTextureARB(GL_TEXTURE0_ARB);
111
     glEnable(GL_TEXTURE_2D);
92
     glEnable(GL_TEXTURE_2D);
112
-    glBindTexture(GL_TEXTURE_2D, mTextureIds[texture0]);
93
+    glBindTexture(GL_TEXTURE_2D, mTextureIds.at(texture0));
113
 
94
 
114
     glActiveTextureARB(GL_TEXTURE1_ARB);
95
     glActiveTextureARB(GL_TEXTURE1_ARB);
115
     glEnable(GL_TEXTURE_2D);
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
         unsigned int width, unsigned int height,
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
     assert(image != NULL);
104
     assert(image != NULL);
135
     assert(width > 0);
105
     assert(width > 0);
136
     assert(height > 0);
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
     unsigned char bytes;
134
     unsigned char bytes;
152
     unsigned int glcMode;
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
     switch (mode) {
137
     switch (mode) {
162
         case GREYSCALE:
138
         case GREYSCALE:
163
-            if (bpp != 8) {
164
-                printf("TextureManager::Load ERROR Unsupported GREYSCALE, %i bpp\n", bpp);
165
-                return -1;
166
-            }
167
             bytes = 1;
139
             bytes = 1;
168
             glcMode = GL_LUMINANCE;
140
             glcMode = GL_LUMINANCE;
169
             break;
141
             break;
170
 
142
 
171
         case RGB:
143
         case RGB:
172
-            if (bpp != 24) {
173
-                printf("TextureManager::Load ERROR Unsupported RGB, %i bpp\n", bpp);
174
-                return -1;
175
-            }
176
             bytes = 3;
144
             bytes = 3;
177
             glcMode = GL_RGB;
145
             glcMode = GL_RGB;
178
             break;
146
             break;
179
 
147
 
180
         case ARGB:
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
             bytes = 4;
150
             bytes = 4;
188
             glcMode = GL_RGBA;
151
             glcMode = GL_RGBA;
189
             break;
152
             break;
190
 
153
 
191
         case RGBA:
154
         case RGBA:
192
-            if (bpp != 32) {
193
-                printf("TextureManager::Load ERROR Unsupported RGBA, %i bpp\n", bpp);
194
-                return -1;
195
-            }
196
             bytes = 4;
155
             bytes = 4;
197
             glcMode = GL_RGBA;
156
             glcMode = GL_RGBA;
198
             break;
157
             break;
199
 
158
 
200
         case BGR:
159
         case BGR:
201
-            if (bpp != 24) {
202
-                printf("TextureManager::Load ERROR Unsupported BGR, %i bpp\n", bpp);
203
-                return -1;
204
-            }
205
             bytes = 3;
160
             bytes = 3;
206
             glcMode = GL_BGR_EXT;
161
             glcMode = GL_BGR_EXT;
207
             break;
162
             break;
208
 
163
 
209
         case BGRA:
164
         case BGRA:
210
-            if (bpp != 32) {
211
-                printf("TextureManager::Load ERROR Unsupported BGRA, %i bpp\n", bpp);
212
-                return -1;
213
-            }
214
             bytes = 4;
165
             bytes = 4;
215
             glcMode = GL_BGRA_EXT;
166
             glcMode = GL_BGRA_EXT;
216
             break;
167
             break;
217
     }
168
     }
218
 
169
 
170
+    glColor3ubv(WHITE);
219
     glClearColor(0.0, 0.0, 0.0, 0.0);
171
     glClearColor(0.0, 0.0, 0.0, 0.0);
220
     glEnable(GL_DEPTH_TEST);
172
     glEnable(GL_DEPTH_TEST);
221
     glShadeModel(GL_SMOOTH);
173
     glShadeModel(GL_SMOOTH);
241
 }
193
 }
242
 
194
 
243
 void TextureManager::bindTextureId(unsigned int n) {
195
 void TextureManager::bindTextureId(unsigned int n) {
244
-    assert(mTextureIds != NULL);
245
-    assert(n <= mTextureCount);
196
+    assert(n < mTextureIds.size());
246
 
197
 
247
     glEnable(GL_TEXTURE_2D);
198
     glEnable(GL_TEXTURE_2D);
248
     //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
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
 int TextureManager::loadPCX(const char *filename) {
204
 int TextureManager::loadPCX(const char *filename) {
266
             delete [] image;
217
             delete [] image;
267
             image = image2;
218
             image = image2;
268
         }
219
         }
269
-        id = loadBuffer(image, w, h, c, bpp);
220
+        id = loadBufferSlot(image, w, h, c, bpp, mTextureIds.size());
270
         delete [] image;
221
         delete [] image;
271
     }
222
     }
272
 
223
 
277
     assert(filename != NULL);
228
     assert(filename != NULL);
278
     assert(filename[0] != '\0');
229
     assert(filename[0] != '\0');
279
 
230
 
280
-    unsigned char *image = NULL;
281
-    unsigned char *image2 = NULL;
231
+    unsigned char *image;
282
     unsigned int w, h;
232
     unsigned int w, h;
283
     char type;
233
     char type;
284
     int id = -1;
234
     int id = -1;
286
     if (!tgaCheck(filename)) {
236
     if (!tgaCheck(filename)) {
287
         tgaLoad(filename, &image, &w, &h, &type);
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
         if (image2) {
240
         if (image2) {
292
             delete [] image;
241
             delete [] image;
293
             image = image2;
242
             image = image2;
294
         }
243
         }
295
-
296
         if (image) {
244
         if (image) {
297
-            id = loadBuffer(image, w, h,
245
+            id = loadBufferSlot(image, w, h,
298
                     (type == 2) ? RGBA : RGB,
246
                     (type == 2) ? RGBA : RGB,
299
-                    (type == 2) ? 32 : 24);
300
-
247
+                    (type == 2) ? 32 : 24,
248
+                    mTextureIds.size());
301
             delete [] image;
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
     return id;
253
     return id;
310
 }
254
 }
311
 
255
 

+ 6
- 0
src/main.cpp View File

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

+ 16
- 0
src/utils/pixel.cpp View File

8
 #include "global.h"
8
 #include "global.h"
9
 #include "utils/pixel.h"
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
 void bgr2rgb24(unsigned char *image, unsigned int w, unsigned int h) {
27
 void bgr2rgb24(unsigned char *image, unsigned int w, unsigned int h) {
12
     assert(image != nullptr);
28
     assert(image != nullptr);
13
     assert(w > 0);
29
     assert(w > 0);

Loading…
Cancel
Save