shithub: pokecrystal

Download patch

ref: 01caa369ec5f3d49d151c8323ac86bd7e1df040a
parent: 17448c51a51bb3ddd20b0349ae274980638b0980
parent: 94577d95532368a940bf0bb9334ba2c7eb858eb5
author: yenatch <yenatch@gmail.com>
date: Sat Sep 23 21:25:03 EDT 2017

Merge pull request #379 from yenatch/tools

Refactor scan_includes and add warnings for tools

--- a/Makefile
+++ b/Makefile
@@ -36,12 +36,10 @@
 crystal: pokecrystal.gbc
 crystal11: pokecrystal11.gbc
 
-# Ensure that the tools are built when making the ROM
-ifneq ($(MAKECMDGOALS),clean)
-ifneq ($(MAKECMDGOALS),tools)
-Makefile: tools
+# Build tools when building the rom
+ifeq (,$(filter clean tools,$(MAKECMDGOALS)))
+Makefile: tools ;
 endif
-endif
 
 clean:
 	rm -f $(roms) $(crystal_obj) $(crystal11_obj) $(roms:.gbc=.map) $(roms:.gbc=.sym)
@@ -79,10 +77,13 @@
 %.lz: hash = $(shell tools/md5 $(*D)/$(*F) | sed "s/\(.\{8\}\).*/\1/")
 %.lz: %
 	$(eval filename := $@.$(hash))
-	$(if $(wildcard $(filename)),cp $(filename) $@,tools/lzcomp $< $@)
+	$(if $(wildcard $(filename)),\
+		cp $(filename) $@,\
+		tools/lzcomp $< $@)
 
-# Terrible hacks to match animations. Delete these rules if you don't care about matching.
 
+### Terrible hacks to match animations. Delete these rules if you don't care about matching.
+
 # Dewgong has an unused tile id in its last frame. The tile itself is missing.
 gfx/pics/dewgong/frames.asm: gfx/pics/dewgong/front.animated.tilemap gfx/pics/dewgong/front.dimensions
 	tools/pokemon_animation -f $^ > $@
@@ -101,7 +102,7 @@
 	tools/pokemon_animation_graphics --girafarig -t $@ $^
 
 
-# Pokemon pic graphics rules
+### Pokemon pic graphics rules
 
 gfx/pics/%/normal.gbcpal: gfx/pics/%/front.png
 	rgbgfx -p $@ $<
@@ -122,7 +123,7 @@
 #	rgbgfx -o $@ $<
 
 
-# Misc file-specific graphics rules
+### Misc file-specific graphics rules
 
 gfx/shrink1.2bpp: rgbgfx += -h
 gfx/shrink2.2bpp: rgbgfx += -h
@@ -139,17 +140,17 @@
 
 gfx/pokedex/%.2bpp: tools/gfx += --trim-whitespace
 
-gfx/title/crystal.2bpp: tools/gfx += --interleave --width=48
-gfx/title/old_fg.2bpp: tools/gfx += --interleave --width=64
+gfx/title/crystal.2bpp: tools/gfx += --interleave --png=$<
+gfx/title/old_fg.2bpp: tools/gfx += --interleave --png=$<
 gfx/title/logo.2bpp: rgbgfx += -x 4
 
 gfx/trade/ball.2bpp: tools/gfx += --remove-whitespace
 gfx/trade/game_boy_n64.2bpp: tools/gfx += --trim-whitespace
 
-gfx/slots_2.2bpp: tools/gfx += --interleave --width=16
-gfx/slots_3.2bpp: tools/gfx += --interleave --width=24 --remove-duplicates --keep-whitespace --remove-xflip
-gfx/slots_3a.2bpp: tools/gfx += --interleave --width=16
-gfx/slots_3b.2bpp: tools/gfx += --interleave --width=24 --remove-duplicates --keep-whitespace --remove-xflip
+gfx/slots_2.2bpp: tools/gfx += --interleave --png=$<
+gfx/slots_3.2bpp: tools/gfx += --interleave --png=$< --remove-duplicates --keep-whitespace --remove-xflip
+gfx/slots_3a.2bpp: tools/gfx += --interleave --png=$<
+gfx/slots_3b.2bpp: tools/gfx += --interleave --png=$< --remove-duplicates --keep-whitespace --remove-xflip
 
 gfx/fx/angels.2bpp: tools/gfx += --trim-whitespace
 gfx/fx/beam.2bpp: tools/gfx += --remove-xflip --remove-yflip --remove-whitespace
@@ -194,11 +195,13 @@
 
 %.2bpp: %.png
 	rgbgfx $(rgbgfx) -o $@ $<
-	$(if $(tools/gfx),tools/gfx $(tools/gfx) -o $@ $@)
+	$(if $(tools/gfx),\
+		tools/gfx $(tools/gfx) -o $@ $@)
 
 %.1bpp: %.png
 	rgbgfx $(rgbgfx) -d1 -o $@ $<
