diff --git a/aee.vcxproj b/aee.vcxproj index 1e11a08..f3ffa8c 100644 --- a/aee.vcxproj +++ b/aee.vcxproj @@ -58,7 +58,7 @@ Windows true - SDL2.lib;SDL2main.lib;SDL2_image.lib;SDL2_mixer.lib;%(AdditionalDependencies) + SDL2.lib;SDL2main.lib;SDL2_mixer.lib;%(AdditionalDependencies) diff --git a/gif.c b/gif.c index b2ee826..6061baf 100644 --- a/gif.c +++ b/gif.c @@ -13,6 +13,7 @@ #define PLAINTEXT_EXTENSION 0x01 #define READ(dst, size) memcpy(dst, buffer, size); buffer += size + typedef struct { unsigned short width; @@ -277,7 +278,7 @@ static int read_sub_blocks( unsigned char* buffer, unsigned char **data ) } data_length += block_size; - *data = realloc( *data, data_length ); + *data = (unsigned char*)realloc( *data, data_length ); // TODO this could be split across block size boundaries READ(*data + index, block_size); @@ -288,7 +289,7 @@ static int read_sub_blocks( unsigned char* buffer, unsigned char **data ) return data_length; } -static int process_image_descriptor( unsigned char* buffer, +unsigned char* process_image_descriptor( unsigned char* buffer, rgb *gct, int gct_size, int resolution_bits ) @@ -299,7 +300,7 @@ static int process_image_descriptor( unsigned char* buffer, unsigned char *compressed_data = NULL; unsigned char lzw_code_size; int uncompressed_data_length = 0; - //unsigned char *uncompressed_data = NULL; + unsigned char *uncompressed_data = NULL; // TODO there could actually be lots of these READ(&image_descriptor, 9); @@ -316,19 +317,17 @@ static int process_image_descriptor( unsigned char* buffer, height = image_descriptor.image_height; uncompressed_data_length = image_descriptor.image_width * image_descriptor.image_height; - uncompressed_data = malloc( uncompressed_data_length ); + uncompressed_data = (unsigned char*)malloc( uncompressed_data_length ); uncompress( lzw_code_size, compressed_data, compressed_data_length, uncompressed_data ); -done: - if ( compressed_data ) - free( compressed_data ); + if ( compressed_data ) free( compressed_data ); //if ( uncompressed_data ) // free( uncompressed_data ); - return disposition; + return uncompressed_data; } static int process_extension( unsigned char* buffer ) @@ -378,6 +377,30 @@ static int process_extension( unsigned char* buffer ) * GIF-encoded file. This should point to the first byte in * the file when invoked. */ + +unsigned char* LoadPalette(unsigned char *buffer) { + unsigned char header[7]; + screen_descriptor_t screen_descriptor; + int color_resolution_bits; + + int global_color_table_size = 0; // number of entries in global_color_table + rgb *global_color_table = NULL; + + READ(header, 6); + READ(&screen_descriptor, 7); + + color_resolution_bits = ((screen_descriptor.fields & 0x70) >> 4) + 1; + global_color_table = (rgb *)calloc(1, 768); + + if (screen_descriptor.fields & 0x80) { + global_color_table_size = 1 << (((screen_descriptor.fields & 0x07) + 1)); + + //global_color_table = (rgb *)malloc(3 * global_color_table_size); + READ(global_color_table, 3 * global_color_table_size); + } + return (unsigned char*)global_color_table; +} + static unsigned char* process_gif_stream(unsigned char *buffer, unsigned short* w, unsigned short* h) { unsigned char header[ 7 ]; @@ -400,7 +423,7 @@ static unsigned char* process_gif_stream(unsigned char *buffer, unsigned short* fprintf( stderr, "Invalid GIF file (header is '%s', should be 'GIF89a')\n", header ); - return; + return NULL; } // Followed by a logical screen descriptor @@ -433,18 +456,15 @@ static unsigned char* process_gif_stream(unsigned char *buffer, unsigned short* switch ( block_type ) { case IMAGE_DESCRIPTOR: - if ( !process_image_descriptor( buffer, - global_color_table, - global_color_table_size, - color_resolution_bits ) ) - { - return; - } + return process_image_descriptor(buffer, + global_color_table, + global_color_table_size, + color_resolution_bits); break; case EXTENSION_INTRODUCER: if ( !process_extension( buffer ) ) { - return; + return NULL; } break; case TRAILER: @@ -452,9 +472,10 @@ static unsigned char* process_gif_stream(unsigned char *buffer, unsigned short* default: fprintf( stderr, "Bailing on unrecognized block type %.02x\n", block_type ); - return; + return NULL; } } + return NULL; } @@ -462,7 +483,7 @@ unsigned char* LoadGif(unsigned char *buffer, unsigned short* w, unsigned short* return process_gif_stream(buffer, w, h); } -int main( int argc, char *argv[] ) +/*int main( int argc, char *argv[] ) { FILE* gif_file; @@ -483,4 +504,4 @@ int main( int argc, char *argv[] ) process_gif_stream( gif_file ); fclose( gif_file ); -} +}*/ diff --git a/jdraw8.cpp b/jdraw8.cpp index e2ea80f..69b3172 100755 --- a/jdraw8.cpp +++ b/jdraw8.cpp @@ -1,8 +1,7 @@ #include "jdraw8.h" -#include "SDL_image.h" -//#include "SDL_opengl.h" #include "jfile.h" #include +#include "gif.c" JD8_Surface screen = NULL; JD8_Palette main_palette = NULL; @@ -27,7 +26,6 @@ void JD8_Init(const char *title) { sdlWindow = SDL_CreateWindow( title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN ); sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, 0); SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "nearest"); - SDL_RenderSetLogicalSize(sdlRenderer, 320, 200); sdlTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, 320, 200); } @@ -54,20 +52,20 @@ JD8_Surface JD8_LoadSurface(const char *file) { int filesize = 0; char *buffer = JF_GetBufferFromResource(file, filesize); - SDL_RWops *rw = SDL_RWFromMem(buffer, filesize); - SDL_Surface *temp = IMG_Load_RW(rw, 1); + unsigned short w, h; + Uint8* pixels = LoadGif((unsigned char*)buffer, &w, &h); free(buffer); - if (temp == NULL) { + if (pixels == NULL) { printf("Unable to load bitmap: %s\n", SDL_GetError()); exit(1); } JD8_Surface image = JD8_NewSurface(); - memcpy( image, temp->pixels, 64000 ); - - SDL_FreeSurface(temp); + memcpy(image, pixels, 64000); + + free(pixels); return image; } @@ -75,26 +73,8 @@ JD8_Palette JD8_LoadPalette(const char *file) { int filesize = 0; char *buffer = NULL; buffer = JF_GetBufferFromResource(file, filesize); - - SDL_RWops *rw = NULL; - rw = SDL_RWFromMem(buffer, filesize); - if( rw == NULL ) { printf("ERROR: No s'ha pogut crear el RWops"); exit(1); } - SDL_Surface *temp = NULL; - temp = IMG_Load_RW(rw, 1); - if( temp == NULL ) { printf("ERROR! No s'ha pogut crear la SDL_Surface: %s", SDL_GetError()); exit(1); } - - free(buffer); - - JD8_Palette palette = (JD8_Palette)malloc( 768 ); - - for( int i = 0; i < 256; i++ ) { - palette[i].r = temp->format->palette->colors[i].r; - palette[i].g = temp->format->palette->colors[i].g; - palette[i].b = temp->format->palette->colors[i].b; - } - - SDL_FreeSurface(temp); + JD8_Palette palette = (JD8_Palette)LoadPalette((unsigned char*)buffer); return palette; } @@ -152,6 +132,8 @@ void JD8_BlitCKToSurface(int x, int y, JD8_Surface surface, int sx, int sy, int } } +SDL_Rect rect{0, 0, 640, 480}; + void JD8_Flip() { for( int x = 0; x < 320; x++ ) { for( int y = 0; y < 200; y++ ) { @@ -160,7 +142,7 @@ void JD8_Flip() { } } SDL_UpdateTexture(sdlTexture, NULL, pixel_data, 320 * sizeof(Uint32)); - SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL); + SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &rect); SDL_RenderPresent(sdlRenderer); }