aboutsummaryrefslogtreecommitdiff
path: root/src/camera
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-02-24 17:55:57 +0100
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-03-30 08:20:21 +0200
commit9eee43d3bb24000077602a62dfdfeee2606f1589 (patch)
tree0e5f8efef62b068e252fe9c98c14fec723e0a7a3 /src/camera
parent18717569acda82b26099c62410df3b398d596ba1 (diff)
downloadrts-9eee43d3bb24000077602a62dfdfeee2606f1589.tar.gz
Add support for keyboard and mouse
Diffstat (limited to 'src/camera')
-rw-r--r--src/camera/CMakeLists.txt6
-rw-r--r--src/camera/inc/camera.h4
-rw-r--r--src/camera/privinc/camera_private.h17
-rw-r--r--src/camera/src/camera.c104
-rw-r--r--src/camera/src/mouse.c37
-rw-r--r--src/camera/src/pad.c106
6 files changed, 169 insertions, 105 deletions
diff --git a/src/camera/CMakeLists.txt b/src/camera/CMakeLists.txt
index fd029b0..3d9d113 100644
--- a/src/camera/CMakeLists.txt
+++ b/src/camera/CMakeLists.txt
@@ -1,3 +1,3 @@
-add_library(camera "src/camera.c")
-target_include_directories(camera PUBLIC "inc")
-target_link_libraries(camera PUBLIC container pad util PRIVATE gfx terrain)
+add_library(camera "src/camera.c" "src/pad.c" "src/mouse.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 151ea74..ec2739b 100644
--- a/src/camera/inc/camera.h
+++ b/src/camera/inc/camera.h
@@ -1,6 +1,7 @@
#ifndef CAMERA_H
#define CAMERA_H
+#include <mouse.h>
#include <pad.h>
#include <util.h>
#include <stdbool.h>
@@ -32,7 +33,8 @@ struct camera
extern struct sprite cursor_sprite;
-void camera_update(struct camera *cam, const struct pad *p);
+void camera_update_pad(struct camera *cam, const struct pad *p);
+void camera_update_mouse(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/privinc/camera_private.h b/src/camera/privinc/camera_private.h
new file mode 100644
index 0000000..f571f2d
--- /dev/null
+++ b/src/camera/privinc/camera_private.h
@@ -0,0 +1,17 @@
+#ifndef CAMERA_PRIVATE_H
+#define CAMERA_PRIVATE_H
+
+#include <camera.h>
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+void camera_update_pos(struct camera *cam);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CAMERA_PRIVATE_H */
diff --git a/src/camera/src/camera.c b/src/camera/src/camera.c
index 4f529df..21c18c7 100644
--- a/src/camera/src/camera.c
+++ b/src/camera/src/camera.c
@@ -1,5 +1,7 @@
#include <camera.h>
+#include <camera_private.h>
#include <gfx.h>
+#include <mouse.h>
#include <pad.h>
#include <terrain.h>
#include <util.h>
@@ -40,37 +42,6 @@ void cursor_pos(const struct camera *const cam, unsigned long *const x,
*y = c->y - cam->y;
}
-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))
- c->x -= STEP;
- else if (pad_pressed(p, PAD_KEY_RIGHT)
- && (c->x + STEP < screen_w)
- && (c->x != c->x_init || cam->x <= -MAP_X))
- c->x += STEP;
-
- if (pad_pressed(p, PAD_KEY_UP)
- && (c->y - STEP)
- && (c->y != c->y_init || !cam->y))
- c->y -= STEP;
- else if (pad_pressed(p, PAD_KEY_DOWN)
- && (c->y + STEP < screen_h)
- && (c->y != c->y_init || cam->y <= -MAP_Y))
- c->y += STEP;
-
- c->state = pad_pressed(p, PAD_KEY_A) || pad_pressed(p, PAD_KEY_B) ?
- CURSOR_STATE_PRESSED: CURSOR_STATE_IDLE;
-}
-
int cursor_render(const struct cursor *const c)
{
sprite_get_or_ret(s, -1);
@@ -105,69 +76,7 @@ void cursor_init(struct cursor *const c)
c->screen.last_h = screen_h;
}
-static void update_speed(struct camera *const cam, const struct pad *const p)
-{
- enum
- {
- MAX_SPEED = 10,
- STEP = 1,
- T_STEP = 3
- };
-
- const struct cursor *const c = &cam->cursor;
-
- if (c->x == c->x_init
- && (!cam->x_speed || ++cam->xt >= T_STEP))
- {
- if (pad_pressed(p, PAD_KEY_RIGHT))
- {
- if (cam->x_speed > 0)
- cam->x_speed = -STEP;
- else if (cam->x_speed - STEP >= -MAX_SPEED)
- cam->x_speed -= STEP;
- }
- else if (pad_pressed(p, PAD_KEY_LEFT))
- {
- if (cam->x_speed < 0)
- cam->x_speed = STEP;
- else if (cam->x_speed + STEP <= MAX_SPEED)
- cam->x_speed += STEP;
- }
- else
- cam->x_speed = 0;
-
- cam->xt = 0;
- }
- else if (c->x != c->x_init)
- cam->x_speed = 0;
-
- if (c->y == c->y_init
- && (!cam->y_speed || ++cam->yt >= T_STEP))
- {
- if (pad_pressed(p, PAD_KEY_DOWN))
- {
- if (cam->y_speed > 0)
- cam->y_speed = STEP;
- else if (cam->y_speed - STEP >= -MAX_SPEED)
- cam->y_speed -= STEP;
- }
- else if (pad_pressed(p, PAD_KEY_UP))
- {
- if (cam->y_speed < 0)
- cam->y_speed = -STEP;
- else if (cam->y_speed + STEP <= MAX_SPEED)
- cam->y_speed += STEP;
- }
- else
- cam->y_speed = 0;
-
- cam->yt = 0;
- }
- else if (c->y != c->y_init)
- cam->y_speed = 0;
-}
-
-static void update_pos(struct camera *const cam)
+void camera_update_pos(struct camera *const cam)
{
const int x = cam->x + cam->x_speed;
@@ -188,13 +97,6 @@ static void update_pos(struct camera *const cam)
cam->y = -MAP_Y;
}
-void camera_update(struct camera *const cam, const struct pad *const p)
-{
- cursor_update(cam, p);
- update_speed(cam, p);
- update_pos(cam);
-}
-
bool camera_translate(const struct camera *const cam, const struct util_rect *const dim,
short *const x, short *const y)
{
diff --git a/src/camera/src/mouse.c b/src/camera/src/mouse.c
new file mode 100644
index 0000000..eff15bf
--- /dev/null
+++ b/src/camera/src/mouse.c
@@ -0,0 +1,37 @@
+#include <camera.h>
+#include <camera_private.h>
+#include <gfx.h>
+#include <mouse.h>
+#include <util.h>
+#include <stdbool.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 void update_speed(struct camera *const cam, const struct mouse *const m)
+{
+ enum
+ {
+ MAX_SPEED = 10,
+ STEP = 1,
+ T_STEP = 3
+ };
+
+ struct cursor *const c = &cam->cursor;
+
+
+}
+
+void camera_update_mouse(struct camera *const cam, const struct mouse *const m)
+{
+ cursor_update(&cam->cursor, m);
+ update_speed(cam, m);
+ camera_update_pos(cam);
+}
diff --git a/src/camera/src/pad.c b/src/camera/src/pad.c
new file mode 100644
index 0000000..b445e53
--- /dev/null
+++ b/src/camera/src/pad.c
@@ -0,0 +1,106 @@
+#include <camera.h>
+#include <camera_private.h>
+#include <pad.h>
+#include <terrain.h>
+#include <util.h>
+#include <stdbool.h>
+
+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))
+ c->x -= STEP;
+ else if (pad_pressed(p, PAD_KEY_RIGHT)
+ && (c->x + STEP < screen_w)
+ && (c->x != c->x_init || cam->x <= -MAP_X))
+ c->x += STEP;
+
+ if (pad_pressed(p, PAD_KEY_UP)
+ && (c->y - STEP)
+ && (c->y != c->y_init || !cam->y))
+ c->y -= STEP;
+ else if (pad_pressed(p, PAD_KEY_DOWN)
+ && (c->y + STEP < screen_h)
+ && (c->y != c->y_init || cam->y <= -MAP_Y))
+ c->y += STEP;
+
+ c->state = pad_pressed(p, PAD_KEY_A) || pad_pressed(p, PAD_KEY_B) ?
+ CURSOR_STATE_PRESSED: CURSOR_STATE_IDLE;
+}
+
+static void update_speed(struct camera *const cam, const struct pad *const p)
+{
+ enum
+ {
+ MAX_SPEED = 10,
+ STEP = 1,
+ T_STEP = 3
+ };
+
+ const struct cursor *const c = &cam->cursor;
+
+ if (c->x == c->x_init
+ && (!cam->x_speed || ++cam->xt >= T_STEP))
+ {
+ if (pad_pressed(p, PAD_KEY_RIGHT))
+ {
+ if (cam->x_speed > 0)
+ cam->x_speed = -STEP;
+ else if (cam->x_speed - STEP >= -MAX_SPEED)
+ cam->x_speed -= STEP;
+ }
+ else if (pad_pressed(p, PAD_KEY_LEFT))
+ {
+ if (cam->x_speed < 0)
+ cam->x_speed = STEP;
+ else if (cam->x_speed + STEP <= MAX_SPEED)
+ cam->x_speed += STEP;
+ }
+ else
+ cam->x_speed = 0;
+
+ cam->xt = 0;
+ }
+ else if (c->x != c->x_init)
+ cam->x_speed = 0;
+
+ if (c->y == c->y_init
+ && (!cam->y_speed || ++cam->yt >= T_STEP))
+ {
+ if (pad_pressed(p, PAD_KEY_DOWN))
+ {
+ if (cam->y_speed > 0)
+ cam->y_speed = STEP;
+ else if (cam->y_speed - STEP >= -MAX_SPEED)
+ cam->y_speed -= STEP;
+ }
+ else if (pad_pressed(p, PAD_KEY_UP))
+ {
+ if (cam->y_speed < 0)
+ cam->y_speed = -STEP;
+ else if (cam->y_speed + STEP <= MAX_SPEED)
+ cam->y_speed += STEP;
+ }
+ else
+ cam->y_speed = 0;
+
+ cam->yt = 0;
+ }
+ else if (c->y != c->y_init)
+ cam->y_speed = 0;
+}
+
+void camera_update_pad(struct camera *const cam, const struct pad *const p)
+{
+ cursor_update(cam, p);
+ update_speed(cam, p);
+ camera_update_pos(cam);
+}