f***ed up and needed to rework packages
This commit is contained in:
parent
b43ab1702f
commit
f7a87d7519
78 changed files with 3713 additions and 0 deletions
40
packages/input/sdl/input.c
Normal file
40
packages/input/sdl/input.c
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#include "arc/input/input.h"
|
||||
#include "input.h"
|
||||
|
||||
#include "arc/input/mouse.h"
|
||||
#include "arc/input/keyboard.h"
|
||||
|
||||
#include <SDL.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void ARC_Input_CreateWithEngineData(ARC_Input **input, ARC_EngineData *data){
|
||||
*input = (ARC_Input *)malloc(sizeof(ARC_Input));
|
||||
|
||||
(*input)->event = (SDL_Event *)malloc(sizeof(SDL_Event));
|
||||
|
||||
ARC_Keyboard_CreateWithInput(&((*input)->keyboard), *input);
|
||||
ARC_Mouse_CreateWithInput(&((*input)->mouse), *input);
|
||||
}
|
||||
|
||||
void ARC_Input_Destroy(ARC_Input *input){
|
||||
ARC_Keyboard_Destroy(input->keyboard);
|
||||
ARC_Mouse_Destroy(input->mouse);
|
||||
|
||||
free(input->event);
|
||||
free(input);
|
||||
}
|
||||
|
||||
void ARC_Input_Update(ARC_Input *input){
|
||||
SDL_PollEvent(input->event);
|
||||
|
||||
ARC_Keyboard_Update(input->keyboard);
|
||||
ARC_Mouse_Update(input->mouse);
|
||||
}
|
||||
|
||||
ARC_Keyboard *ARC_Input_GetKeyboard(ARC_Input *input){
|
||||
return input->keyboard;
|
||||
}
|
||||
|
||||
ARC_Mouse *ARC_Input_GetMouse(ARC_Input *input){
|
||||
return input->mouse;
|
||||
}
|
||||
15
packages/input/sdl/input.h
Normal file
15
packages/input/sdl/input.h
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#ifndef ARC_SDL_INPUT_H_
|
||||
#define ARC_SDL_INPUT_H_
|
||||
|
||||
#include "arc/input/keyboard.h"
|
||||
#include "arc/input/mouse.h"
|
||||
#include <SDL.h>
|
||||
|
||||
struct ARC_Input {
|
||||
ARC_Keyboard *keyboard;
|
||||
ARC_Mouse *mouse;
|
||||
|
||||
SDL_Event *event;
|
||||
};
|
||||
|
||||
#endif // !ARC_SDL_INPUT_H_
|
||||
98
packages/input/sdl/keyboard.c
Normal file
98
packages/input/sdl/keyboard.c
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
#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;
|
||||
}
|
||||
}
|
||||
14
packages/input/sdl/keyboard.h
Normal file
14
packages/input/sdl/keyboard.h
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#ifndef ARC_SDL_KEYBOARD_H_
|
||||
#define ARC_SDL_KEYBOARD_H_
|
||||
|
||||
#include "arc/input/keyboard.h"
|
||||
#include <SDL.h>
|
||||
|
||||
struct ARC_Keyboard {
|
||||
SDL_Event *event;
|
||||
|
||||
ARC_KeyboardState *keys;
|
||||
ARC_KeyboardState *released;
|
||||
};
|
||||
|
||||
#endif // !ARC_SDL_KEYBOARD_H_
|
||||
105
packages/input/sdl/mouse.c
Normal file
105
packages/input/sdl/mouse.c
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
#include "arc/input/mouse.h"
|
||||
|
||||
#include "mouse.h"
|
||||
#include "input.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_CreateWithInput(ARC_Mouse **mouse, ARC_Input *input){
|
||||
*mouse = (ARC_Mouse *)malloc(sizeof(ARC_Mouse));
|
||||
(*mouse)->event = input->event;
|
||||
(*mouse)->coords = (ARC_Point *)malloc(sizeof(ARC_Point));
|
||||
(*mouse)->scrollY = (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)->scrollY = 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->scrollY);
|
||||
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->scrollY = 0;
|
||||
if(mouse->event->type == SDL_MOUSEWHEEL){
|
||||
*mouse->scrollY = mouse->event->wheel.y;
|
||||
}
|
||||
|
||||
uint32_t buttons = SDL_GetMouseState(&(mouse->coords->x), &(mouse->coords->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;
|
||||
}
|
||||
|
||||
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];
|
||||
}
|
||||
|
||||
int32_t *ARC_Mouse_GetScrollY(ARC_Mouse *mouse){
|
||||
return mouse->scrollY;
|
||||
}
|
||||
17
packages/input/sdl/mouse.h
Normal file
17
packages/input/sdl/mouse.h
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#ifndef ARC_SDL_MOUSE_H_
|
||||
#define ARC_SDL_MOUSE_H_
|
||||
|
||||
#include <SDL.h>
|
||||
#include "arc/input/mouse.h"
|
||||
#include "arc/math/point.h"
|
||||
|
||||
struct ARC_Mouse {
|
||||
SDL_Event *event;
|
||||
ARC_Point *coords;
|
||||
int32_t *scrollY;
|
||||
|
||||
ARC_MouseState *buttons;
|
||||
uint8_t *buttonsReleased;
|
||||
};
|
||||
|
||||
#endif // !ARC_SDL_MOUSE_H_
|
||||
Loading…
Add table
Add a link
Reference in a new issue