Player graphics

This commit is contained in:
Juhani Krekelä 2022-01-15 17:27:23 +00:00
parent 922d49d6a0
commit a30f728255
6 changed files with 91 additions and 10 deletions

View File

@ -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)

View File

@ -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))

15
player_stand_tile.txt Normal file
View File

@ -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

15
player_walk1_tile.txt Normal file
View File

@ -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

15
player_walk2_tile.txt Normal file
View File

@ -0,0 +1,15 @@
xPLAYER_OUTLINE
sPLAYER_SKIN
oPLAYER_EYE
BG
----------
xxxxxxx
xssxxsoox
xsxssxsx
xsxsssxx
xssxsssx
xxxxssx
xx xxxx
xxx xsx
xsssxxssx
xxx xx

View File

@ -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('};')