From e5de2e9ca1eb79d90e6bc662e49a6c23cbf62f40 Mon Sep 17 00:00:00 2001 From: Robin Krens Date: Sun, 12 Jun 2022 15:41:35 +0200 Subject: [PATCH] tools: bmp2tiles init --- tools/Makefile | 5 --- tools/bmp2tiles/Makefile | 5 +++ tools/bmp2tiles/bmp2tiles.c | 85 +++++++++++++++++++++++++++++++++++++++++++++ tools/romheader.c | 83 ------------------------------------------- 4 files changed, 90 insertions(+), 88 deletions(-) delete mode 100644 tools/Makefile create mode 100644 tools/bmp2tiles/Makefile create mode 100644 tools/bmp2tiles/bmp2tiles.c delete mode 100644 tools/romheader.c diff --git a/tools/Makefile b/tools/Makefile deleted file mode 100644 index 2d1a03a..0000000 --- a/tools/Makefile +++ /dev/null @@ -1,5 +0,0 @@ -all: - cc -O2 romheader.c -o romheader - -clean: - rm romheader diff --git a/tools/bmp2tiles/Makefile b/tools/bmp2tiles/Makefile new file mode 100644 index 0000000..1f710ba --- /dev/null +++ b/tools/bmp2tiles/Makefile @@ -0,0 +1,5 @@ +all: + cc -O2 bmp2tiles.c -o bmp2tiles -lSDL2 + +clean: + rm bmp2tiles diff --git a/tools/bmp2tiles/bmp2tiles.c b/tools/bmp2tiles/bmp2tiles.c new file mode 100644 index 0000000..42a8805 --- /dev/null +++ b/tools/bmp2tiles/bmp2tiles.c @@ -0,0 +1,85 @@ +/** + * File : bmp2tiles.c + * Author : Robin Krens + * Date : 04.06.2022 + * Last Modified Date: 12.06.2022 + * Last Modified By : Robin Krens + */ + +#include +#include +#include +#include +#include + +#define TILE_SZ 32 +#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); +} + + +void generate_4bpp_tile_planar(unsigned char *buf, void * userdata, int sz) +{ + 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; + } +} + +void generate_4bpp_tile_packed(unsigned char *buf, void * userdata, int sz) +{ + unsigned char * ptr = (unsigned char *) userdata; + for (int i = 0; i < 32; 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]); + } + } +} + +int main(void) +{ + SDL_Surface * rawbmp; + SDL_PixelFormat * fmt; + char filename[] = "test.bmp"; + rawbmp = SDL_LoadBMP(filename); + + if (!rawbmp) { + fprintf(stderr, "can not load .bmp file\n"); + exit(EXIT_FAILURE); + } + + bmp_info(rawbmp); + + SDL_LockSurface(rawbmp); + rawbmp->userdata = rawbmp->pixels; + SDL_UnlockSurface(rawbmp); + + unsigned char *tile_buf = malloc(sizeof(unsigned char) * TILE_SZ); + + generate_4bpp_tile_packed(tile_buf, rawbmp->userdata, 64); + + /* if (rawbmp->format->BitsPerPixel != 24) { + fprintf(stderr, "format %d not supported\n", rawbmp->format->BitsPerPixel); + exit(EXIT_FAILURE); + } */ + + + SDL_FreeSurface(rawbmp); + +} diff --git a/tools/romheader.c b/tools/romheader.c deleted file mode 100644 index 0b23559..0000000 --- a/tools/romheader.c +++ /dev/null @@ -1,83 +0,0 @@ -/** - * File : romheader.c - * Author : Robin Krens - * Date : 04.06.2022 - * Last Modified Date: 04.06.2022 - * Last Modified By : Robin Krens - */ - -#include -#include -#include -#include - -#define VERBOSE 1 -#define SEGMENT_SIZE 0x10000 - -/* simple tool to pad 0xFF and to add ROM header - * romheader structure is as follows: */ - -struct __attribute__((__packed__)) rom_header { - uint8_t jmpf; - uint16_t label; - uint16_t segment; - uint8_t padding; - uint8_t dev_id; - uint8_t ws_type; - uint8_t cart_nr; - uint8_t padding2; - uint8_t romsize; - uint8_t ramsize; - uint8_t wsspec; - uint8_t padding3; - uint16_t checksum; -}; - -static struct rom_header header = { - .jmpf = 0xEA, - .label = 0x0, - .segment = 0xF000, - .dev_id = 0x42, - .ws_type = 0x01, - .cart_nr = 0x01, - .romsize = 0x03, - .ramsize = 0x00, - .wsspec = 0x04, - .checksum = 0x0000 -}; - - -int main(void) -{ - FILE *fp; - /* open for appending, so seek postion is at end */ - fp = fopen("test.wsc", "a"); - if (fp == NULL) { - perror("can not open file"); - exit(EXIT_FAILURE); - } - - unsigned pos = ftell(fp); - int gap = (SEGMENT_SIZE - pos) - sizeof(struct rom_header); - if (gap <= 0) - goto cleanup; - - char dummy[SEGMENT_SIZE + 1]; - memset(dummy, 0xFF, SEGMENT_SIZE); - - unsigned wc = fwrite(dummy, sizeof(char), gap, fp); - wc += fwrite(&header, sizeof(char), sizeof(struct rom_header), fp); - - if (VERBOSE) { - printf("size of struct: %ld\n", sizeof(struct rom_header)); - printf("pos: %d, gap to fill: %d\n", pos, gap); - printf("written %d\n", wc); - } - -cleanup: - - printf("done\n"); - fflush(fp); - fclose(fp); -} - -- 2.7.4