aboutsummaryrefslogtreecommitdiff
path: root/src/camera
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-03-30 08:36:16 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-03-31 06:50:33 +0200
commit45337576dff70a3b5991b79d0fe13420110b3594 (patch)
treed3f18d3d92a9da8c7d2dd9e596b44eb579cf368c /src/camera
parent82c61e3d1d97c6e829bfbe0e35539abfe75d1380 (diff)
downloadrts-45337576dff70a3b5991b79d0fe13420110b3594.tar.gz
Implement touch controls
Diffstat (limited to 'src/camera')
-rw-r--r--src/camera/CMakeLists.txt2
-rw-r--r--src/camera/inc/camera.h1
-rw-r--r--src/camera/src/touch.c57
3 files changed, 59 insertions, 1 deletions
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 <camera.h>
+#include <mouse.h>
+#include <camera_private.h>
+#include <gfx.h>
+
+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;
+}