sdl3 migrate now compiling, need to go through all the files and clean them up

This commit is contained in:
herbglitch 2025-03-22 23:25:21 -06:00
parent df4390ddba
commit 1b2e2cb7f1
26 changed files with 1139 additions and 0 deletions

View file

@ -0,0 +1,45 @@
#include "arc/input/input.h"
#include "input.h"
#include "arc/input/mouse.h"
#include "arc/input/keyboard.h"
#include <SDL3/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);
}
ARC_Bool ARC_Input_Update(ARC_Input *input){
SDL_PollEvent(input->event);
if(input->event->type == SDL_EVENT_QUIT){
return ARC_False;
}
ARC_Keyboard_Update(input->keyboard);
ARC_Mouse_Update(input->mouse);
return ARC_True;
}
ARC_Keyboard *ARC_Input_GetKeyboard(ARC_Input *input){
return input->keyboard;
}
ARC_Mouse *ARC_Input_GetMouse(ARC_Input *input){
return input->mouse;
}

View 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 <SDL3/SDL.h>
struct ARC_Input {
ARC_Keyboard *keyboard;
ARC_Mouse *mouse;
SDL_Event *event;
};
#endif // !ARC_SDL_INPUT_H_

View 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 <SDL3/SDL_keyboard.h>
#include <SDL3/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_EVENT_KEY_DOWN && keyboard->event->type != SDL_EVENT_KEY_UP){
return;
}
if(keyboard->event->key.key >= 239 || keyboard->event->key.key < 0){
return;
}
if(keyboard->event->type == SDL_EVENT_KEY_DOWN){
keyboard->keys[keyboard->event->key.key] = ARC_KEY_PRESSED;
return;
}
keyboard->keys[keyboard->event->key.key] = ARC_KEY_RELEASED;
keyboard->released = (keyboard->keys + keyboard->event->key.key);
}
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;
}
}

View file

@ -0,0 +1,14 @@
#ifndef ARC_SDL_KEYBOARD_H_
#define ARC_SDL_KEYBOARD_H_
#include "arc/input/keyboard.h"
#include <SDL3/SDL.h>
struct ARC_Keyboard {
SDL_Event *event;
ARC_KeyboardState *keys;
ARC_KeyboardState *released;
};
#endif // !ARC_SDL_KEYBOARD_H_

105
packages/input/sdl3/mouse.c Normal file
View 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 <SDL3/SDL_mouse.h>
#include <SDL3/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_FPoint *)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_FPoint){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_EVENT_MOUSE_WHEEL){
*mouse->scrollY = mouse->event->wheel.y;
}
uint32_t buttons = SDL_GetMouseState(&(mouse->coords->x), &(mouse->coords->y));
if(mouse->event->type != SDL_EVENT_MOUSE_BUTTON_DOWN && mouse->event->type != SDL_EVENT_MOUSE_BUTTON_UP){
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_FPoint *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;
}

View file

@ -0,0 +1,17 @@
#ifndef ARC_SDL_MOUSE_H_
#define ARC_SDL_MOUSE_H_
#include <SDL3/SDL.h>
#include "arc/input/mouse.h"
#include "arc/math/point.h"
struct ARC_Mouse {
SDL_Event *event;
ARC_FPoint *coords;
int32_t *scrollY;
ARC_MouseState *buttons;
uint8_t *buttonsReleased;
};
#endif // !ARC_SDL_MOUSE_H_