Parcourir la source

Added utils/pixels

Thomas Buck il y a 10 ans
Parent
révision
0617cfc738
8 fichiers modifiés avec 74 ajouts et 31 suppressions
  1. 0
    1
      TODO.md
  2. 9
    9
      include/Texture.h
  3. 14
    0
      include/utils/pixel.h
  4. 23
    19
      src/Texture.cpp
  5. 1
    0
      src/utils/CMakeLists.txt
  6. 25
    0
      src/utils/pixel.cpp
  7. 1
    1
      src/utils/strings.cpp
  8. 1
    1
      src/utils/time.cpp

+ 0
- 1
TODO.md Voir le fichier

@@ -10,7 +10,6 @@
10 10
     * Rewrite Console and use operator << to write to the console?
11 11
 * SDL_TTF 2.0.12+ can do line breaks, use it: http://stackoverflow.com/questions/17847818/how-to-do-line-breaks-and-line-wrapping-with-sdl-ttf/18418688#18418688
12 12
 * Mesh has 2 different approaches of storing the same data (eg. mColors and mColorArray), but half of ‘em isn’t implemented. Unify this, probably even reusing the Mesh class for the model_meshes...
13
-* glTexImage2D has alignment requirements?!?! https://www.khronos.org/opengles/sdk/1.1/docs/man/glTexImage2D.xml
14 13
 
15 14
 ## Changes
16 15
 

+ 9
- 9
include/Texture.h Voir le fichier

@@ -9,8 +9,6 @@
9 9
 #ifndef _TEXTURE_H
10 10
 #define _TEXTURE_H
11 11
 
12
-#include <stdio.h>
13
-
14 12
 /*!
15 13
  * \brief Texture registry
16 14
  */
