2016-08-25 01:56:07 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2017-05-29 03:22:10 +00:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <getopt.h>
|
2017-05-29 03:49:12 +00:00
|
|
|
#include <stdint.h>
|
2016-08-25 01:56:07 +00:00
|
|
|
|
|
|
|
static void usage(void) {
|
|
|
|
printf("Usage: pokemon_animation_graphics [-o outfile] [-t mapfile] 2bpp_file dimensions_file\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2017-05-29 03:22:10 +00:00
|
|
|
struct Options {
|
|
|
|
int girafarig;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Options Options = {0};
|
|
|
|
|
|
|
|
|
2016-08-25 01:56:07 +00:00
|
|
|
struct Tilemap {
|
|
|
|
uint8_t* data;
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Graphic {
|
|
|
|
uint8_t* data;
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
|
|
|
void transpose_tiles(uint8_t* tiles, int width, int size, int tile_size) {
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
uint8_t* new_tiles;
|
|
|
|
new_tiles = malloc(size);
|
|
|
|
for (i = 0; i < size; i++) {
|
|
|
|
j = i / tile_size * width * tile_size;
|
|
|
|
j = j % size + tile_size * (j / size) + i % tile_size;
|
|
|
|
new_tiles[j] = tiles[i];
|
|
|
|
}
|
|
|
|
memcpy(tiles, new_tiles, size);
|
|
|
|
free(new_tiles);
|
|
|
|
}
|
|
|
|
|
2017-05-29 03:22:10 +00:00
|
|
|
bool compare_tile(uint8_t *tile, uint8_t *other) {
|
2017-05-29 03:52:23 +00:00
|
|
|
int j;
|
|
|
|
for (j = 0; j < 16; j++) {
|
2017-05-29 03:22:10 +00:00
|
|
|
if (tile[j] != other[j]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_tile_index(uint8_t* tile, uint8_t* tiles, int num_tiles, int preferred_tile_id) {
|
|
|
|
if (preferred_tile_id >= 0 && preferred_tile_id < num_tiles) {
|
|
|
|
uint8_t *other = &tiles[preferred_tile_id * 16];
|
|
|
|
if (compare_tile(tile, other)) {
|
|
|
|
return preferred_tile_id;
|
2016-08-25 01:56:07 +00:00
|
|
|
}
|
2017-05-29 03:22:10 +00:00
|
|
|
}
|
2017-05-29 03:52:23 +00:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < num_tiles; i++) {
|
2017-05-29 03:22:10 +00:00
|
|
|
uint8_t *other = &tiles[i * 16];
|
|
|
|
if (compare_tile(tile, other)) {
|
2016-08-25 01:56:07 +00:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-05-28 05:11:32 +00:00
|
|
|
FILE *fopen_verbose(char *filename, char *mode) {
|
|
|
|
FILE *f = fopen(filename, mode);
|
|
|
|
if (!f) {
|
|
|
|
fprintf(stderr, "Could not open file: \"%s\"\n", filename);
|
|
|
|
}
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2016-08-25 01:56:07 +00:00
|
|
|
void create_tilemap(struct Tilemap* tilemap, struct Graphic* graphic, char* graphics_filename, int width, int height) {
|
|
|
|
long graphics_size;
|
|
|
|
uint8_t* graphics;
|
|
|
|
FILE* f;
|
|
|
|
int i;
|
2017-05-28 05:11:32 +00:00
|
|
|
int tile;
|
2016-08-25 01:56:07 +00:00
|
|
|
|
2017-05-28 05:11:32 +00:00
|
|
|
f = fopen_verbose(graphics_filename, "rb");
|
|
|
|
if (!f) {
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-08-25 01:56:07 +00:00
|
|
|
fseek(f, 0, SEEK_END);
|
|
|
|
graphics_size = ftell(f);
|
|
|
|
rewind(f);
|
|
|
|
graphics = malloc(graphics_size);
|
|
|
|
fread(graphics, 1, graphics_size, f);
|
|
|
|
fclose(f);
|
|
|
|
|
2017-05-28 05:11:32 +00:00
|
|
|
int num_tiles_per_frame = width * height;
|
|
|
|
int tile_size = 16;
|
|
|
|
int num_frames = graphics_size / (tile_size * num_tiles_per_frame);
|
|
|
|
int frame_size = num_tiles_per_frame * tile_size;
|
|
|
|
|
2016-08-25 01:56:07 +00:00
|
|
|
// transpose each frame
|
2017-05-28 05:11:32 +00:00
|
|
|
for (i = 0; i < num_frames; i++) {
|
|
|
|
transpose_tiles(graphics + i * frame_size, width, frame_size, tile_size);
|
2016-08-25 01:56:07 +00:00
|
|
|
}
|
|
|
|
|
2017-05-28 05:11:32 +00:00
|
|
|
// first frame is naively populated with redundant tiles,
|
|
|
|
// so fill it unconditionally and start from the second frame
|
|
|
|
int num_tiles = width * height;
|
|
|
|
int tilemap_size = graphics_size / tile_size;
|
2017-05-29 03:22:10 +00:00
|
|
|
tilemap->data = malloc(tilemap_size * 2);
|
2016-08-25 01:56:07 +00:00
|
|
|
for (i = 0; i < num_tiles; i++) {
|
|
|
|
tilemap->data[tilemap->size] = i;
|
|
|
|
tilemap->size++;
|
|
|
|
}
|
2017-05-28 05:11:32 +00:00
|
|
|
for (i = num_tiles; i < tilemap_size; i++) {
|
2017-05-29 03:22:10 +00:00
|
|
|
int preferred = i % num_tiles_per_frame;
|
|
|
|
int index = get_tile_index(graphics + i * tile_size, graphics, i, preferred);
|
|
|
|
if (Options.girafarig && index == 0) {
|
|
|
|
tile = num_tiles;
|
|
|
|
} else if (index == -1) {
|
|
|
|
tile = num_tiles++;
|
2016-08-25 01:56:07 +00:00
|
|
|
} else {
|
2017-05-29 03:22:10 +00:00
|
|
|
tile = tilemap->data[index];
|
2016-08-25 01:56:07 +00:00
|
|
|
}
|
2017-05-29 03:22:10 +00:00
|
|
|
tilemap->data[tilemap->size] = tile;
|
|
|
|
tilemap->size++;
|
2016-08-25 01:56:07 +00:00
|
|
|
}
|
|
|
|
|
2017-05-29 03:22:10 +00:00
|
|
|
int graphic_size = tilemap->size * 16;
|
|
|
|
if (Options.girafarig) {
|
|
|
|
// This is probably not needed, but just in case...
|
|
|
|
graphic_size += 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
graphic->data = malloc(graphic_size);
|
2016-08-25 01:56:07 +00:00
|
|
|
graphic->size = 16 * width * height;
|
|
|
|
memcpy(graphic->data, graphics, graphic->size);
|
|
|
|
for (i = width * height; i < tilemap->size; i++) {
|
2017-05-29 03:22:10 +00:00
|
|
|
tile = get_tile_index(graphics + 16 * i, graphic->data, graphic->size / 16, i % num_tiles_per_frame);
|
2016-08-25 01:56:07 +00:00
|
|
|
if (tile == -1) {
|
|
|
|
memcpy(graphic->data + graphic->size, graphics + 16 * i, 16);
|
|
|
|
graphic->size += 16;
|
|
|
|
}
|
|
|
|
}
|
2017-05-29 03:22:10 +00:00
|
|
|
if (Options.girafarig) {
|
|
|
|
// Add a duplicate of tile 0 to the end.
|
|
|
|
memcpy(graphic->data + graphic->size, graphics, 16);
|
|
|
|
graphic->size += 16;
|
|
|
|
}
|
2016-08-25 01:56:07 +00:00
|
|
|
|
|
|
|
free(graphics);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
2017-05-28 05:11:32 +00:00
|
|
|
int opt;
|
2016-08-25 01:56:07 +00:00
|
|
|
char* dimensions_filename;
|
|
|
|
char* graphics_filename;
|
2017-05-29 03:22:10 +00:00
|
|
|
char* outfile = NULL;
|
|
|
|
char* mapfile = NULL;
|
2016-08-25 01:56:07 +00:00
|
|
|
FILE* f;
|
|
|
|
long size;
|
|
|
|
uint8_t bytes[1];
|
|
|
|
int width;
|
|
|
|
int height;
|
|
|
|
struct Graphic graphic = {0};
|
|
|
|
struct Tilemap tilemap = {0};
|
|
|
|
|
2017-05-29 03:22:10 +00:00
|
|
|
while (1) {
|
|
|
|
struct option long_options[] = {
|
|
|
|
{"girafarig", no_argument, &Options.girafarig, 1},
|
|
|
|
{"tilemap", required_argument, 0, 't'},
|
|
|
|
{"output", required_argument, 0, 'o'},
|
|
|
|
{0}
|
|
|
|
};
|
|
|
|
int long_option_index = 0;
|
|
|
|
int opt = getopt_long(argc, argv, "o:t:", long_options, &long_option_index);
|
|
|
|
if (opt == -1) {
|
|
|
|
break;
|
|
|
|
}
|
2017-05-28 05:11:32 +00:00
|
|
|
switch (opt) {
|
2017-05-29 03:22:10 +00:00
|
|
|
case 0:
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
outfile = optarg;
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
mapfile = optarg;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
break;
|
2016-08-25 01:56:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
graphics_filename = argv[0];
|
|
|
|
dimensions_filename = argv[1];
|
|
|
|
|
2017-05-28 05:11:32 +00:00
|
|
|
f = fopen_verbose(dimensions_filename, "rb");
|
|
|
|
if (!f) {
|
|
|
|
exit(1);
|
|
|
|
}
|
2016-08-25 01:56:07 +00:00
|
|
|
fread(bytes, 1, 1, f);
|
|
|
|
fclose(f);
|
|
|
|
width = bytes[0] & 0xf;
|
|
|
|
height = bytes[0] >> 4;
|
|
|
|
|
|
|
|
create_tilemap(&tilemap, &graphic, graphics_filename, width, height);
|
|
|
|
|
2017-05-28 05:11:32 +00:00
|
|
|
if (outfile) {
|
|
|
|
f = fopen_verbose(outfile, "wb");
|
|
|
|
if (f) {
|
|
|
|
fwrite(graphic.data, 1, graphic.size, f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
2016-08-25 01:56:07 +00:00
|
|
|
}
|
|
|
|
|
2017-05-28 05:11:32 +00:00
|
|
|
if (mapfile) {
|
|
|
|
f = fopen_verbose(mapfile, "wb");
|
|
|
|
if (f) {
|
|
|
|
fwrite(tilemap.data, 1, tilemap.size, f);
|
|
|
|
fclose(f);
|
|
|
|
}
|
2016-08-25 01:56:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(graphic.data);
|
|
|
|
free(tilemap.data);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|