Merge pull request #379 from yenatch/tools

Refactor scan_includes and add warnings for tools
This commit is contained in:
yenatch 2017-09-24 01:25:03 -04:00 committed by GitHub
commit 01caa369ec
8 changed files with 103 additions and 89 deletions

View File

@ -36,11 +36,9 @@ all: crystal
crystal: pokecrystal.gbc crystal: pokecrystal.gbc
crystal11: pokecrystal11.gbc crystal11: pokecrystal11.gbc
# Ensure that the tools are built when making the ROM # Build tools when building the rom
ifneq ($(MAKECMDGOALS),clean) ifeq (,$(filter clean tools,$(MAKECMDGOALS)))
ifneq ($(MAKECMDGOALS),tools) Makefile: tools ;
Makefile: tools
endif
endif endif
clean: clean:
@ -79,9 +77,12 @@ pokecrystal.gbc: $(crystal_obj)
%.lz: hash = $(shell tools/md5 $(*D)/$(*F) | sed "s/\(.\{8\}\).*/\1/") %.lz: hash = $(shell tools/md5 $(*D)/$(*F) | sed "s/\(.\{8\}\).*/\1/")
%.lz: % %.lz: %
$(eval filename := $@.$(hash)) $(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. # 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 gfx/pics/dewgong/frames.asm: gfx/pics/dewgong/front.animated.tilemap gfx/pics/dewgong/front.dimensions
@ -101,7 +102,7 @@ gfx/pics/girafarig/front.animated.tilemap: gfx/pics/girafarig/front.2bpp gfx/pic
tools/pokemon_animation_graphics --girafarig -t $@ $^ tools/pokemon_animation_graphics --girafarig -t $@ $^
# Pokemon pic graphics rules ### Pokemon pic graphics rules
gfx/pics/%/normal.gbcpal: gfx/pics/%/front.png gfx/pics/%/normal.gbcpal: gfx/pics/%/front.png
rgbgfx -p $@ $< rgbgfx -p $@ $<
@ -122,7 +123,7 @@ gfx/pics/%/front.animated.tilemap: gfx/pics/%/front.2bpp gfx/pics/%/front.dimens
# rgbgfx -o $@ $< # rgbgfx -o $@ $<
# Misc file-specific graphics rules ### Misc file-specific graphics rules
gfx/shrink1.2bpp: rgbgfx += -h gfx/shrink1.2bpp: rgbgfx += -h
gfx/shrink2.2bpp: rgbgfx += -h gfx/shrink2.2bpp: rgbgfx += -h
@ -139,17 +140,17 @@ gfx/mail/0b9cfe.1bpp: tools/gfx += --remove-whitespace
gfx/pokedex/%.2bpp: tools/gfx += --trim-whitespace gfx/pokedex/%.2bpp: tools/gfx += --trim-whitespace
gfx/title/crystal.2bpp: tools/gfx += --interleave --width=48 gfx/title/crystal.2bpp: tools/gfx += --interleave --png=$<
gfx/title/old_fg.2bpp: tools/gfx += --interleave --width=64 gfx/title/old_fg.2bpp: tools/gfx += --interleave --png=$<
gfx/title/logo.2bpp: rgbgfx += -x 4 gfx/title/logo.2bpp: rgbgfx += -x 4
gfx/trade/ball.2bpp: tools/gfx += --remove-whitespace gfx/trade/ball.2bpp: tools/gfx += --remove-whitespace
gfx/trade/game_boy_n64.2bpp: tools/gfx += --trim-whitespace gfx/trade/game_boy_n64.2bpp: tools/gfx += --trim-whitespace
gfx/slots_2.2bpp: tools/gfx += --interleave --width=16 gfx/slots_2.2bpp: tools/gfx += --interleave --png=$<
gfx/slots_3.2bpp: tools/gfx += --interleave --width=24 --remove-duplicates --keep-whitespace --remove-xflip gfx/slots_3.2bpp: tools/gfx += --interleave --png=$< --remove-duplicates --keep-whitespace --remove-xflip
gfx/slots_3a.2bpp: tools/gfx += --interleave --width=16 gfx/slots_3a.2bpp: tools/gfx += --interleave --png=$<
gfx/slots_3b.2bpp: tools/gfx += --interleave --width=24 --remove-duplicates --keep-whitespace --remove-xflip gfx/slots_3b.2bpp: tools/gfx += --interleave --png=$< --remove-duplicates --keep-whitespace --remove-xflip
gfx/fx/angels.2bpp: tools/gfx += --trim-whitespace gfx/fx/angels.2bpp: tools/gfx += --trim-whitespace
gfx/fx/beam.2bpp: tools/gfx += --remove-xflip --remove-yflip --remove-whitespace gfx/fx/beam.2bpp: tools/gfx += --remove-xflip --remove-yflip --remove-whitespace
@ -194,11 +195,13 @@ gfx/unknown/171db1.2bpp: tools/gfx += --trim-whitespace
%.2bpp: %.png %.2bpp: %.png
rgbgfx $(rgbgfx) -o $@ $< rgbgfx $(rgbgfx) -o $@ $<
$(if $(tools/gfx),tools/gfx $(tools/gfx) -o $@ $@) $(if $(tools/gfx),\
tools/gfx $(tools/gfx) -o $@ $@)
%.1bpp: %.png %.1bpp: %.png
rgbgfx $(rgbgfx) -d1 -o $@ $< rgbgfx $(rgbgfx) -d1 -o $@ $<
$(if $(tools/gfx),tools/gfx $(tools/gfx) -d1 -o $@ $@) $(if $(tools/gfx),\
tools/gfx $(tools/gfx) -d1 -o $@ $@)
%.tilemap: %.png %.tilemap: %.png
rgbgfx -t $@ $< rgbgfx -t $@ $<

View File

@ -1,7 +1,7 @@
.PHONY: all clean .PHONY: all clean
CC := gcc CC := gcc
CFLAGS := -std=c99 CFLAGS := -std=c99 -Wall -Wextra
tools := \ tools := \
lzcomp \ lzcomp \

View File

@ -8,7 +8,7 @@
#include "common.h" #include "common.h"
static void usage(void) { 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) { static void error(char *message) {
@ -23,11 +23,11 @@ struct Options {
char *outfile; char *outfile;
int depth; int depth;
int interleave; int interleave;
int width;
int remove_duplicates; int remove_duplicates;
int keep_whitespace; int keep_whitespace;
int remove_xflip; int remove_xflip;
int remove_yflip; int remove_yflip;
char *png_file;
}; };
struct Options Options = { struct Options Options = {
@ -43,13 +43,13 @@ void get_args(int argc, char *argv[]) {
{"keep-whitespace", no_argument, &Options.keep_whitespace, 1}, {"keep-whitespace", no_argument, &Options.keep_whitespace, 1},
{"remove-xflip", no_argument, &Options.remove_xflip, 1}, {"remove-xflip", no_argument, &Options.remove_xflip, 1},
{"remove-yflip", no_argument, &Options.remove_yflip, 1}, {"remove-yflip", no_argument, &Options.remove_yflip, 1},
{"width", required_argument, 0, 'w'}, {"png", required_argument, 0, 'p'},
{"depth", required_argument, 0, 'd'}, {"depth", required_argument, 0, 'd'},
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 'h'},
{0} {0}
}; };
for (int opt = 0; opt != -1;) { 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': case 'h':
Options.help = true; Options.help = true;
break; break;
@ -59,8 +59,8 @@ void get_args(int argc, char *argv[]) {
case 'd': case 'd':
Options.depth = strtoul(optarg, NULL, 0); Options.depth = strtoul(optarg, NULL, 0);
break; break;
case 'w': case 'p':
Options.width = strtoul(optarg, NULL, 0); Options.png_file = optarg;
break; break;
case 0: case 0:
case -1: case -1:
@ -221,6 +221,25 @@ void interleave(struct Graphic *graphic, int width) {
free(interleaved); 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[]) { int main(int argc, char *argv[]) {
get_args(argc, argv); get_args(argc, argv);
@ -241,12 +260,13 @@ int main(int argc, char *argv[]) {
trim_whitespace(&graphic); trim_whitespace(&graphic);
} }
if (Options.interleave) { if (Options.interleave) {
if (!Options.width) { if (!Options.png_file) {
error("interleave: must set --width to a nonzero value"); error("interleave: need --png to infer dimensions");
usage(); usage();
exit(1); exit(1);
} }
interleave(&graphic, Options.width); int width = png_get_width(Options.png_file);
interleave(&graphic, width);
} }
if (Options.remove_duplicates) { if (Options.remove_duplicates) {
remove_duplicates(&graphic); remove_duplicates(&graphic);

View File

@ -113,7 +113,7 @@ void write_command_to_file (FILE * fp, struct command command, const unsigned ch
*(pos ++) = command.value; *(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; if (command.command) return;
command.count ++; command.count ++;
if (fwrite(input_stream + command.value, 1, command.count, fp) != command.count) error_exit(1, "could not write data to compressed output"); if (fwrite(input_stream + command.value, 1, command.count, fp) != command.count) error_exit(1, "could not write data to compressed output");

View File

@ -32,7 +32,7 @@ static const uint32_t K[64] = {
}; };
#define rotate_left_32(value, by) \ #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) { void md5_wikipedia(uint8_t *data, int length, uint8_t *result) {

View File

@ -3,7 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
void usage(void) { void usage(void) {
fprintf(stderr, "Usage: png_dimensions infile, outfile\n"); fprintf(stderr, "Usage: png_dimensions infile outfile\n");
exit(1); exit(1);
} }

View File

@ -162,13 +162,11 @@ void create_tilemap(struct Tilemap* tilemap, struct Graphic* graphic, char* grap
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
int opt;
char* dimensions_filename; char* dimensions_filename;
char* graphics_filename; char* graphics_filename;
char* outfile = NULL; char* outfile = NULL;
char* mapfile = NULL; char* mapfile = NULL;
FILE* f; FILE* f;
long size;
uint8_t bytes[1]; uint8_t bytes[1];
int width; int width;
int height; int height;

View File

@ -19,16 +19,8 @@ struct Options {
struct Options Options = {0}; struct Options Options = {0};
void scan_file(char* filename) { void scan_file(char* filename) {
FILE* f; FILE *f = fopen(filename, "rb");
long size;
char* orig;
char* buffer;
char* include;
int length;
f = fopen(filename, "r");
if (!f) { if (!f) {
if (Options.strict) { if (Options.strict) {
fprintf(stderr, "Could not open file: '%s'\n", filename); fprintf(stderr, "Could not open file: '%s'\n", filename);
@ -39,38 +31,40 @@ void scan_file(char* filename) {
} }
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
size = ftell(f); long size = ftell(f);
rewind(f); rewind(f);
buffer = malloc(size + 1); char *buffer = malloc(size + 1);
orig = buffer; char *orig = buffer;
fread(buffer, 1, size, f); size = fread(buffer, 1, size, f);
buffer[size] = '\0'; buffer[size] = '\0';
fclose(f); fclose(f);
for (; buffer && (buffer - orig < size); buffer++) { for (; buffer && (buffer - orig < size); buffer++) {
if (buffer[0] == ';') { bool is_include = false;
bool is_incbin = false;
switch (*buffer) {
case ';':
buffer = strchr(buffer, '\n'); buffer = strchr(buffer, '\n');
if (!buffer) { if (!buffer) {
fprintf(stderr, "%s: no newline at end of file\n", filename); fprintf(stderr, "%s: no newline at end of file\n", filename);
}
break; break;
}
continue; case 'i':
} case 'I':
bool is_include = false;
bool is_incbin = false;
if ((strncmp(buffer, "INCBIN", 6) == 0) || (strncmp(buffer, "incbin", 6) == 0)) { if ((strncmp(buffer, "INCBIN", 6) == 0) || (strncmp(buffer, "incbin", 6) == 0)) {
is_incbin = true; is_incbin = true;
} else if ((strncmp(buffer, "INCLUDE", 7) == 0) || (strncmp(buffer, "include", 7) == 0)) { } else if ((strncmp(buffer, "INCLUDE", 7) == 0) || (strncmp(buffer, "include", 7) == 0)) {
is_include = true; is_include = true;
} }
if (is_incbin || is_include) { if (is_incbin || is_include) {
buffer = strchr(buffer, '"') + 1; buffer = strchr(buffer, '"');
if (!buffer) { if (!buffer++) {
break; break;
} }
length = strcspn(buffer, "\""); int length = strcspn(buffer, "\"");
include = malloc(length + 1); char *include = malloc(length + 1);
strncpy(include, buffer, length); strncpy(include, buffer, length);
include[length] = '\0'; include[length] = '\0';
printf("%s ", include); printf("%s ", include);
@ -78,26 +72,29 @@ void scan_file(char* filename) {
scan_file(include); scan_file(include);
} }
free(include); free(include);
buffer = strchr(buffer, '"');
} }
break;
}
if (!buffer) {
break;
}
} }
free(orig); free(orig);
} }
void get_args(int argc, char *argv[]) { int main(int argc, char* argv[]) {
while (1) { int i = 0;
struct option long_options[] = { struct option long_options[] = {
{"strict", no_argument, 0, 's'}, {"strict", no_argument, 0, 's'},
{"help", no_argument, 0, 'h'}, {"help", no_argument, 0, 'h'},
{0} {0}
}; };
int i = 0; int opt = -1;
int opt = getopt_long(argc, argv, "sh", long_options, &i); while ((opt = getopt_long(argc, argv, "sh", long_options, &i)) != -1) {
if (opt == -1) {
break;
}
switch (opt) { switch (opt) {
case 's': case 's':
Options.strict = true; Options.strict = true;
@ -111,10 +108,6 @@ void get_args(int argc, char *argv[]) {
break; break;
} }
} }
}
int main(int argc, char* argv[]) {
get_args(argc, argv);
argc -= optind; argc -= optind;
argv += optind; argv += optind;
if (Options.help) { if (Options.help) {