From 3fa74e8f9e4dd189bbb68d912da5cd64b31a83c1 Mon Sep 17 00:00:00 2001 From: herbglitch Date: Fri, 11 Nov 2022 01:15:54 -0700 Subject: [PATCH] input and handler possibly fixed and point --- include/arc/input/keyboard.h | 32 ++++++++++ include/arc/input/mouse.h | 40 ++++++++++++ include/arc/input/sdl/keyboard.h | 21 +++++++ include/arc/input/sdl/mouse.h | 24 ++++++++ include/arc/math/point.h | 14 +++++ src/input/sdl/keyboard.c | 56 +++++++++++++++++ src/input/sdl/mouse.c | 101 +++++++++++++++++++++++++++++++ 7 files changed, 288 insertions(+) create mode 100644 include/arc/input/keyboard.h create mode 100644 include/arc/input/mouse.h create mode 100644 include/arc/input/sdl/keyboard.h create mode 100644 include/arc/input/sdl/mouse.h create mode 100644 include/arc/math/point.h create mode 100644 src/input/sdl/keyboard.c create mode 100644 src/input/sdl/mouse.c diff --git a/include/arc/input/keyboard.h b/include/arc/input/keyboard.h new file mode 100644 index 0000000..7d562a4 --- /dev/null +++ b/include/arc/input/keyboard.h @@ -0,0 +1,32 @@ +#ifndef ARC_GRAPHICS_KEYBOARD_H_ +#define ARC_GRAPHICS_KEYBOARD_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +typedef struct ARC_Keyboard ARC_Keyboard; + +typedef struct ARC_KeyboardInfo ARC_KeyboardInfo; + +typedef enum ARC_KeyboardState { + ARC_KEY_NONE, + ARC_KEY_PRESSED, + ARC_KEY_RELEASED +} ARC_KeyboardState; + +#define ARC_KEYBOARD_BUTTON_NUM 239 + +void ARC_Keyboard_Create(ARC_Keyboard **keyboard, ARC_KeyboardInfo *info); +void ARC_Keyboard_Destroy(ARC_Keyboard *keyboard); +void ARC_Keyboard_Update(ARC_Keyboard *keyboard); + +ARC_KeyboardState ARC_Keyboard_GetState(ARC_Keyboard *keyboard, uint8_t keys); + +#ifdef __cplusplus +} +#endif + +#endif // !ARC_GRAPHICS_KEYBOARD_H_ \ No newline at end of file diff --git a/include/arc/input/mouse.h b/include/arc/input/mouse.h new file mode 100644 index 0000000..5369a22 --- /dev/null +++ b/include/arc/input/mouse.h @@ -0,0 +1,40 @@ +#ifndef ARC_GRAPHICS_MOUSE_H_ +#define ARC_GRAPHICS_MOUSE_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "arc/math/point.h" + +typedef struct ARC_Mouse ARC_Mouse; + +typedef struct ARC_MouseInfo ARC_MouseInfo; + +typedef enum ARC_MouseState { + ARC_MOUSE_NONE, + ARC_MOUSE_PRESSED, + ARC_MOUSE_RELEASED +} ARC_MouseState; + +typedef enum ARC_MouseButton { + ARC_MOUSE_LEFT = 0, + ARC_MOUSE_MIDDLE = 1, + ARC_MOUSE_RIGHT = 2, + ARC_MOUSE_X1 = 3, + ARC_MOUSE_X2 = 4 +} ARC_MouseButton; + +#define ARC_MOUSE_BUTTON_NUM 5 + +void ARC_Mouse_Create(ARC_Mouse **mouse, ARC_MouseInfo *info); +void ARC_Mouse_Destroy(ARC_Mouse *mouse); +void ARC_Mouse_Update(ARC_Mouse *mouse); +ARC_Point ARC_Mouse_GetCoords(ARC_Mouse *mouse); +ARC_MouseState ARC_Mouse_GetState(ARC_Mouse *mouse, ARC_MouseButton button); + +#ifdef __cplusplus +} +#endif + +#endif // !ARC_GRAPHICS_MOUSE_H_ \ No newline at end of file diff --git a/include/arc/input/sdl/keyboard.h b/include/arc/input/sdl/keyboard.h new file mode 100644 index 0000000..c1b04bc --- /dev/null +++ b/include/arc/input/sdl/keyboard.h @@ -0,0 +1,21 @@ +#ifndef ARC_SDL_KEYBOARD_H_ +#define ARC_SDL_KEYBOARD_H_ + +#ifdef ARC_SDL +#include +#include "arc/input/keyboard.h" + +struct ARC_Keyboard { + SDL_Event *event; + + ARC_KeyboardState *keys; + ARC_KeyboardState *released; +}; + +struct ARC_KeyboardInfo { + SDL_Event *event; +}; + +#endif // ARC_SDL + +#endif // !ARC_SDL_KEYBOARD_H_ \ No newline at end of file diff --git a/include/arc/input/sdl/mouse.h b/include/arc/input/sdl/mouse.h new file mode 100644 index 0000000..1acea38 --- /dev/null +++ b/include/arc/input/sdl/mouse.h @@ -0,0 +1,24 @@ +#ifndef ARC_SDL_MOUSE_H_ +#define ARC_SDL_MOUSE_H_ + +#ifdef ARC_SDL +#include +#include "arc/input/mouse.h" +#include "arc/math/point.h" + +struct ARC_Mouse { + SDL_Event *event; + ARC_Point *coords; + int32_t *scroll; + + ARC_MouseState *buttons; + uint8_t *buttonsReleased; +}; + +struct ARC_MouseInfo { + SDL_Event *event; +}; + +#endif // ARC_SDL + +#endif // !ARC_SDL_MOUSE_H_ \ No newline at end of file diff --git a/include/arc/math/point.h b/include/arc/math/point.h new file mode 100644 index 0000000..bfdad69 --- /dev/null +++ b/include/arc/math/point.h @@ -0,0 +1,14 @@ +#ifndef ARC_MATH_POINT_H_ +#define ARC_MATH_POINT_H_ + +#include + +typedef struct ARC_Point { + int32_t x, y; +} ARC_Point; + +typedef struct ARC_UPoint { + uint32_t x, y; +} ARC_UPoint; + +#endif // ARC_MATH_POINT_H_ diff --git a/src/input/sdl/keyboard.c b/src/input/sdl/keyboard.c new file mode 100644 index 0000000..ef273a4 --- /dev/null +++ b/src/input/sdl/keyboard.c @@ -0,0 +1,56 @@ +#ifdef ARC_SDL +#include "arc/input/sdl/keyboard.h" +#include "arc/input/keyboard.h" +#include "arc/math/point.h" +#include "arc/std/errno.h" +#include +#include +#include +#include + +void ARC_Keyboard_Create(ARC_Keyboard **keyboard, ARC_KeyboardInfo *info){ + *keyboard = (ARC_Keyboard *)malloc(sizeof(ARC_Keyboard)); + (*keyboard)->event = info->event; + (*keyboard)->keys = (ARC_KeyboardState *)malloc(sizeof(ARC_KeyboardState) * ARC_KEYBOARD_BUTTON_NUM); + + (*keyboard)->released = NULL; + + for(uint8_t i = 0; i < ARC_KEYBOARD_BUTTON_NUM; i++){ + (*keyboard)->keys[i] = ARC_KEY_NONE; + } +} + +void ARC_Keyboard_Destroy(ARC_Keyboard *keyboard){ + free(keyboard->keys); + + free(keyboard); +} + +void ARC_Keyboard_Update(ARC_Keyboard *keyboard){ + if(keyboard->released){ + *keyboard->released = ARC_KEY_NONE; + keyboard->released = NULL; + } + + if(keyboard->event->type != SDL_KEYDOWN && keyboard->event->type != SDL_KEYUP){ + return; + } + + if(keyboard->event->key.keysym.sym >= 239 || keyboard->event->key.keysym.sym < 0){ + return; + } + + if(keyboard->event->type == SDL_KEYDOWN){ + keyboard->keys[keyboard->event->key.keysym.sym] = ARC_KEY_PRESSED; + return; + } + + keyboard->keys[keyboard->event->key.keysym.sym] = ARC_KEY_RELEASED; + keyboard->released = (keyboard->keys + keyboard->event->key.keysym.sym); +} + +ARC_KeyboardState ARC_Keyboard_GetState(ARC_Keyboard *keyboard, uint8_t key){ + return keyboard->keys[key]; +} + +#endif // ARC_SDL \ No newline at end of file diff --git a/src/input/sdl/mouse.c b/src/input/sdl/mouse.c new file mode 100644 index 0000000..6bdbc3c --- /dev/null +++ b/src/input/sdl/mouse.c @@ -0,0 +1,101 @@ +#ifdef ARC_SDL +#include "arc/input/sdl/mouse.h" +#include "arc/input/mouse.h" +#include "arc/math/point.h" +#include "arc/std/errno.h" +#include +#include +#include +#include + +void ARC_Mouse_Create(ARC_Mouse **mouse, ARC_MouseInfo *info){ + *mouse = (ARC_Mouse *)malloc(sizeof(ARC_Mouse)); + (*mouse)->event = info->event; + (*mouse)->coords = (ARC_Point *)malloc(sizeof(ARC_Point)); + (*mouse)->scroll = (int32_t *)malloc(sizeof(int32_t )); + (*mouse)->buttons = (ARC_MouseState *)malloc(sizeof(ARC_MouseState) * ARC_MOUSE_BUTTON_NUM); + + (*mouse)->buttonsReleased = (uint8_t *)malloc(sizeof(uint8_t)); + + *(*mouse)->coords = (ARC_Point){0, 0}; + *(*mouse)->scroll = 0; + + for(uint8_t i = 0; i < ARC_MOUSE_BUTTON_NUM; i++){ + (*mouse)->buttons[i] = ARC_MOUSE_NONE; + } + + *(*mouse)->buttonsReleased = 0; +} + +void ARC_Mouse_Destroy(ARC_Mouse *mouse){ + free(mouse->buttonsReleased); + + free(mouse->buttons); + free(mouse->scroll ); + free(mouse->coords ); + + free(mouse); +} + +void ARC_Mouse_UpdateButton(ARC_Mouse *mouse, uint8_t button, uint32_t *buttons, uint32_t mask){ + if(*buttons & mask){ + mouse->buttons[button] = ARC_MOUSE_PRESSED; + return; + } + + if(mouse->buttons[button] == ARC_MOUSE_NONE){ + return; + } + + if(mouse->buttons[button] == ARC_MOUSE_RELEASED){ + mouse->buttons[button] = ARC_MOUSE_NONE; + --*mouse->buttonsReleased; + return; + } + + mouse->buttons[button] = ARC_MOUSE_RELEASED; + ++*mouse->buttonsReleased; +} + +void ARC_Mouse_Update(ARC_Mouse *mouse){ + *mouse->scroll = 0; + if(mouse->event->type == SDL_MOUSEWHEEL){ + *mouse->scroll = mouse->event->wheel.y; + } + + if(mouse->event->type != SDL_MOUSEBUTTONDOWN && mouse->event->type != SDL_MOUSEBUTTONUP){ + if(!*mouse->buttonsReleased){ + return; + } + + for(uint8_t i = *mouse->buttonsReleased; i > 0; i--){ + if(mouse->buttons[i - 1] == ARC_MOUSE_RELEASED){ + mouse->buttons[i - 1] = ARC_MOUSE_NONE; + --*mouse->buttonsReleased; + } + } + + if(*mouse->buttonsReleased){ + arc_errno = ARC_ERRNO_DATA; + ARC_DEBUG_LOG(arc_errno, "in ARC_Mouse_Update mouse->buttonsReleased == %u, it needs to be 0\n", *(mouse->buttonsReleased)); + } + return; + } + + uint32_t buttons = SDL_GetMouseState(&(mouse->coords->x), &(mouse->coords->y)); + ARC_Mouse_UpdateButton(mouse, ARC_MOUSE_LEFT , &buttons, SDL_BUTTON_LMASK ); + ARC_Mouse_UpdateButton(mouse, ARC_MOUSE_MIDDLE, &buttons, SDL_BUTTON_MMASK ); + ARC_Mouse_UpdateButton(mouse, ARC_MOUSE_RIGHT , &buttons, SDL_BUTTON_RMASK ); + ARC_Mouse_UpdateButton(mouse, ARC_MOUSE_X1 , &buttons, SDL_BUTTON_X1MASK); + ARC_Mouse_UpdateButton(mouse, ARC_MOUSE_X2 , &buttons, SDL_BUTTON_X2MASK); +} + +ARC_Point ARC_Mouse_GetCoords(ARC_Mouse *mouse){ + return *mouse->coords; +} + +ARC_MouseState ARC_Mouse_GetState(ARC_Mouse *mouse, ARC_MouseButton button){ + return mouse->buttons[button]; +} + +#endif // ARC_SDL \ No newline at end of file