From ce8b1c43caf9e97fe0e93a51d6ca6b82460305d7 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Wed, 30 Mar 2022 08:36:16 +0200 Subject: Implement touch controls --- src/camera/CMakeLists.txt | 2 +- src/camera/inc/camera.h | 1 + src/camera/src/touch.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 src/camera/src/touch.c (limited to 'src/camera') diff --git a/src/camera/CMakeLists.txt b/src/camera/CMakeLists.txt index 3d9d113..f234098 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") +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) diff --git a/src/camera/inc/camera.h b/src/camera/inc/camera.h index ec2739b..93ad740 100644 --- a/src/camera/inc/camera.h +++ b/src/camera/inc/camera.h @@ -35,6 +35,7 @@ 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); 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/src/touch.c b/src/camera/src/touch.c new file mode 100644 index 0000000..475b308 --- /dev/null +++ b/src/camera/src/touch.c @@ -0,0 +1,57 @@ +#include +#include +#include +#include + +static void cursor_update(struct cursor *const c, const struct mouse *const m) +{ + if (c->screen.last_w != screen_w + || c->screen.last_h != screen_h) + cursor_init(c); + + c->x = m->x; + c->y = m->y; +} + +static bool 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)) + { + *sx = m->dx; + *sy = m->dy; + + ret = *sx || *sy; + } + else if (*sx || *sy) + { + const int qx = *sx / 4; + + if (qx) + *sx -= qx; + else + *sx = 0; + + const int qy = *sy / 4; + + if (qy) + *sy -= qy; + else + *sy = 0; + } + + return ret; +} + +bool camera_update_touch(struct camera *const cam, const struct mouse *const m) +{ + bool ret; + + cursor_update(&cam->cursor, m); + ret = update_speed(cam, m); + camera_update_pos(cam); + + return ret; +} -- cgit v1.2.3