Compare commits

..

4 Commits

5 changed files with 83 additions and 23 deletions

View File

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

7
gif.c
View File

@@ -12,7 +12,9 @@
#define COMMENT_EXTENSION 0xFE #define COMMENT_EXTENSION 0xFE
#define PLAINTEXT_EXTENSION 0x01 #define PLAINTEXT_EXTENSION 0x01
#define READ(dst, size) memcpy(dst, buffer, size); buffer += size unsigned char* bbb;
#define READ(dst, size) memcpy(dst, bbb, size); bbb += size
typedef struct typedef struct
{ {
@@ -379,6 +381,8 @@ static int process_extension( unsigned char* buffer )
*/ */
unsigned char* LoadPalette(unsigned char *buffer) { unsigned char* LoadPalette(unsigned char *buffer) {
bbb = buffer;
unsigned char header[7]; unsigned char header[7];
screen_descriptor_t screen_descriptor; screen_descriptor_t screen_descriptor;
int color_resolution_bits; int color_resolution_bits;
@@ -483,6 +487,7 @@ static unsigned char* process_gif_stream(unsigned char *buffer, unsigned short*
unsigned char* LoadGif(unsigned char *buffer, unsigned short* w, unsigned short* h) { unsigned char* LoadGif(unsigned char *buffer, unsigned short* w, unsigned short* h) {
bbb = buffer;
return process_gif_stream(buffer, w, h); return process_gif_stream(buffer, w, h);
} }

View File

@@ -165,6 +165,13 @@ void UpdateMiniMap() {
free(pixels); 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() { void Init() {
LoadConfig(); LoadConfig();
@@ -188,10 +195,40 @@ void Init() {
int c; int c;
FILE* f = fopen(GetPath(tiles_filename), "rb"); FILE* f = fopen(GetPath(tiles_filename), "rb");
if (!f) { error = 2; return; } if (!f) { error = 2; return; }
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); 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); sdlTilesTexture = SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STATIC, tilemap_width, tilemap_height);
SDL_UpdateTexture(sdlTilesTexture, NULL, buffer, tilemap_width * sizeof(Uint32)); SDL_UpdateTexture(sdlTilesTexture, NULL, buffer, tilemap_width * sizeof(Uint32));
SDL_SetTextureBlendMode(sdlTilesTexture, SDL_BLENDMODE_BLEND); SDL_SetTextureBlendMode(sdlTilesTexture, SDL_BLENDMODE_BLEND);
}
fclose(f); fclose(f);
const int tmtw = (tilemap_width / tile_width); const int tmtw = (tilemap_width / tile_width);
@@ -216,7 +253,11 @@ void Init() {
minitiles[tmx + tmy*tmtw] = 0xFF000000 + b + (g << 8) + (r << 16); minitiles[tmx + tmy*tmtw] = 0xFF000000 + b + (g << 8) + (r << 16);
} }
} }
if (IsGIF(tiles_filename)) {
free(buffer);
} else {
stbi_image_free(buffer); stbi_image_free(buffer);
}
f = fopen(GetPath(map_filename), "rb"); f = fopen(GetPath(map_filename), "rb");
map = (unsigned char*)calloc(map_width*map_height*2, 1); map = (unsigned char*)calloc(map_width*map_height*2, 1);
@@ -303,7 +344,7 @@ void Print(int x, int y, const char* text) {
} }
} }
bool Button(int x, int y, char* text) { bool Button(int x, int y, const char* text) {
bool inside = (mouse.x >= x) && (mouse.y >= y) && (mouse.x < x + 55) && (mouse.y < y + 12); bool inside = (mouse.x >= x) && (mouse.y >= y) && (mouse.x < x + 55) && (mouse.y < y + 12);
if (inside) { if (inside) {
FillRect(x, y, 55, 13, 128, 64, 64, 255); FillRect(x, y, 55, 13, 128, 64, 64, 255);
@@ -483,9 +524,22 @@ void DoTileMap() {
if (keyJustPressed == SDL_SCANCODE_F) { Undo_Op(); FloodFill(); } if (keyJustPressed == SDL_SCANCODE_F) { Undo_Op(); FloodFill(); }
if (keyJustPressed == SDL_SCANCODE_R) { Undo_Op(); ReplaceTile(); } if (keyJustPressed == SDL_SCANCODE_R) { Undo_Op(); ReplaceTile(); }
static int scroll_x = 0;
static int scroll_y = 0;
static int old_map_x = 0;
static int old_map_y = 0;
if (mouse.mouse_down[MOUSE_BUTTON_LEFT] || mouse.mouse_down[MOUSE_BUTTON_RIGHT]) { Undo_Op(); } if (mouse.mouse_down[MOUSE_BUTTON_LEFT] || mouse.mouse_down[MOUSE_BUTTON_RIGHT]) { Undo_Op(); }
if (mouse.mouse_down[MOUSE_BUTTON_LEFT]) {
old_map_x = map_x; old_map_y = map_y;
scroll_x = cur_x; scroll_y = cur_y;
}
if (mouse.buttons[MOUSE_BUTTON_RIGHT]) { if (tile_back < sprites) map[(map_x + cur_x) + (map_y + cur_y)*map_width] = tile_back; } if (mouse.buttons[MOUSE_BUTTON_RIGHT]) { if (tile_back < sprites) map[(map_x + cur_x) + (map_y + cur_y)*map_width] = tile_back; }
if (mouse.buttons[MOUSE_BUTTON_LEFT]) { if (mouse.buttons[MOUSE_BUTTON_LEFT]) {
if (keyboard[SDL_SCANCODE_SPACE]) {
map_x = old_map_x - (cur_x - scroll_x);
map_y = old_map_y - (cur_y - scroll_y);
} else {
const int pos = (map_x + cur_x) + (map_y + cur_y)*map_width; const int pos = (map_x + cur_x) + (map_y + cur_y)*map_width;
if (tile_sel >= sprites) { if (tile_sel >= sprites) {
ignoreMouse = true; ignoreMouse = true;
@@ -498,6 +552,7 @@ void DoTileMap() {
map[pos] = tile_sel; map[pos] = tile_sel;
} }
} }
}
if (mouse.buttons[MOUSE_BUTTON_MIDDLE]) { tile_sel = map[(map_x + cur_x) + (map_y + cur_y)*map_width]; } if (mouse.buttons[MOUSE_BUTTON_MIDDLE]) { tile_sel = map[(map_x + cur_x) + (map_y + cur_y)*map_width]; }
if (keyJustPressed == SDL_SCANCODE_Z && keyboard[CTRL]) { Undo_Undo(); } if (keyJustPressed == SDL_SCANCODE_Z && keyboard[CTRL]) { Undo_Undo(); }

BIN
map.map

Binary file not shown.

BIN
test.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.3 KiB