Multiple tiles

This commit is contained in:
Juhani Krekelä 2022-01-15 15:21:30 +00:00
parent b69249d230
commit e19444181a
7 changed files with 108 additions and 27 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
platformer
*.inc

View File

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

13
dirt_tile.txt Normal file
View File

@ -0,0 +1,13 @@
GRASS_BROWN
.GRASS_DARK
----------
.
. .
. .
. .
.
.

14
flag_tile.txt Normal file
View File

@ -0,0 +1,14 @@
|FLAG_STEM
xFLAG_FLAG
.BG
----------
.|xx......
.|xxxx....
.|xxxxxx..
.|xxxxxxx.
.|xxxxxx..
.|xxxx....
.|xx......
.|........
.|........
.|........

14
grass_tile.txt Normal file
View File

@ -0,0 +1,14 @@
xGRASS_TOP
GRASS_BROWN
.GRASS_DARK
----------
xxxxxxxxxx
xx xx x x
x x x
. .
. .
.
.

View File

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

32
tile_compiler.py Executable file
View File

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