Quellcode durchsuchen

Fixed strange Texture offset in GL, added asserts.

Thomas Buck vor 11 Jahren
Ursprung
Commit
14978be957
3 geänderte Dateien mit 74 neuen und 73 gelöschten Zeilen
  1. 0
    1
      include/Texture.h
  2. 6
    16
      src/Render.cpp
  3. 68
    56
      src/Texture.cpp

+ 0
- 1
include/Texture.h Datei anzeigen

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

+ 6
- 16
src/Render.cpp Datei anzeigen

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

+ 68
- 56
src/Texture.cpp Datei anzeigen

10
 #include <stdlib.h>
10
 #include <stdlib.h>
11
 #include <stdio.h>
11
 #include <stdio.h>
12
 #include <stdarg.h>
12
 #include <stdarg.h>
13
+#include <assert.h>
13
 
14
 
14
 #ifdef __APPLE__
15
 #ifdef __APPLE__
15
 #include <OpenGL/gl.h>
16
 #include <OpenGL/gl.h>
22
 #include "utils/tga.h"
23
 #include "utils/tga.h"
23
 #include "Texture.h"
24
 #include "Texture.h"
24
 
25
 
25
-#define TEXTURE_OFFSET -1
26
-
27
 Texture::Texture() {
26
 Texture::Texture() {
28
     mTextureIds = NULL;
27
     mTextureIds = NULL;
29
     mFlags = 0;
28
     mFlags = 0;
42
     unsigned char *image;
41
     unsigned char *image;
43
     unsigned int i, size;
42
     unsigned int i, size;
44
 
43
 
44
+    assert(rgba != NULL);
45
+    assert(width > 0);
46
+    assert(height > 0);
47
+
45
     image = new unsigned char[height*width*4];
48
     image = new unsigned char[height*width*4];
46
 
49
 
47
     for (i = 0, size = width*height; i < size; ++i) {
50
     for (i = 0, size = width*height; i < size; ++i) {
60
     unsigned char *image;
63
     unsigned char *image;
61
     int id;
64
     int id;
62
 
65
 
66
+    assert(rgba != NULL);
67
+    assert(width > 0);
68
+    assert(height > 0);
69
+
63
     image = generateColorTexture(rgba, width, height);
70
     image = generateColorTexture(rgba, width, height);
64
     id = loadBuffer(image, width, height, RGBA, 32);
71
     id = loadBuffer(image, width, height, RGBA, 32);
65
     delete [] image;
72
     delete [] image;
81
         delete [] mTextureIds;
88
         delete [] mTextureIds;
82
     }
89
     }
83
 
90
 
84
-    mTextureIds = 0x0;
91
+    mTextureIds = NULL;
85
     mTextureCount = 0;
92
     mTextureCount = 0;
86
     mTextureLimit = 0;
93
     mTextureLimit = 0;
87
 }
94
 }
112
 }
119
 }
113
 
120
 
