diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-06-12 22:34:23 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-06-12 23:18:57 +0200 |
| commit | 5226dc466354cd15dd50b3c11660db7cb38eabed (patch) | |
| tree | a885b4af03b88cb08709f87a1bb643241c505616 /src/camera | |
| parent | b2af4c6bccc4263e5c7aa96efd0f94e1b7a38231 (diff) | |
| download | jancity-5226dc466354cd15dd50b3c11660db7cb38eabed.tar.gz | |
Split peripheral-related logic into its own component
This has several advantages:
- `camera` no longer needs to define public functions for each
peripheral type.
- Peripheral-related is now no longer tighly coupled to human_player,
so peripheral logic can be reused elsewhere e.g.: on menus.
- Makes camera_update_touch consistent compared to equivalent functions,
since now `pan` has now been moved to `camera` (as it should be).
Diffstat (limited to 'src/camera')
| -rw-r--r-- | src/camera/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/camera/inc/camera.h | 6 | ||||
| -rw-r--r-- | src/camera/privinc/camera_private.h | 3 | ||||
| -rw-r--r-- | src/camera/src/camera.c | 18 | ||||
| -rw-r--r-- | src/camera/src/pad.c | 1 | ||||
| -rw-r--r-- | src/camera/src/touch.c | 15 |
6 files changed, 30 insertions, 15 deletions
diff --git a/src/camera/CMakeLists.txt b/src/camera/CMakeLists.txt index f234098..1cb1f63 100644 --- a/src/camera/CMakeLists.txt +++ b/src/camera/CMakeLists.txt @@ -1,3 +1,3 @@ add_library(camera "src/camera.c" "src/pad.c" "src/mouse.c" "src/touch.c") target_include_directories(camera PUBLIC "inc" PRIVATE "privinc") -target_link_libraries(camera PUBLIC container mouse pad terrain util PRIVATE gfx) +target_link_libraries(camera PUBLIC container mouse pad peripheral terrain util PRIVATE gfx) diff --git a/src/camera/inc/camera.h b/src/camera/inc/camera.h index 93ad740..8356bd8 100644 --- a/src/camera/inc/camera.h +++ b/src/camera/inc/camera.h @@ -3,6 +3,7 @@ #include <mouse.h> #include <pad.h> +#include <peripheral.h> #include <util.h> #include <stdbool.h> @@ -15,6 +16,7 @@ struct camera { int x, y, x_speed, y_speed; unsigned int xt, yt; + bool pan; struct cursor { @@ -33,9 +35,7 @@ struct camera extern struct sprite cursor_sprite; -void camera_update_pad(struct camera *cam, const struct pad *p); -void camera_update_mouse(struct camera *cam, const struct mouse *m); -bool camera_update_touch(struct camera *cam, const struct mouse *m); +void camera_update(struct camera *cam, const union peripheral *p); bool camera_translate(const struct camera *cam, const struct util_rect *dim, short *x, short *y); void cursor_init(struct cursor *c); bool cursor_collision(const struct camera *cam, const struct util_rect *d); diff --git a/src/camera/privinc/camera_private.h b/src/camera/privinc/camera_private.h index b9f4773..c3cb64c 100644 --- a/src/camera/privinc/camera_private.h +++ b/src/camera/privinc/camera_private.h @@ -15,6 +15,9 @@ enum }; void camera_update_pos(struct camera *cam); +void camera_update_pad(struct camera *cam, const struct pad *p); +void camera_update_mouse(struct camera *cam, const struct mouse *m); +void camera_update_touch(struct camera *cam, const struct mouse *m); #ifdef __cplusplus } diff --git a/src/camera/src/camera.c b/src/camera/src/camera.c index 0b96629..73f8bf4 100644 --- a/src/camera/src/camera.c +++ b/src/camera/src/camera.c @@ -103,3 +103,21 @@ bool camera_translate(const struct camera *const cam, const struct util_rect *co *y = ty; return true; } + +void camera_update(struct camera *const cam, const union peripheral *const p) +{ + switch (p->common.type) + { + case PERIPHERAL_TYPE_PAD: + camera_update_pad(cam, &p->pad.pad); + break; + + case PERIPHERAL_TYPE_KEYBOARD_MOUSE: + camera_update_mouse(cam, &p->kbm.mouse); + break; + + case PERIPHERAL_TYPE_TOUCH: + camera_update_touch(cam, &p->kbm.mouse); + break; + } +} diff --git a/src/camera/src/pad.c b/src/camera/src/pad.c index 79c05ff..d61ef8b 100644 --- a/src/camera/src/pad.c +++ b/src/camera/src/pad.c @@ -2,6 +2,7 @@ #include <camera_private.h> #include <pad.h> #include <terrain.h> +#include <peripheral.h> #include <util.h> #include <stdbool.h> diff --git a/src/camera/src/touch.c b/src/camera/src/touch.c index 475b308..e4fc118 100644 --- a/src/camera/src/touch.c +++ b/src/camera/src/touch.c @@ -13,9 +13,8 @@ static void cursor_update(struct cursor *const c, const struct mouse *const m) c->y = m->y; } -static bool update_speed(struct camera *const cam, const struct mouse *const m) +static void update_speed(struct camera *const cam, const struct mouse *const m) { - bool ret = false; int *const sx = &cam->x_speed, *const sy = &cam->y_speed; if (mouse_pressed(m, MOUSE_BUTTON_LEFT)) @@ -23,7 +22,7 @@ static bool update_speed(struct camera *const cam, const struct mouse *const m) *sx = m->dx; *sy = m->dy; - ret = *sx || *sy; + cam->pan = *sx || *sy; } else if (*sx || *sy) { @@ -41,17 +40,11 @@ static bool update_speed(struct camera *const cam, const struct mouse *const m) else *sy = 0; } - - return ret; } -bool camera_update_touch(struct camera *const cam, const struct mouse *const m) +void camera_update_touch(struct camera *const cam, const struct mouse *const m) { - bool ret; - cursor_update(&cam->cursor, m); - ret = update_speed(cam, m); + update_speed(cam, m); camera_update_pos(cam); - - return ret; } |
