Compare commits

...

2 Commits

Author SHA1 Message Date
Xavier Del Campo Romero bb2abb98ec
terrain: Split tile/object rendering 2024-01-27 17:10:35 +01:00
Xavier Del Campo Romero c006570fec
Add btn_small 2024-01-27 17:09:39 +01:00
9 changed files with 90 additions and 99 deletions

View File

@ -56,6 +56,14 @@ sprite(NAME btn_right
CY 48
TRANSPARENT TRUE)
sprite(NAME btn_small
X 384
Y 0
BPP 4
CX 384
CY 48
TRANSPARENT TRUE)
level(NAME city1)
container(NAME jancity
@ -67,4 +75,5 @@ container(NAME jancity
btn_left
btn_mid
btn_right
btn_small
)

View File

@ -19,36 +19,48 @@ static const struct container c[] =
.type = CONTAINER_TYPE_SPRITE,
.data.sprite = &terrain_sprites[SIDEWALK]
},
{
.path = "roof1",
.type = CONTAINER_TYPE_SPRITE,
.data.sprite = &terrain_sprites[ROOF1]
},
{
.path = "roof2",
.type = CONTAINER_TYPE_SPRITE,
.data.sprite = &terrain_sprites[ROOF2]
},
{
.path = "cursor",
.type = CONTAINER_TYPE_SPRITE,
.data.sprite = &cursor_sprite
},
{
.path = "btn_left",
.type = CONTAINER_TYPE_SPRITE,
.data.sprite = &gui_button_sprites[GUI_BUTTON_LEFT]
},
{
.path = "btn_mid",
.type = CONTAINER_TYPE_SPRITE,
.data.sprite = &gui_button_sprites[GUI_BUTTON_MID]
},
{
.path = "btn_right",
.type = CONTAINER_TYPE_SPRITE,
.data.sprite = &gui_button_sprites[GUI_BUTTON_RIGHT]
},
{
.path = "btn_small",
.type = CONTAINER_TYPE_SPRITE,
.data.sprite = &gui_button_sprites[GUI_BUTTON_SMALL]
},
};
static bool init;

View File

@ -50,6 +50,7 @@ enum
GUI_BUTTON_LEFT,
GUI_BUTTON_MID,
GUI_BUTTON_RIGHT,
GUI_BUTTON_SMALL,
MAX_GUI_BUTTON_SPRITES
};

View File

@ -71,7 +71,7 @@ static int render_mid(const struct gui_button *const b,
}
else
{
fprintf(stderr, "%s: unexpected negative size for button %p\n",
fprintf(stderr, "%s: unexpected zero or negative size for button %p\n",
__func__, (void *)b);
return -1;
}

View File

@ -462,6 +462,7 @@ int human_player_render(const struct human_player *const h,
if (render_target(h)
|| render_own_units(h)
|| render_own_buildings(h)
|| human_player_gui_render(h)
|| input_render(&h->in, h->periph))
return -1;

View File

@ -5,6 +5,7 @@
#include <unit.h>
#include <gui.h>
#include <gui/bar.h>
#include <gui/button.h>
#include <gui/container.h>
#include <gui/label.h>
#include <gui/progress_bar.h>
@ -96,6 +97,19 @@ static int render_sel_multiple(const struct human_player *const h,
return gui_render(r);
}
static int render_top(const struct human_player *const h)
{
struct gui_button b;
gui_button_init(&b, GUI_BUTTON_TYPE_SPRITE);
b.on_pressed = NULL;
b.arg = NULL;
b.u.sprite.s = &gui_button_sprites[GUI_BUTTON_SMALL];
b.common.x = screen_w - b.u.sprite.s->w;
return gui_render(&b.common);
}
static int render_sel(const struct human_player *const h)
{
struct gui_rounded_rect r;
@ -106,15 +120,15 @@ static int render_sel(const struct human_player *const h)
r.h = screen_h / 4;
r.common.y = screen_h - r.h;
if (h->n_sel == 1)
return render_sel_single(h, &r.common);
return render_sel_multiple(h, &r.common);
return h->n_sel == 1 ? render_sel_single(h, &r.common)
: render_sel_multiple(h, &r.common);
}
int human_player_gui_render(const struct human_player *const h)
{
if (h->n_sel && render_sel(h))
if (render_top(h))
return -1;
else if (h->n_sel && render_sel(h))
return -1;
return 0;

View File

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

View File

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

View File

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