From 9f9c0226d03c060c3c5b12b7a8d37d2e8b372a74 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Tue, 8 Feb 2022 15:16:57 +0100 Subject: Refresh camera and terrain rendering on screen resize --- src/camera/inc/camera.h | 4 ++++ src/camera/src/camera.c | 6 ++++++ src/game/src/game.c | 1 + src/terrain/inc/terrain.h | 3 ++- src/terrain/src/terrain.c | 23 +++++++++++++++++++---- 5 files changed, 32 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/camera/inc/camera.h b/src/camera/inc/camera.h index 1daebd2..151ea74 100644 --- a/src/camera/inc/camera.h +++ b/src/camera/inc/camera.h @@ -23,6 +23,10 @@ struct camera CURSOR_STATE_IDLE, CURSOR_STATE_PRESSED } state; + struct + { + int last_w, last_h; + } screen; } cursor; }; diff --git a/src/camera/src/camera.c b/src/camera/src/camera.c index 4b046e1..5146108 100644 --- a/src/camera/src/camera.c +++ b/src/camera/src/camera.c @@ -45,6 +45,10 @@ static void cursor_update(struct camera *const cam, const struct pad *const p) struct cursor *const c = &cam->cursor; enum {STEP = 4}; + if (c->screen.last_w != screen_w + || c->screen.last_h != screen_h) + cursor_init(c); + if (pad_pressed(p, PAD_KEY_LEFT) && (c->x - STEP) && (!cam->x || c->x != c->x_init)) @@ -97,6 +101,8 @@ void cursor_init(struct cursor *const c) { c->x = c->x_init = (screen_w / 2) - CURSOR_WIDTH; c->y = c->y_init = (screen_h / 2) - CURSOR_HEIGHT; + c->screen.last_w = screen_w; + c->screen.last_h = screen_h; } static void update_speed(struct camera *const cam, const struct pad *const p) diff --git a/src/game/src/game.c b/src/game/src/game.c index 2f13a64..8907be9 100644 --- a/src/game/src/game.c +++ b/src/game/src/game.c @@ -79,6 +79,7 @@ int game(void) }; exit |= human_player_update(&humans[i], &o); + terrain_update(&map); if (terrain_render(&map, &h->cam) || human_player_render(h, &o) diff --git a/src/terrain/inc/terrain.h b/src/terrain/inc/terrain.h index 9552555..d5f4e12 100644 --- a/src/terrain/inc/terrain.h +++ b/src/terrain/inc/terrain.h @@ -26,10 +26,11 @@ enum terrain_type struct terrain_map { enum terrain_type m[MAP_TILES][MAP_TILES]; - int nx, ny; + int nx, ny, last_w, last_h; }; void terrain_init(struct terrain_map *map); +void terrain_update(struct terrain_map *map); int terrain_render(const struct terrain_map *map, const struct camera *cam); extern struct sprite grass_sprite; diff --git a/src/terrain/src/terrain.c b/src/terrain/src/terrain.c index 04df6e6..a528c10 100644 --- a/src/terrain/src/terrain.c +++ b/src/terrain/src/terrain.c @@ -7,6 +7,25 @@ struct sprite grass_sprite; +void terrain_update(struct terrain_map *const map) +{ + if (map->last_w != screen_w) + { + const int extra = !!(screen_w % TERRAIN_SZ); + + map->nx = screen_w / TERRAIN_SZ + extra; + map->last_w = screen_w; + } + + if (map->last_h != screen_h) + { + const int extra = !!(screen_h % TERRAIN_SZ); + + map->ny = screen_h / TERRAIN_SZ + extra; + map->last_h = screen_h; + } +} + int terrain_render(const struct terrain_map *const map, const struct camera *const cam) { @@ -59,9 +78,5 @@ int terrain_render(const struct terrain_map *const map, void terrain_init(struct terrain_map *const map) { - const int extra = !!(screen_w % TERRAIN_SZ); - memset(map, 0, sizeof *map); - map->nx = screen_w / TERRAIN_SZ + extra; - map->ny = screen_h / TERRAIN_SZ + extra; } -- cgit v1.2.3