@@ -18,10 +16,12 @@ class Texture {
18 16
 public:
19 17
 
20 18
     enum ColorMode {
21
-        GREYSCALE = 1,
19
+        GREYSCALE,
22 20
         RGB,
23 21
         RGBA,
24
-        ARGB
22
+        ARGB,
23
+        BGR,
24
+        BGRA
25 25
     };
26 26
 
27 27
     enum TextureFlag {
@@ -40,6 +40,11 @@ public:
40 40
     ~Texture();
41 41
 
42 42
     /*!
43
+     * \brief Resets all texture data
44
+     */
45
+    void reset();
46
+
47
+    /*!
43 48
      * \brief Generates a texture buffer with (width * height * 4) bytes.
44 49
      * \param rgba 32bpp RGBA color to fill into buffer
45 50
      * \param width width of newly allocated buffer, power of 2, pref same as height
@@ -125,11 +130,6 @@ public:
125 130
     int loadPCX(const char *filename);
126 131
 
127 132
     /*!
128
-     * \brief Resets all texture data
129
-     */
130
-    void reset();
131
-
132
-    /*!
133 133
      * \brief Sets an option flag
134 134
      * \param flag flag to set
135 135
      */

+ 14
- 0
include/utils/pixel.h Voir le fichier

@@ -0,0 +1,14 @@
1
+/*!
2
+ * \file include/utils/pixel.h
3
+ * \brief Pixel buffer utilities
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _UTILS_PIXEL_H_
9
+#define _UTILS_PIXEL_H_
10
+
11
+void argb2rgba32(unsigned char *image, unsigned int w, unsigned int h);
12
+
13
+#endif
14
+

+ 23
- 19
src/Texture.cpp Voir le fichier

@@ -13,6 +13,7 @@
13 13
 
14 14
 #include "global.h"
15 15
 #include "utils/pcx.h"
16
+#include "utils/pixel.h"
16 17
 #include "utils/strings.h"
17 18
 #include "utils/tga.h"
18 19
 #include "Texture.h"
@@ -164,24 +165,6 @@ int Texture::loadBuffer(unsigned char *image,
164 165
     return id;
165 166
 }
166 167
 
167
-void convertARGB32bppToRGBA32bpp(unsigned char *image,
168
-        unsigned int w, unsigned int h) {
169
-    unsigned int i, size = w * h;
170
-
171
-    assert(image != NULL);
172
-    assert(w > 0);
173
-    assert(h > 0);
174
-
175
-    for (i = 0; i < size; ++i) {
176
-        /* 32-bit ARGB to RGBA */
177
-        unsigned char swap = image[(i * 4) + 3];
178
-        image[(i * 4)] = image[(i * 4) + 1];
179
-        image[(i * 4) + 1] = image[(i * 4) + 2];
180
-        image[(i * 4) + 2] = image[(i * 4) + 3];
181
-        image[(i * 4) + 3] = swap;
182
-    }
183
-}
184
-
185 168
 int Texture::loadBufferSlot(unsigned char *image,
186 169
         unsigned int width, unsigned int height,
187 170
         ColorMode mode, unsigned int bpp,
@@ -205,6 +188,7 @@ int Texture::loadBufferSlot(unsigned char *image,
205 188
             bytes = 1;
206 189
             glcMode = GL_LUMINANCE;
207 190
             break;
191
+
208 192
         case RGB:
209 193
             if (bpp != 24) {
210 194
                 printf("Texture::Load ERROR Unsupported RGB, %i bpp\n", bpp);
@@ -213,9 +197,10 @@ int Texture::loadBufferSlot(unsigned char *image,
213 197
             bytes = 3;
214 198
             glcMode = GL_RGB;
215 199
             break;
200
+
216 201
         case ARGB:
217 202
             if (bpp == 32) {
218
-                convertARGB32bppToRGBA32bpp(image, width, height);
203
+                argb2rgba32(image, width, height);
219 204
             } else {
220 205
                 printf("Texture::Load ERROR Unsupported ARGB, %i bpp\n", bpp);
221 206
                 return -1;
@@ -223,6 +208,7 @@ int Texture::loadBufferSlot(unsigned char *image,
223 208
             bytes = 4;
224 209
             glcMode = GL_RGBA;
225 210
             break;
211
+
226 212
         case RGBA:
227 213
             if (bpp != 32) {
228 214
                 printf("Texture::Load ERROR Unsupported RGBA, %i bpp\n", bpp);
@@ -231,6 +217,24 @@ int Texture::loadBufferSlot(unsigned char *image,
231 217
             bytes = 4;
232 218
             glcMode = GL_RGBA;
233 219
             break;
220
+
221
+        case BGR:
222
+            if (bpp != 24) {
223
+                printf("Texture::Load ERROR Unsupported BGR, %i bpp\n", bpp);
224
+                return -1;
225
+            }
226
+            bytes = 3;
227
+            glcMode = GL_BGR_EXT;
228
+            break;
229
+
230
+        case BGRA:
231
+            if (bpp != 32) {
232
+                printf("Texture::Load ERROR Unsupported BGRA, %i bpp\n", bpp);
233
+                return -1;
234
+            }
235
+            bytes = 4;
236
+            glcMode = GL_BGRA_EXT;
237
+            break;
234 238
     }
235 239
 
236 240
     glClearColor(0.0, 0.0, 0.0, 0.0);

+ 1
- 0
src/utils/CMakeLists.txt Voir le fichier

@@ -1,5 +1,6 @@
1 1
 # Source files
2 2
 set (UTIL_SRCS ${UTIL_SRCS} "pcx.cpp")
3
+set (UTIL_SRCS ${UTIL_SRCS} "pixel.cpp")
3 4
 set (UTIL_SRCS ${UTIL_SRCS} "strings.cpp")
4 5
 set (UTIL_SRCS ${UTIL_SRCS} "tga.cpp")
5 6
 set (UTIL_SRCS ${UTIL_SRCS} "time.cpp")

+ 25
- 0
src/utils/pixel.cpp Voir le fichier

@@ -0,0 +1,25 @@
1
+/*!
2
+ * \file src/utils/pixel.cpp
3
+ * \brief Pixel buffer utilities
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#include "global.h"
9
+#include "utils/pixel.h"
10
+
11
+void argb2rgba32(unsigned char *image, unsigned int w, unsigned int h) {
12
+    assert(image != nullptr);
13
+    assert(w > 0);
14
+    assert(h > 0);
15
+
16
+    for (unsigned int i = 0; i < (w * h); ++i) {
17
+        /* 32-bit ARGB to RGBA */
18
+        unsigned char swap = image[(i * 4) + 3];
19
+        image[(i * 4)] = image[(i * 4) + 1];
20
+        image[(i * 4) + 1] = image[(i * 4) + 2];
21
+        image[(i * 4) + 2] = image[(i * 4) + 3];
22
+        image[(i * 4) + 3] = swap;
23
+    }
24
+}
25
+

+ 1
- 1
src/utils/strings.cpp Voir le fichier

@@ -1,5 +1,5 @@
1 1
 /*!
2
- * \file include/utils/strings.h
2
+ * \file src/utils/strings.cpp
3 3
  * \brief String handling utilities
4 4
  *
5 5
  * \author xythobuz

+ 1
- 1
src/utils/time.cpp Voir le fichier

@@ -1,5 +1,5 @@
1 1
 /*!
2
- * \file include/utils/time.h
2
+ * \file src/utils/time.cpp
3 3
  * \brief Time handling utilities
4 4
  *
5 5
  * \author xythobuz

Chargement…
Annuler
Enregistrer