3 * Author : Robin Krens <robin@robinkrens.nl>
5 * Last Modified Date: 12.06.2022
6 * Last Modified By : Robin Krens <robin@robinkrens.nl>
16 #include <SDL2/SDL_image.h>
28 void set_info(swan_gfx_t * gfx, SDL_Surface * img)
32 gfx->bpp = img->format->BitsPerPixel;
34 /* set userdata to iterate */
36 img->userdata = img->pixels;
37 SDL_UnlockSurface(img);
40 void generate_4bpp_tile(unsigned char *buf, void * userdata,
43 unsigned char * ptr = (unsigned char *) userdata;
44 for (int i = 0; i < TILE_SZ; i+=4) {
45 for (int j = 0; j < 4; ++j) {
46 if (packed) { /* packed format */
47 buf[i+j] = ptr[0] << 4;
50 } else { /* planar format */
51 for (int x = 0; x < 8; ++x) {
52 buf[i + j] = ptr[x] << j;
61 int main(int argc, char *argv[])
70 while ((opt = getopt(argc, argv, "po:sv:")) != -1) {
73 printf("include palette");
76 printf("output file %s\n", optarg);
77 strcpy(outfile, optarg);
81 printf("generate gfx per 16x16 tile \n");
86 fprintf(stderr, "Usage: %s [-s] [-o output] file \n", argv[0]);
92 fprintf(stderr, "Expected infile\n");
93 fprintf(stderr, "Usage: %s [-s] [-o output] [file]\n", argv[0]);
97 infile = argv[optind];
99 rawbmp = SDL_LoadBMP(infile);
101 fprintf(stderr, "can not load %s file\n", infile);
105 swan_gfx_t * gfx = malloc(sizeof(swan_gfx_t));
106 set_info(gfx, rawbmp);
108 unsigned char *tile_buf = malloc(sizeof(unsigned char) * TILE_SZ);
111 fprintf(stderr, "warning: %d bpp format detected\n", rawbmp->format->BitsPerPixel);
112 fprintf(stderr, "use --force in command to run\n");
117 ostream = fopen("out.gfx", "w");
119 printf("opening %s\n", outfile);
120 ostream = fopen(outfile, "w");
124 fprintf(stderr, "can not open file for writing!\n");
128 unsigned nr_tiles = 1;
130 for (int i = 0; i < nr_tiles; ++i) {
131 generate_4bpp_tile(tile_buf, rawbmp->userdata, true);
132 int ret = fwrite(tile_buf, sizeof(unsigned char), TILE_SZ, ostream);
133 if (ret != TILE_SZ) {
134 fprintf(stderr, "failed to convert, only write %d instead of %d\n", ret, TILE_SZ);
141 SDL_FreeSurface(rawbmp);