From 885af5e5438f9d09f822d67c587f7a80b8da15f9 Mon Sep 17 00:00:00 2001 From: yenatch Date: Sun, 20 Aug 2017 12:32:15 -0400 Subject: [PATCH 1/8] Refactor scan_includes --- tools/scan_includes.c | 107 ++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 57 deletions(-) diff --git a/tools/scan_includes.c b/tools/scan_includes.c index b6fcca03a..2babdcfb5 100644 --- a/tools/scan_includes.c +++ b/tools/scan_includes.c @@ -19,16 +19,8 @@ struct Options { 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 @@ void scan_file(char* filename) { } 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 @@ void get_args(int argc, char *argv[]) { break; } } -} - -int main(int argc, char* argv[]) { - get_args(argc, argv); argc -= optind; argv += optind; if (Options.help) { From c719ff4f0b79833e588bf9bb53954f995c6c775d Mon Sep 17 00:00:00 2001 From: yenatch Date: Sun, 20 Aug 2017 12:33:12 -0400 Subject: [PATCH 2/8] Fix unused variable warnings --- tools/pokemon_animation_graphics.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/tools/pokemon_animation_graphics.c b/tools/pokemon_animation_graphics.c index ae96d7f17..3ee1bd4cb 100644 --- a/tools/pokemon_animation_graphics.c +++ b/tools/pokemon_animation_graphics.c @@ -162,13 +162,11 @@ void create_tilemap(struct Tilemap* tilemap, struct Graphic* graphic, char* grap } 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; From 50d163895d16352144161ada1a74356e7226fa92 Mon Sep 17 00:00:00 2001 From: yenatch Date: Sun, 20 Aug 2017 12:33:36 -0400 Subject: [PATCH 3/8] Remove comma from png_dimensions usage --- tools/png_dimensions.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/png_dimensions.c b/tools/png_dimensions.c index 5bdc12e4c..7bd8550aa 100644 --- a/tools/png_dimensions.c +++ b/tools/png_dimensions.c @@ -3,7 +3,7 @@ #include void usage(void) { - fprintf(stderr, "Usage: png_dimensions infile, outfile\n"); + fprintf(stderr, "Usage: png_dimensions infile outfile\n"); exit(1); } From 3064e84c5f6e2cc4b812713f89725124ebc757a9 Mon Sep 17 00:00:00 2001 From: yenatch Date: Sun, 20 Aug 2017 12:35:36 -0400 Subject: [PATCH 4/8] Use $(filter) to check targets --- Makefile | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 063cdc27b..329269cf4 100644 --- a/Makefile +++ b/Makefile @@ -36,11 +36,9 @@ all: crystal crystal: pokecrystal.gbc crystal11: pokecrystal11.gbc -# Ensure that the tools are built when making the ROM -ifneq ($(MAKECMDGOALS),clean) -ifneq ($(MAKECMDGOALS),tools) -Makefile: tools -endif +# Build tools when building the rom +ifeq (,$(filter clean tools,$(MAKECMDGOALS))) +Makefile: tools ; endif clean: From 7ba068d45b6e1744402ab910f2ded4e9e32429c6 Mon Sep 17 00:00:00 2001 From: yenatch Date: Sun, 20 Aug 2017 12:38:32 -0400 Subject: [PATCH 5/8] Makefile formatting --- Makefile | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 329269cf4..aa3d73c17 100644 --- a/Makefile +++ b/Makefile @@ -75,9 +75,12 @@ pokecrystal.gbc: $(crystal_obj) %.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 @@ -97,7 +100,7 @@ gfx/pics/girafarig/front.animated.tilemap: gfx/pics/girafarig/front.2bpp gfx/pic tools/pokemon_animation_graphics --girafarig -t $@ $^ -# Pokemon pic graphics rules +### Pokemon pic graphics rules gfx/pics/%/normal.gbcpal: gfx/pics/%/front.png rgbgfx -p $@ $< @@ -118,7 +121,7 @@ gfx/pics/%/front.animated.tilemap: gfx/pics/%/front.2bpp gfx/pics/%/front.dimens # rgbgfx -o $@ $< -# Misc file-specific graphics rules +### Misc file-specific graphics rules gfx/shrink1.2bpp: rgbgfx += -h gfx/shrink2.2bpp: rgbgfx += -h @@ -190,11 +193,13 @@ gfx/unknown/172f1f.2bpp: tools/gfx += --trim-whitespace %.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 $@ $< From 59a27d5907874a43662b1b95b84f8558abae0473 Mon Sep 17 00:00:00 2001 From: yenatch Date: Sun, 20 Aug 2017 13:34:26 -0400 Subject: [PATCH 6/8] tools/gfx: Replace --width with --png --- Makefile | 12 ++++++------ tools/gfx.c | 38 +++++++++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index aa3d73c17..208af0aed 100644 --- a/Makefile +++ b/Makefile @@ -138,16 +138,16 @@ gfx/mail/0b9cfe.1bpp: tools/gfx += --remove-whitespace 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/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 diff --git a/tools/gfx.c b/tools/gfx.c index 207dcb6c2..3e5624e95 100644 --- 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 @@ struct Options { 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 @@ void get_args(int argc, char *argv[]) { {"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 @@ void get_args(int argc, char *argv[]) { 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,6 +221,25 @@ void interleave(struct Graphic *graphic, int width) { 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); @@ -241,12 +260,13 @@ int main(int argc, char *argv[]) { 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); From 0aee932b5f83243027f03bbec8c1c0a9079bbf0a Mon Sep 17 00:00:00 2001 From: yenatch Date: Sun, 20 Aug 2017 13:43:04 -0400 Subject: [PATCH 7/8] Add warnings for tools --- tools/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/Makefile b/tools/Makefile index 3dd4fb62a..a4f691074 100644 --- 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 \ From 94577d95532368a940bf0bb9334ba2c7eb858eb5 Mon Sep 17 00:00:00 2001 From: yenatch Date: Sun, 24 Sep 2017 01:03:26 -0400 Subject: [PATCH 8/8] fix warnings in lzcomp and md5 --- tools/lzcomp.c | 2 +- tools/md5.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/lzcomp.c b/tools/lzcomp.c index c3fae1001..e0a88e44a 100644 --- a/tools/lzcomp.c +++ b/tools/lzcomp.c @@ -113,7 +113,7 @@ void write_command_to_file (FILE * fp, struct command command, const unsigned ch *(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"); diff --git a/tools/md5.c b/tools/md5.c index e043e8537..45562330a 100644 --- a/tools/md5.c +++ b/tools/md5.c @@ -32,7 +32,7 @@ static const uint32_t K[64] = { }; #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) {