From ac4433a899dbc7292e47b839cc17c0d502097fde Mon Sep 17 00:00:00 2001 From: Robin Krens Date: Sun, 12 Jun 2022 16:36:17 +0200 Subject: [PATCH] tools: bmp2tiles swan_gfx_t struct + function call changes --- tools/bmp2tiles/bmp2tiles.c | 86 +++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 38 deletions(-) diff --git a/tools/bmp2tiles/bmp2tiles.c b/tools/bmp2tiles/bmp2tiles.c index 25ea7d5..472442a 100644 --- a/tools/bmp2tiles/bmp2tiles.c +++ b/tools/bmp2tiles/bmp2tiles.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -16,46 +17,48 @@ #define FOURBBP_ROW 8 #define FOURBPP_COL 8 -void bmp_info(SDL_Surface * img) -{ - SDL_PixelFormat * fmt; - fmt = img->format; - fprintf(stdout,"WIDTH: %d, HEIGHT: %d, BPP: %d\n", img->w, img->h, fmt->BitsPerPixel); -} +typedef struct { + unsigned width; + unsigned height; + unsigned bpp; +} swan_gfx_t; - -void generate_4bpp_tile_planar(unsigned char *buf, void * userdata, int sz) +void set_info(swan_gfx_t * gfx, SDL_Surface * img) { - unsigned char * ptr = (unsigned char *) userdata; - for (int i = 0; i < 32; i+=4) { - /* fprintf(stdout, "userdata: pos: %d - %d\n", i, *ptr++); */ - for (int j = 0; j < 4; ++j) { - for (int x = 0; x < 8; ++x) { - buf[i + j] = ptr[x] << j; - } - /* printf("%d: %x\n", i+j, buf[i+j]); */ - } - ptr += 8; - } + gfx->width = img->w; + gfx->height = img->h; + gfx->bpp = img->format->BitsPerPixel; + + /* set userdata to iterate */ + SDL_LockSurface(img); + img->userdata = img->pixels; + SDL_UnlockSurface(img); } -void generate_4bpp_tile_packed(unsigned char *buf, void * userdata, int sz) +void generate_4bpp_tile(unsigned char *buf, void * userdata, + bool packed) { unsigned char * ptr = (unsigned char *) userdata; - for (int i = 0; i < 32; i+=4) { + for (int i = 0; i < TILE_SZ; i+=4) { for (int j = 0; j < 4; ++j) { - buf[i+j] = ptr[0] << 4; - buf[i+j] |= ptr[1]; - ptr += 2; - printf("%d: %x\n", i+j, buf[i+j]); + if (packed) { /* packed format */ + buf[i+j] = ptr[0] << 4; + buf[i+j] |= ptr[1]; + ptr += 2; + } else { /* planar format */ + for (int x = 0; x < 8; ++x) { + buf[i + j] = ptr[x] << j; + } + } } + if (!packed) + ptr += 8; } } int main(void) { SDL_Surface * rawbmp; - SDL_PixelFormat * fmt; FILE * ostream; char filename[] = "test.bmp"; char outname[] = "test.gfx"; @@ -66,18 +69,16 @@ int main(void) exit(EXIT_FAILURE); } - bmp_info(rawbmp); - - SDL_LockSurface(rawbmp); - rawbmp->userdata = rawbmp->pixels; - SDL_UnlockSurface(rawbmp); + swan_gfx_t * gfx = malloc(sizeof(swan_gfx_t)); + set_info(gfx, rawbmp); unsigned char *tile_buf = malloc(sizeof(unsigned char) * TILE_SZ); - /* if (rawbmp->format->BitsPerPixel != 24) { - fprintf(stderr, "format %d not supported\n", rawbmp->format->BitsPerPixel); + if (gfx->bpp != 8) { + fprintf(stderr, "warning: %d bpp format detected\n", rawbmp->format->BitsPerPixel); + fprintf(stderr, "use --force in command to run\n"); exit(EXIT_FAILURE); - } */ + } ostream = fopen(outname, "w"); if (!ostream) { @@ -85,10 +86,19 @@ int main(void) exit(EXIT_FAILURE); } - generate_4bpp_tile_packed(tile_buf, rawbmp->userdata, 64); - int ret = fwrite(tile_buf, sizeof(unsigned char), 32, ostream); - printf("written: %d\n", ret); + unsigned nr_tiles = 1; - SDL_FreeSurface(rawbmp); + for (int i = 0; i < nr_tiles; ++i) { + generate_4bpp_tile(tile_buf, rawbmp->userdata, true); + int ret = fwrite(tile_buf, sizeof(unsigned char), TILE_SZ, ostream); + if (ret != TILE_SZ) { + fprintf(stderr, "failed to convert, only write %d instead of %d\n", ret, TILE_SZ); + goto cleanup; + } + } +cleanup: + free(gfx); + SDL_FreeSurface(rawbmp); + fclose(ostream); } -- 2.7.4