From 6c2f3fe28998776174e987c60d1496de525d83aa Mon Sep 17 00:00:00 2001 From: Robin Krens Date: Sun, 12 Jun 2022 19:30:38 +0200 Subject: [PATCH] tools: bmp2tiles create pallete data --- src/main.c | 14 ++++++++++---- tools/bmp2tiles/bmp2tiles.c | 40 +++++++++++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/src/main.c b/src/main.c index 5a18283..2d9f037 100644 --- a/src/main.c +++ b/src/main.c @@ -21,10 +21,16 @@ unsigned char bgtile_gfx[] = { }; /* palette data */ +//unsigned char bgtile_pal[] = { +// 0x65, 0x06, 0x67, 0x06, 0x79, 0x07, 0x8c, 0x08, 0xff, 0x0f, 0xff, 0x0f, +// 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, +// 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f +//}; + unsigned char bgtile_pal[] = { - 0x65, 0x06, 0x67, 0x06, 0x79, 0x07, 0x8c, 0x08, 0xff, 0x0f, 0xff, 0x0f, - 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, - 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f, 0xff, 0x0f + 0x00, 0x00, 0x67, 0x06, 0x79, 0x07, 0x8c, 0x08, 0x00, 0x00, 0x12, 0x0b, + 0x00, 0x00, 0x10, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; @@ -82,7 +88,7 @@ void setup_ivec(void) *keyboard_intr++ = (&dummyS_isr); *keyboard_intr = (0x2000); - outport(IO_INT_ENABLE, INT_VBLANK_START); + //outport(IO_INT_ENABLE, INT_VBLANK_START); //outport(IO_INT_ENABLE, INT_KEY_PRESS); __asm__("sti"); } diff --git a/tools/bmp2tiles/bmp2tiles.c b/tools/bmp2tiles/bmp2tiles.c index 40db317..96aaf2e 100644 --- a/tools/bmp2tiles/bmp2tiles.c +++ b/tools/bmp2tiles/bmp2tiles.c @@ -23,6 +23,9 @@ typedef struct { unsigned width; unsigned height; unsigned bpp; + unsigned char * data; + SDL_Colour * palette; + unsigned short outpal[8]; } swan_gfx_t; void set_info(swan_gfx_t * gfx, SDL_Surface * img) @@ -34,16 +37,22 @@ void set_info(swan_gfx_t * gfx, SDL_Surface * img) /* set userdata to iterate */ SDL_LockSurface(img); img->userdata = img->pixels; + gfx->data = (unsigned char *) img->userdata; + gfx->palette = img->format->palette->colors; SDL_UnlockSurface(img); + } -void generate_4bpp_tile(unsigned char *buf, void * userdata, +void generate_4bpp_tile(unsigned char *buf, swan_gfx_t * gfx, bool packed) { - unsigned char * ptr = (unsigned char *) userdata; + unsigned char * ptr = gfx->data; for (int i = 0; i < TILE_SZ; i+=4) { for (int j = 0; j < 4; ++j) { if (packed) { /* packed format */ + gfx->outpal[ptr[0]] = (gfx->palette[ptr[0]].r >> 4) << 8 | + (gfx->palette[ptr[0]].g >> 4) << 4 | + gfx->palette[ptr[0]].b >> 4; buf[i+j] = ptr[0] << 4; buf[i+j] |= ptr[1]; ptr += 2; @@ -61,7 +70,8 @@ void generate_4bpp_tile(unsigned char *buf, void * userdata, int main(int argc, char *argv[]) { SDL_Surface * rawbmp; - FILE * ostream; + FILE * gfxstream; + FILE * palstream; int opt; char * infile; char outfile[256]; @@ -114,13 +124,13 @@ int main(int argc, char *argv[]) } if (!bout) - ostream = fopen("out.gfx", "w"); + gfxstream = fopen("out.gfx", "w"); else { printf("opening %s\n", outfile); - ostream = fopen(outfile, "w"); + gfxstream = fopen(outfile, "w"); } - if (!ostream) { + if (!gfxstream) { fprintf(stderr, "can not open file for writing!\n"); exit(EXIT_FAILURE); } @@ -128,16 +138,28 @@ int main(int argc, char *argv[]) unsigned nr_tiles = 1; 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); + generate_4bpp_tile(tile_buf, gfx, true); + int ret = fwrite(tile_buf, sizeof(unsigned char), TILE_SZ, gfxstream); if (ret != TILE_SZ) { fprintf(stderr, "failed to convert, only write %d instead of %d\n", ret, TILE_SZ); goto cleanup; } } + + palstream = fopen("out.pal", "w"); + if (!palstream) { + fprintf(stderr, "can not open file for writing!\n"); + exit(EXIT_FAILURE); + } + + fwrite(gfx->outpal, sizeof(unsigned short), 8, palstream); + const short fill = 0xFFFF; + for (int i = 0; i < 8; ++i) + fwrite(&fill, sizeof(unsigned short), 1, palstream); cleanup: free(gfx); SDL_FreeSurface(rawbmp); - fclose(ostream); + fclose(gfxstream); + fclose(palstream); } -- 2.7.4