diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-03-30 08:36:16 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-03-31 06:50:33 +0200 |
| commit | ce8b1c43caf9e97fe0e93a51d6ca6b82460305d7 (patch) | |
| tree | 462ed058f56f76bff22ed55da34a36dc348a04dc /src/camera | |
| parent | 1950fe7b0679c6b6486cc7b25bef813db2b1bb4e (diff) | |
| download | jancity-ce8b1c43caf9e97fe0e93a51d6ca6b82460305d7.tar.gz | |
Implement touch controls
Diffstat (limited to 'src/camera')
| -rw-r--r-- | src/camera/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/camera/inc/camera.h | 1 | ||||
| -rw-r--r-- | src/camera/src/touch.c | 57 |
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; +} |
