textureloader - png - loading from memory without fmemopen

This commit is contained in:
Henrique Santiago 2011-03-18 17:39:21 -03:00
parent b80dc4d6e0
commit c70e27494c
5 changed files with 31 additions and 15 deletions

View File

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

View File

@ -27,18 +27,23 @@
#include <png.h> #include <png.h>
Texture *TextureLoader::loadPNG(const unsigned char *fileData, unsigned int fileSize) void png_read_from_mem(png_structp png_ptr, png_bytep data, png_size_t size)
{ {
FILE *pngFile = fmemopen((void*)fileData, fileSize, "rb"); File *file = (File*)png_get_io_ptr(png_ptr);
if(!pngFile) /* Copy data from image buffer */
return NULL; memcpy(data, file->data + file->offset, size);
png_byte sig[8]; /* Advance in the file */
if(!fread(&sig, 8, 1, pngFile)) file->offset += size;
return NULL; }
if(png_sig_cmp(sig, 0, 8)) Texture *TextureLoader::loadPNG(unsigned char *fileData, unsigned int fileSize)
{
File file;
file.data = fileData;
if(png_sig_cmp(file.data, 0, 8))
return NULL; return NULL;
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); 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
return NULL; return NULL;
} }
png_init_io(png_ptr, pngFile); // Set "png_read" callback function and give source of data
png_set_sig_bytes(png_ptr, 8); png_set_read_fn(png_ptr, (png_voidp *)&file, png_read_from_mem);
png_read_info(png_ptr, info_ptr); png_read_info(png_ptr, info_ptr);
int bitDepth = png_get_bit_depth(png_ptr, info_ptr); int bitDepth = png_get_bit_depth(png_ptr, info_ptr);
@ -113,7 +119,6 @@ Texture *TextureLoader::loadPNG(const unsigned char *fileData, unsigned int file
png_read_end(png_ptr, NULL); png_read_end(png_ptr, NULL);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL); png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(pngFile);
delete[] row_pointers; delete[] row_pointers;
Texture *texture = new Texture(width, height, components, pixels); Texture *texture = new Texture(width, height, components, pixels);

View File

@ -29,10 +29,20 @@
class Texture; class Texture;
struct File
{
File() {
offset = 0;
}
unsigned char *data;
unsigned long offset;
};
namespace TextureLoader namespace TextureLoader
{ {
/// Load a png textures using libpng /// Load a png textures using libpng
Texture *loadPNG(const unsigned char *fileData, unsigned int fileSize); Texture *loadPNG(unsigned char *fileData, unsigned int fileSize);
} }
#endif // TEXTURELOADER_H #endif // TEXTURELOADER_H

View File

@ -0,0 +1 @@

View File

@ -34,7 +34,7 @@ void signal_handler(int sig)
switch(sig) { switch(sig) {
case SIGTERM: case SIGTERM:
case SIGINT: case SIGINT:
case SIGQUIT: //case SIGQUIT:
{ {
static bool stopping = false; static bool stopping = false;
if(!stopping) { if(!stopping) {
@ -58,7 +58,7 @@ int main(int argc, const char *argv[])
// install our signal handler // install our signal handler
signal(SIGTERM, signal_handler); signal(SIGTERM, signal_handler);
signal(SIGINT, signal_handler); signal(SIGINT, signal_handler);
signal(SIGQUIT, signal_handler); //signal(SIGQUIT, signal_handler);
// setup resources // setup resources
g_resources.init(argv[0]); g_resources.init(argv[0]);