From a30f728255c9c24e376337b00bc9c0e96861f74a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Sat, 15 Jan 2022 17:27:23 +0000 Subject: [PATCH] Player graphics --- Makefile | 11 ++++++++++- platformer.c | 38 ++++++++++++++++++++++++++++++-------- player_stand_tile.txt | 15 +++++++++++++++ player_walk1_tile.txt | 15 +++++++++++++++ player_walk2_tile.txt | 15 +++++++++++++++ tile_compiler.py | 7 ++++++- 6 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 player_stand_tile.txt create mode 100644 player_walk1_tile.txt create mode 100644 player_walk2_tile.txt diff --git a/Makefile b/Makefile index c45c13c..014f008 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,10 @@ .SUFFIXES: .inc .txt NAME = platformer -TILES = grass_tile.inc dirt_tile.inc flag_tile.inc \ +TILES = player_stand_tile.inc \ + player_walk1_tile.inc player_walk1_tile_reversed.inc \ + player_walk2_tile.inc player_walk2_tile_reversed.inc \ + grass_tile.inc dirt_tile.inc flag_tile.inc \ switch1_off_tile.inc switch1_on_tile.inc \ tile1_off_tile.inc tile1_on_tile.inc \ stage1_tile.inc stage2_tile.inc star_tile.inc @@ -15,6 +18,12 @@ $(NAME): $(NAME).c $(TILES) .txt.inc: ./tile_compiler.py $< > $@ +player_walk1_tile_reversed.inc: player_walk1_tile.txt + ./tile_compiler.py $< reversed > $@ + +player_walk2_tile_reversed.inc: player_walk2_tile.txt + ./tile_compiler.py $< reversed > $@ + run: $(NAME) ./$(NAME) diff --git a/platformer.c b/platformer.c index c4dedef..78e67d8 100644 --- a/platformer.c +++ b/platformer.c @@ -24,6 +24,9 @@ bool stages_beat[STAGES]; enum palette { BG = 0, TEXT, + PLAYER_OUTLINE, + PLAYER_SKIN, + PLAYER_EYE, GRASS_TOP, GRASS_BROWN, GRASS_DARK, @@ -37,6 +40,12 @@ enum palette { }; static enum palette playfield[PLAYFIELD_SIDE * PLAYFIELD_SIDE]; +#include "player_stand_tile.inc" +#include "player_walk1_tile.inc" +#include "player_walk2_tile.inc" +#include "player_walk1_tile_reversed.inc" +#include "player_walk2_tile_reversed.inc" + #include "grass_tile.inc" #include "dirt_tile.inc" #include "flag_tile.inc" @@ -77,7 +86,7 @@ enum switches_state { ALL_OFF, SWITCH1 }; enum switches_state switches_state; static uint32_t main_window = 0; -static uint32_t window_width = 600; +static uint32_t window_width = 500; static uint32_t window_height = 500; static bool game_running = true; @@ -376,13 +385,23 @@ static void draw_tiles(void) { } } -static void draw_stage(void) { +static void draw_stage(struct timespec now) { memset(playfield, 0, sizeof(playfield)); draw_tiles(); - draw_tile(grass_tile, player_x, player_y); + if (player_dx > 0 && now.tv_nsec % 500000000L < 250000000L) + draw_tile(player_walk1_tile, player_x, player_y); + else if (player_dx > 0) + draw_tile(player_walk2_tile, player_x, player_y); + else if (player_dx < 0 && now.tv_nsec % 500000000L < 250000000L) + draw_tile(player_walk1_tile_reversed, player_x, player_y); + else if (player_dx < 0) + draw_tile(player_walk2_tile_reversed, player_x, player_y); + else + draw_tile(player_stand_tile, player_x, player_y); } -static void draw_titlescreen(void) { +static void draw_titlescreen(struct timespec now) { + (void) now; memset(playfield, 0, sizeof(playfield)); for (size_t i = 0; i < STAGES; i++) { size_t x = i * TILE_SIDE + TILE_SIDE; @@ -398,10 +417,10 @@ static void draw_titlescreen(void) { playfield[underline_y * PLAYFIELD_SIDE + underline_x + x] = TEXT; } -static void draw(void) { +static void draw(struct timespec now) { switch (game_mode) { - case TITLESCREEN: draw_titlescreen(); break; - case STAGE: draw_stage(); break; + case TITLESCREEN: draw_titlescreen(now); break; + case STAGE: draw_stage(now); break; default: printf("%i\n", game_mode); break; } } @@ -426,6 +445,9 @@ int main(int argc, char *argv[]) { uint32_t palette_colours[] = { [BG] = make_color(128, 128, 255), [TEXT] = make_color(255, 255, 255), + [PLAYER_OUTLINE] = make_color(0, 0, 0), + [PLAYER_SKIN] = make_color(180, 100, 64), + [PLAYER_EYE] = make_color(255, 255, 255), [GRASS_TOP] = make_color(0, 220, 0), [GRASS_BROWN] = make_color(200, 128, 0), [GRASS_DARK] = make_color(100, 64, 0), @@ -466,7 +488,7 @@ int main(int argc, char *argv[]) { fps++; update(now, dt); - draw(); + draw(now); size_t pixels; if (__builtin_mul_overflow(window_width, window_height, &pixels)) diff --git a/player_stand_tile.txt b/player_stand_tile.txt new file mode 100644 index 0000000..d1b4879 --- /dev/null +++ b/player_stand_tile.txt @@ -0,0 +1,15 @@ +xPLAYER_OUTLINE +sPLAYER_SKIN +oPLAYER_EYE + BG +---------- + xxxxxxx + xsxoooxsx + xsxsssxsx + xsxsssxsx + xsxsssxsx + xsxxxxxsx + xx xx + xssx xssx + xssx xssx + xx xx diff --git a/player_walk1_tile.txt b/player_walk1_tile.txt new file mode 100644 index 0000000..b32aa00 --- /dev/null +++ b/player_walk1_tile.txt @@ -0,0 +1,15 @@ +xPLAYER_OUTLINE +sPLAYER_SKIN +oPLAYER_EYE + BG +---------- + xxxxxxx + xssxxsoox + xsxssxxx + xsxsssssx + xssxssssx + xxxxxxx + xxx xxx +xsxx xxx +xssx xsssx + xx xxx diff --git a/player_walk2_tile.txt b/player_walk2_tile.txt new file mode 100644 index 0000000..76a084c --- /dev/null +++ b/player_walk2_tile.txt @@ -0,0 +1,15 @@ +xPLAYER_OUTLINE +sPLAYER_SKIN +oPLAYER_EYE + BG +---------- + xxxxxxx + xssxxsoox + xsxssxsx + xsxsssxx + xssxsssx + xxxxssx + xx xxxx + xxx xsx + xsssxxssx + xxx xx diff --git a/tile_compiler.py b/tile_compiler.py index bc48dbc..42932f5 100755 --- a/tile_compiler.py +++ b/tile_compiler.py @@ -26,7 +26,12 @@ for i, line in enumerate(lines): 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]}[] = {{') +name = sys.argv[1].rsplit(".", 1)[0] +if len(sys.argv) == 3 and sys.argv[2] == 'reversed': + data = [''.join(reversed(line)) for line in data] + name = name + '_reversed' + +print(f'enum palette {name}[] = {{') for line in data: print('\t' + ', '.join(colours[char] for char in line) + ',') print('};')