terrain: Split tile/object rendering
This commit is contained in:
parent
c006570fec
commit
bb2abb98ec
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include <terrain.h>
|
||||
#include <terrain_tiles.h>
|
||||
#include <camera.h>
|
||||
#include <gfx.h>
|
||||
#include <util.h>
|
||||
|
@ -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)
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue