Skip to content

Commit

Permalink
Slightly refactor some C tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Rangi42 committed Mar 9, 2022
1 parent e068f9c commit 1a6b051
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ clean: tidy
find gfx/pokemon -mindepth 1 ! -path "gfx/pokemon/unown/*" \( -name "bitmask.asm" -o -name "frames.asm" -o -name "front.animated.tilemap" -o -name "front.dimensions" \) -delete

tidy:
rm -f $(roms) $(pokecrystal_obj) $(pokecrystal11_obj) $(pokecrystal_au_obj) $(pokecrystal_debug_obj) $(pokecrystal11_debug_obj) $(roms:.gbc=.map) $(roms:.gbc=.sym) rgbdscheck.o
$(RM) $(roms) $(pokecrystal_obj) $(pokecrystal11_obj) $(pokecrystal_au_obj) $(pokecrystal_debug_obj) $(pokecrystal11_debug_obj) $(roms:.gbc=.map) $(roms:.gbc=.sym) rgbdscheck.o
$(MAKE) clean -C tools/

compare: $(roms)
Expand Down
3 changes: 2 additions & 1 deletion tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ tools := \
pokemon_animation_graphics \
scan_includes \
stadium

all: $(tools)
@:

clean:
rm -f $(tools)
$(RM) $(tools)

gfx: common.h
png_dimensions: common.h
Expand Down
15 changes: 4 additions & 11 deletions tools/gfx.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,12 @@ void remove_whitespace(struct Graphic *graphic) {
graphic->size &= ~(tile_size - 1);
int i = 0;
for (int j = 0, d = 0; i < graphic->size && j < graphic->size; i += tile_size, j += tile_size) {
while (j < graphic->size && is_whitespace(&graphic->data[j], tile_size) && !is_preserved(j / tile_size - d)) {
for (; j < graphic->size && is_whitespace(&graphic->data[j], tile_size) && !is_preserved(j / tile_size - d); j += tile_size, d++) {
shift_preserved(j / tile_size - d);
d++;
j += tile_size;
}
if (j >= graphic->size) {
break;
}
if (j > i) {
} else if (j > i) {
memcpy(&graphic->data[i], &graphic->data[j], tile_size);
}
}
Expand Down Expand Up @@ -170,13 +167,11 @@ void remove_duplicates(struct Graphic *graphic) {
graphic->size &= ~(tile_size - 1);
int num_tiles = 0;
for (int i = 0, j = 0, d = 0; i < graphic->size && j < graphic->size; i += tile_size, j += tile_size) {
while (j < graphic->size && tile_exists(&graphic->data[j], graphic->data, tile_size, num_tiles)) {
for (; j < graphic->size && tile_exists(&graphic->data[j], graphic->data, tile_size, num_tiles); j += tile_size, d++) {
if ((options.keep_whitespace && is_whitespace(&graphic->data[j], tile_size)) || is_preserved(j / tile_size - d)) {
break;
}
shift_preserved(j / tile_size - d);
d++;
j += tile_size;
}
if (j >= graphic->size) {
break;
Expand Down Expand Up @@ -227,13 +222,11 @@ void remove_flip(struct Graphic *graphic, bool xflip, bool yflip) {
graphic->size &= ~(tile_size - 1);
int num_tiles = 0;
for (int i = 0, j = 0, d = 0; i < graphic->size && j < graphic->size; i += tile_size, j += tile_size) {
while (j < graphic->size && flip_exists(&graphic->data[j], graphic->data, tile_size, num_tiles, xflip, yflip)) {
for (; j < graphic->size && flip_exists(&graphic->data[j], graphic->data, tile_size, num_tiles, xflip, yflip); j += tile_size, d++) {
if ((options.keep_whitespace && is_whitespace(&graphic->data[j], tile_size)) || is_preserved(j / tile_size - d)) {
break;
}
shift_preserved(j / tile_size - d);
d++;
j += tile_size;
}
if (j >= graphic->size) {
break;
Expand Down
28 changes: 13 additions & 15 deletions tools/scan_includes.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,35 +47,33 @@ void scan_file(const char *filename, bool strict) {
ptr = strchr(ptr, '\n');
if (!ptr) {
fprintf(stderr, "%s: no newline at end of file\n", filename);
break;
}
break;
case '"':
ptr++;
ptr = strchr(ptr, '"');
if (!ptr) {
if (ptr) {
ptr++;
} else {
fprintf(stderr, "%s: unterminated string\n", filename);
break;
}
ptr++;
break;
case 'I':
case 'i':
is_incbin = !strncmp(ptr, "INCBIN", 6) || !strncmp(ptr, "incbin", 6);
is_include = !strncmp(ptr, "INCLUDE", 7) || !strncmp(ptr, "include", 7);
if (is_incbin || is_include) {
ptr = strchr(ptr, '"');
if (!ptr) {
break;
}
ptr++;
char *include_path = ptr;
size_t length = strcspn(ptr, "\"");
ptr += length + 1;
include_path[length] = '\0';
printf("%s ", include_path);
if (is_include) {
scan_file(include_path, strict);
if (ptr) {
ptr++;
char *include_path = ptr;
size_t length = strcspn(ptr, "\"");
ptr += length + 1;
include_path[length] = '\0';
printf("%s ", include_path);
if (is_include) {
scan_file(include_path, strict);
}
}
}
break;
Expand Down

0 comments on commit 1a6b051

Please sign in to comment.