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
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:
@ -79,9 +77,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
@ -101,7 +102,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 $@ $<
@ -122,7 +123,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
@ -139,17 +140,17 @@ 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/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 @@ gfx/unknown/171db1.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 $@ $<

View File

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

View File

@ -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);

View File

@ -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");

View File

@ -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) {

View File

@ -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);
}

View File

@ -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;

View File

@ -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) {