diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 000000000..a73b3c028 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +root = true + +[*] +indent_style = tab +indent_size = tab +tab_width = 4 +charset = utf-8 +insert_final_newline = true +trim_trailing_whitespace = true +end_of_line = lf diff --git a/tools/.clang-format b/tools/.clang-format new file mode 100644 index 000000000..86f251787 --- /dev/null +++ b/tools/.clang-format @@ -0,0 +1,45 @@ +BasedOnStyle: LLVM + +# Line length limit +ColumnLimit: 200 + +# Indentation +UseTab: AlignWithSpaces +IndentWidth: 4 +TabWidth: 4 +ContinuationIndentWidth: 4 +IndentCaseLabels: false +PenaltyIndentedWhitespace: 1 + +# Whitespace +MaxEmptyLinesToKeep: 1 +SpaceBeforeParens: ControlStatements +BreakBeforeTernaryOperators: true + +# Braces: keep opening brace on same line +BraceWrapping: + AfterControlStatement: false + AfterEnum: false + AfterFunction: false + AfterStruct: false + SplitEmptyFunction: false + +# Alignment +AlignAfterOpenBracket: BlockIndent +AlignArrayOfStructures: Left +AlignConsecutiveAssignments: None +AlignConsecutiveDeclarations: None + +# Single-line short constructs +AllowShortBlocksOnASingleLine: false +AllowShortEnumsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: false + +# Pointer style: "void *data" +PointerAlignment: Right +DerivePointerAlignment: false + +# Preprocessor +SortIncludes: false +AlignEscapedNewlines: DontAlign diff --git a/tools/Makefile b/tools/Makefile index 8882fcd1d..d9aed7798 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -1,4 +1,4 @@ -.PHONY: all clean +.PHONY: all clean format CC := gcc CFLAGS := -O3 -std=c11 -Wall -Wextra -pedantic @@ -15,5 +15,8 @@ all: $(tools) clean: $(RM) $(tools) +format: + clang-format -i $$(git ls-files '*.h' '*.c') + %: %.c common.h $(CC) $(CFLAGS) -o $@ $< diff --git a/tools/common.h b/tools/common.h index 2c690423b..0120b1260 100644 --- a/tools/common.h +++ b/tools/common.h @@ -120,8 +120,8 @@ uint32_t read_png_width(const char *filename) { xfread(header, sizeof(header), filename, f); static uint8_t expected_header[16] = { 0x89, 'P', 'N', 'G', '\r', '\n', 0x1A, '\n', // signature - 0, 0, 0, 13, // IHDR chunk length - 'I', 'H', 'D', 'R', // IHDR chunk type + 0, 0, 0, 13, // IHDR chunk length + 'I', 'H', 'D', 'R', // IHDR chunk type }; if (memcmp(header, expected_header, sizeof(header))) { fclose(f); diff --git a/tools/gfx.c b/tools/gfx.c index 4264e669c..f844ce7f3 100644 --- a/tools/gfx.c +++ b/tools/gfx.c @@ -1,5 +1,8 @@ #define PROGRAM_NAME "gfx" -#define USAGE_OPTS "[-h|--help] [--trim-whitespace] [--remove-whitespace] [--interleave] [--remove-duplicates [--keep-whitespace]] [--remove-xflip] [--remove-yflip] [--preserve indexes] [-d|--depth depth] [-p|--png filename.png] [-o|--out outfile] infile" +#define USAGE_OPTS \ + "[-h|--help] [--trim-whitespace] [--remove-whitespace] [--interleave] " \ + "[--remove-duplicates [--keep-whitespace]] [--remove-xflip] [--remove-yflip] " \ + "[--preserve indexes] [-d|--depth depth] [-p|--png filename.png] [-o|--out outfile] infile" #include "common.h" @@ -34,7 +37,7 @@ void parse_args(int argc, char *argv[]) { {"depth", required_argument, 0, 'd'}, {"out", required_argument, 0, 'o'}, {"help", no_argument, 0, 'h'}, - {0} + {0}, }; for (int opt; (opt = getopt_long(argc, argv, "d:o:p:h", long_options)) != -1;) { switch (opt) { @@ -62,7 +65,7 @@ void parse_args(int argc, char *argv[]) { case 'r': for (char *token = strtok(optarg, ","); token; token = strtok(NULL, ",")) { options.preserved = xrealloc(options.preserved, ++options.num_preserved * sizeof(*options.preserved)); - options.preserved[options.num_preserved-1] = strtoul(token, NULL, 0); + options.preserved[options.num_preserved - 1] = strtoul(token, NULL, 0); } break; case 'd': @@ -188,6 +191,7 @@ void remove_duplicates(struct Graphic *graphic) { // for (int bit = 0; bit < 8; bit++) { // flipped[i] |= ((i >> bit) & 1) << (7 - bit); const uint8_t flipped[256] = { + // clang-format off 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, @@ -203,7 +207,8 @@ const uint8_t flipped[256] = { 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, - 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff + 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff, + // clang-format on }; bool flip_exists(const uint8_t *tile, const uint8_t *tiles, int tile_size, int num_tiles, bool xflip, bool yflip) { diff --git a/tools/make_patch.c b/tools/make_patch.c index 0d874fdcb..98c87e381 100644 --- a/tools/make_patch.c +++ b/tools/make_patch.c @@ -50,9 +50,8 @@ void symbol_append(struct Symbol **symbols, const char *name, int bank, int addr size_t name_len = strlen(name) + 1; struct Symbol *symbol = xmalloc(sizeof(*symbol) + name_len); symbol->address = address; - symbol->offset = address < 0x8000 - ? (bank > 0 ? address + (bank - 1) * 0x4000 : address) // ROM addresses are relative to their bank - : address - 0x8000; // RAM addresses are relative to the start of all RAM + symbol->offset = address < 0x8000 ? (bank > 0 ? address + (bank - 1) * 0x4000 : address) // ROM addresses are relative to their bank + : address - 0x8000; // RAM addresses are relative to the start of all RAM memcpy(symbol->name, name, name_len); symbol->next = *symbols; *symbols = symbol; @@ -117,7 +116,12 @@ struct Symbol *parse_symbols(const char *filename) { FILE *file = xfopen(filename, 'r'); struct Buffer *buffer = buffer_create(1); - enum { SYM_PRE, SYM_VALUE, SYM_SPACE, SYM_NAME } state = SYM_PRE; + enum { + SYM_PRE, + SYM_VALUE, + SYM_SPACE, + SYM_NAME, + } state = SYM_PRE; int bank = 0; int address = 0; struct Symbol *symbols = NULL; @@ -184,7 +188,11 @@ int parse_arg_value(const char *arg, bool absolute, const struct Symbol *symbols } // Symbols may take the low or high part - enum { SYM_WHOLE, SYM_LOW, SYM_HIGH } part = SYM_WHOLE; + enum { + SYM_WHOLE, + SYM_LOW, + SYM_HIGH, + } part = SYM_WHOLE; if (arg[0] == '<') { part = SYM_LOW; arg++; @@ -209,7 +217,17 @@ int parse_arg_value(const char *arg, bool absolute, const struct Symbol *symbols return part == SYM_LOW ? value & 0xff : part == SYM_HIGH ? value >> 8 : value; } -void interpret_command(char *command, const struct Symbol *current_hook, const struct Symbol *symbols, struct Buffer *patches, FILE *restrict new_rom, FILE *restrict orig_rom, FILE *restrict output) { +void interpret_command( + // clang-format off + char *command, + const struct Symbol *current_hook, + const struct Symbol *symbols, + struct Buffer *patches, + FILE *restrict new_rom, + FILE *restrict orig_rom, + FILE *restrict output + // clang-format on +) { // Strip all leading spaces and all but one trailing space int x = 0; for (int i = 0; command[i]; i++) { @@ -301,7 +319,7 @@ void interpret_command(char *command, const struct Symbol *current_hook, const s if (i) { putc(' ', output); } - fprintf(output, isupper((unsigned)command[0]) ? "%02X %02X": "%02x %02x", value & 0xff, value >> 8); + fprintf(output, isupper((unsigned)command[0]) ? "%02X %02X" : "%02x %02x", value & 0xff, value >> 8); } } else if (vstrfind(command, "db", "DB", "db_", "DB_", "db/", "DB/") >= 0) { @@ -350,6 +368,7 @@ void skip_to_next_line(FILE *restrict input, FILE *restrict output) { } struct Buffer *process_template( + // clang-format off const char *template_filename, const char *patch_filename, FILE *restrict new_rom, @@ -357,6 +376,7 @@ struct Buffer *process_template( const struct Symbol *symbols, unsigned int ignore_addr, unsigned int ignore_size + // clang-format on ) { FILE *input = xfopen(template_filename, 'r'); FILE *output = xfopen(patch_filename, 'w'); @@ -445,7 +465,7 @@ int compare_patch(const void *patch1, const void *patch2) { bool verify_completeness(FILE *restrict orig_rom, FILE *restrict new_rom, struct Buffer *patches) { qsort(patches->data, patches->size, patches->item_size, compare_patch); - for (unsigned int offset = 0, index = 0; ; offset++) { + for (unsigned int offset = 0, index = 0;; offset++) { int orig_byte = getc(orig_rom); int new_byte = getc(new_rom); if (orig_byte == EOF || new_byte == EOF) { @@ -475,7 +495,7 @@ void parse_args(int argc, char *argv[], unsigned int *ignore_addr, unsigned int struct option long_options[] = { {"ignore", required_argument, 0, 'i'}, {"help", no_argument, 0, 'h'}, - {0} + {0}, }; for (int opt; (opt = getopt_long(argc, argv, "h", long_options)) != -1;) { switch (opt) { diff --git a/tools/pkmncompress.c b/tools/pkmncompress.c index 60d6fd0f0..a35b5bd58 100644 --- a/tools/pkmncompress.c +++ b/tools/pkmncompress.c @@ -7,7 +7,7 @@ void parse_args(int argc, char *argv[], bool *uncomp) { struct option long_options[] = { {"uncompress", no_argument, 0, 'u'}, {"help", no_argument, 0, 'h'}, - {0} + {0}, }; for (int opt; (opt = getopt_long(argc, argv, "uh", long_options)) != -1;) { switch (opt) { @@ -239,8 +239,10 @@ int read_int(uint8_t *data, int count) { uint8_t *fill_plane(uint8_t *data, int width) { static int table[0x10] = { + // clang-format off 0x0001, 0x0003, 0x0007, 0x000F, 0x001F, 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, + // clang-format on }; int mode = read_bit(data); int size = width * width * 0x20; diff --git a/tools/scan_includes.c b/tools/scan_includes.c index 9ec5f3c5d..82f75d3ec 100644 --- a/tools/scan_includes.c +++ b/tools/scan_includes.c @@ -9,7 +9,7 @@ void parse_args(int argc, char *argv[], bool *strict) { struct option long_options[] = { {"strict", no_argument, 0, 's'}, {"help", no_argument, 0, 'h'}, - {0} + {0}, }; for (int opt; (opt = getopt_long(argc, argv, "sh", long_options)) != -1;) { switch (opt) {