diff --git a/config.ini b/config.ini index 0a20dcf..dd4cc56 100644 --- a/config.ini +++ b/config.ini @@ -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 \ No newline at end of file diff --git a/gif.c b/gif.c index 042456e..578fd1a 100644 --- a/gif.c +++ b/gif.c @@ -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; diff --git a/main.cpp b/main.cpp index e0d5413..09c23ed 100644 --- a/main.cpp +++ b/main.cpp @@ -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); diff --git a/map.map b/map.map index 00d6b6b..5b5dbe4 100644 Binary files a/map.map and b/map.map differ diff --git a/test.gif b/test.gif new file mode 100644 index 0000000..a7b5048 Binary files /dev/null and b/test.gif differ