Преглед изворни кода

Fixed strange Texture offset in GL, added asserts.

Thomas Buck пре 10 година
родитељ
комит
14978be957
3 измењених фајлова са 74 додато и 73 уклоњено
  1. 0
    1
      include/Texture.h
  2. 6
    16
      src/Render.cpp
  3. 68
    56
      src/Texture.cpp

+ 0
- 1
include/Texture.h Прегледај датотеку

@@ -27,7 +27,6 @@ public:
27 27
     enum TextureFlag {
28 28
         fUseMipmaps      = (1 << 0),
29 29
         fUseMultiTexture = (1 << 1),
30
-        fUseSDL_TTF      = (1 << 2)
31 30
     };
32 31
 
33 32
     /*!

+ 6
- 16
src/Render.cpp Прегледај датотеку

@@ -149,11 +149,9 @@ void Render::initTextures(char *textureDir, unsigned int *numLoaded,
149 149
     char filename[128];
150 150
     int snow1_id;
151 151
     int snow2_id;
152
-    int bg_id;
153 152
     unsigned int numTextures = 0;
154 153
     unsigned char color[4];
155 154
 
156
-
157 155
     // We want to update as needed later
158 156
     mNumTexturesLoaded = numLoaded;
159 157
     mNextTextureId = nextId;
@@ -172,33 +170,25 @@ void Render::initTextures(char *textureDir, unsigned int *numLoaded,
172 170
     color[3] = 0xff;
173 171
 
174 172
     if (mTexture.loadColorTexture(color, 32, 32) > -1)
175
-    {
176
-        ++numTextures;
177
-    }
173
+        numTextures++;
178 174
 
179 175
     snprintf(filename, 126, "%s/%s", textureDir, "splash.tga");
180 176
     filename[127] = 0;
181 177
 
182
-    if ((bg_id = mTexture.loadTGA(filename)) > -1)
183
-    {
184
-        ++numTextures;
185
-    }
178
+    if (mTexture.loadTGA(filename) > -1)
179
+        numTextures++;
186 180
 
187 181
     snprintf(filename, 126, "%s/%s", textureDir, "snow.tga");
188 182
     filename[127] = 0;
189 183
 
190 184
     if ((snow1_id = mTexture.loadTGA(filename)) > -1)
191
-    {
192
-        ++numTextures;
193
-    }
185
+        numTextures++;
194 186
 
195 187
     snprintf(filename, 126, "%s/%s", textureDir, "snow2.tga");
196 188
     filename[127] = 0;
197 189
 
198 190
     if ((snow2_id = mTexture.loadTGA(filename)) > -1)
199
-    {
200
-        ++numTextures;
201
-    }
191
+        numTextures++;
202 192
 
203 193
     // Weird that it isn't linear, must be some storage deal in Texture
204 194
     // I forgot about Id allocation
@@ -791,7 +781,7 @@ void Render::drawLoadScreen()
791 781
     glTranslatef(0.0f, 0.0f, -2000.0f);
792 782
     glRotatef(180.0f, 1.0f, 0.0f, 0.0f);
793 783
 
794
-    glBindTexture(GL_TEXTURE_2D, 2); //! \fixme store texture id somewhere
784
+    glBindTexture(GL_TEXTURE_2D, 3); //! \fixme store texture id somewhere
795 785
 
796 786
     glBegin(GL_TRIANGLE_STRIP);
797 787
     glTexCoord2f(1.0, 1.0);

+ 68
- 56
src/Texture.cpp Прегледај датотеку

@@ -10,6 +10,7 @@
10 10
 #include <stdlib.h>
11 11
 #include <stdio.h>
12 12
 #include <stdarg.h>
13
+#include <assert.h>
13 14
 
14 15
 #ifdef __APPLE__
15 16
 #include <OpenGL/gl.h>
@@ -22,8 +23,6 @@
22 23
 #include "utils/tga.h"
23 24
 #include "Texture.h"
24 25
 
25
-#define TEXTURE_OFFSET -1
26
-
27 26
 Texture::Texture() {
28 27
     mTextureIds = NULL;
29 28
     mFlags = 0;
@@ -42,6 +41,10 @@ unsigned char *Texture::generateColorTexture(unsigned char rgba[4],
42 41
     unsigned char *image;
43 42
     unsigned int i, size;
44 43
 
44
+    assert(rgba != NULL);
45
+    assert(width > 0);
46
+    assert(height > 0);
47
+
45 48
     image = new unsigned char[height*width*4];
46 49
 
47 50
     for (i = 0, size = width*height; i < size; ++i) {
@@ -60,6 +63,10 @@ int Texture::loadColorTexture(unsigned char rgba[4],
60 63
     unsigned char *image;
61 64
     int id;
62 65
 
66
+    assert(rgba != NULL);
67
+    assert(width > 0);
68
+    assert(height > 0);
69
+
63 70
     image = generateColorTexture(rgba, width, height);
64 71
     id = loadBuffer(image, width, height, RGBA, 32);
65 72
     delete [] image;
@@ -81,7 +88,7 @@ void Texture::reset() {
81 88
         delete [] mTextureIds;
82 89
     }
83 90
 
84
-    mTextureIds = 0x0;
91
+    mTextureIds = NULL;
85 92
     mTextureCount = 0;
86 93
     mTextureLimit = 0;
87 94
 }
@@ -112,12 +119,11 @@ void Texture::useMultiTexture(float u, float v) {
112 119
 }
113 120
 
114 121
 void Texture::bindMultiTexture(int texture0, int texture1) {
115
-    if (//(int)a == mTextureId && (int)b == mTextureId2 ||
116
-            !mTextureIds ||
117
-            texture0 < 0 || texture0 > (int)mTextureCount ||
118
-            texture1 < 0 || texture1 > (int)mTextureCount) {
119
-        return;
120
-    }
122
+    assert(mTextureIds != NULL);
123
+    assert(texture0 >= 0);
124
+    assert((unsigned int)texture0 <= mTextureCount);
125
+    assert(texture1 >= 0);
126
+    assert((unsigned int)texture1 <= mTextureCount);
121 127
 
122 128
     mFlags |= fUseMultiTexture;
123 129
     mTextureId  = texture0;
@@ -125,14 +131,16 @@ void Texture::bindMultiTexture(int texture0, int texture1) {
125 131
 
126 132
     glActiveTextureARB(GL_TEXTURE0_ARB);
127 133
     glEnable(GL_TEXTURE_2D);
128
-    glBindTexture(GL_TEXTURE_2D, mTextureIds[texture0] + TEXTURE_OFFSET);
134
+    glBindTexture(GL_TEXTURE_2D, mTextureIds[texture0]);
129 135
 
130 136
     glActiveTextureARB(GL_TEXTURE1_ARB);
131 137
     glEnable(GL_TEXTURE_2D);
132
-    glBindTexture(GL_TEXTURE_2D, mTextureIds[texture1] + TEXTURE_OFFSET);
138
+    glBindTexture(GL_TEXTURE_2D, mTextureIds[texture1]);
133 139
 }
134 140
 
135 141
 void Texture::setMaxTextureCount(unsigned int n) {
142
+    assert(n > 0);
143
+
136 144
     mTextureLimit = n;
137 145
 
138 146
     mTextureIds = new unsigned int[n];
@@ -141,7 +149,7 @@ void Texture::setMaxTextureCount(unsigned int n) {
141 149
 }
142 150
 
143 151
 int Texture::getTextureCount() {
144
-    return (mTextureCount-1);
152
+    return mTextureCount - 1;
145 153
 }
146 154
 
147 155
 int Texture::loadBuffer(unsigned char *image,
@@ -149,27 +157,35 @@ int Texture::loadBuffer(unsigned char *image,
149 157
         ColorMode mode, unsigned int bpp) {
150 158
     int id;
151 159
 
160
+    assert(image != NULL);
161
+    assert(width > 0);
162
+    assert(height > 0);
163
+    assert((bpp == 8) || (bpp == 24) || (bpp == 32));
164
+
152 165
     id = loadBufferSlot(image, width, height, mode, bpp, mTextureCount++);
153 166
 
154 167
     if (id < 0)
155 168
         return id;
156 169
 
157
-    return ++id;
170
+    return id;
158 171
 }
159 172
 
160 173
 void convertARGB32bppToRGBA32bpp(unsigned char *image,
161 174
         unsigned int w, unsigned int h) {
162
-    unsigned int i, size = w*h;
175
+    unsigned int i, size = w * h;
163 176
     unsigned char swap;
164 177
 
165
-    for (i = 0; i < size; ++i)
166
-    {
178
+    assert(image != NULL);
179
+    assert(w > 0);
180
+    assert(h > 0);
181
+
182
+    for (i = 0; i < size; ++i) {
167 183
         /* 32-bit ARGB to RGBA */
168
-        swap = image[i*4+3];
169
-        image[i*4]   = image[i*4+1];
170
-        image[i*4+1] = image[i*4+2];
171
-        image[i*4+2] = image[i*4+3];
172
-        image[i*4+3] = swap;
184
+        swap = image[(i * 4) + 3];
185
+        image[(i * 4)] = image[(i * 4) + 1];
186
+        image[(i * 4) + 1] = image[(i * 4) + 2];
187
+        image[(i * 4) + 2] = image[(i * 4) + 3];
188
+        image[(i * 4) + 3] = swap;
173 189
     }
174 190
 }
175 191
 
@@ -188,30 +204,26 @@ int Texture::loadBufferSlot(unsigned char *image,
188 204
     unsigned char bytes;
189 205
     unsigned int glcMode;
190 206
 
191
-
192
-    if (!mTextureIds || slot >= mTextureLimit) {
193
-        printf("Texture::Load> ERROR Not initialized or out of free slots\n");
194
-        return -1000;
195
-    }
196
-
197
-    if (!width || !height || !image) {
198
-        printf("Texture::Load> ERROR Assertion 'image is valid' failed\n");
199
-        return -1;
200
-    }
207
+    assert(mTextureIds != NULL);
208
+    assert(slot < mTextureLimit);
209
+    assert(image != NULL);
210
+    assert(width > 0);
211
+    assert(height > 0);
212
+    assert((bpp == 8) || (bpp == 24) || (bpp == 32));
201 213
 
202 214
     switch (mode) {
203 215
         case GREYSCALE:
204 216
             if (bpp != 8) {
205
-                printf("Texture::Load> ERROR Unsupported GREYSCALE, %i bpp\n", bpp);
206
-                return -2;
217
+                printf("Texture::Load ERROR Unsupported GREYSCALE, %i bpp\n", bpp);
218
+                return -1;
207 219
             }
208 220
             bytes = 1;
209 221
             glcMode = GL_LUMINANCE;
210 222
             break;
211 223
         case RGB:
212 224
             if (bpp != 24) {
213
-                printf("Texture::Load> ERROR Unsupported RGB, %i bpp\n", bpp);
214
-                return -2;
225
+                printf("Texture::Load ERROR Unsupported RGB, %i bpp\n", bpp);
226
+                return -1;
215 227
             }
216 228
             bytes = 3;
217 229
             glcMode = GL_RGB;
@@ -220,16 +232,16 @@ int Texture::loadBufferSlot(unsigned char *image,
220 232
             if (bpp == 32) {
221 233
                 convertARGB32bppToRGBA32bpp(image, width, height);
222 234
             } else {
223
-                printf("Texture::Load> ERROR Unsupported ARGB, %i bpp\n", bpp);
224
-                return -2;
235
+                printf("Texture::Load ERROR Unsupported ARGB, %i bpp\n", bpp);
236
+                return -1;
225 237
             }
226 238
             bytes = 4;
227 239
             glcMode = GL_RGBA;
228 240
             break;
229 241
         case RGBA:
230 242
             if (bpp != 32) {
231
-                printf("Texture::Load> ERROR Unsupported RGBA, %i bpp\n", bpp);
232
-                return -2;
243
+                printf("Texture::Load ERROR Unsupported RGBA, %i bpp\n", bpp);
244
+                return -1;
233 245
             }
234 246
             bytes = 4;
235 247
             glcMode = GL_RGBA;
@@ -242,7 +254,7 @@ int Texture::loadBufferSlot(unsigned char *image,
242 254
 
243 255
     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
244 256
 
245
-    glBindTexture(GL_TEXTURE_2D, mTextureIds[slot] + TEXTURE_OFFSET);
257
+    glBindTexture(GL_TEXTURE_2D, mTextureIds[slot]);
246 258
 
247 259
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
248 260
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
@@ -271,16 +283,16 @@ int Texture::loadBufferSlot(unsigned char *image,
271 283
 }
272 284
 
273 285
 void Texture::bindTextureId(unsigned int n) {
274
-    if ((int)n == mTextureId || !mTextureIds || n > mTextureCount) {
275
-        return;
276
-    }
286
+    assert(mTextureIds != NULL);
287
+    assert((int)n != mTextureId);
288
+    assert(n <= mTextureCount);
277 289
 
278 290
     mTextureId = n;
279 291
 
280 292
     glEnable(GL_TEXTURE_2D);
281 293
     //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
282 294
 
283
-    glBindTexture(GL_TEXTURE_2D, mTextureIds[n] + TEXTURE_OFFSET);
295
+    glBindTexture(GL_TEXTURE_2D, mTextureIds[n]);
284 296
 }
285 297
 
286 298
 void Texture::glScreenShot(char *base, unsigned int width, unsigned int height) {
@@ -291,13 +303,9 @@ void Texture::glScreenShot(char *base, unsigned int width, unsigned int height)
291 303
     static int count = 0;
292 304
     bool done = false;
293 305
 
294
-    if (!image || !width || !height) {
295
-        if (image)
296
-            delete [] image;
297
-
298
-        printf("glScreenShot> ERROR: Couldn't allocate image!\n");
299
-        return;
300
-    }
306
+    assert(base != NULL);
307
+    assert(width > 0);
308
+    assert(height > 0);
301 309
 
302 310
     // Don't overwrite files
303 311
     while (!done) {
@@ -328,6 +336,9 @@ int Texture::loadTGA(const char *filename) {
328 336
     char type;
329 337
     int id = -1;
330 338
 
339
+    assert(filename != NULL);
340
+    assert(filename[0] != '\0');
341
+
331 342
     f = fopen(filename, "rb");
332 343
 
333 344
     if (!f) {
@@ -363,14 +374,14 @@ int Texture::loadTGA(const char *filename) {
363 374
 }
364 375
 
365 376
 int Texture::nextPower(int seed) {
366
-    int i;
367
-    for (i = 1; i < seed; i *= 2);
377
+    int i = 1;
378
+    for (; i < seed; i *= 2);
368 379
     return i;
369 380
 }
370 381
 
371 382
 /* This code based off on gluScaleImage()  */
372 383
 unsigned char *Texture::scaleBuffer(unsigned char *image,
373
-        int width,  int height, int components) {
384
+        int width, int height, int components) {
374 385
     int i, j, k;
375 386
     float* tempin;
376 387
     float* tempout;
@@ -380,8 +391,9 @@ unsigned char *Texture::scaleBuffer(unsigned char *image,
380 391
     int original_height = height;
381 392
     int original_width = width;
382 393
 
383
-    if (!image || !width || !height)
384
-        return NULL;
394
+    assert(image != NULL);
395
+    assert(width > 0);
396
+    assert(height > 0);
385 397
 
386 398
     height = nextPower(height);
387 399
     width = nextPower(width);

Loading…
Откажи
Сачувај