diff --git a/.gitignore b/.gitignore index 3e7eb50..526f524 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ platformer +*.inc diff --git a/Makefile b/Makefile index 2ee6cc4..07435cc 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,22 @@ +.SUFFIXES: +.SUFFIXES: .inc .txt + NAME = platformer +TILES = grass_tile.inc dirt_tile.inc flag_tile.inc all: $(NAME) -$(NAME): $(NAME).c +$(NAME): $(NAME).c $(TILES) $(CC) -O3 -Wall -Wextra -Werror -pedantic -o $@ $< -ldisplay +.txt.inc: + ./tile_compiler.py $< > $@ + run: $(NAME) ./$(NAME) clean: - rm -f $(NAME) + rm -f $(NAME) $(TILES) distclean: clean diff --git a/dirt_tile.txt b/dirt_tile.txt new file mode 100644 index 0000000..fa0f095 --- /dev/null +++ b/dirt_tile.txt @@ -0,0 +1,13 @@ + GRASS_BROWN +.GRASS_DARK +---------- + + . + . . + + . . + + . . + . + . + diff --git a/flag_tile.txt b/flag_tile.txt new file mode 100644 index 0000000..21fb599 --- /dev/null +++ b/flag_tile.txt @@ -0,0 +1,14 @@ +|FLAG_STEM +xFLAG_FLAG +.BG +---------- +.|xx...... +.|xxxx.... +.|xxxxxx.. +.|xxxxxxx. +.|xxxxxx.. +.|xxxx.... +.|xx...... +.|........ +.|........ +.|........ diff --git a/grass_tile.txt b/grass_tile.txt new file mode 100644 index 0000000..7238a37 --- /dev/null +++ b/grass_tile.txt @@ -0,0 +1,14 @@ +xGRASS_TOP + GRASS_BROWN +.GRASS_DARK +---------- +xxxxxxxxxx +xx xx x x + x x x + + . . + + . . + . + . + diff --git a/platformer.c b/platformer.c index 7780ce6..f5d5d16 100644 --- a/platformer.c +++ b/platformer.c @@ -21,20 +21,28 @@ enum palette { TEXT, GRASS_TOP, GRASS_BROWN, + GRASS_DARK, + FLAG_STEM, + FLAG_FLAG, ERROR, }; static enum palette playfield[PLAYFIELD_SIDE * PLAYFIELD_SIDE]; + +#include "grass_tile.inc" +#include "dirt_tile.inc" +#include "flag_tile.inc" + static char tilemap[] = " " " " " " - " g " + "! g " "gg g " " g" " g " " g " - " g g " - "gggggggggg"; + " gdg " + "ggggdddggg"; static uint32_t main_window = 0; static uint32_t window_width = 600; @@ -228,28 +236,12 @@ static void update(struct timespec now, struct timespec dt_timespec) { } } -static void draw_grass_tile(size_t x, size_t y) { +static void draw_tile(enum palette tile[], size_t x, size_t y) { assert(x + TILE_SIDE <= PLAYFIELD_SIDE); assert(y + TILE_SIDE <= PLAYFIELD_SIDE); - char grass_tile[] = "xxxxxxxxxx" - "xx xx x x " - " x x x " - " " - " " - " " - " " - " " - " " - " "; - assert(strlen(grass_tile) == TILE_SIDE * TILE_SIDE); for (size_t ty = 0; ty < TILE_SIDE; ty++) { for (size_t tx = 0; tx < TILE_SIDE; tx++) { - enum palette pixel; - switch (grass_tile[ty * TILE_SIDE + tx]) { - case 'x': pixel = GRASS_TOP; break; - case ' ': pixel = GRASS_BROWN; break; - default: pixel = ERROR; break; - } + enum palette pixel = tile[ty * TILE_SIDE + tx]; playfield[(y + ty) * PLAYFIELD_SIDE + x + tx] = pixel; } } @@ -259,9 +251,14 @@ static void draw_tiles(void) { assert(strlen(tilemap) == TILES * TILES); for (size_t ty = 0; ty < TILES; ty++) { for (size_t tx = 0; tx < TILES; tx++) { + enum palette *tile = NULL; switch(tilemap[ty * TILES + tx]) { - case 'g': draw_grass_tile(tx * TILE_SIDE, ty * TILE_SIDE); break; + case 'g': tile = grass_tile; break; + case 'd': tile = dirt_tile; break; + case '!': tile = flag_tile; break; } + if (tile) + draw_tile(tile, tx * TILE_SIDE, ty * TILE_SIDE); } } } @@ -269,7 +266,7 @@ static void draw_tiles(void) { static void draw(void) { memset(playfield, 0, sizeof(playfield)); draw_tiles(); - draw_grass_tile(player_x, player_y); + draw_tile(grass_tile, player_x, player_y); } int main(int argc, char *argv[]) { @@ -292,12 +289,15 @@ int main(int argc, char *argv[]) { uint32_t palette_colours[] = { [BG] = make_color(128, 128, 255), [TEXT] = make_color(255, 255, 255), - [GRASS_TOP] = make_color(0, 255, 0), + [GRASS_TOP] = make_color(0, 220, 0), [GRASS_BROWN] = make_color(200, 128, 0), + [GRASS_DARK] = make_color(100, 64, 0), + [FLAG_STEM] = make_color(0, 0, 0), + [FLAG_FLAG] = make_color(255, 0, 0), [ERROR] = make_color(255, 0, 255), }; - long fps_cap = 60; + long fps_cap = 100; struct timespec min_dt = timespec_make(0, 1000L * 1000L * 1000L / fps_cap); struct timespec last_frame; clock_gettime(CLOCK_MONOTONIC, &last_frame); diff --git a/tile_compiler.py b/tile_compiler.py new file mode 100755 index 0000000..bc48dbc --- /dev/null +++ b/tile_compiler.py @@ -0,0 +1,32 @@ +#!python +import sys + +with open(sys.argv[1], 'r') as f: + lines = [line.rstrip('\n') for line in f] + +colours = {} +colour_lines = True +data = [] +for i, line in enumerate(lines): + linenum = i + 1 + if colour_lines and line == '----------': + colour_lines = False + continue + + if colour_lines: + char = line[0] + colour = line[1:] + colours[char] = colour + elif len(line) != 10: + print(f'{sys.argv[0]}: {sys.argv[1]}: line {linenum} is {len(line)} wide, expected 10', file=sys.stderr) + sys.exit(1) + else: + data.append(line) + +if len(data) != 10: + print(f'{sys.argv[0]}: {sys.argv[1]}: {len(data)} lines of data, expected 10', file=sys.stderr) + +print(f'enum palette {sys.argv[1].rsplit(".", 1)[0]}[] = {{') +for line in data: + print('\t' + ', '.join(colours[char] for char in line) + ',') +print('};')