input and handler possibly fixed and point
This commit is contained in:
parent
d6281e8eac
commit
3fa74e8f9e
7 changed files with 288 additions and 0 deletions
56
src/input/sdl/keyboard.c
Normal file
56
src/input/sdl/keyboard.c
Normal file
|
|
@ -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 <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <SDL_keyboard.h>
|
||||
#include <SDL_events.h>
|
||||
|
||||
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
|
||||
101
src/input/sdl/mouse.c
Normal file
101
src/input/sdl/mouse.c
Normal file
|
|
@ -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 <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <SDL_mouse.h>
|
||||
#include <SDL_events.h>
|
||||
|
||||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue