From db3ee0d63620bd42f27a6b359f0996df927481dd Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Sat, 27 Jan 2024 17:10:35 +0100 Subject: [PATCH] terrain: Split tile/object rendering --- res/CMakeLists.txt | 18 ++++ src/game/src/res.c | 12 +++ src/terrain/CMakeLists.txt | 2 +- src/terrain/inc/terrain.h | 69 +------------ src/terrain/privinc/terrain_tiles.h | 153 ++++++++++++++++++++++++++++ src/terrain/src/render.c | 70 ++++++++----- 6 files changed, 231 insertions(+), 93 deletions(-) create mode 100644 src/terrain/privinc/terrain_tiles.h diff --git a/res/CMakeLists.txt b/res/CMakeLists.txt index 87da184..75ea4c1 100644 --- a/res/CMakeLists.txt +++ b/res/CMakeLists.txt @@ -64,10 +64,27 @@ sprite(NAME btn_small CY 48 TRANSPARENT TRUE) +sprite(NAME font + X 384 + Y 0 + BPP 4 + CX 384 + CY 48 + TRANSPARENT TRUE) + +sprite(NAME grass + X 384 + Y 0 + BPP 4 + CX 384 + CY 48 + TRANSPARENT TRUE) + level(NAME city1) container(NAME jancity SPRITES + grass sidewalk roof1 roof2 @@ -76,4 +93,5 @@ container(NAME jancity btn_mid btn_right btn_small + font ) diff --git a/src/game/src/res.c b/src/game/src/res.c index cb3060e..f38443e 100644 --- a/src/game/src/res.c +++ b/src/game/src/res.c @@ -61,6 +61,18 @@ static const struct container c[] = .type = CONTAINER_TYPE_SPRITE, .data.sprite = &gui_button_sprites[GUI_BUTTON_SMALL] }, + + { + .path = "font", + .type = CONTAINER_TYPE_SPRITE, + .data.sprite = &font_sprite + }, + + { + .path = "grass", + .type = CONTAINER_TYPE_SPRITE, + .data.sprite = &terrain_sprites[GRASS] + }, }; static bool init; diff --git a/src/terrain/CMakeLists.txt b/src/terrain/CMakeLists.txt index 18726c8..8ecded3 100644 --- a/src/terrain/CMakeLists.txt +++ b/src/terrain/CMakeLists.txt @@ -3,5 +3,5 @@ add_library(terrain "src/render.c" "src/update.c" ) -target_include_directories(terrain PUBLIC "inc") +target_include_directories(terrain PUBLIC "inc" PRIVATE "privinc") target_link_libraries(terrain PUBLIC container camera gfx util) diff --git a/src/terrain/inc/terrain.h b/src/terrain/inc/terrain.h index f6cf37f..7997025 100644 --- a/src/terrain/inc/terrain.h +++ b/src/terrain/inc/terrain.h @@ -13,78 +13,12 @@ extern "C" enum { - MAP_TILES = 64, + MAP_TILES = 120, TERRAIN_SZ = 16, MAP_W = MAP_TILES * TERRAIN_SZ, MAP_H = MAP_TILES * TERRAIN_SZ }; -enum terrain_type -{ - ROOF1_1_NW, - ROOF1_1_N, - ROOF1_1_NE, - ROOF1_2_NW, - ROOF1_2_NE, - ROOF1_3_NW, - ROOF1_3_N, - ROOF1_3_NE, - ROOF1_4_N, - - ROOF1_1_W, - ROOF1_1_C, - ROOF1_1_E, - ROOF1_2_SW, - ROOF1_2_SE, - ROOF1_3_SW, - ROOF1_3_S, - ROOF1_3_SE, - ROOF1_4_C, - - ROOF1_1_SW, - ROOF1_1_S, - ROOF1_1_SE, - ROOF1_5_W, - ROOF1_5_C, - ROOF1_5_E, - ROOF1_6, - ROOF1_4_S, - - ROOF2_1_NW, - ROOF2_1_N, - ROOF2_1_NE, - ROOF2_2_NW, - ROOF2_2_NE, - ROOF2_3_NW, - ROOF2_3_N, - ROOF2_3_NE, - ROOF2_4_N, - - ROOF2_1_W, - ROOF2_1_C, - ROOF2_1_E, - ROOF2_2_SW, - ROOF2_2_SE, - ROOF2_3_SW, - ROOF2_3_S, - ROOF2_3_SE, - ROOF2_4_C, - - ROOF2_1_SW, - ROOF2_1_S, - ROOF2_1_SE, - ROOF2_5_W, - ROOF2_5_C, - ROOF2_5_E, - ROOF2_6, - ROOF2_4_S, - - ROOF1_START = ROOF1_1_NW, - ROOF1_END = ROOF1_4_S, - ROOF2_START = ROOF2_1_NW, - ROOF2_END = ROOF2_4_S -}; - struct terrain_map { struct terrain_tile @@ -102,6 +36,7 @@ int terrain_render(const struct terrain_map *map, const struct camera *cam); enum { SIDEWALK, + GRASS, ROOF1, ROOF2, BUILDING1, diff --git a/src/terrain/privinc/terrain_tiles.h b/src/terrain/privinc/terrain_tiles.h new file mode 100644 index 0000000..962bafe --- /dev/null +++ b/src/terrain/privinc/terrain_tiles.h @@ -0,0 +1,153 @@ +#ifndef TERRAIN_TILES_H +#define TERRAIN_TILES_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +enum +{ + /* SIDEWALK */ + SIDEWALK_1_NW, + SIDEWALK_1_N, + SIDEWALK_1_NE, + SIDEWALK_2_NW, + SIDEWALK_2_NE, + SIDEWALK_3_NW, + SIDEWALK_3_N, + SIDEWALK_3_NE, + SIDEWALK_4_N, + + SIDEWALK_1_W, + SIDEWALK_1_C, + SIDEWALK_1_E, + SIDEWALK_2_SW, + SIDEWALK_2_SE, + SIDEWALK_3_SW, + SIDEWALK_3_S, + SIDEWALK_3_SE, + SIDEWALK_4_C, + + SIDEWALK_1_SW, + SIDEWALK_1_S, + SIDEWALK_1_SE, + SIDEWALK_5_W, + SIDEWALK_5_C, + SIDEWALK_5_E, + SIDEWALK_6, + SIDEWALK_4_S, + + SIDEWALK_START = SIDEWALK_1_NW, + SIDEWALK_END = SIDEWALK_4_S, + + /* ROOF1 */ + ROOF1_1_NW, + ROOF1_1_N, + ROOF1_1_NE, + ROOF1_2_NW, + ROOF1_2_NE, + ROOF1_3_NW, + ROOF1_3_N, + ROOF1_3_NE, + ROOF1_4_N, + + ROOF1_1_W, + ROOF1_1_C, + ROOF1_1_E, + ROOF1_2_SW, + ROOF1_2_SE, + ROOF1_3_SW, + ROOF1_3_S, + ROOF1_3_SE, + ROOF1_4_C, + + ROOF1_1_SW, + ROOF1_1_S, + ROOF1_1_SE, + ROOF1_5_W, + ROOF1_5_C, + ROOF1_5_E, + ROOF1_6, + ROOF1_4_S, + + ROOF1_START = ROOF1_1_NW, + ROOF1_END = ROOF1_4_S, + + /* ROOF2 */ + ROOF2_1_NW, + ROOF2_1_N, + ROOF2_1_NE, + ROOF2_2_NW, + ROOF2_2_NE, + ROOF2_3_NW, + ROOF2_3_N, + ROOF2_3_NE, + ROOF2_4_N, + + ROOF2_1_W, + ROOF2_1_C, + ROOF2_1_E, + ROOF2_2_SW, + ROOF2_2_SE, + ROOF2_3_SW, + ROOF2_3_S, + ROOF2_3_SE, + ROOF2_4_C, + + ROOF2_1_SW, + ROOF2_1_S, + ROOF2_1_SE, + ROOF2_5_W, + ROOF2_5_C, + ROOF2_5_E, + ROOF2_6, + ROOF2_4_S, + + ROOF2_START = ROOF2_1_NW, + ROOF2_END = ROOF2_4_S +}; + +enum +{ + OBJECT_NONE, + + /* GRASS */ + GRASS_1_NW, + GRASS_1_N, + GRASS_1_NE, + GRASS_2_NW, + GRASS_2_NE, + GRASS_3_NW, + GRASS_3_N, + GRASS_3_NE, + GRASS_4_N, + + GRASS_1_W, + GRASS_1_C, + GRASS_1_E, + GRASS_2_SW, + GRASS_2_SE, + GRASS_3_SW, + GRASS_3_S, + GRASS_3_SE, + GRASS_4_C, + + GRASS_1_SW, + GRASS_1_S, + GRASS_1_SE, + GRASS_5_W, + GRASS_5_C, + GRASS_5_E, + GRASS_6, + GRASS_4_S, + + GRASS_START = GRASS_1_NW, + GRASS_END = GRASS_4_S +}; + +#ifdef __cplusplus +} +#endif + +#endif /* TERRAIN_TILES_H */ diff --git a/src/terrain/src/render.c b/src/terrain/src/render.c index de354d2..8b35d95 100644 --- a/src/terrain/src/render.c +++ b/src/terrain/src/render.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -12,33 +13,20 @@ struct sprite terrain_sprites[MAX_TERRAIN_SPRITES]; -static int render_tile(const struct terrain_tile *const t, const short x, - const short y) +struct tile { - static const struct tile - { - const struct sprite *s; - int start, end; - } tiles[] = - { - { - .s = &terrain_sprites[ROOF1], - .start = ROOF1_START, - .end = ROOF1_END - }, + const struct sprite *s; + int start, end; +}; - { - .s = &terrain_sprites[ROOF2], - .start = ROOF2_START, - .end = ROOF2_END - } - }; - - for (size_t i = 0; i < sizeof tiles / sizeof *tiles; i++) +static int render_tile(const unsigned char id, const short x, + const short y, const struct tile *const tiles, const size_t n) +{ + for (size_t i = 0; i < n / n; i++) { const struct tile *const rt = &tiles[i]; - if (t->t >= rt->start && t->t <= rt->end) + if (id >= rt->start && id <= rt->end) { sprite_get_or_ret(s, -1); @@ -48,7 +36,7 @@ static int render_tile(const struct terrain_tile *const t, const short x, return -1; } - const unsigned char pos = t->t - rt->start; + const unsigned char pos = id - rt->start; const short tx = pos % TERRAIN_SZ, ty = pos / TERRAIN_SZ; s->x = x; @@ -67,10 +55,41 @@ static int render_tile(const struct terrain_tile *const t, const short x, } } - fprintf(stderr, "%s: unknown tile %#hhx\n", __func__, t->t); + fprintf(stderr, "%s: unknown tile %#hhx\n", __func__, id); return -1; } +static int render_ground(const struct terrain_tile *const t, const short x, + const short y) +{ + static const struct tile tiles[] = + { +#define TILE(t) {.s = &terrain_sprites[t], .start = t##_START, .end = t##_END} + TILE(SIDEWALK), + TILE(ROOF1), + TILE(ROOF2) +#undef TILE + }; + + return render_tile(t->t, x, y, tiles, sizeof tiles / sizeof *tiles); +} + +static int render_object(const struct terrain_tile *const t, const short x, + const short y) +{ + if (t->obj == OBJECT_NONE) + return 0; + + static const struct tile tiles[] = + { +#define TILE(t) {.s = &terrain_sprites[t], .start = t##_START, .end = t##_END} + TILE(GRASS), +#undef TILE + }; + + return render_tile(t->obj, x, y, tiles, sizeof tiles / sizeof *tiles); +} + int terrain_render(const struct terrain_map *const map, const struct camera *const cam) { @@ -101,7 +120,8 @@ int terrain_render(const struct terrain_map *const map, { const struct terrain_tile *const t = &map->m[y.i][x.i]; - if (render_tile(t, x.p, y.p)) + if (render_ground(t, x.p, y.p) + || render_object(t, x.p, y.p)) return -1; }