typedef struct {
unsigned width;
unsigned height;
+ unsigned row_tiles;
+ unsigned col_tiles;
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)
+int set_info(swan_gfx_t * gfx, SDL_Surface * img)
{
gfx->width = img->w;
gfx->height = img->h;
gfx->bpp = img->format->BitsPerPixel;
-
- /* set userdata to iterate */
+
+ if (gfx->bpp != 8) {
+ fprintf(stderr, "warning: %d bpp format detected\n", img->format->BitsPerPixel);
+ return -1;
+ }
+
+ if (gfx->width % 8 | gfx->height % 8) {
+ fprintf(stderr, "warning: image not multiple of 16x16 tiles\n \
+ width: %d\theight: %d\n", gfx->width, gfx->height);
+ return -1;
+ }
+
+ gfx->row_tiles = gfx->height / 8;
+ gfx->col_tiles = gfx->width / 8;
+
+ /* set userdata to iterate over
+ * pixels */
SDL_LockSurface(img);
img->userdata = img->pixels;
gfx->data = (unsigned char *) img->userdata;
gfx->palette = img->format->palette->colors;
SDL_UnlockSurface(img);
-
+
+ return 0;
}
void generate_4bpp_tile(unsigned char *buf, swan_gfx_t * gfx,
}
swan_gfx_t * gfx = malloc(sizeof(swan_gfx_t));
- set_info(gfx, rawbmp);
+ int ret = set_info(gfx, rawbmp);
+
+ if (ret != 0)
+ goto cleanup;
unsigned char *tile_buf = malloc(sizeof(unsigned char) * TILE_SZ);
- 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);
- }
-
if (!bout)
gfxstream = fopen("out.gfx", "w");
else {
if (!gfxstream) {
fprintf(stderr, "can not open file for writing!\n");
- exit(EXIT_FAILURE);
+ goto close;
}
unsigned nr_tiles = 1;
palstream = fopen("out.pal", "w");
if (!palstream) {
fprintf(stderr, "can not open file for writing!\n");
- exit(EXIT_FAILURE);
+ fclose(palstream);
+ goto close;
}
fwrite(gfx->outpal, sizeof(unsigned short), 8, palstream);
for (int i = 0; i < 8; ++i)
fwrite(&fill, sizeof(unsigned short), 1, palstream);
+ fclose(palstream);
+
+close:
+ fclose(gfxstream);
cleanup:
free(gfx);
SDL_FreeSurface(rawbmp);
- fclose(gfxstream);
- fclose(palstream);
}