Remove independent tools and link to pokemon-asm-tools
palfix.py still exists since it is often needed
This commit is contained in:
@@ -20,6 +20,7 @@ To set up the repository, see [INSTALL.md](INSTALL.md).
|
|||||||
- [**Documentation**][docs]
|
- [**Documentation**][docs]
|
||||||
- [**Wiki**][wiki] (includes [tutorials][tutorials])
|
- [**Wiki**][wiki] (includes [tutorials][tutorials])
|
||||||
- [**Symbols**][symbols]
|
- [**Symbols**][symbols]
|
||||||
|
- [**Tools**][tools]
|
||||||
|
|
||||||
You can find us on [Discord (pret, #pokecrystal)](https://discord.gg/d5dubZ3).
|
You can find us on [Discord (pret, #pokecrystal)](https://discord.gg/d5dubZ3).
|
||||||
|
|
||||||
@@ -29,5 +30,6 @@ For other pret projects, see [pret.github.io](https://pret.github.io/).
|
|||||||
[wiki]: https://github.com/pret/pokecrystal/wiki
|
[wiki]: https://github.com/pret/pokecrystal/wiki
|
||||||
[tutorials]: https://github.com/pret/pokecrystal/wiki/Tutorials
|
[tutorials]: https://github.com/pret/pokecrystal/wiki/Tutorials
|
||||||
[symbols]: https://github.com/pret/pokecrystal/tree/symbols
|
[symbols]: https://github.com/pret/pokecrystal/tree/symbols
|
||||||
|
[tools]: https://github.com/pret/pokemon-asm-tools
|
||||||
[ci]: https://github.com/pret/pokecrystal/actions
|
[ci]: https://github.com/pret/pokecrystal/actions
|
||||||
[ci-badge]: https://github.com/pret/pokecrystal/actions/workflows/main.yml/badge.svg
|
[ci-badge]: https://github.com/pret/pokecrystal/actions/workflows/main.yml/badge.svg
|
||||||
|
1
tools/.gitignore
vendored
1
tools/.gitignore
vendored
@@ -1,4 +1,3 @@
|
|||||||
bpp2png
|
|
||||||
gfx
|
gfx
|
||||||
lzcomp
|
lzcomp
|
||||||
make_patch
|
make_patch
|
||||||
|
@@ -4,7 +4,6 @@ CC := gcc
|
|||||||
CFLAGS := -O3 -flto -std=c11 -Wall -Wextra -pedantic
|
CFLAGS := -O3 -flto -std=c11 -Wall -Wextra -pedantic
|
||||||
|
|
||||||
tools := \
|
tools := \
|
||||||
bpp2png \
|
|
||||||
lzcomp \
|
lzcomp \
|
||||||
gfx \
|
gfx \
|
||||||
make_patch \
|
make_patch \
|
||||||
@@ -27,9 +26,6 @@ pokemon_animation_graphics: common.h
|
|||||||
scan_includes: common.h
|
scan_includes: common.h
|
||||||
stadium: common.h
|
stadium: common.h
|
||||||
|
|
||||||
bpp2png: bpp2png.c lodepng/lodepng.c common.h lodepng/lodepng.h
|
|
||||||
$(CC) $(CFLAGS) -o $@ bpp2png.c lodepng/lodepng.c
|
|
||||||
|
|
||||||
lzcomp: CFLAGS += -Wno-strict-overflow -Wno-sign-compare
|
lzcomp: CFLAGS += -Wno-strict-overflow -Wno-sign-compare
|
||||||
lzcomp: $(wildcard lz/*.c) $(wildcard lz/*.h)
|
lzcomp: $(wildcard lz/*.c) $(wildcard lz/*.h)
|
||||||
$(CC) $(CFLAGS) -o $@ lz/*.c
|
$(CC) $(CFLAGS) -o $@ lz/*.c
|
||||||
|
243
tools/bpp2png.c
243
tools/bpp2png.c
@@ -1,243 +0,0 @@
|
|||||||
#define PROGRAM_NAME "bpp2png"
|
|
||||||
#define USAGE_OPTS "[-h|--help] [-w width] [-d depth] [-p in.gbcpal] [-t] in.2bpp|in.1bpp out.png"
|
|
||||||
|
|
||||||
#include "common.h"
|
|
||||||
#include "lodepng/lodepng.h"
|
|
||||||
|
|
||||||
struct Options {
|
|
||||||
unsigned int width;
|
|
||||||
unsigned int depth;
|
|
||||||
const char *palette;
|
|
||||||
bool transpose;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef uint8_t Palette[4][3];
|
|
||||||
|
|
||||||
void parse_args(int argc, char *argv[], struct Options *options) {
|
|
||||||
struct option long_options[] = {
|
|
||||||
{"width", required_argument, 0, 'w'},
|
|
||||||
{"depth", required_argument, 0, 'd'},
|
|
||||||
{"palette", required_argument, 0, 'p'},
|
|
||||||
{"transpose", no_argument, 0, 't'},
|
|
||||||
{"help", no_argument, 0, 'h'},
|
|
||||||
{0}
|
|
||||||
};
|
|
||||||
for (int opt; (opt = getopt_long(argc, argv, "w:d:p:th", long_options)) != -1;) {
|
|
||||||
switch (opt) {
|
|
||||||
case 'w':
|
|
||||||
options->width = (unsigned int)strtoul(optarg, NULL, 0);
|
|
||||||
if (options->width % 8) {
|
|
||||||
error_exit("Width not divisible by 8 px: %u\n", options->width);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'd':
|
|
||||||
options->depth = (unsigned int)strtoul(optarg, NULL, 0);
|
|
||||||
if (options->depth != 1 && options->depth != 2) {
|
|
||||||
error_exit("Depth is not 1 or 2: %u\n", options->depth);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 'p':
|
|
||||||
options->palette = optarg;
|
|
||||||
break;
|
|
||||||
case 't':
|
|
||||||
options->transpose = true;
|
|
||||||
break;
|
|
||||||
case 'h':
|
|
||||||
usage_exit(0);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
usage_exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool is_1bpp(const char *filename) {
|
|
||||||
const char *end = strrchr(filename, '.');
|
|
||||||
return end && strlen(end) == 5 && end[1] == '1' && (end[2] == 'b' || end[2] == 'B')
|
|
||||||
&& (end[3] == 'p' || end[3] == 'P') && (end[4] == 'p' || end[4] == 'P');
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t *extend_1bpp_to_2bpp(uint8_t *bpp_data, long *size) {
|
|
||||||
*size *= 2;
|
|
||||||
bpp_data = xrealloc(bpp_data, *size);
|
|
||||||
for (long i = *size - 2; i >= 0; i -= 2) {
|
|
||||||
bpp_data[i] = bpp_data[i + 1] = bpp_data[i >> 1];
|
|
||||||
}
|
|
||||||
return bpp_data;
|
|
||||||
}
|
|
||||||
|
|
||||||
void mingle_2bpp_planes(uint8_t *bpp_data, long size) {
|
|
||||||
for (long i = 0; i < size; i += 2) {
|
|
||||||
// Interleave aka "mingle" bits
|
|
||||||
// <https://graphics.stanford.edu/~seander/bithacks.html#Interleave64bitOps>
|
|
||||||
#define EXPAND_PLANE(b) (((((b) * 0x0101010101010101ULL & 0x8040201008040201ULL) * 0x0102040810204081ULL) >> 48) & 0xAAAA)
|
|
||||||
uint16_t r = (EXPAND_PLANE(bpp_data[i]) >> 1) | EXPAND_PLANE(bpp_data[i + 1]);
|
|
||||||
bpp_data[i] = r >> 8;
|
|
||||||
bpp_data[i + 1] = r & 0xff;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int calculate_size(long bytes, unsigned int *width) {
|
|
||||||
long pixels = bytes * 4;
|
|
||||||
if (pixels % (8 * 8)) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
if (!*width) {
|
|
||||||
// If no width is specified, try to guess an appropriate one
|
|
||||||
#define GUESS_SIZE(w, h) pixels == (w) * (h) * 8 * 8 ? (w) * 8
|
|
||||||
*width = GUESS_SIZE(5, 5) // mon pic
|
|
||||||
: GUESS_SIZE(6, 6) // mon front/back pic
|
|
||||||
: GUESS_SIZE(7, 7) // mon/trainer pic
|
|
||||||
: GUESS_SIZE(2, 4) // mon icon
|
|
||||||
: GUESS_SIZE(2, 12) // walking sprite
|
|
||||||
: GUESS_SIZE(2, 6) // standing sprite
|
|
||||||
: GUESS_SIZE(2, 2) // still sprite
|
|
||||||
: GUESS_SIZE(4, 4) // big sprite
|
|
||||||
: pixels > 16 * 8 * 8 ? 16 * 8 // maximum width
|
|
||||||
: (unsigned int)(pixels / 8);
|
|
||||||
}
|
|
||||||
unsigned int height = (unsigned int)((pixels + *width * 8 - 1) / (*width * 8) * 8);
|
|
||||||
if (*width == 0 || height == 0) {
|
|
||||||
error_exit("Not divisible into 8x8-px tiles: %ux%u\n", *width, height);
|
|
||||||
}
|
|
||||||
return height;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t *pad_to_rectangle(uint8_t *bpp_data, long filesize, unsigned int width, unsigned int height) {
|
|
||||||
unsigned int size = width * height / 4;
|
|
||||||
if (size > filesize) {
|
|
||||||
bpp_data = xrealloc(bpp_data, size);
|
|
||||||
// Fill padding with white
|
|
||||||
memset(bpp_data + filesize, 0, size - filesize);
|
|
||||||
}
|
|
||||||
return bpp_data;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t *transpose_tiles(uint8_t *bpp_data, unsigned int width, unsigned int height) {
|
|
||||||
unsigned int size = width * height / 4;
|
|
||||||
uint8_t *transposed = xmalloc(size);
|
|
||||||
unsigned int cols = width / 8;
|
|
||||||
for (unsigned int i = 0; i < size; i++) {
|
|
||||||
unsigned int j = (i / 0x10) * cols * 0x10;
|
|
||||||
j = (j % size) + 0x10 * (j / size) + (i % 0x10);
|
|
||||||
transposed[j] = bpp_data[i];
|
|
||||||
}
|
|
||||||
free(bpp_data);
|
|
||||||
return transposed;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t *rearrange_tiles_to_scanlines(uint8_t *bpp_data, unsigned int width, unsigned int height) {
|
|
||||||
unsigned int size = width * height / 4;
|
|
||||||
uint8_t *rearranged = xmalloc(size);
|
|
||||||
unsigned int cols = width / 8;
|
|
||||||
unsigned int j = 0;
|
|
||||||
for (unsigned int line = 0; line < height; line++) {
|
|
||||||
unsigned int row = line / 8;
|
|
||||||
for (unsigned int col = 0; col < cols; col++) {
|
|
||||||
unsigned int i = ((row * cols + col) * 8 + line % 8) * 2;
|
|
||||||
rearranged[j] = bpp_data[i];
|
|
||||||
rearranged[j + 1] = bpp_data[i + 1];
|
|
||||||
j += 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
free(bpp_data);
|
|
||||||
return rearranged;
|
|
||||||
}
|
|
||||||
|
|
||||||
void read_gbcpal(Palette palette, const char *filename) {
|
|
||||||
long filesize;
|
|
||||||
uint8_t *pal_data = read_u8(filename, &filesize);
|
|
||||||
if (filesize < 4 * 2) {
|
|
||||||
error_exit("Invalid .gbcpal file: \"%s\"\n", filename);
|
|
||||||
}
|
|
||||||
for (int i = 0; i < 4; i++) {
|
|
||||||
uint8_t b1 = pal_data[i * 2], b2 = pal_data[i * 2 + 1];
|
|
||||||
#define RGB5_TO_RGB8(x) (uint8_t)(((x) << 3) | ((x) >> 2))
|
|
||||||
palette[i][0] = RGB5_TO_RGB8(b1 & 0x1f); // red
|
|
||||||
palette[i][1] = RGB5_TO_RGB8(((b1 & 0xe0) >> 5) | ((b2 & 0x03) << 3)); // green
|
|
||||||
palette[i][2] = RGB5_TO_RGB8((b2 & 0x7c) >> 2); // blue
|
|
||||||
}
|
|
||||||
free(pal_data);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int write_png(const char *filename, uint8_t *bpp_data, unsigned int depth,
|
|
||||||
unsigned int width, unsigned int height, Palette palette) {
|
|
||||||
LodePNGState state;
|
|
||||||
lodepng_state_init(&state);
|
|
||||||
state.encoder.auto_convert = 0; // Always use the colortype we specify
|
|
||||||
state.info_raw.bitdepth = state.info_png.color.bitdepth = depth;
|
|
||||||
|
|
||||||
if (palette) {
|
|
||||||
state.info_raw.colortype = state.info_png.color.colortype = LCT_PALETTE;
|
|
||||||
for (int i = 0; i < 4; i++) {
|
|
||||||
uint8_t *color = palette[i];
|
|
||||||
lodepng_palette_add(&state.info_raw, color[0], color[1], color[2], 0xff);
|
|
||||||
lodepng_palette_add(&state.info_png.color, color[0], color[1], color[2], 0xff);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
state.info_raw.colortype = state.info_png.color.colortype = LCT_GREY;
|
|
||||||
// 2-bit PNG white/light/dark/gray indexes are the inverse of 2bpp
|
|
||||||
for (unsigned int i = 0; i < width * height / 4; i++) {
|
|
||||||
bpp_data[i] = ~bpp_data[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char *buffer;
|
|
||||||
size_t buffer_size;
|
|
||||||
lodepng_encode(&buffer, &buffer_size, bpp_data, width, height, &state);
|
|
||||||
unsigned int error = state.error;
|
|
||||||
lodepng_state_cleanup(&state);
|
|
||||||
if (!error) {
|
|
||||||
error = lodepng_save_file(buffer, buffer_size, filename);
|
|
||||||
}
|
|
||||||
|
|
||||||
free(buffer);
|
|
||||||
return error;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
|
||||||
struct Options options = {0};
|
|
||||||
parse_args(argc, argv, &options);
|
|
||||||
|
|
||||||
argc -= optind;
|
|
||||||
argv += optind;
|
|
||||||
if (argc != 2) {
|
|
||||||
usage_exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
Palette palette = {0};
|
|
||||||
if (options.palette) {
|
|
||||||
read_gbcpal(palette, options.palette);
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *bpp_filename = argv[0];
|
|
||||||
long filesize;
|
|
||||||
uint8_t *bpp_data = read_u8(bpp_filename, &filesize);
|
|
||||||
if (!options.depth) {
|
|
||||||
options.depth = is_1bpp(bpp_filename) ? 1 : 2;
|
|
||||||
}
|
|
||||||
if (!filesize || filesize % (8 * options.depth)) {
|
|
||||||
error_exit("Invalid .%dbpp file: \"%s\"\n", options.depth, bpp_filename);
|
|
||||||
}
|
|
||||||
if (options.depth == 1) {
|
|
||||||
bpp_data = extend_1bpp_to_2bpp(bpp_data, &filesize);
|
|
||||||
}
|
|
||||||
mingle_2bpp_planes(bpp_data, filesize);
|
|
||||||
|
|
||||||
unsigned int height = calculate_size(filesize, &options.width);
|
|
||||||
bpp_data = pad_to_rectangle(bpp_data, filesize, options.width, height);
|
|
||||||
if (options.transpose) {
|
|
||||||
bpp_data = transpose_tiles(bpp_data, options.width, height);
|
|
||||||
}
|
|
||||||
bpp_data = rearrange_tiles_to_scanlines(bpp_data, options.width, height);
|
|
||||||
|
|
||||||
const char *png_filename = argv[1];
|
|
||||||
unsigned int error = write_png(png_filename, bpp_data, options.depth, options.width, height,
|
|
||||||
options.palette ? palette : NULL);
|
|
||||||
if (error) {
|
|
||||||
error_exit("Could not write to file \"%s\": %s\n", png_filename, lodepng_error_text(error));
|
|
||||||
}
|
|
||||||
|
|
||||||
free(bpp_data);
|
|
||||||
return 0;
|
|
||||||
}
|
|
@@ -1,11 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
# Compares baserom.gbc and pokecrystal.gbc
|
|
||||||
|
|
||||||
# create baserom.txt if necessary
|
|
||||||
if [ ! -f baserom.txt ]; then
|
|
||||||
hexdump -C baserom.gbc > baserom.txt
|
|
||||||
fi
|
|
||||||
|
|
||||||
hexdump -C pokecrystal.gbc > pokecrystal.txt
|
|
||||||
|
|
||||||
diff -u baserom.txt pokecrystal.txt | less
|
|
@@ -1,51 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
# Compares baserom.gbc and pokecrystal.gbc
|
|
||||||
|
|
||||||
# create baserom.txt if necessary
|
|
||||||
crystal_md5=9f2922b235a5eeb78d65594e82ef5dde
|
|
||||||
if [ ! -f baserom.gbc ]; then
|
|
||||||
echo "FATAL: Baserom not found"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ $1 ]; then
|
|
||||||
if [ $1 == "-v" ]; then
|
|
||||||
verbose=1
|
|
||||||
else
|
|
||||||
verbose = 0
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
verbose=0
|
|
||||||
fi
|
|
||||||
|
|
||||||
base_md5=`md5sum baserom.gbc | cut -d' ' -f1`
|
|
||||||
if [ $verbose == 1 ]; then
|
|
||||||
echo "baserom.gbc: $base_md5"
|
|
||||||
fi
|
|
||||||
if [ $base_md5 != $crystal_md5 ]; then
|
|
||||||
echo "FATAL: Baserom is incorrect"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
built_md5=`md5sum pokecrystal.gbc | cut -d' ' -f1`
|
|
||||||
if [ $verbose == 1 ]; then
|
|
||||||
echo "pokecrystal.gbc: $built_md5"
|
|
||||||
fi
|
|
||||||
if [ $built_md5 != $crystal_md5 ]
|
|
||||||
then
|
|
||||||
if [ $verbose == 1 ]; then
|
|
||||||
echo "Checksums do not match, here's where the ROMs differ..."
|
|
||||||
fi
|
|
||||||
if [ ! -f baserom.txt ]; then
|
|
||||||
hexdump -C baserom.gbc > baserom.txt
|
|
||||||
fi
|
|
||||||
|
|
||||||
hexdump -C pokecrystal.gbc > pokecrystal.txt
|
|
||||||
|
|
||||||
diff -u baserom.txt pokecrystal.txt | less
|
|
||||||
else
|
|
||||||
if [ $verbose == 1 ]; then
|
|
||||||
echo "Checksums match! :D"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
@@ -1,56 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
"""
|
|
||||||
Usage: python consts.py constants/some_constants.asm
|
|
||||||
|
|
||||||
View numeric values of `const`ants.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import re
|
|
||||||
|
|
||||||
const_value = 0
|
|
||||||
const_inc = 1
|
|
||||||
|
|
||||||
def asm_int(s):
|
|
||||||
base = {'$': 16, '&': 8, '%': 2}.get(s[0], 10)
|
|
||||||
return int(s if base == 10 else s[1:], base)
|
|
||||||
|
|
||||||
def print_const(s, v):
|
|
||||||
print(f'{s} == {v} == ${v:x}')
|
|
||||||
|
|
||||||
def parse_for_constants(line):
|
|
||||||
global const_value, const_inc
|
|
||||||
if not (m := re.match(r'^\s+([A-Za-z_][A-Za-z0-9_@#]*)(?:\s+([^;\\n]+))?', line)):
|
|
||||||
return
|
|
||||||
macro, rest = m.groups()
|
|
||||||
args = [arg.strip() for arg in rest.split(',')] if rest else []
|
|
||||||
if args and not args[-1]:
|
|
||||||
args = args[:-1]
|
|
||||||
if macro == 'const_def':
|
|
||||||
const_value = asm_int(args[0]) if len(args) >= 1 else 0
|
|
||||||
const_inc = asm_int(args[1]) if len(args) >= 2 else 1
|
|
||||||
elif macro == 'const':
|
|
||||||
print_const(args[0], const_value)
|
|
||||||
const_value += const_inc
|
|
||||||
elif macro == 'shift_const':
|
|
||||||
print_const(args[0], 1 << const_value)
|
|
||||||
print_const(args[0] + '_F', const_value)
|
|
||||||
const_value += const_inc
|
|
||||||
elif macro == 'const_skip':
|
|
||||||
const_value += const_inc * (asm_int(args[0]) if len(args) >= 1 else 1)
|
|
||||||
elif macro == 'const_next':
|
|
||||||
const_value = asm_int(args[0])
|
|
||||||
|
|
||||||
def main():
|
|
||||||
if len(sys.argv) < 2:
|
|
||||||
print(f'Usage: {sys.argv[0]} constants/some_constants.asm', file=sys.stderr)
|
|
||||||
sys.exit(1)
|
|
||||||
for filename in sys.argv[1:]:
|
|
||||||
with open(filename, 'r', encoding='utf-8') as file:
|
|
||||||
for line in file:
|
|
||||||
parse_for_constants(line)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
@@ -1,32 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
"""
|
|
||||||
Usage: python dupeframes.py
|
|
||||||
|
|
||||||
Check for duplicate frames in Pokemon sprites (gfx/pokemon/*/front.png).
|
|
||||||
"""
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import glob
|
|
||||||
|
|
||||||
import png
|
|
||||||
|
|
||||||
def duplicate_frames(filename):
|
|
||||||
with open(filename, 'rb') as file:
|
|
||||||
width, height, rows = png.Reader(file).asRGBA8()[:3]
|
|
||||||
rows = list(rows)
|
|
||||||
if height % width:
|
|
||||||
print(f'{filename} is not a vertical strip of square frames!', file=sys.stderr)
|
|
||||||
return
|
|
||||||
num_frames = height // width
|
|
||||||
frames = [rows[i*width:(i+1)*width] for i in range(num_frames)]
|
|
||||||
yield from ((i, j) for i in range(num_frames) for j in range(i+1, num_frames) if frames[i] == frames[j])
|
|
||||||
|
|
||||||
def main():
|
|
||||||
for filename in sorted(glob.glob('gfx/pokemon/*/front.png')):
|
|
||||||
for (i, j) in duplicate_frames(filename):
|
|
||||||
print(f'{filename}: frame {j} is a duplicate of frame {i}', file=sys.stderr)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
@@ -1,78 +0,0 @@
|
|||||||
#!/usr/bin/gawk -f
|
|
||||||
|
|
||||||
# Usage: tools/free_space.awk [BANK=<bank_spec>] pokecrystal.map
|
|
||||||
|
|
||||||
# The BANK argument allows printing free space in one, all, or none of the ROM's banks.
|
|
||||||
# Valid arguments are numbers (in decimal "42" or hexadecimal "0x2a"), "all" or "none".
|
|
||||||
# If not specified, defaults to "none".
|
|
||||||
# The `BANK` argument MUST be before the map file name, otherwise it has no effect!
|
|
||||||
# Yes: tools/free_space.awk BANK=all pokecrystal.map
|
|
||||||
# No: tools/free_space.awk pokecrystal.map BANK=42
|
|
||||||
|
|
||||||
# Copyright (c) 2020, Eldred Habert.
|
|
||||||
# SPDX-License-Identifier: MIT
|
|
||||||
|
|
||||||
BEGIN {
|
|
||||||
nb_banks = 0
|
|
||||||
free = 0
|
|
||||||
rom_bank = 0 # Safety net for malformed files
|
|
||||||
|
|
||||||
# Default settings
|
|
||||||
# Variables assigned via the command-line (except through `-v`) are *after* `BEGIN`
|
|
||||||
BANK="none"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Only accept ROM banks, ignore everything else
|
|
||||||
toupper($0) ~ /^[ \t]*ROM[0X][ \t]+BANK[ \t]+#/ {
|
|
||||||
nb_banks++
|
|
||||||
rom_bank = 1
|
|
||||||
split($0, fields, /[ \t]/)
|
|
||||||
bank_num = strtonum(substr(fields[3], 2))
|
|
||||||
}
|
|
||||||
|
|
||||||
function register_bank(amount) {
|
|
||||||
free += amount
|
|
||||||
rom_bank = 0 # Reject upcoming banks by default
|
|
||||||
|
|
||||||
if (BANK ~ /all/ || BANK == bank_num) {
|
|
||||||
printf "Bank %3d: %5d/16384 (%.2f%%)\n", bank_num, amount, amount * 100 / 16384
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function register_bank_str(str) {
|
|
||||||
if (str ~ /\$[0-9A-F]+/) {
|
|
||||||
register_bank(strtonum("0x" substr(str, 2)))
|
|
||||||
} else {
|
|
||||||
printf "Malformed number? \"%s\" does not start with '$'\n", str
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
rom_bank && toupper($0) ~ /^[ \t]*EMPTY$/ {
|
|
||||||
# Empty bank
|
|
||||||
register_bank(16384)
|
|
||||||
}
|
|
||||||
rom_bank && toupper($0) ~ /^[ \t]*SLACK:[ \t]/ {
|
|
||||||
# Old (rgbds <=0.6.0) end-of-bank free space
|
|
||||||
register_bank_str($2)
|
|
||||||
}
|
|
||||||
rom_bank && toupper($0) ~ /^[ \t]*TOTAL EMPTY:[ \t]/ {
|
|
||||||
# New (rgbds >=0.6.1) total free space
|
|
||||||
register_bank_str($3)
|
|
||||||
}
|
|
||||||
|
|
||||||
END {
|
|
||||||
# Determine number of banks, by rounding to upper power of 2
|
|
||||||
total_banks = 2 # Smallest size is 2 banks
|
|
||||||
while (total_banks < nb_banks) {
|
|
||||||
total_banks *= 2
|
|
||||||
}
|
|
||||||
|
|
||||||
# RGBLINK omits "trailing" ROM banks, so fake them
|
|
||||||
bank_num = nb_banks
|
|
||||||
while (bank_num < total_banks) {
|
|
||||||
register_bank(16384)
|
|
||||||
bank_num++
|
|
||||||
}
|
|
||||||
|
|
||||||
total = total_banks * 16384
|
|
||||||
printf "Free space: %5d/%5d (%.2f%%)\n", free, total, free * 100 / total
|
|
||||||
}
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,38 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
"""
|
|
||||||
Usage: python rgb555.py image.png
|
|
||||||
|
|
||||||
Round all colors of the input image to RGB555.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import sys
|
|
||||||
|
|
||||||
import png
|
|
||||||
|
|
||||||
def rgb8_to_rgb5(c):
|
|
||||||
return (c & 0b11111000) | (c >> 5)
|
|
||||||
|
|
||||||
def round_pal(filename):
|
|
||||||
with open(filename, 'rb') as file:
|
|
||||||
width, height, rows = png.Reader(file).asRGBA8()[:3]
|
|
||||||
rows = list(rows)
|
|
||||||
for row in rows:
|
|
||||||
del row[3::4]
|
|
||||||
rows = [[rgb8_to_rgb5(c) for c in row] for row in rows]
|
|
||||||
writer = png.Writer(width, height, greyscale=False, bitdepth=8, compression=9)
|
|
||||||
with open(filename, 'wb') as file:
|
|
||||||
writer.write(file, rows)
|
|
||||||
|
|
||||||
def main():
|
|
||||||
if len(sys.argv) < 2:
|
|
||||||
print(f'Usage: {sys.argv[0]} pic.png', file=sys.stderr)
|
|
||||||
sys.exit(1)
|
|
||||||
for filename in sys.argv[1:]:
|
|
||||||
if not filename.lower().endswith('.png'):
|
|
||||||
print(f'{filename} is not a .png file!', file=sys.stderr)
|
|
||||||
round_pal(filename)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
@@ -1,52 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
"""
|
|
||||||
Usage: python sym_comments.py file.asm [pokecrystal.sym] > file_commented.asm
|
|
||||||
|
|
||||||
Comments each label in file.asm with its bank:address from the sym file.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import re
|
|
||||||
|
|
||||||
def main():
|
|
||||||
if len(sys.argv) not in {2, 3}:
|
|
||||||
print(f'Usage: {sys.argv[0]} file.asm [pokecrystal.sym] > file_commented.asm', file=sys.stderr)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
wram_name = sys.argv[1]
|
|
||||||
sym_name = sys.argv[2] if len(sys.argv) == 3 else 'pokecrystal.sym'
|
|
||||||
|
|
||||||
sym_def_rx = re.compile(r'(^{sym})(:.*)|(^\.{sym})(.*)'.format(sym=r'[A-Za-z_][A-Za-z0-9_#@]*'))
|
|
||||||
|
|
||||||
sym_addrs = {}
|
|
||||||
with open(sym_name, 'r', encoding='utf-8') as file:
|
|
||||||
for line in file:
|
|
||||||
line = line.split(';', 1)[0].rstrip()
|
|
||||||
parts = line.split(' ', 1)
|
|
||||||
if len(parts) != 2:
|
|
||||||
continue
|
|
||||||
addr, sym = parts
|
|
||||||
sym_addrs[sym] = addr
|
|
||||||
|
|
||||||
with open(wram_name, 'r', encoding='utf-8') as file:
|
|
||||||
cur_label = None
|
|
||||||
for line in file:
|
|
||||||
line = line.rstrip()
|
|
||||||
if (m := re.match(sym_def_rx, line)):
|
|
||||||
sym, rest = m.group(1), m.group(2)
|
|
||||||
if sym is None and rest is None:
|
|
||||||
sym, rest = m.group(3), m.group(4)
|
|
||||||
key = sym
|
|
||||||
if not sym.startswith('.'):
|
|
||||||
cur_label = sym
|
|
||||||
elif cur_label:
|
|
||||||
key = cur_label + sym
|
|
||||||
if key in sym_addrs:
|
|
||||||
addr = sym_addrs[key]
|
|
||||||
line = sym + rest + ' ; ' + addr
|
|
||||||
print(line)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
99
tools/toc.py
99
tools/toc.py
@@ -1,99 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
"""
|
|
||||||
Usage: python toc.py file.md
|
|
||||||
|
|
||||||
Replace a "## TOC" heading in a Markdown file with a table of contents,
|
|
||||||
generated from the other headings in the file. Supports multiple files.
|
|
||||||
Headings must start with "##" signs to be detected.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import re
|
|
||||||
from collections import namedtuple
|
|
||||||
from urllib.parse import quote
|
|
||||||
|
|
||||||
toc_name = 'Contents'
|
|
||||||
valid_toc_headings = {'## TOC', '##TOC'}
|
|
||||||
|
|
||||||
TocItem = namedtuple('TocItem', ['name', 'anchor', 'level'])
|
|
||||||
punctuation_rx = re.compile(r'[^\w\- ]+')
|
|
||||||
numbered_heading_rx = re.compile(r'^[0-9]+\. ')
|
|
||||||
specialchar_rx = re.compile(r'[⅔]+')
|
|
||||||
|
|
||||||
def name_to_anchor(name):
|
|
||||||
# GitHub's algorithm for generating anchors from headings
|
|
||||||
# https://github.com/jch/html-pipeline/blob/master/lib/html/pipeline/toc_filter.rb
|
|
||||||
anchor = name.strip().lower() # lowercase
|
|
||||||
anchor = re.sub(punctuation_rx, '', anchor) # remove punctuation
|
|
||||||
anchor = anchor.replace(' ', '-') # replace spaces with dash
|
|
||||||
anchor = re.sub(specialchar_rx, '', anchor) # remove misc special chars
|
|
||||||
anchor = quote(anchor) # url encode
|
|
||||||
return anchor
|
|
||||||
|
|
||||||
def get_toc_index(lines):
|
|
||||||
toc_index = None
|
|
||||||
for i, line in enumerate(lines):
|
|
||||||
if line.rstrip() in valid_toc_headings:
|
|
||||||
toc_index = i
|
|
||||||
break
|
|
||||||
return toc_index
|
|
||||||
|
|
||||||
def get_toc_items(lines, toc_index):
|
|
||||||
for i, line in enumerate(lines):
|
|
||||||
if i <= toc_index:
|
|
||||||
continue
|
|
||||||
if line.startswith('##'):
|
|
||||||
name = line.lstrip('#')
|
|
||||||
level = len(line) - len(name) - len('##')
|
|
||||||
name = name.strip()
|
|
||||||
anchor = name_to_anchor(name)
|
|
||||||
yield TocItem(name, anchor, level)
|
|
||||||
|
|
||||||
def toc_string(toc_items):
|
|
||||||
lines = [f'## {toc_name}', '']
|
|
||||||
for name, anchor, level in toc_items:
|
|
||||||
padding = ' ' * level
|
|
||||||
if re.match(numbered_heading_rx, name):
|
|
||||||
bullet, name = name.split('.', 1)
|
|
||||||
bullet += '.'
|
|
||||||
name = name.lstrip()
|
|
||||||
else:
|
|
||||||
bullet = '-'
|
|
||||||
lines.append(f'{padding}{bullet} [{name}](#{anchor})')
|
|
||||||
return '\n'.join(lines) + '\n'
|
|
||||||
|
|
||||||
def add_toc(filename):
|
|
||||||
with open(filename, 'r', encoding='utf-8') as file:
|
|
||||||
lines = file.readlines()
|
|
||||||
toc_index = get_toc_index(lines)
|
|
||||||
if toc_index is None:
|
|
||||||
return None # no TOC heading
|
|
||||||
toc_items = list(get_toc_items(lines, toc_index))
|
|
||||||
if not toc_items:
|
|
||||||
return False # no content headings
|
|
||||||
with open(filename, 'w', encoding='utf-8') as file:
|
|
||||||
for i, line in enumerate(lines):
|
|
||||||
if i == toc_index:
|
|
||||||
file.write(toc_string(toc_items))
|
|
||||||
else:
|
|
||||||
file.write(line)
|
|
||||||
return True # OK
|
|
||||||
|
|
||||||
def main():
|
|
||||||
if len(sys.argv) < 2:
|
|
||||||
print(f'Usage: {sys.argv[0]} file.md', file=sys.stderr)
|
|
||||||
sys.exit(1)
|
|
||||||
for filename in sys.argv[1:]:
|
|
||||||
print(filename)
|
|
||||||
result = add_toc(filename)
|
|
||||||
if result is None:
|
|
||||||
print('Warning: No "## TOC" heading found', file=sys.stderr)
|
|
||||||
elif result is False:
|
|
||||||
print('Warning: No content headings found', file=sys.stderr)
|
|
||||||
else:
|
|
||||||
print('OK')
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
106
tools/unique.py
106
tools/unique.py
@@ -1,106 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
"""
|
|
||||||
Usage: python unique.py [-f|--flip] [-x|--cross] image.png
|
|
||||||
|
|
||||||
Erase duplicate tiles from an input image.
|
|
||||||
-f or --flip counts X/Y mirrored tiles as duplicates.
|
|
||||||
-x or --cross erases with a cross instead of a blank tile.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import sys
|
|
||||||
|
|
||||||
import png
|
|
||||||
|
|
||||||
def rgb5_pixels(row):
|
|
||||||
yield from (tuple(c // 8 for c in row[x:x+3]) for x in range(0, len(row), 4))
|
|
||||||
|
|
||||||
def rgb8_pixels(row):
|
|
||||||
yield from (c * 8 + c // 4 for pixel in row for c in pixel)
|
|
||||||
|
|
||||||
def gray_pixels(row):
|
|
||||||
yield from (pixel[0] // 10 for pixel in row)
|
|
||||||
|
|
||||||
def rows_to_tiles(rows, width, height):
|
|
||||||
assert len(rows) == height and len(rows[0]) == width
|
|
||||||
yield from (tuple(tuple(row[x:x+8]) for row in rows[y:y+8])
|
|
||||||
for y in range(0, height, 8) for x in range(0, width, 8))
|
|
||||||
|
|
||||||
def tiles_to_rows(tiles, width, height):
|
|
||||||
assert width % 8 == 0 and height % 8 == 0
|
|
||||||
width, height = width // 8, height // 8
|
|
||||||
tiles = list(tiles)
|
|
||||||
assert len(tiles) == width * height
|
|
||||||
tile_rows = (tiles[y:y+width] for y in range(0, width * height, width))
|
|
||||||
yield from ([tile[y][x] for tile in tile_row for x in range(8)]
|
|
||||||
for tile_row in tile_rows for y in range(8))
|
|
||||||
|
|
||||||
def tile_variants(tile, flip):
|
|
||||||
yield tile
|
|
||||||
if flip:
|
|
||||||
yield tile[::-1]
|
|
||||||
yield tuple(row[::-1] for row in tile)
|
|
||||||
yield tuple(row[::-1] for row in tile[::-1])
|
|
||||||
|
|
||||||
def unique_tiles(tiles, flip, cross):
|
|
||||||
if cross:
|
|
||||||
blank = [[(0, 0, 0)] * 8 for _ in range(8)]
|
|
||||||
for y in range(8):
|
|
||||||
blank[y][y] = blank[y][7 - y] = (31, 31, 31)
|
|
||||||
blank = tuple(tuple(row) for row in blank)
|
|
||||||
else:
|
|
||||||
blank = tuple(tuple([(31, 31, 31)] * 8) for _ in range(8))
|
|
||||||
seen = set()
|
|
||||||
for tile in tiles:
|
|
||||||
if any(variant in seen for variant in tile_variants(tile, flip)):
|
|
||||||
yield blank
|
|
||||||
else:
|
|
||||||
yield tile
|
|
||||||
seen.add(tile)
|
|
||||||
|
|
||||||
def is_grayscale(colors):
|
|
||||||
return (colors.issubset({(31, 31, 31), (21, 21, 21), (10, 10, 10), (0, 0, 0)}) or
|
|
||||||
colors.issubset({(31, 31, 31), (20, 20, 20), (10, 10, 10), (0, 0, 0)}))
|
|
||||||
|
|
||||||
def erase_duplicates(filename, flip, cross):
|
|
||||||
with open(filename, 'rb') as file:
|
|
||||||
width, height, rows = png.Reader(file).asRGBA8()[:3]
|
|
||||||
rows = [list(rgb5_pixels(row)) for row in rows]
|
|
||||||
if width % 8 or height % 8:
|
|
||||||
return False
|
|
||||||
tiles = unique_tiles(rows_to_tiles(rows, width, height), flip, cross)
|
|
||||||
rows = list(tiles_to_rows(tiles, width, height))
|
|
||||||
if is_grayscale({c for row in rows for c in row}):
|
|
||||||
rows = [list(gray_pixels(row)) for row in rows]
|
|
||||||
writer = png.Writer(width, height, greyscale=True, bitdepth=2, compression=9)
|
|
||||||
else:
|
|
||||||
rows = [list(rgb8_pixels(row)) for row in rows]
|
|
||||||
writer = png.Writer(width, height, greyscale=False, bitdepth=8, compression=9)
|
|
||||||
with open(filename, 'wb') as file:
|
|
||||||
writer.write(file, rows)
|
|
||||||
return True
|
|
||||||
|
|
||||||
def main():
|
|
||||||
flip = cross = False
|
|
||||||
while True:
|
|
||||||
if len(sys.argv) < 2:
|
|
||||||
print(f'Usage: {sys.argv[0]} [-f|--flip] [-x|--cross] tileset.png', file=sys.stderr)
|
|
||||||
sys.exit(1)
|
|
||||||
elif sys.argv[1] in {'-f', '--flip'}:
|
|
||||||
flip = True
|
|
||||||
elif sys.argv[1] in {'-x', '--cross'}:
|
|
||||||
cross = True
|
|
||||||
elif sys.argv[1] in {'-fx', '-xf'}:
|
|
||||||
flip = cross = True
|
|
||||||
else:
|
|
||||||
break
|
|
||||||
sys.argv.pop(1)
|
|
||||||
for filename in sys.argv[1:]:
|
|
||||||
if not filename.lower().endswith('.png'):
|
|
||||||
print(f'{filename} is not a .png file!', file=sys.stderr)
|
|
||||||
elif not erase_duplicates(filename, flip, cross):
|
|
||||||
print(f'{filename} is not divisible into 8x8 tiles!', file=sys.stderr)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
Reference in New Issue
Block a user