shithub: pokecrystal

Download patch

ref: 6cce67350186d4a0c37766eb5534e76adaad5a48
parent: 853abd3aa35233e344134486a46ff9585cac3386
author: yenatch <[email protected]>
date: Sun May 28 19:22:10 EDT 2017

Fix the pokemon animation tools.

Fixed a lot of serious bugs. These tools were not functional.
Also added a --girafarig flag for a special case.

--- a/tools/pokemon_animation.c
+++ b/tools/pokemon_animation.c
@@ -29,8 +29,8 @@
 
 
 void make_frames(struct Frames* frames, struct Bitmasks* bitmasks, char* tilemap_filename, char* dimensions_filename);
-int bitmask_exists(struct Bitmask bitmask, struct Bitmasks bitmasks);
-void print_frames();
+int bitmask_exists(struct Bitmask *bitmask, struct Bitmasks *bitmasks);
+void print_frames(struct Frames* frames);
 
 
 void make_frames(struct Frames* frames, struct Bitmasks* bitmasks, char* tilemap_filename, char* dimensions_filename) {
@@ -41,8 +41,6 @@
 	int width;
 	int height;
 	uint8_t byte;
-	struct Frame* frame;
-	struct Bitmask* bitmask;
 	int frame_size;
 	int num_frames;
 	int i, j;
@@ -77,33 +75,40 @@
 	num_frames = size / frame_size - 1;
 	//fprintf(stderr, "num_frames: %d\n", num_frames);
 
-	bitmasks->bitmasks = malloc((sizeof (struct Bitmask*)) * num_frames);
+	bitmasks->bitmasks = malloc((sizeof (struct Bitmask)) * num_frames);
 	bitmasks->num_bitmasks = 0;
 
-	frames->frames = malloc((sizeof (struct Frame*)) * num_frames);
+	frames->frames = malloc((sizeof (struct Frame)) * num_frames);
 	frames->frame_size = frame_size;
 	frames->num_frames = 0;
 
+	uint8_t *first_frame = tilemap;
 	this_frame = tilemap + frame_size;
 	for (i = 0; i < num_frames; i++) {
-		frame = (struct Frame*)malloc(sizeof(struct Frame));
+		struct Frame *frame = (struct Frame*)malloc(sizeof(struct Frame));
 		frame->data = malloc(frame_size);
 		frame->size = 0;
-		bitmask = (struct Bitmask*)malloc(sizeof(struct Bitmask));
-		bitmask->data = malloc((frame_size + 7) / 8);
+		struct Bitmask *bitmask = (struct Bitmask*)malloc(sizeof(struct Bitmask));
+		bitmask->data = calloc((frame_size + 7) / 8, 1);
 		bitmask->bitlength = 0;
 		for (j = 0; j < frame_size; j++) {
-			if (this_frame[j] != tilemap[j]) {
+			if (bitmask->bitlength % 8 == 0) {
+				bitmask->data[bitmask->bitlength / 8] = 0;
+			}
+			bitmask->data[bitmask->bitlength / 8] >>= 1;
+			if (this_frame[j] != first_frame[j]) {
 				frame->data[frame->size] = this_frame[j];
 				frame->size++;
-				bitmask->data[bitmask->bitlength / 8] |= 1;
+				bitmask->data[bitmask->bitlength / 8] |= (1 << 7);
 			}
 			bitmask->bitlength++;
-			if (bitmask->bitlength % 8 != 0) {
-				bitmask->data[bitmask->bitlength / 8] <<= 1;
-			}
 		}
-		frame->bitmask = bitmask_exists(*bitmask, *bitmasks);
+		// 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);
 		if (frame->bitmask == -1) {
 			frame->bitmask = bitmasks->num_bitmasks;
 			bitmasks->bitmasks[bitmasks->num_bitmasks] = *bitmask;
@@ -135,20 +140,22 @@
 	free(tilemap);
 }
 
-int bitmask_exists(struct Bitmask bitmask, struct Bitmasks bitmasks) {
+int bitmask_exists(struct Bitmask *bitmask, struct Bitmasks *bitmasks) {
 	int i, j;
 	struct Bitmask existing;
-	for (i = 0; i < bitmasks.num_bitmasks; i++) {
-		existing = bitmasks.bitmasks[i];
-		if (bitmask.bitlength != existing.bitlength) {
+	for (i = 0; i < bitmasks->num_bitmasks; i++) {
+		existing = bitmasks->bitmasks[i];
+		if (bitmask->bitlength != existing.bitlength) {
 			continue;
 		}
-		for (j = 0; j < (bitmask.bitlength + 7) / 8; j++) {
-			if (bitmask.data[j] != existing.data[j]) {
+		bool match = true;
+		for (j = 0; j < (bitmask->bitlength + 7) / 8; j++) {
+			if (bitmask->data[j] != existing.data[j]) {
+				match = false;
 				break;
 			}
 		}
-		if (j == (bitmask.bitlength + 7) / 8) {
+		if (match) {
 			return i;
 		}
 	}
@@ -158,18 +165,23 @@
 void print_frames(struct Frames* frames) {
 	int i;
 	int j;
-	struct Frame frame;
 	for (i = 0; i < frames->num_frames; i++) {
 		printf("\tdw .frame%d\n", i + 1);
 	}
 	for (i = 0; i < frames->num_frames; i++) {
-		frame = frames->frames[i];
+		struct Frame *frame = &frames->frames[i];
 		printf(".frame%d\n", i + 1);
-		printf("\tdb %d\n", frame.bitmask);
-		if (frame.size > 0) {
-			printf("\tdb %d", frame.data[0]);
-			for (j = 1; j < frame.size; j++) {
-				printf(", %d", frame.data[j]);
+		printf("\tdb $%02x ; bitmask\n", frame->bitmask);
+		if (frame->size > 0) {
+			for (j = 0; j < frame->size; j++) {
+				if (j % 12 == 0) {
+					if (j) {
+						printf("\n");
+					}
+					printf("\tdb $%02x", frame->data[j]);
+				} else {
+					printf(", $%02x", frame->data[j]);
+				}
 			}
 			printf("\n");
 		}
--- a/tools/pokemon_animation_graphics.c
+++ b/tools/pokemon_animation_graphics.c
@@ -2,6 +2,8 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <stdbool.h>
+#include <getopt.h>
 
 static void usage(void) {
 	printf("Usage: pokemon_animation_graphics [-o outfile] [-t mapfile] 2bpp_file dimensions_file\n");
@@ -8,6 +10,13 @@
 	exit(1);
 }
 
+struct Options {
+	int girafarig;
+};
+
+struct Options Options = {0};
+
+
 struct Tilemap {
 	uint8_t* data;
 	int size;
@@ -32,16 +41,25 @@
 	free(new_tiles);
 }
 
-int get_tile_index(uint8_t* tile, uint8_t* tiles, int num_tiles) {
-	int i;
-	int j;
-	for (i = 0; i < num_tiles; i++) {
-		for (j = 0; j < 16; j++) {
-			if (tile[j] != tiles[16 * i + j]) {
-				break;
-			}
+bool compare_tile(uint8_t *tile, uint8_t *other) {
+	for (int j = 0; j < 16; j++) {
+		if (tile[j] != other[j]) {
+			return false;
 		}
-		if (j == 16) {
+	}
+	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;
+		}
+	}
+	for (int i = 0; i < num_tiles; i++) {
+		uint8_t *other = &tiles[i * 16];
+		if (compare_tile(tile, other)) {
 			return i;
 		}
 	}
@@ -88,33 +106,46 @@
 	// so fill it unconditionally and start from the second frame
 	int num_tiles = width * height;
 	int tilemap_size = graphics_size / tile_size;
-	tilemap->data = malloc(tilemap_size);
+	tilemap->data = malloc(tilemap_size * 2);
 	for (i = 0; i < num_tiles; i++) {
 		tilemap->data[tilemap->size] = i;
 		tilemap->size++;
 	}
 	for (i = num_tiles; i < tilemap_size; i++) {
-		tile = get_tile_index(graphics + i * tile_size, graphics, i);
-		if (tile == -1) {
-			tilemap->data[tilemap->size] = num_tiles;
-			tilemap->size++;
-			num_tiles++;
+		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++;
 		} else {
-			tilemap->data[tilemap->size] = tile;
-			tilemap->size++;
+			tile = tilemap->data[index];
 		}
+		tilemap->data[tilemap->size] = tile;
+		tilemap->size++;
 	}
 
-	graphic->data = malloc(tilemap->size * 16);
+	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);
 	graphic->size = 16 * width * height;
 	memcpy(graphic->data, graphics, graphic->size);
 	for (i = width * height; i < tilemap->size; i++) {
-		tile = get_tile_index(graphics + 16 * i, graphic->data, graphic->size / 16);
+		tile = get_tile_index(graphics + 16 * i, graphic->data, graphic->size / 16, i % num_tiles_per_frame);
 		if (tile == -1) {
 			memcpy(graphic->data + graphic->size, graphics + 16 * i, 16);
 			graphic->size += 16;
 		}
 	}
+	if (Options.girafarig) {
+		// Add a duplicate of tile 0 to the end.
+		memcpy(graphic->data + graphic->size, graphics, 16);
+		graphic->size += 16;
+	}
 
 	free(graphics);
 }
@@ -123,8 +154,8 @@
 	int opt;
 	char* dimensions_filename;
 	char* graphics_filename;
-	char* outfile;
-	char* mapfile;
+	char* outfile = NULL;
+	char* mapfile = NULL;
 	FILE* f;
 	long size;
 	uint8_t bytes[1];
@@ -133,17 +164,30 @@
 	struct Graphic graphic = {0};
 	struct Tilemap tilemap = {0};
 
-	while ((opt = getopt(argc, argv, "o:t:")) != -1) {
+	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;
+		}
 		switch (opt) {
-			case 'o':
-				outfile = optarg;
-				break;
-			case 't':
-				mapfile = optarg;
-				break;
-			default:
-				usage();
-				break;
+		case 0:
+			break;
+		case 'o':
+			outfile = optarg;
+			break;
+		case 't':
+			mapfile = optarg;
+			break;
+		default:
+			usage();
+			break;
 		}
 	}
 	argc -= optind;
--- a/tools/scan_includes.c
+++ b/tools/scan_includes.c
@@ -105,6 +105,10 @@
 		case 'h':
 			Options.help = true;
 			break;
+		default:
+			usage();
+			exit(1);
+			break;
 		}
 	}
 }