Browse Source

textureloader - png - loading from memory without fmemopen

Henrique Santiago 10 years ago
parent
commit
c70e27494c

+ 1
- 1
src/framework/engine.cpp View File

@@ -60,7 +60,7 @@ void Engine::init()
60 60
     // finally show the window
61 61
     onResize(width, height);
62 62
     Platform::showWindow();
63
-    Platform::hideMouseCursor();
63
+    //Platform::hideMouseCursor();
64 64
 }
65 65
 
66 66
 void Engine::terminate()

+ 16
- 11
src/framework/textureloader.cpp View File

@@ -27,18 +27,23 @@
27 27
 
28 28
 #include <png.h>
29 29
 
30
-Texture *TextureLoader::loadPNG(const unsigned char *fileData, unsigned int fileSize)
30
+void png_read_from_mem(png_structp png_ptr, png_bytep data, png_size_t size)
31 31
 {
32
-    FILE *pngFile = fmemopen((void*)fileData, fileSize, "rb");
32
+  File *file = (File*)png_get_io_ptr(png_ptr);
33 33
 
34
-    if(!pngFile)
35
-        return NULL;
34
+  /* Copy data from image buffer */
35
+  memcpy(data, file->data + file->offset, size);
36 36
 
37
-    png_byte sig[8];
38
-    if(!fread(&sig, 8, 1, pngFile))
39
-        return NULL;
37
+  /* Advance in the file */
38
+  file->offset += size;
39
+}
40 40
 
41
-    if(png_sig_cmp(sig, 0, 8))
41
+Texture *TextureLoader::loadPNG(unsigned char *fileData, unsigned int fileSize)
42
+{
43
+    File file;
44
+    file.data = fileData;
45
+
46
+    if(png_sig_cmp(file.data, 0, 8))
42 47
         return NULL;
43 48
 
44 49
     png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
@@ -56,8 +61,9 @@ Texture *TextureLoader::loadPNG(const unsigned char *fileData, unsigned int file
56 61
         return NULL;
57 62
     }
58 63
 
59
-    png_init_io(png_ptr, pngFile);
60
-    png_set_sig_bytes(png_ptr, 8);
64
+    // Set "png_read" callback function and give source of data
65
+    png_set_read_fn(png_ptr, (png_voidp *)&file, png_read_from_mem);
66
+
61 67
     png_read_info(png_ptr, info_ptr);
62 68
 
63 69
     int bitDepth = png_get_bit_depth(png_ptr, info_ptr);
@@ -113,7 +119,6 @@ Texture *TextureLoader::loadPNG(const unsigned char *fileData, unsigned int file
113 119
     png_read_end(png_ptr, NULL);
114 120
 
115 121
     png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
116
-    fclose(pngFile);
117 122
     delete[] row_pointers;
118 123
 
119 124
     Texture *texture = new Texture(width, height, components, pixels);

+ 11
- 1
src/framework/textureloader.h View File

@@ -29,10 +29,20 @@
29 29
 
30 30
 class Texture;
31 31
 
32
+struct File
33
+{
34
+    File() {
35
+        offset = 0;
36
+    }
37
+
38
+    unsigned char *data;
39
+    unsigned long offset;
40
+};
41
+
32 42
 namespace TextureLoader
33 43
 {
34 44
     /// Load a png textures using libpng
35
-    Texture *loadPNG(const unsigned char *fileData, unsigned int fileSize);
45
+    Texture *loadPNG(unsigned char *fileData, unsigned int fileSize);
36 46
 }
37 47
 
38 48
 #endif // TEXTURELOADER_H

+ 1
- 0
src/framework/win32platform.cpp View File

@@ -0,0 +1 @@
1
+

+ 2
- 2
src/main.cpp View File

@@ -34,7 +34,7 @@ void signal_handler(int sig)
34 34
     switch(sig) {
35 35
         case SIGTERM:
36 36
         case SIGINT:
37
-        case SIGQUIT:
37
+        //case SIGQUIT:
38 38
         {
39 39
             static bool stopping = false;
40 40
             if(!stopping) {
@@ -58,7 +58,7 @@ int main(int argc, const char *argv[])
58 58
     // install our signal handler
59 59
     signal(SIGTERM, signal_handler);
60 60
     signal(SIGINT, signal_handler);
61
-    signal(SIGQUIT, signal_handler);
61
+    //signal(SIGQUIT, signal_handler);
62 62
 
63 63
     // setup resources
64 64
     g_resources.init(argv[0]);

Loading…
Cancel
Save