-	$(if $(tools/gfx),tools/gfx $(tools/gfx) -d1 -o $@ $@)
+	$(if $(tools/gfx),\
+		tools/gfx $(tools/gfx) -d1 -o $@ $@)
 
 %.tilemap: %.png
 	rgbgfx -t $@ $<
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -1,7 +1,7 @@
 .PHONY: all clean
 
 CC := gcc
-CFLAGS := -std=c99
+CFLAGS := -std=c99 -Wall -Wextra
 
 tools := \
 	lzcomp \
--- a/tools/gfx.c
+++ b/tools/gfx.c
@@ -8,7 +8,7 @@
 #include "common.h"
 
 static void usage(void) {
-	fprintf(stderr, "Usage: gfx [--trim-whitespace] [--remove-whitespace] [--interleave] [--remove-duplicates [--keep-whitespace]] [--remove-xflip] [--remove-yflip] [-w width] [-d depth] [-h] [-o outfile] infile\n");
+	fprintf(stderr, "Usage: gfx [--trim-whitespace] [--remove-whitespace] [--interleave] [--remove-duplicates [--keep-whitespace]] [--remove-xflip] [--remove-yflip] [--png filename] [-d depth] [-h] [-o outfile] infile\n");
 }
 
 static void error(char *message) {
@@ -23,11 +23,11 @@
 	char *outfile;
 	int depth;
 	int interleave;
-	int width;
 	int remove_duplicates;
 	int keep_whitespace;
 	int remove_xflip;
 	int remove_yflip;
+	char *png_file;
 };
 
 struct Options Options = {
@@ -43,13 +43,13 @@
 		{"keep-whitespace", no_argument, &Options.keep_whitespace, 1},
 		{"remove-xflip", no_argument, &Options.remove_xflip, 1},
 		{"remove-yflip", no_argument, &Options.remove_yflip, 1},
-		{"width", required_argument, 0, 'w'},
+		{"png", required_argument, 0, 'p'},
 		{"depth", required_argument, 0, 'd'},
 		{"help", no_argument, 0, 'h'},
 		{0}
 	};
 	for (int opt = 0; opt != -1;) {
-		switch (opt = getopt_long(argc, argv, "ho:d:", long_options)) {
+		switch (opt = getopt_long(argc, argv, "ho:d:p:", long_options)) {
 		case 'h':
 			Options.help = true;
 			break;
@@ -59,8 +59,8 @@
 		case 'd':
 			Options.depth = strtoul(optarg, NULL, 0);
 			break;
-		case 'w':
-			Options.width = strtoul(optarg, NULL, 0);
+		case 'p':
+			Options.png_file = optarg;
 			break;
 		case 0:
 		case -1:
@@ -221,7 +221,26 @@
 	free(interleaved);
 }
 
+int png_get_width(char *filename) {
+	FILE *f = fopen_verbose(filename, "rb");
+	if (!f) {
+		exit(1);
+	}
 
+	const int OFFSET_WIDTH = 16;
+	uint8_t bytes[4];
+	fseek(f, OFFSET_WIDTH, SEEK_SET);
+	fread(bytes, 1, 4, f);
+	fclose(f);
+
+	int width = 0;
+	for (int i = 0; i < 4; i++) {
+		width |= bytes[i] << (8 * (3 - i));
+	}
+	return width;
+}
+
+
 int main(int argc, char *argv[]) {
 	get_args(argc, argv);
 	argc -= optind;
@@ -241,12 +260,13 @@
 		trim_whitespace(&graphic);
 	}
 	if (Options.interleave) {
-		if (!Options.width) {
-			error("interleave: must set --width to a nonzero value");
+		if (!Options.png_file) {
+			error("interleave: need --png to infer dimensions");
 			usage();
 			exit(1);
 		}
-		interleave(&graphic, Options.width);
+		int width = png_get_width(Options.png_file);
+		interleave(&graphic, width);
 	}
 	if (Options.remove_duplicates) {
 		remove_duplicates(&graphic);
--- a/tools/lzcomp.c
+++ b/tools/lzcomp.c
@@ -113,7 +113,7 @@
         *(pos ++) = command.value;
       }
   }
-  if (fwrite(buf, 1, pos - buf, fp) != (pos - buf)) error_exit(1, "could not write command to compressed output");
+  if ((int)fwrite(buf, 1, pos - buf, fp) != (pos - buf)) error_exit(1, "could not write command to compressed output");
   if (command.command) return;
   command.count ++;
   if (fwrite(input_stream + command.value, 1, command.count, fp) != command.count) error_exit(1, "could not write data to compressed output");
--- a/tools/md5.c
+++ b/tools/md5.c
@@ -32,7 +32,7 @@
 };
 
 #define rotate_left_32(value, by) \
-	((((value) << (by)) & 0xffffffff) | ((value) >> 32 - (by)))
+	((((value) << (by)) & 0xffffffff) | ((value) >> (32 - (by))))
 
 void md5_wikipedia(uint8_t *data, int length, uint8_t *result) {
 
--- a/tools/png_dimensions.c
+++ b/tools/png_dimensions.c
@@ -3,7 +3,7 @@
 #include <stdlib.h>
 
 void usage(void) {
-	fprintf(stderr, "Usage: png_dimensions infile, outfile\n");
+	fprintf(stderr, "Usage: png_dimensions infile outfile\n");
 	exit(1);
 }
 
--- a/tools/pokemon_animation_graphics.c
+++ b/tools/pokemon_animation_graphics.c
@@ -162,13 +162,11 @@
 }
 
 int main(int argc, char* argv[]) {
-	int opt;
 	char* dimensions_filename;
 	char* graphics_filename;
 	char* outfile = NULL;
 	char* mapfile = NULL;
 	FILE* f;
-	long size;
 	uint8_t bytes[1];
 	int width;
 	int height;
--- a/tools/scan_includes.c
+++ b/tools/scan_includes.c
@@ -19,16 +19,8 @@
 
 struct Options Options = {0};
 
-
 void scan_file(char* filename) {
-	FILE* f;
-	long size;
-	char* orig;
-	char* buffer;
-	char* include;
-	int length;
-
-	f = fopen(filename, "r");
+	FILE *f = fopen(filename, "rb");
 	if (!f) {
 		if (Options.strict) {
 			fprintf(stderr, "Could not open file: '%s'\n", filename);
@@ -39,65 +31,70 @@
 	}
 
 	fseek(f, 0, SEEK_END);
-	size = ftell(f);
+	long size = ftell(f);
 	rewind(f);
 
-	buffer = malloc(size + 1);
-	orig = buffer;
-	fread(buffer, 1, size, f);
+	char *buffer = malloc(size + 1);
+	char *orig = buffer;
+	size = fread(buffer, 1, size, f);
 	buffer[size] = '\0';
 	fclose(f);
 
 	for (; buffer && (buffer - orig < size); buffer++) {
-		if (buffer[0] == ';') {
-			buffer = strchr(buffer, '\n');
-			if (!buffer) {
-				fprintf(stderr, "%s: no newline at end of file\n", filename);
-				break;
-			}
-			continue;
-		}
 		bool is_include = false;
 		bool is_incbin = false;
-		if ((strncmp(buffer, "INCBIN", 6) == 0) || (strncmp(buffer, "incbin", 6) == 0)) {
-			is_incbin = true;
-		} else if ((strncmp(buffer, "INCLUDE", 7) == 0) || (strncmp(buffer, "include", 7) == 0)) {
-			is_include = true;
-		}
-		if (is_incbin || is_include) {
-			buffer = strchr(buffer, '"') + 1;
-			if (!buffer) {
+		switch (*buffer) {
+			case ';':
+				buffer = strchr(buffer, '\n');
+				if (!buffer) {
+					fprintf(stderr, "%s: no newline at end of file\n", filename);
+				}
 				break;
-			}
-			length = strcspn(buffer, "\"");
-			include = malloc(length + 1);
-			strncpy(include, buffer, length);
-			include[length] = '\0';
-			printf("%s ", include);
-			if (is_include) {
-				scan_file(include);
-			}
-			free(include);
+
+			case 'i':
+			case 'I':
+				if ((strncmp(buffer, "INCBIN", 6) == 0) || (strncmp(buffer, "incbin", 6) == 0)) {
+					is_incbin = true;
+				} else if ((strncmp(buffer, "INCLUDE", 7) == 0) || (strncmp(buffer, "include", 7) == 0)) {
+					is_include = true;
+				}
+				if (is_incbin || is_include) {
+					buffer = strchr(buffer, '"');
+					if (!buffer++) {
+						break;
+					}
+					int length = strcspn(buffer, "\"");
+					char *include = malloc(length + 1);
+					strncpy(include, buffer, length);
+					include[length] = '\0';
+					printf("%s ", include);
+					if (is_include) {
+						scan_file(include);
+					}
+					free(include);
+					buffer = strchr(buffer, '"');
+				}
+				break;
+
 		}
+		if (!buffer) {
+			break;
+		}
+
 	}
 
 	free(orig);
 }
 
-void get_args(int argc, char *argv[]) {
-	while (1) {
-		struct option long_options[] = {
-			{"strict", no_argument, 0, 's'},
-			{"help", no_argument, 0, 'h'},
-			{0}
-		};
-		int i = 0;
-		int opt = getopt_long(argc, argv, "sh", long_options, &i);
-
-		if (opt == -1) {
-			break;
-		}
-
+int main(int argc, char* argv[]) {
+	int i = 0;
+	struct option long_options[] = {
+		{"strict", no_argument, 0, 's'},
+		{"help", no_argument, 0, 'h'},
+		{0}
+	};
+	int opt = -1;
+	while ((opt = getopt_long(argc, argv, "sh", long_options, &i)) != -1) {
 		switch (opt) {
 		case 's':
 			Options.strict = true;
@@ -111,10 +108,6 @@
 			break;
 		}
 	}
-}
-
-int main(int argc, char* argv[]) {
-	get_args(argc, argv);
 	argc -= optind;
 	argv += optind;
 	if (Options.help) {