aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-02-08 15:16:57 +0100
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-03-30 08:20:20 +0200
commit68383dad338b634e271daca1c1f64a159dd475af (patch)
tree5330ab6fbdb08370ff09fdc0335a070566f0cf90 /src
parent91f80f9a2f7908cadf8d602ece41921d4a13a6b4 (diff)
downloadrts-68383dad338b634e271daca1c1f64a159dd475af.tar.gz
Refresh camera and terrain rendering on screen resize
Diffstat (limited to 'src')
-rw-r--r--src/camera/inc/camera.h4
-rw-r--r--src/camera/src/camera.c6
-rw-r--r--src/game/src/game.c1
-rw-r--r--src/terrain/inc/terrain.h3
-rw-r--r--src/terrain/src/terrain.c23
5 files changed, 32 insertions, 5 deletions
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;
}