SDL:Tutorials:Displaying a Bitmap from a zip file using SDL RWops and zlib
From GPWikiUsing zlib, I was able to open and read an image from SDL_RWops. I downloaded the pre-built zlib DLLs, and included it into my project, making sure to add the zlib include files and libraries to the project. [edit] IntroductionUsing zip files is a good way to store lots of individual resources into one big file. The difficulty is extracting the information. The pre-built zlib file zlib123-dll.zip includes some helpful functions for opening and reading files from within a zip archive. These functions were available in the unzip.h header included in the pre-built package. Here is an example of how to display an image from within a zip file using zlib and SDL_RWops: [edit] Source Code#include <stdio.h> #include "SDL.h" #include <zlib.h> #include <unzip.h> int main(int argc, char *argv[]) { // useful things from zlib/unzip.h unzFile zip; unz_file_info info; Uint8 *buffer; int result; SDL_RWops *rw; SDL_Surface *screen; SDL_Surface *bmp; SDL_Rect rect; // Initialize SDL SDL_Init(SDL_INIT_VIDEO); atexit(SDL_Quit); screen = SDL_SetVideoMode(640, 480, 16, SDL_DOUBLEBUF); zip = unzOpen( "data.zip" ); if ( zip == NULL ) { printf("Error opening unzOpen('zipfile.zip')\n"); exit(1); } result = unzLocateFile( zip, "ImageInZipfile.bmp", 0 ); if ( result != UNZ_OK ) { printf("unzLocateFile(\"ImageInZipfile.bmp\") != UNZ_OK(%d)\n", result); } result = unzGetCurrentFileInfo( zip, &info, NULL, 0, NULL, 0, NULL, 0 ); if ( result != UNZ_OK ) { printf("unzGetCurrentFileInfo() != UNZ_OK (%d)\n", result); exit(1); } result = unzOpenCurrentFile( zip ); if ( result != UNZ_OK ) { printf("unzOpenCurrentFile() != UNZ_OK (%d)\n", result); exit(1); } buffer = (Uint8*)malloc(info.uncompressed_size); result = unzReadCurrentFile( zip, buffer, info.uncompressed_size ); if ( result != info.uncompressed_size ) { printf("unzReadCurrentFile() Error (%d != %d)\n", result, info.uncompressed_size); exit(1); } rw = SDL_RWFromMem(buffer, info.uncompressed_size); if(!rw) { printf("Error with SDL_RWFromMem: %s\n", SDL_GetError() ); exit(1); } /* The function that does the loading doesn't change at all */ bmp = SDL_LoadBMP_RW(rw, 0); if(!bmp) { printf("Error loading to SDL_Surface: %s\n", SDL_GetError() ); exit(1); } /* Clean up after ourselves */ free(buffer); SDL_FreeRW(rw); unzClose(zip); /* Haphazard way of getting stuff to the screen */ rect.x = rect.y = 20; rect.w = bmp -> w; rect.y = bmp -> h; SDL_BlitSurface(bmp, NULL, screen, &rect); SDL_Flip(screen); SDL_Delay(3000); return(0); } --- Please feel free to fix/correct this entry. |


