fixed errors in gif loading

This commit is contained in:
2021-05-12 19:59:44 +02:00
parent 7f60bf88f2
commit bd2905ffa2
5 changed files with 54 additions and 11 deletions

View File

@@ -1,9 +1,9 @@
tile_width = 16
tile_height = 16
map_width = 240
map_height = 238
tile_width = 8
tile_height = 8
map_width = 100
map_height = 100
screen_width = 320
screen_height = 224
screen_height = 240
zoom = 3
tiles = tiles.png
tiles = test.gif
map = map.map

2
gif.c
View File

@@ -381,6 +381,8 @@ static int process_extension( unsigned char* buffer )
*/
unsigned char* LoadPalette(unsigned char *buffer) {
bbb = buffer;
unsigned char header[7];
screen_descriptor_t screen_descriptor;
int color_resolution_bits;

View File

@@ -165,6 +165,13 @@ void UpdateMiniMap() {
free(pixels);
}
const bool IsGIF(const char* filename) {
const int filename_size = strlen(filename);
if ((filename[filename_size-3]=='g') && (filename[filename_size-2]=='i') && (filename[filename_size-1]=='f') ||
(filename[filename_size-3]=='G') && (filename[filename_size-2]=='I') && (filename[filename_size-1]=='F')) return true;
return false;
}
void Init() {
LoadConfig();
@@ -188,10 +195,40 @@ void Init() {
int c;
FILE* f = fopen(GetPath(tiles_filename), "rb");
if (!f) { error = 2; return; }
buffer = stbi_load_from_file(f, &tilemap_width, &tilemap_height, &c, 4);
sdlTilesTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STATIC, tilemap_width, tilemap_height);
SDL_UpdateTexture(sdlTilesTexture, NULL, buffer, tilemap_width * sizeof(Uint32));
SDL_SetTextureBlendMode(sdlTilesTexture, SDL_BLENDMODE_BLEND);
if (IsGIF(tiles_filename)) {
fseek(f, 0, SEEK_END);
const long fsize = ftell(f);
fseek(f, 0, SEEK_SET); /* same as rewind(f); */
char *string = (char*)malloc(fsize + 1);
fread(string, 1, fsize, f);
string[fsize] = 0;
unsigned short w, h;
buffer = LoadGif((unsigned char*)string, &w, &h);
tilemap_width = w; tilemap_height = h;
Uint8* palette = LoadPalette((unsigned char*)string);
Uint32 pal[256];
for (int i=0;i<256;++i) {
pal[i] = 0xFF000000 + (palette[(i*3)]) + (palette[(i*3)+1] << 8) + (palette[(i*3)+2] << 16);
}
free(palette);
free(string);
if (buffer == NULL) { error = 2; return; }
sdlTilesTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STATIC, tilemap_width, tilemap_height);
Uint32* pixels = (Uint32*)malloc(w*h*sizeof(Uint32));
for (int i = 0; i < w*h; i++) { pixels[i] = pal[buffer[i]]; }
SDL_UpdateTexture(sdlTilesTexture, NULL, pixels, w * sizeof(Uint32));
SDL_SetTextureBlendMode(sdlTilesTexture, SDL_BLENDMODE_BLEND);
free(buffer);
buffer = (Uint8*)malloc(w*h*sizeof(Uint32));
memcpy(buffer, pixels, w*h*sizeof(Uint32));
free(pixels);
} else {
buffer = stbi_load_from_file(f, &tilemap_width, &tilemap_height, &c, 4);
sdlTilesTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STATIC, tilemap_width, tilemap_height);
SDL_UpdateTexture(sdlTilesTexture, NULL, buffer, tilemap_width * sizeof(Uint32));
SDL_SetTextureBlendMode(sdlTilesTexture, SDL_BLENDMODE_BLEND);
}
fclose(f);
const int tmtw = (tilemap_width / tile_width);
@@ -216,7 +253,11 @@ void Init() {
minitiles[tmx + tmy*tmtw] = 0xFF000000 + b + (g << 8) + (r << 16);
}
}
stbi_image_free(buffer);
if (IsGIF(tiles_filename)) {
free(buffer);
} else {
stbi_image_free(buffer);
}
f = fopen(GetPath(map_filename), "rb");
map = (unsigned char*)calloc(map_width*map_height*2, 1);

BIN
map.map

Binary file not shown.

BIN
test.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB