summaryrefslogtreecommitdiff
path: root/src/util/input.cpp
diff options
context:
space:
mode:
authornavewindre <boneyaard@gmail.com>2025-09-03 20:10:09 +0200
committernavewindre <boneyaard@gmail.com>2025-09-03 20:10:09 +0200
commitf8b92ce3aa08b1445c9f956d8166830946562d12 (patch)
tree94e63a5aec9f8f52b577f56799e0c9201fd976a5 /src/util/input.cpp
a
Diffstat (limited to 'src/util/input.cpp')
-rw-r--r--src/util/input.cpp74
1 files changed, 74 insertions, 0 deletions
diff --git a/src/util/input.cpp b/src/util/input.cpp
new file mode 100644
index 0000000..4a786aa
--- /dev/null
+++ b/src/util/input.cpp
@@ -0,0 +1,74 @@
+#include "input.h"
+#include "SDL_events.h"
+
+INPUT_DATA input;
+
+void input_on_event( SDL_Event* e ) {
+ input.on_input.each( fn( ON_INPUT_FN* func ) { (*func)( e ); } );
+
+ switch( e->type ) {
+ case SDL_MOUSEBUTTONDOWN: {
+ switch( e->button.button ) {
+ case 1: input.mouse.left = 1; break;
+ case 2: input.mouse.middle = 1; break;
+ case 3: input.mouse.right = 1; break;
+ }
+ } break;
+ case SDL_MOUSEBUTTONUP: {
+ switch( e->button.button ) {
+ case 1: input.mouse.left = 0; break;
+ case 2: input.mouse.middle = 0; break;
+ case 3: input.mouse.right = 0; break;
+ }
+ } break;
+ case SDL_MOUSEWHEEL: {
+ input.mouse.wheel = e->wheel.y;
+ } break;
+ case SDL_MOUSEMOTION: {
+ input.mouse.pos.x = (F32)e->motion.x;
+ input.mouse.pos.y = (F32)e->motion.y;
+ } break;
+ case SDL_KEYDOWN: {
+ input.keys[e->key.keysym.sym & 0xff] = 1;
+ } break;
+ case SDL_KEYUP: {
+ input.keys[e->key.keysym.sym & 0xff] = 0;
+ } break;
+ }
+}
+
+
+void input_on_mouse( I32 type, I32 x, I32 y ) {
+ if( type == MOUSEEV_MOVE ) {
+ input.mouse.pos.x = (F32)x;
+ input.mouse.pos.y = (F32)y;
+ }
+
+ if( type == MOUSEEV_DOWN ) {
+ I32 button = x;
+ if( button == MOUSE_LEFT )
+ input.mouse.left = 1;
+ if( button == MOUSE_RIGHT )
+ input.mouse.right = 1;
+ if( button == MOUSE_MIDDLE )
+ input.mouse.middle = 1;
+ if( button == MOUSE_WHEEL )
+ input.mouse.wheel = -1;
+ }
+
+ if( type == MOUSEEV_UP ) {
+ I32 button = x;
+ if( button == MOUSE_LEFT )
+ input.mouse.left = 0;
+ if( button == MOUSE_RIGHT )
+ input.mouse.right = 0;
+ if( button == MOUSE_MIDDLE )
+ input.mouse.middle = 0;
+ if( button == MOUSE_WHEEL )
+ input.mouse.wheel = 1;
+ }
+}
+
+void input_frame_end() {
+ input.mouse.wheel = 0;
+}