GifLoader done. SDL_Image dependency removed from Windows version. TBD on OSX and Linux

This commit is contained in:
2016-02-19 16:49:39 +01:00
parent fb8bb0504c
commit f9ef26deda
3 changed files with 53 additions and 50 deletions

View File

@@ -58,7 +58,7 @@
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>SDL2.lib;SDL2main.lib;SDL2_image.lib;SDL2_mixer.lib;%(AdditionalDependencies)</AdditionalDependencies> <AdditionalDependencies>SDL2.lib;SDL2main.lib;SDL2_mixer.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">

57
gif.c
View File

@@ -13,6 +13,7 @@
#define PLAINTEXT_EXTENSION 0x01 #define PLAINTEXT_EXTENSION 0x01
#define READ(dst, size) memcpy(dst, buffer, size); buffer += size #define READ(dst, size) memcpy(dst, buffer, size); buffer += size
typedef struct typedef struct
{ {
unsigned short width; unsigned short width;
@@ -277,7 +278,7 @@ static int read_sub_blocks( unsigned char* buffer, unsigned char **data )
} }
data_length += block_size; 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 // TODO this could be split across block size boundaries
READ(*data + index, block_size); READ(*data + index, block_size);
@@ -288,7 +289,7 @@ static int read_sub_blocks( unsigned char* buffer, unsigned char **data )
return data_length; return data_length;
} }
static int process_image_descriptor( unsigned char* buffer, unsigned char* process_image_descriptor( unsigned char* buffer,
rgb *gct, rgb *gct,
int gct_size, int gct_size,
int resolution_bits ) int resolution_bits )
@@ -299,7 +300,7 @@ static int process_image_descriptor( unsigned char* buffer,
unsigned char *compressed_data = NULL; unsigned char *compressed_data = NULL;
unsigned char lzw_code_size; unsigned char lzw_code_size;
int uncompressed_data_length = 0; int uncompressed_data_length = 0;
//unsigned char *uncompressed_data = NULL; unsigned char *uncompressed_data = NULL;
// TODO there could actually be lots of these // TODO there could actually be lots of these
READ(&image_descriptor, 9); READ(&image_descriptor, 9);
@@ -316,19 +317,17 @@ static int process_image_descriptor( unsigned char* buffer,
height = image_descriptor.image_height; height = image_descriptor.image_height;
uncompressed_data_length = image_descriptor.image_width * uncompressed_data_length = image_descriptor.image_width *
image_descriptor.image_height; 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, uncompress( lzw_code_size, compressed_data, compressed_data_length,
uncompressed_data ); uncompressed_data );
done: if ( compressed_data ) free( compressed_data );
if ( compressed_data )
free( compressed_data );
//if ( uncompressed_data ) //if ( uncompressed_data )
// free( uncompressed_data ); // free( uncompressed_data );
return disposition; return uncompressed_data;
} }
static int process_extension( unsigned char* buffer ) 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 * GIF-encoded file. This should point to the first byte in
* the file when invoked. * 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) static unsigned char* process_gif_stream(unsigned char *buffer, unsigned short* w, unsigned short* h)
{ {
unsigned char header[ 7 ]; unsigned char header[ 7 ];
@@ -400,7 +423,7 @@ static unsigned char* process_gif_stream(unsigned char *buffer, unsigned short*
fprintf( stderr, fprintf( stderr,
"Invalid GIF file (header is '%s', should be 'GIF89a')\n", "Invalid GIF file (header is '%s', should be 'GIF89a')\n",
header ); header );
return; return NULL;
} }
// Followed by a logical screen descriptor // Followed by a logical screen descriptor
@@ -433,18 +456,15 @@ static unsigned char* process_gif_stream(unsigned char *buffer, unsigned short*
switch ( block_type ) switch ( block_type )
{ {
case IMAGE_DESCRIPTOR: case IMAGE_DESCRIPTOR:
if ( !process_image_descriptor( buffer, return process_image_descriptor(buffer,
global_color_table, global_color_table,
global_color_table_size, global_color_table_size,
color_resolution_bits ) ) color_resolution_bits);
{
return;
}
break; break;
case EXTENSION_INTRODUCER: case EXTENSION_INTRODUCER:
if ( !process_extension( buffer ) ) if ( !process_extension( buffer ) )
{ {
return; return NULL;
} }
break; break;
case TRAILER: case TRAILER:
@@ -452,9 +472,10 @@ static unsigned char* process_gif_stream(unsigned char *buffer, unsigned short*
default: default:
fprintf( stderr, "Bailing on unrecognized block type %.02x\n", fprintf( stderr, "Bailing on unrecognized block type %.02x\n",
block_type ); 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); return process_gif_stream(buffer, w, h);
} }
int main( int argc, char *argv[] ) /*int main( int argc, char *argv[] )
{ {
FILE* gif_file; FILE* gif_file;
@@ -483,4 +504,4 @@ int main( int argc, char *argv[] )
process_gif_stream( gif_file ); process_gif_stream( gif_file );
fclose( gif_file ); fclose( gif_file );
} }*/

View File

@@ -1,8 +1,7 @@
#include "jdraw8.h" #include "jdraw8.h"
#include "SDL_image.h"
//#include "SDL_opengl.h"
#include "jfile.h" #include "jfile.h"
#include <fstream> #include <fstream>
#include "gif.c"
JD8_Surface screen = NULL; JD8_Surface screen = NULL;
JD8_Palette main_palette = 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 ); sdlWindow = SDL_CreateWindow( title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN );
sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, 0); sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, 0);
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "nearest"); 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); 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; int filesize = 0;
char *buffer = JF_GetBufferFromResource(file, filesize); char *buffer = JF_GetBufferFromResource(file, filesize);
SDL_RWops *rw = SDL_RWFromMem(buffer, filesize); unsigned short w, h;
SDL_Surface *temp = IMG_Load_RW(rw, 1); Uint8* pixels = LoadGif((unsigned char*)buffer, &w, &h);
free(buffer); free(buffer);
if (temp == NULL) { if (pixels == NULL) {
printf("Unable to load bitmap: %s\n", SDL_GetError()); printf("Unable to load bitmap: %s\n", SDL_GetError());
exit(1); exit(1);
} }
JD8_Surface image = JD8_NewSurface(); JD8_Surface image = JD8_NewSurface();
memcpy( image, temp->pixels, 64000 ); memcpy(image, pixels, 64000);
SDL_FreeSurface(temp); free(pixels);
return image; return image;
} }
@@ -76,25 +74,7 @@ JD8_Palette JD8_LoadPalette(const char *file) {
char *buffer = NULL; char *buffer = NULL;
buffer = JF_GetBufferFromResource(file, filesize); buffer = JF_GetBufferFromResource(file, filesize);
SDL_RWops *rw = NULL; JD8_Palette palette = (JD8_Palette)LoadPalette((unsigned char*)buffer);
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);
return palette; 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() { void JD8_Flip() {
for( int x = 0; x < 320; x++ ) { for( int x = 0; x < 320; x++ ) {
for( int y = 0; y < 200; y++ ) { for( int y = 0; y < 200; y++ ) {
@@ -160,7 +142,7 @@ void JD8_Flip() {
} }
} }
SDL_UpdateTexture(sdlTexture, NULL, pixel_data, 320 * sizeof(Uint32)); SDL_UpdateTexture(sdlTexture, NULL, pixel_data, 320 * sizeof(Uint32));
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL); SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, &rect);
SDL_RenderPresent(sdlRenderer); SDL_RenderPresent(sdlRenderer);
} }