kiba-engine
input.c
Go to the documentation of this file.
1 
6 #include <kiba/core/input.h>
7 
8 #include <kiba/core/log.h>
9 #include <kiba/core/memory.h>
10 
15 typedef struct input_state {
23  b8 keys[KEY_CODE_MAX];
25  u32 mouse_x;
27  u32 mouse_y;
37 
39 static input_state current_input_state;
41 static input_state previous_input_state;
42 
43 b8 input_initialize(void) {
44  memory_zero(&current_input_state, sizeof(input_state));
45  memory_zero(&previous_input_state, sizeof(input_state));
46  return true;
47 }
48 
49 void input_shutdown(void) {}
50 
51 void input_update(f64 delta_time) {
52  UNUSED(delta_time);
53  memory_copy(&previous_input_state, &current_input_state, sizeof(input_state));
54  current_input_state.mouse_wheel = 0;
55 }
56 
58  KB_ASSERT_DEBUG(key >= 0 && key < KEY_CODE_MAX, "requested invalid keycode {i32}", key);
59  return current_input_state.keys[key];
60 }
61 
63  KB_ASSERT_DEBUG(key >= 0 && key < KEY_CODE_MAX, "requested invalid keycode {i32}", key);
64  return previous_input_state.keys[key];
65 }
66 
68  KB_ASSERT_DEBUG(key >= 0 && key < KEY_CODE_MAX, "requested invalid keycode {i32}", key);
69  return !(current_input_state.keys[key]);
70 }
71 
73  KB_ASSERT_DEBUG(key >= 0 && key < KEY_CODE_MAX, "requested invalid keycode {i32}", key);
74  return !previous_input_state.keys[key];
75 }
76 
78  KB_ASSERT_DEBUG(key >= 0 && key < KEY_CODE_MAX, "requested invalid keycode {i32}", key);
79  return current_input_state.keys[key] && previous_input_state.keys[key];
80 }
81 
82 void input_mouse_position(u32 *x, u32 *y) {
83  *x = current_input_state.mouse_x;
84  *y = current_input_state.mouse_y;
85 }
86 
87 b8 input_mouse_moved(u32 *x, u32 *y) {
88  *x = current_input_state.mouse_x - previous_input_state.mouse_x;
89  *y = current_input_state.mouse_y - previous_input_state.mouse_y;
90  return *x != 0 && *y != 0;
91 }
92 
93 i8 input_mouse_wheel(void) { return current_input_state.mouse_wheel; }
94 
95 void input_process_key(key_code key, b8 pressed) {
96  if (key < KEY_CODE_MAX)
97  current_input_state.keys[key] = pressed;
98 }
99 
100 void input_process_mouse_position(u32 x, u32 y) {
101  current_input_state.mouse_x = x;
102  current_input_state.mouse_y = y;
103 }
104 
105 void input_process_mouse_wheel(i8 wheel_pos) { current_input_state.mouse_wheel = wheel_pos; }
void * memory_copy(void *dst, const void *src, usize size)
Copy memory.
Definition: memory.c:83
void * memory_zero(void *mem, usize size)
Zero out memory.
Definition: memory.c:79
Lightweight layer between platform and other engine components to enable tracing/monitoring.
#define UNUSED(x)
Mark parameter as unused.
Definition: defines.h:21
b8 input_was_key_down(key_code key)
Check if a key was down before the last update.
Definition: input.c:62
i8 input_mouse_wheel(void)
Get current mouse wheel scrolling direction.
Definition: input.c:93
b8 input_is_key_down(key_code key)
Check if a key is currently down.
Definition: input.c:57
void input_shutdown(void)
Shutdown input system.
Definition: input.c:49
b8 input_is_key_held(key_code key)
Check if a key is held.
Definition: input.c:77
b8 input_was_key_up(key_code key)
Check if a key was up before the last update.
Definition: input.c:72
b8 input_is_key_up(key_code key)
Check if a key is currently up.
Definition: input.c:67
void input_process_mouse_position(u32 x, u32 y)
Set current state for the mouse position.
Definition: input.c:100
struct input_state input_state
State of the input system.
void input_process_mouse_wheel(i8 wheel_pos)
Set current scrolling wheel direction for the mouse.
Definition: input.c:105
void input_update(f64 delta_time)
Updates the input system.
Definition: input.c:51
void input_process_key(key_code key, b8 pressed)
Set current state for a key.
Definition: input.c:95
b8 input_mouse_moved(u32 *x, u32 *y)
Check if mouse moved.
Definition: input.c:87
void input_mouse_position(u32 *x, u32 *y)
Get current mouse position.
Definition: input.c:82
b8 input_initialize(void)
Initialize the input system.
Definition: input.c:43
Input abstraction layer.
key_code
All available keycodes.
Definition: input.h:13
Logging system.
#define KB_ASSERT_DEBUG(expr,...)
Perform runtime debug assertion and log failures.
Definition: log.h:165
State of the input system.
Definition: input.c:15
b8 keys[KEY_CODE_MAX]
State of keys on keypoard and mouse.
Definition: input.c:23
u32 mouse_x
Mouse x position on screen.
Definition: input.c:25
i8 mouse_wheel
Mouse wheel direction.
Definition: input.c:35
u32 mouse_y
Mouse y position on screen.
Definition: input.c:27