summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorrealtradam <[email protected]>2023-04-13 23:40:15 -0400
committerrealtradam <[email protected]>2023-04-13 23:40:15 -0400
commit3387c9fef273f2cc0674ea77ed950c0965299c4e (patch)
tree9d56a18e8e4c27f80b6ca720a3f0b4946ba19501 /src
parentf250a4090cf4d535a163388fb5faea5ff1ce48e4 (diff)
downloadRodeoKit-3387c9fef273f2cc0674ea77ed950c0965299c4e.tar.gz
RodeoKit-3387c9fef273f2cc0674ea77ed950c0965299c4e.zip
foundation of input system and initial implementation
Diffstat (limited to 'src')
-rw-r--r--src/input/irodeo_input.h16
-rw-r--r--src/input/rodeo_input.c163
-rw-r--r--src/rodeo.c9
3 files changed, 181 insertions, 7 deletions
diff --git a/src/input/irodeo_input.h b/src/input/irodeo_input.h
new file mode 100644
index 0000000..6cd9992
--- /dev/null
+++ b/src/input/irodeo_input.h
@@ -0,0 +1,16 @@
+
+// -- internal --
+// public
+#include "rodeo/input_t.h"
+
+typedef rodeo_input_scene_t *rodeo_input_scene_p;
+#define i_tag input_scene
+#define i_key rodeo_input_scene_p
+#include <stc/cset.h>
+
+typedef
+struct
+{
+ cset_input_scene active_scenes;
+}
+irodeo_input_state_t;
diff --git a/src/input/rodeo_input.c b/src/input/rodeo_input.c
new file mode 100644
index 0000000..0137e69
--- /dev/null
+++ b/src/input/rodeo_input.c
@@ -0,0 +1,163 @@
+
+// -- internal --
+// public
+#include "rodeo/input_t.h"
+#include "rodeo/input.h"
+#include "irodeo_input.h"
+
+// -- external --
+#include "SDL2/SDL.h"
+
+static irodeo_input_state_t state = {0};
+
+bool
+rodeo_input_events_poll(void)
+{
+ SDL_Event event = {0};
+ while(SDL_PollEvent(&event))
+ {
+ switch(event.type)
+ {
+ case SDL_QUIT:
+ {
+ return true;
+ }
+ break;
+ case SDL_KEYUP:
+ case SDL_KEYDOWN:
+ {
+ c_foreach(i, cset_input_scene, state.active_scenes)
+ {
+ const cmap_input_scancode_scene_value *value = cmap_input_scancode_scene_get(
+ &((*i.ref)->scancode),
+ (rodeo_input_scancode_t)event.key.keysym.scancode
+ );
+ if(value == NULL)
+ {
+ continue;
+ }
+ else
+ {
+ rodeo_input_any_state_t key_state = {
+ .binary_state = event.key.state,
+ .type = rodeo_input_type_Binary
+ };
+ c_foreach(
+ j,
+ cvec_input_callback_functions,
+ value->second
+ )
+ {
+ (**j.ref)(key_state);
+ }
+ }
+ }
+ }
+ break;
+ case SDL_MOUSEBUTTONUP:
+ case SDL_MOUSEBUTTONDOWN:
+ {
+ //event.button.button // which sdl mouse button it was
+ //event.button.state // if thue button was clicked or released
+ c_foreach(i, cset_input_scene, state.active_scenes)
+ {
+ const cmap_input_mouse_scene_value *value = cmap_input_mouse_scene_get(
+ &((*i.ref)->mouse),
+ (rodeo_input_mouse_t)event.button.button
+ );
+ if(value == NULL)
+ {
+ continue;
+ }
+ else
+ {
+ rodeo_input_any_state_t key_state = {
+ .binary_state = event.button.state,
+ .type = rodeo_input_type_Binary
+ };
+ c_foreach(
+ j,
+ cvec_input_callback_functions,
+ value->second
+ )
+ {
+ (**j.ref)(key_state);
+ }
+ }
+ }
+ }
+ break;
+ }
+ }
+ return false;
+}
+
+void
+rodeo_input_scene_activate(
+ rodeo_input_scene_t *scene
+)
+{
+ cset_input_scene_insert(&state.active_scenes, scene);
+}
+
+void
+rodeo_input_scene_deactivate(
+ rodeo_input_scene_t *scene
+)
+{
+ cset_input_scene_erase(&state.active_scenes, scene);
+}
+
+void
+rodeo_input_scene_register_callback(
+ rodeo_input_callback_function func,
+ rodeo_input_scene_t *scene,
+ rodeo_input_register_type_t type
+)
+{
+ switch (type.type)
+ {
+ case rodeo_input_type_Binary:
+ switch (type.binary_type) {
+ case rodeo_input_binary_Scancode:
+ {
+ if(!cmap_input_scancode_scene_contains(&scene->scancode, type.scancode))
+ {
+ cmap_input_scancode_scene_insert(&scene->scancode, type.scancode, cvec_input_callback_functions_init());
+ }
+ cvec_input_callback_functions* callbacks = cmap_input_scancode_scene_at_mut(&scene->scancode, type.scancode);
+ cvec_input_callback_functions_push(callbacks, func);
+ }
+ break;
+
+ case rodeo_input_binary_Mouse:
+ {
+ if(!cmap_input_mouse_scene_contains(&scene->mouse, type.mouse))
+ {
+ cmap_input_mouse_scene_insert(&scene->mouse, type.mouse, cvec_input_callback_functions_init());
+ }
+ cvec_input_callback_functions* callbacks = cmap_input_mouse_scene_at_mut(&scene->mouse, type.mouse);
+ cvec_input_callback_functions_push(callbacks, func);
+ }
+ break;
+
+ case rodeo_input_binary_Invalid:
+ // TODO make it a log
+ break;
+ }
+ break;
+ case rodeo_input_type_Invalid:
+ // TODO make it log
+ break;
+ }
+}
+
+void
+rodeo_input_scene_unregister_callback(
+ rodeo_input_callback_function func,
+ rodeo_input_scene_t *scene,
+ rodeo_input_register_type_t type
+)
+{
+
+}
diff --git a/src/rodeo.c b/src/rodeo.c
index d92a9db..dcac005 100644
--- a/src/rodeo.c
+++ b/src/rodeo.c
@@ -2,6 +2,7 @@
// public
#include "rodeo.h"
#include "rodeo_types.h"
+#include "rodeo/input.h"
// private
#include "rodeo_internal.h"
#include "rodeo_internal_types.h"
@@ -315,13 +316,7 @@ rodeo_frame_end(void)
bgfx_frame(false);
- while(SDL_PollEvent(&state.sdl_event))
- {
- if(state.sdl_event.type == SDL_QUIT)
- {
- state.quit = true;
- }
- }
+ state.quit = rodeo_input_events_poll();
#ifndef __EMSCRIPTEN__
irodeo_frame_stall();
#endif