2021-09-21 21:37:43 +00:00
|
|
|
#define PROGRAM_NAME "pokemon_animation"
|
|
|
|
#define USAGE_OPTS "[-h|--help] [-b|--bitmasks] [-f|--frames] front.animated.tilemap front.dimensions"
|
|
|
|
|
2021-09-02 22:37:36 +00:00
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
struct Options {
|
|
|
|
bool use_bitmasks;
|
|
|
|
bool use_frames;
|
|
|
|
};
|
|
|
|
|
|
|
|
void parse_args(int argc, char *argv[], struct Options *options) {
|
|
|
|
struct option long_options[] = {
|
|
|
|
{"bitmasks", no_argument, 0, 'b'},
|
|
|
|
{"frames", no_argument, 0, 'f'},
|
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{0}
|
|
|
|
};
|
|
|
|
for (int opt; (opt = getopt_long(argc, argv, "bfh", long_options)) != -1;) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'b':
|
|
|
|
options->use_bitmasks = true;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
options->use_frames = true;
|
|
|
|
break;
|
|
|
|
case 'h':
|
2021-09-21 21:37:43 +00:00
|
|
|
usage_exit(0);
|
2021-09-02 22:37:36 +00:00
|
|
|
break;
|
|
|
|
default:
|
2021-09-21 21:37:43 +00:00
|
|
|
usage_exit(1);
|
2021-09-02 22:37:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-08-25 01:56:07 +00:00
|
|
|
|
|
|
|
struct Frame {
|
2021-09-02 22:37:36 +00:00
|
|
|
uint8_t *data;
|
2016-08-25 01:56:07 +00:00
|
|
|
int size;
|
|
|
|
int bitmask;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Frames {
|
2021-09-02 22:37:36 +00:00
|
|
|
struct Frame *frames;
|
2016-08-25 01:56:07 +00:00
|
|
|
int num_frames;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Bitmask {
|
2021-09-02 22:37:36 +00:00
|
|
|
uint8_t *data;
|
2016-08-25 01:56:07 +00:00
|
|
|
int bitlength;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Bitmasks {
|
2021-09-02 22:37:36 +00:00
|
|
|
struct Bitmask *bitmasks;
|
2016-08-25 01:56:07 +00:00
|
|
|
int num_bitmasks;
|
|
|
|
};
|
|
|
|
|
2021-09-02 22:37:36 +00:00
|
|
|
int bitmask_exists(const struct Bitmask *bitmask, const struct Bitmasks *bitmasks) {
|
|
|
|
for (int i = 0; i < bitmasks->num_bitmasks; i++) {
|
|
|
|
struct Bitmask existing = bitmasks->bitmasks[i];
|
|
|
|
if (bitmask->bitlength != existing.bitlength) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
bool match = true;
|
|
|
|
int length = (bitmask->bitlength + 7) / 8;
|
|
|
|
for (int j = 0; j < length; j++) {
|
|
|
|
if (bitmask->data[j] != existing.data[j]) {
|
|
|
|
match = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (match) {
|
|
|
|
return i;
|
|
|
|
}
|
2016-08-25 01:56:07 +00:00
|
|
|
}
|
2021-09-02 22:37:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2016-08-25 01:56:07 +00:00
|
|
|
|
2021-09-02 22:37:36 +00:00
|
|
|
void make_frames(const uint8_t *tilemap, long tilemap_size, int width, struct Frames *frames, struct Bitmasks *bitmasks) {
|
|
|
|
int num_tiles_per_frame = width * width;
|
|
|
|
int num_frames = tilemap_size / num_tiles_per_frame - 1;
|
2016-08-25 01:56:07 +00:00
|
|
|
|
2021-09-02 22:37:36 +00:00
|
|
|
frames->frames = malloc_verbose((sizeof *frames->frames) * num_frames);
|
|
|
|
frames->num_frames = num_frames;
|
2016-08-25 01:56:07 +00:00
|
|
|
|
2021-09-02 22:37:36 +00:00
|
|
|
bitmasks->bitmasks = malloc_verbose((sizeof *bitmasks->bitmasks) * num_frames);
|
2016-08-25 01:56:07 +00:00
|
|
|
bitmasks->num_bitmasks = 0;
|
|
|
|
|
2021-09-02 22:37:36 +00:00
|
|
|
const uint8_t *first_frame = &tilemap[0];
|
|
|
|
const uint8_t *this_frame = &tilemap[num_tiles_per_frame];
|
|
|
|
for (int i = 0; i < num_frames; i++) {
|
|
|
|
struct Frame *frame = malloc_verbose(sizeof *frame);
|
|
|
|
frame->data = malloc_verbose(num_tiles_per_frame);
|
2016-08-25 01:56:07 +00:00
|
|
|
frame->size = 0;
|
2021-09-02 22:37:36 +00:00
|
|
|
|
|
|
|
struct Bitmask *bitmask = malloc_verbose(sizeof *bitmask);
|
|
|
|
bitmask->data = calloc_verbose((num_tiles_per_frame + 7) / 8);
|
2016-08-25 01:56:07 +00:00
|
|
|
bitmask->bitlength = 0;
|
2021-09-02 22:37:36 +00:00
|
|
|
|
|
|
|
for (int j = 0; j < num_tiles_per_frame; j++) {
|
2017-05-29 03:22:10 +00:00
|
|
|
if (bitmask->bitlength % 8 == 0) {
|
|
|
|
bitmask->data[bitmask->bitlength / 8] = 0;
|
|
|
|
}
|
|
|
|
bitmask->data[bitmask->bitlength / 8] >>= 1;
|
|
|
|
if (this_frame[j] != first_frame[j]) {
|
2016-08-25 01:56:07 +00:00
|
|
|
frame->data[frame->size] = this_frame[j];
|
|
|
|
frame->size++;
|
2017-05-29 03:22:10 +00:00
|
|
|
bitmask->data[bitmask->bitlength / 8] |= (1 << 7);
|
2016-08-25 01:56:07 +00:00
|
|
|
}
|
|
|
|
bitmask->bitlength++;
|
|
|
|
}
|
2017-05-29 03:22:10 +00:00
|
|
|
// I don't remember exactly why this works.
|
|
|
|
// I think it was that the bits are read backwards, but not indexed backwards.
|
|
|
|
int last = bitmask->bitlength - 1;
|
|
|
|
bitmask->data[last / 8] >>= (7 - (last % 8));
|
|
|
|
|
|
|
|
frame->bitmask = bitmask_exists(bitmask, bitmasks);
|
2016-08-25 01:56:07 +00:00
|
|
|
if (frame->bitmask == -1) {
|
|
|
|
frame->bitmask = bitmasks->num_bitmasks;
|
|
|
|
bitmasks->bitmasks[bitmasks->num_bitmasks] = *bitmask;
|
|
|
|
bitmasks->num_bitmasks++;
|
|
|
|
} else {
|
|
|
|
free(bitmask->data);
|
|
|
|
free(bitmask);
|
|
|
|
}
|
|
|
|
frames->frames[i] = *frame;
|
2021-09-02 22:37:36 +00:00
|
|
|
this_frame += num_tiles_per_frame;
|
2016-08-25 01:56:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 22:37:36 +00:00
|
|
|
void print_frames(struct Frames *frames) {
|
|
|
|
for (int i = 0; i < frames->num_frames; i++) {
|
2016-08-25 01:56:07 +00:00
|
|
|
printf("\tdw .frame%d\n", i + 1);
|
|
|
|
}
|
2021-09-02 22:37:36 +00:00
|
|
|
for (int i = 0; i < frames->num_frames; i++) {
|
|
|
|
const struct Frame *frame = &frames->frames[i];
|
2016-08-25 01:56:07 +00:00
|
|
|
printf(".frame%d\n", i + 1);
|
2017-05-29 03:22:10 +00:00
|
|
|
printf("\tdb $%02x ; bitmask\n", frame->bitmask);
|
|
|
|
if (frame->size > 0) {
|
2021-09-02 22:37:36 +00:00
|
|
|
for (int j = 0; j < frame->size; j++) {
|
2017-05-29 03:22:10 +00:00
|
|
|
if (j % 12 == 0) {
|
|
|
|
if (j) {
|
2021-09-02 22:37:36 +00:00
|
|
|
putchar('\n');
|
2017-05-29 03:22:10 +00:00
|
|
|
}
|
|
|
|
printf("\tdb $%02x", frame->data[j]);
|
|
|
|
} else {
|
|
|
|
printf(", $%02x", frame->data[j]);
|
|
|
|
}
|
2016-08-25 01:56:07 +00:00
|
|
|
}
|
2021-09-02 22:37:36 +00:00
|
|
|
putchar('\n');
|
2016-08-25 01:56:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 22:37:36 +00:00
|
|
|
void print_bitmasks(const struct Bitmasks *bitmasks) {
|
|
|
|
for (int i = 0; i < bitmasks->num_bitmasks; i++) {
|
|
|
|
struct Bitmask bitmask = bitmasks->bitmasks[i];
|
2016-08-25 01:56:07 +00:00
|
|
|
printf("; %d\n", i);
|
2021-09-02 22:37:36 +00:00
|
|
|
int length = (bitmask.bitlength + 7) / 8;
|
|
|
|
for (int j = 0; j < length; j++) {
|
2016-08-25 01:56:07 +00:00
|
|
|
printf("\tdb %%");
|
2021-09-02 22:37:36 +00:00
|
|
|
for (int k = 0; k < 8; k++) {
|
|
|
|
putchar(((bitmask.data[j] >> (7 - k)) & 1) ? '1' : '0');
|
2016-08-25 01:56:07 +00:00
|
|
|
}
|
2021-09-02 22:37:36 +00:00
|
|
|
putchar('\n');
|
2016-08-25 01:56:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-02 22:37:36 +00:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
struct Options options = {0};
|
|
|
|
parse_args(argc, argv, &options);
|
2016-08-25 01:56:07 +00:00
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
if (argc < 2) {
|
2021-09-21 21:37:43 +00:00
|
|
|
usage_exit(1);
|
2016-08-25 01:56:07 +00:00
|
|
|
}
|
|
|
|
|
2021-09-02 22:37:36 +00:00
|
|
|
int width;
|
|
|
|
read_dimensions(argv[1], &width);
|
|
|
|
long tilemap_size;
|
|
|
|
uint8_t *tilemap = read_u8(argv[0], &tilemap_size);
|
|
|
|
|
|
|
|
struct Frames frames = {0};
|
|
|
|
struct Bitmasks bitmasks = {0};
|
|
|
|
make_frames(tilemap, tilemap_size, width, &frames, &bitmasks);
|
2016-08-25 01:56:07 +00:00
|
|
|
|
2021-09-02 22:37:36 +00:00
|
|
|
if (options.use_frames) {
|
2016-08-25 01:56:07 +00:00
|
|
|
print_frames(&frames);
|
|
|
|
}
|
2021-09-02 22:37:36 +00:00
|
|
|
if (options.use_bitmasks) {
|
2016-08-25 01:56:07 +00:00
|
|
|
print_bitmasks(&bitmasks);
|
|
|
|
}
|
2021-09-02 22:37:36 +00:00
|
|
|
|
|
|
|
free(tilemap);
|
2016-08-25 01:56:07 +00:00
|
|
|
return 0;
|
|
|
|
}
|