Loading...
Searching...
No Matches
keyboard.c
Go to the documentation of this file.
#include "arc/input/keyboard.h"
#include "keyboard.h"
#include "input.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_CreateWithInput(ARC_Keyboard **keyboard, ARC_Input *input){
*keyboard = (ARC_Keyboard *)malloc(sizeof(ARC_Keyboard));
(*keyboard)->event = input->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, enum ARC_KeyboardKey key){
switch(key){
case ARC_KEY_A: return keyboard->keys[SDLK_a];
case ARC_KEY_B: return keyboard->keys[SDLK_b];
case ARC_KEY_C: return keyboard->keys[SDLK_c];
case ARC_KEY_D: return keyboard->keys[SDLK_d];
case ARC_KEY_E: return keyboard->keys[SDLK_e];
case ARC_KEY_F: return keyboard->keys[SDLK_f];
case ARC_KEY_G: return keyboard->keys[SDLK_g];
case ARC_KEY_H: return keyboard->keys[SDLK_h];
case ARC_KEY_I: return keyboard->keys[SDLK_i];
case ARC_KEY_J: return keyboard->keys[SDLK_j];
case ARC_KEY_K: return keyboard->keys[SDLK_k];
case ARC_KEY_L: return keyboard->keys[SDLK_l];
case ARC_KEY_M: return keyboard->keys[SDLK_m];
case ARC_KEY_N: return keyboard->keys[SDLK_n];
case ARC_KEY_O: return keyboard->keys[SDLK_o];
case ARC_KEY_P: return keyboard->keys[SDLK_p];
case ARC_KEY_Q: return keyboard->keys[SDLK_q];
case ARC_KEY_R: return keyboard->keys[SDLK_r];
case ARC_KEY_S: return keyboard->keys[SDLK_s];
case ARC_KEY_T: return keyboard->keys[SDLK_t];
case ARC_KEY_U: return keyboard->keys[SDLK_u];
case ARC_KEY_V: return keyboard->keys[SDLK_v];
case ARC_KEY_W: return keyboard->keys[SDLK_w];
case ARC_KEY_X: return keyboard->keys[SDLK_x];
case ARC_KEY_Y: return keyboard->keys[SDLK_y];
case ARC_KEY_Z: return keyboard->keys[SDLK_z];
case ARC_KEY_0: return keyboard->keys[SDLK_0];
case ARC_KEY_1: return keyboard->keys[SDLK_1];
case ARC_KEY_2: return keyboard->keys[SDLK_2];
case ARC_KEY_3: return keyboard->keys[SDLK_3];
case ARC_KEY_4: return keyboard->keys[SDLK_4];
case ARC_KEY_5: return keyboard->keys[SDLK_5];
case ARC_KEY_6: return keyboard->keys[SDLK_6];
case ARC_KEY_7: return keyboard->keys[SDLK_7];
case ARC_KEY_8: return keyboard->keys[SDLK_8];
case ARC_KEY_9: return keyboard->keys[SDLK_9];
case ARC_KEY_SPACE: return keyboard->keys[SDLK_SPACE ];
case ARC_KEY_ESC: return keyboard->keys[SDLK_ESCAPE];
case ARC_KEY_ENTER: return keyboard->keys[SDLK_RETURN];
default: return ARC_KEY_NONE;
}
}
errno.h
keyboard.h
ARC_KeyboardKey
ARC_KeyboardKey
Definition keyboard.h:32
ARC_KEY_F
@ ARC_KEY_F
Definition keyboard.h:38
ARC_KEY_4
@ ARC_KEY_4
Definition keyboard.h:64
ARC_KEY_2
@ ARC_KEY_2
Definition keyboard.h:62
ARC_KEY_0
@ ARC_KEY_0
Definition keyboard.h:60
ARC_KEY_M
@ ARC_KEY_M
Definition keyboard.h:45
ARC_KEY_K
@ ARC_KEY_K
Definition keyboard.h:43
ARC_KEY_P
@ ARC_KEY_P
Definition keyboard.h:48
ARC_KEY_E
@ ARC_KEY_E
Definition keyboard.h:37
ARC_KEY_8
@ ARC_KEY_8
Definition keyboard.h:68
ARC_KEY_T
@ ARC_KEY_T
Definition keyboard.h:52
ARC_KEY_Z
@ ARC_KEY_Z
Definition keyboard.h:58
ARC_KEY_A
@ ARC_KEY_A
Definition keyboard.h:33
ARC_KEY_Y
@ ARC_KEY_Y
Definition keyboard.h:57
ARC_KEY_9
@ ARC_KEY_9
Definition keyboard.h:69
ARC_KEY_ESC
@ ARC_KEY_ESC
Definition keyboard.h:72
ARC_KEY_W
@ ARC_KEY_W
Definition keyboard.h:55
ARC_KEY_5
@ ARC_KEY_5
Definition keyboard.h:65
ARC_KEY_3
@ ARC_KEY_3
Definition keyboard.h:63
ARC_KEY_J
@ ARC_KEY_J
Definition keyboard.h:42
ARC_KEY_N
@ ARC_KEY_N
Definition keyboard.h:46
ARC_KEY_O
@ ARC_KEY_O
Definition keyboard.h:47
ARC_KEY_L
@ ARC_KEY_L
Definition keyboard.h:44
ARC_KEY_R
@ ARC_KEY_R
Definition keyboard.h:50
ARC_KEY_C
@ ARC_KEY_C
Definition keyboard.h:35
ARC_KEY_6
@ ARC_KEY_6
Definition keyboard.h:66
ARC_KEY_X
@ ARC_KEY_X
Definition keyboard.h:56
ARC_KEY_I
@ ARC_KEY_I
Definition keyboard.h:41
ARC_KEY_1
@ ARC_KEY_1
Definition keyboard.h:61
ARC_KEY_7
@ ARC_KEY_7
Definition keyboard.h:67
ARC_KEY_ENTER
@ ARC_KEY_ENTER
Definition keyboard.h:73
ARC_KEY_Q
@ ARC_KEY_Q
Definition keyboard.h:49
ARC_KEY_SPACE
@ ARC_KEY_SPACE
Definition keyboard.h:71
ARC_KEY_U
@ ARC_KEY_U
Definition keyboard.h:53
ARC_KEY_V
@ ARC_KEY_V
Definition keyboard.h:54
ARC_KEY_B
@ ARC_KEY_B
Definition keyboard.h:34
ARC_KEY_D
@ ARC_KEY_D
Definition keyboard.h:36
ARC_KEY_G
@ ARC_KEY_G
Definition keyboard.h:39
ARC_KEY_S
@ ARC_KEY_S
Definition keyboard.h:51
ARC_KEY_H
@ ARC_KEY_H
Definition keyboard.h:40
ARC_Keyboard_Update
void ARC_Keyboard_Update(ARC_Keyboard *keyboard)
Definition keyboard.c:29
ARC_KeyboardState
ARC_KeyboardState
Definition keyboard.h:19
ARC_KEY_RELEASED
@ ARC_KEY_RELEASED
Definition keyboard.h:22
ARC_KEY_PRESSED
@ ARC_KEY_PRESSED
Definition keyboard.h:21
ARC_KEY_NONE
@ ARC_KEY_NONE
Definition keyboard.h:20
ARC_Keyboard_CreateWithInput
void ARC_Keyboard_CreateWithInput(ARC_Keyboard **keyboard, ARC_Input *input)
Definition keyboard.c:11
ARC_KEYBOARD_BUTTON_NUM
#define ARC_KEYBOARD_BUTTON_NUM
Definition keyboard.h:25
ARC_Keyboard_Destroy
void ARC_Keyboard_Destroy(ARC_Keyboard *keyboard)
Definition keyboard.c:23
ARC_Keyboard_GetState
ARC_KeyboardState ARC_Keyboard_GetState(ARC_Keyboard *keyboard, enum ARC_KeyboardKey key)
Definition keyboard.c:52
input.h
keyboard.h
point.h
ARC_Input
Definition input.h:8
ARC_Input::event
SDL_Event * event
Definition input.h:12
ARC_Keyboard
Definition keyboard.h:7
ARC_Keyboard::keys
ARC_KeyboardState * keys
Definition keyboard.h:10
ARC_Keyboard::event
SDL_Event * event
Definition keyboard.h:8
ARC_Keyboard::released
ARC_KeyboardState * released
Definition keyboard.h:11