shithub: pokered

Download patch

ref: 9d7b581310eb500910a4cf89e9f2a317eee40ec8
parent: c37cc15f44380af3fdd9030fc196b4951cd6920c
author: Rangi <remy.oukaour+rangi42@gmail.com>
date: Fri Oct 14 12:21:42 EDT 2022

Add `COUNTOF` macro to tools/common.h

--- a/tools/common.h
+++ b/tools/common.h
@@ -19,6 +19,8 @@
 #error Define USAGE_OPTS before including common.h!
 #endif
 
+#define COUNTOF(...) (sizeof(__VA_ARGS__) / sizeof(*(__VA_ARGS__)))
+
 #define error_exit(...) exit((fprintf(stderr, PROGRAM_NAME ": " __VA_ARGS__), 1))
 
 noreturn void usage_exit(int status) {
--- a/tools/make_patch.c
+++ b/tools/make_patch.c
@@ -165,7 +165,7 @@
 	return -1;
 }
 
-#define vstrfind(s, ...) strfind(s, (const char *[]){__VA_ARGS__}, sizeof (const char *[]){__VA_ARGS__} / sizeof(const char *))
+#define vstrfind(s, ...) strfind(s, (const char *[]){__VA_ARGS__}, COUNTOF((const char *[]){__VA_ARGS__}))
 
 int parse_arg_value(const char *arg, bool absolute, const struct Symbol *symbols, const char *patch_name) {
 	// Comparison operators for "ConditionValueB" evaluate to their particular values
--- a/tools/pkmncompress.c
+++ b/tools/pkmncompress.c
@@ -32,7 +32,7 @@
 }
 
 void compress_plane(uint8_t *plane, int width) {
-	static int nybble_lookup[2][0x10] = {
+	static int gray_codes[2][0x10] = {
 		{0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4, 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9, 0x8},
 		{0x8, 0x9, 0xB, 0xA, 0xE, 0xF, 0xD, 0xC, 0x4, 0x5, 0x7, 0x6, 0x2, 0x3, 0x1, 0x0},
 	};
@@ -44,10 +44,10 @@
 		}
 		int j = i / width + m * width * 8;
 		int nybble_hi = (plane[j] >> 4) & 0xF;
-		int code_1 = nybble_lookup[nybble_lo & 1][nybble_hi];
+		int code_hi = gray_codes[nybble_lo & 1][nybble_hi];
 		nybble_lo = plane[j] & 0xF;
-		int code_2 = nybble_lookup[nybble_hi & 1][nybble_lo];
-		plane[j] = (code_1 << 4) | code_2;
+		int code_lo = gray_codes[nybble_hi & 1][nybble_lo];
+		plane[j] = (code_hi << 4) | code_lo;
 	}
 }
 
@@ -105,7 +105,7 @@
 	}
 	cur_bit = 7;
 	cur_byte = 0;
-	memset(compressed, 0, sizeof(compressed) / sizeof(*compressed));
+	memset(compressed, 0, COUNTOF(compressed));
 	compressed[0] = (width << 4) | width;
 	write_bit(order);
 	uint8_t bit_groups[0x1000] = {0};
@@ -113,7 +113,7 @@
 	for (int plane = 0; plane < 2; plane++) {
 		int type = 0;
 		int nums = 0;
-		memset(bit_groups, 0, sizeof(bit_groups) / sizeof(*bit_groups));
+		memset(bit_groups, 0, COUNTOF(bit_groups));
 		for (int x = 0; x < width; x++) {
 			for (int bit = 0; bit < 8; bit += 2) {
 				for (int y = 0, byte = x * width * 8; y < width * 8; y++, byte++) {
@@ -129,7 +129,7 @@
 							write_bit(0);
 						}
 						type = 1;
-						memset(bit_groups, 0, sizeof(bit_groups) / sizeof(*bit_groups));
+						memset(bit_groups, 0, COUNTOF(bit_groups));
 						index = 0;
 					} else {
 						if (!type) {
@@ -171,7 +171,7 @@
 		plane1[i] = data[i * 2];
 		plane2[i] = data[i * 2 + 1];
 	}
-	uint8_t current[sizeof(compressed) / sizeof(*compressed)] = {0};
+	uint8_t current[COUNTOF(compressed)] = {0};
 	int compressed_size = -1;
 	for (int mode = 1; mode < 4; mode++) {
 		for (int order = 0; order < 2; order++) {
@@ -181,12 +181,12 @@
 			int new_size = interpret_compress(plane1, plane2, mode, order, width);
 			if (compressed_size == -1 || new_size < compressed_size) {
 				compressed_size = new_size;
-				memset(current, 0, sizeof(current) / sizeof(*current));
+				memset(current, 0, COUNTOF(current));
 				memcpy(current, compressed, compressed_size / 8);
 			}
 		}
 	}
-	memset(compressed, 0, sizeof(compressed) / sizeof(*compressed));
+	memset(compressed, 0, COUNTOF(compressed));
 	memcpy(compressed, current, compressed_size / 8);
 	free(plane1);
 	free(plane2);