114
 void Texture::bindMultiTexture(int texture0, int texture1) {
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
     mFlags |= fUseMultiTexture;
128
     mFlags |= fUseMultiTexture;
123
     mTextureId  = texture0;
129
     mTextureId  = texture0;
125
 
131
 
126
     glActiveTextureARB(GL_TEXTURE0_ARB);
132
     glActiveTextureARB(GL_TEXTURE0_ARB);
127
     glEnable(GL_TEXTURE_2D);
133
     glEnable(GL_TEXTURE_2D);
128
-    glBindTexture(GL_TEXTURE_2D, mTextureIds[texture0] + TEXTURE_OFFSET);
134
+    glBindTexture(GL_TEXTURE_2D, mTextureIds[texture0]);
129
 
135
 
130
     glActiveTextureARB(GL_TEXTURE1_ARB);
136
     glActiveTextureARB(GL_TEXTURE1_ARB);
131
     glEnable(GL_TEXTURE_2D);
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
 void Texture::setMaxTextureCount(unsigned int n) {
141
 void Texture::setMaxTextureCount(unsigned int n) {
142
+    assert(n > 0);
143
+
136
     mTextureLimit = n;
144
     mTextureLimit = n;
137
 
145
 
138
     mTextureIds = new unsigned int[n];
146
     mTextureIds = new unsigned int[n];
141
 }
149
 }
142
 
150
 
143
 int Texture::getTextureCount() {
151
 int Texture::getTextureCount() {
144
-    return (mTextureCount-1);
152
+    return mTextureCount - 1;
145
 }
153
 }
146
 
154
 
147
 int Texture::loadBuffer(unsigned char *image,
155
 int Texture::loadBuffer(unsigned char *image,
149
         ColorMode mode, unsigned int bpp) {
157
         ColorMode mode, unsigned int bpp) {
150
     int id;
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
     id = loadBufferSlot(image, width, height, mode, bpp, mTextureCount++);
165
     id = loadBufferSlot(image, width, height, mode, bpp, mTextureCount++);
153
 
166
 
154
     if (id < 0)
167
     if (id < 0)
155
         return id;
168
         return id;
156
 
169
 
157
-    return ++id;
170
+    return id;
158
 }
171
 }
159
 
172
 
160
 void convertARGB32bppToRGBA32bpp(unsigned char *image,
173
 void convertARGB32bppToRGBA32bpp(unsigned char *image,
161
         unsigned int w, unsigned int h) {
174
         unsigned int w, unsigned int h) {
162
-    unsigned int i, size = w*h;
175
+    unsigned int i, size = w * h;
163
     unsigned char swap;
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
         /* 32-bit ARGB to RGBA */
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
     unsigned char bytes;
204
     unsigned char bytes;
189
     unsigned int glcMode;
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
     switch (mode) {
214
     switch (mode) {
203
         case GREYSCALE:
215
         case GREYSCALE:
204
             if (bpp != 8) {
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
             bytes = 1;
220
             bytes = 1;
209
             glcMode = GL_LUMINANCE;
221
             glcMode = GL_LUMINANCE;
210
             break;
222
             break;
211
         case RGB:
223
         case RGB:
212
             if (bpp != 24) {
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
             bytes = 3;
228
             bytes = 3;
217
             glcMode = GL_RGB;
229
             glcMode = GL_RGB;
220
             if (bpp == 32) {
232
             if (bpp == 32) {
221
                 convertARGB32bppToRGBA32bpp(image, width, height);
233
                 convertARGB32bppToRGBA32bpp(image, width, height);
222
             } else {
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
             bytes = 4;
238
             bytes = 4;
227
             glcMode = GL_RGBA;
239
             glcMode = GL_RGBA;
228
             break;
240
             break;
229
         case RGBA:
241
         case RGBA:
230
             if (bpp != 32) {
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
             bytes = 4;
246
             bytes = 4;
235
             glcMode = GL_RGBA;
247
             glcMode = GL_RGBA;
242
 
254
 
243
     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
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
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
259
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
248
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
260
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
271
 }
283
 }
272
 
284
 
273
 void Texture::bindTextureId(unsigned int n) {
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
     mTextureId = n;
290
     mTextureId = n;
279
 
291
 
280
     glEnable(GL_TEXTURE_2D);
292
     glEnable(GL_TEXTURE_2D);
281
     //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
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
 void Texture::glScreenShot(char *base, unsigned int width, unsigned int height) {
298
 void Texture::glScreenShot(char *base, unsigned int width, unsigned int height) {
291
     static int count = 0;
303
     static int count = 0;
292
     bool done = false;
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
     // Don't overwrite files
310
     // Don't overwrite files
303
     while (!done) {
311
     while (!done) {
328
     char type;
336
     char type;
329
     int id = -1;
337
     int id = -1;
330
 
338
 
339
+    assert(filename != NULL);
340
+    assert(filename[0] != '\0');
341
+
331
     f = fopen(filename, "rb");
342
     f = fopen(filename, "rb");
332
 
343
 
333
     if (!f) {
344
     if (!f) {
363
 }
374
 }
364
 
375
 
365
 int Texture::nextPower(int seed) {
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
     return i;
379
     return i;
369
 }
380
 }
370
 
381
 
371
 /* This code based off on gluScaleImage()  */
382
 /* This code based off on gluScaleImage()  */
372
 unsigned char *Texture::scaleBuffer(unsigned char *image,
383
 unsigned char *Texture::scaleBuffer(unsigned char *image,
373
-        int width,  int height, int components) {
384
+        int width, int height, int components) {
374
     int i, j, k;
385
     int i, j, k;
375
     float* tempin;
386
     float* tempin;
376
     float* tempout;
387
     float* tempout;
380
     int original_height = height;
391
     int original_height = height;
381
     int original_width = width;
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
     height = nextPower(height);
398
     height = nextPower(height);
387
     width = nextPower(width);
399
     width = nextPower(width);

Laden…
Abbrechen
Speichern