From d6281e8eac8d196b255b660b947eda88d85f15af Mon Sep 17 00:00:00 2001 From: herbglitch Date: Fri, 11 Nov 2022 01:15:33 -0700 Subject: [PATCH] input and handler possibly fixed --- include/arc/engine/engine.h | 4 ++ include/arc/engine/state.h | 7 +++- include/arc/graphics/config.h | 4 +- include/arc/graphics/renderer.h | 2 +- include/arc/math/vector2.h | 20 +--------- include/arc/std/errno.h | 4 +- src/engine/engine.c | 71 ++++++++++++++++++++++----------- src/engine/state.c | 7 ++-- src/graphics/sdl/renderer.c | 3 +- src/graphics/sdl/window.c | 8 +++- src/std/handler.c | 13 ++++-- 11 files changed, 84 insertions(+), 59 deletions(-) diff --git a/include/arc/engine/engine.h b/include/arc/engine/engine.h index 0da5f25..292428f 100644 --- a/include/arc/engine/engine.h +++ b/include/arc/engine/engine.h @@ -7,12 +7,16 @@ extern "C" { #include "arc/graphics/window.h" #include "arc/graphics/renderer.h" +#include "arc/input/mouse.h" +#include "arc/input/keyboard.h" #include "arc/std/handler.h" typedef struct ARC_EngineData { ARC_Window *window; ARC_Renderer *renderer; ARC_Handler *state; + ARC_Mouse *mouse; + ARC_Keyboard *keyboard; double dt; } ARC_EngineData; diff --git a/include/arc/engine/state.h b/include/arc/engine/state.h index 8efb2f8..ae43005 100644 --- a/include/arc/engine/state.h +++ b/include/arc/engine/state.h @@ -5,9 +5,12 @@ extern "C" { #endif +typedef void (* ARC_State_UpdateFn)(void *data); +typedef void (* ARC_State_RenderFn)(void *data); typedef struct ARC_State { - void (* updateFn)(); - void (* renderFn)(); + ARC_State_UpdateFn updateFn; + ARC_State_RenderFn renderFn; + void *data; } ARC_State; void ARC_State_Update(void *data); diff --git a/include/arc/graphics/config.h b/include/arc/graphics/config.h index 9b9e021..b173267 100644 --- a/include/arc/graphics/config.h +++ b/include/arc/graphics/config.h @@ -4,7 +4,7 @@ #ifdef __cplusplus extern "C" { #endif - + #include "arc/std/config.h" #include "arc/graphics/renderer.h" @@ -14,4 +14,4 @@ void ARC_GraphicsConfig_Init(ARC_Config *config, ARC_Renderer *renderer); } #endif -#endif // !ARC_GRAPHICS_CONFIG_H_ +#endif // !ARC_GRAPHICS_CONFIG_H_ \ No newline at end of file diff --git a/include/arc/graphics/renderer.h b/include/arc/graphics/renderer.h index 619d03e..692b771 100644 --- a/include/arc/graphics/renderer.h +++ b/include/arc/graphics/renderer.h @@ -18,7 +18,7 @@ typedef struct ARC_RenderInfo ARC_RenderInfo; * @param renderer ARC_Renderer to initialize * @param info Info on how to create ARC_Window */ -void ARC_Renderer_Create(ARC_Renderer **renderer, ARC_RenderInfo *data); +void ARC_Renderer_Create(ARC_Renderer **renderer, ARC_RenderInfo *info); /** * @brief destroys ARC_Renderer type diff --git a/include/arc/math/vector2.h b/include/arc/math/vector2.h index 61fd6da..c68c085 100644 --- a/include/arc/math/vector2.h +++ b/include/arc/math/vector2.h @@ -1,30 +1,12 @@ #ifndef ARC_MATH_VECTOR2_H_ #define ARC_MATH_VECTOR2_H_ -#include - typedef struct ARC_Vector2 { - int32_t x, y; -} ARC_Vector2; - -typedef struct ARC_UVector2 { - uint32_t x, y; -} ARC_UVector2; - -typedef struct ARC_FVector2 { float x, y; -} ARC_FVector2; +} ARC_Vector2; typedef struct ARC_DVector2 { double x, y; } ARC_DVector2; -typedef struct ARC_LVector2 { - int64_t x, y; -} ARC_LVector2; - -typedef struct ARC_ULVector2 { - uint64_t x, y; -} ARC_ULVector2; - #endif // ARC_MATH_VECTOR2_H_ diff --git a/include/arc/std/errno.h b/include/arc/std/errno.h index 813cbdd..47bbf0c 100644 --- a/include/arc/std/errno.h +++ b/include/arc/std/errno.h @@ -8,12 +8,12 @@ #define ARC_ERRNO_COPY -0x03 #define ARC_ERRNO_EXISTS -0x04 #define ARC_ERRNO_OVERFLOW -0x05 - +#define ARC_ERRNO_INIT -0x06 #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-variable" static int32_t arc_errno = 0; -// #pragma GCC diagnostic pop +#pragma GCC diagnostic pop #ifdef __cplusplus diff --git a/src/engine/engine.c b/src/engine/engine.c index 35180c9..c973ebc 100644 --- a/src/engine/engine.c +++ b/src/engine/engine.c @@ -4,76 +4,101 @@ #include "arc/engine/state.h" #include "arc/graphics/window.h" #include "arc/graphics/renderer.h" +#include "arc/input/mouse.h" +#include "arc/input/keyboard.h" +#include "arc/std/errno.h" #include "arc/std/handler.h" -//NOTE: this is very temp, mostly to get smthn running so I can test out different ideas +//NOTE: some of this file is temporary, mostly to get smthn running so I can test out different ideas #ifdef ARC_SDL #include #include "arc/graphics/sdl/window.h" #include "arc/graphics/sdl/renderer.h" +#include "arc/input/sdl/mouse.h" +#include "arc/input/sdl/keyboard.h" #endif // ARC_SDL void ARC_EngineData_Create(ARC_EngineData **data){ *data = (ARC_EngineData *)malloc(sizeof(ARC_EngineData)); (*data)->window = NULL; (*data)->renderer = NULL; + (*data)->mouse = NULL; + ARC_Handler_Create(&((*data)->state), sizeof(ARC_State)); - ARC_WindowInfo windowInfo; - ARC_RenderInfo renderInfo; + ARC_WindowInfo windowInfo; + ARC_RenderInfo renderInfo; + ARC_MouseInfo mouseInfo; + ARC_KeyboardInfo keyboardInfo; #ifdef ARC_SDL - if(SDL_Init(SDL_INIT_VIDEO) < 0){ - printf("Error: initializing SDL\nSDL Error: %s\n", SDL_GetError()); - free(*data); - return; - } - - windowInfo = (ARC_WindowInfo){ "title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 720, 480, 0 }; + windowInfo = (ARC_WindowInfo){ "title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 2560, 1440, 0 }; #endif // ARC_SDL ARC_Window_Create(&((*data)->window), &windowInfo); - //TODO: handle arc_errno errors here + if(arc_errno){ + free(data); + return; + } #ifdef ARC_SDL renderInfo = (ARC_RenderInfo){ (SDL_Window *)(*data)->window->window, -1, SDL_RENDERER_ACCELERATED }; #endif // ARC_SDL ARC_Renderer_Create(&((*data)->renderer), &renderInfo); - //TODO: handle arc_errno errors here + if(arc_errno){ + ARC_Window_Destroy((*data)->window); + free(data); + } + +#ifdef ARC_SDL + SDL_Event *event = (SDL_Event *)malloc(sizeof(SDL_Event)); + mouseInfo = (ARC_MouseInfo ){ event }; + keyboardInfo = (ARC_KeyboardInfo){ event }; +#endif // ARC_SDL + + ARC_Mouse_Create(&((*data)->mouse), &mouseInfo); + + ARC_Keyboard_Create(&((*data)->keyboard), &keyboardInfo); } void ARC_EngineData_Destroy(ARC_EngineData *data){ #ifdef ARC_SDL - ARC_Handler_Destroy(data->state, NULL); //TODO: replace null with cleanup function + free(data->mouse->event); +#endif // ARC_SDL + + ARC_Mouse_Destroy(data->mouse); ARC_Renderer_Destroy(data->renderer); ARC_Window_Destroy(data->window); -#endif // ARC_SDL + ARC_Handler_Destroy(data->state, NULL); //TODO: replace null with cleanup function } void ARC_Engine_Run(ARC_EngineData *data){ - double lastTime = 0, currentTime; + if(arc_errno){ + return; + } #ifdef ARC_SDL - SDL_Event event; + SDL_Event *event = data->mouse->event; #endif // ARC_SDL + double lastTime = 0, currentTime; + while(1){ #ifdef ARC_SDL currentTime = SDL_GetTicks(); data->dt = currentTime - lastTime; lastTime = currentTime; - while(SDL_PollEvent(&event)){ - if(event.type == SDL_QUIT){ return; } - if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE){ return; } - } + SDL_PollEvent(data->mouse->event); + if(event->type == SDL_QUIT){ return; } + if(event->type == SDL_KEYDOWN && event->key.keysym.sym == SDLK_ESCAPE){ return; } #endif // ARC_SDL - // data->mouse.update(data->event); - // data->keyboard.update(data->event); + ARC_Mouse_Update(data->mouse); + + ARC_Keyboard_Update(data->keyboard); - // data->state.update(); ARC_Handler_Iterate(data->state, ARC_State_Update); ARC_Renderer_Clear(data->renderer); diff --git a/src/engine/state.c b/src/engine/state.c index bca0a34..5d3f707 100644 --- a/src/engine/state.c +++ b/src/engine/state.c @@ -1,11 +1,10 @@ #include "arc/engine/state.h" +#include void ARC_State_Update(void *data){ - ARC_State *temp = (ARC_State *) data; - ((ARC_State *)data)->updateFn(); + ((ARC_State *)data)->updateFn(((ARC_State *)data)->data); } void ARC_State_Render(void *data){ - ARC_State *temp = (ARC_State *) data; - ((ARC_State *)data)->renderFn(); + ((ARC_State *)data)->renderFn(((ARC_State *)data)->data); } \ No newline at end of file diff --git a/src/graphics/sdl/renderer.c b/src/graphics/sdl/renderer.c index e8878a0..ac6ea8a 100644 --- a/src/graphics/sdl/renderer.c +++ b/src/graphics/sdl/renderer.c @@ -17,9 +17,10 @@ void ARC_Renderer_Create(ARC_Renderer **renderer, ARC_RenderInfo *info){ *renderer = (ARC_Renderer *)malloc(sizeof(ARC_Renderer)); (*renderer)->renderer = SDL_CreateRenderer((SDL_Window *)info->window, info->index, info->flags); - if(!renderer){ + if(!(*renderer)->renderer){ arc_errno = ARC_ERRNO_NULL; ARC_DEBUG_LOG(arc_errno, "SDL_CreateRenderer(%p, %d, %u);", info->window, info->index, info->flags); + free(renderer); } } diff --git a/src/graphics/sdl/window.c b/src/graphics/sdl/window.c index 752ddfe..c9ed8b8 100644 --- a/src/graphics/sdl/window.c +++ b/src/graphics/sdl/window.c @@ -12,13 +12,19 @@ void ARC_Window_Create(ARC_Window **window, ARC_WindowInfo *info){ return; } - // (*window)->window = SDL_CreateWindow((const char *)info->title, info->x, info->y, info->w, info->h, info->flags); + if(SDL_Init(SDL_INIT_VIDEO) < 0){ + arc_errno = ARC_ERRNO_INIT; + printf("Error: initializing SDL\nSDL Error: %s\n", SDL_GetError()); + return; + } + *window = (ARC_Window *)malloc(sizeof(ARC_Window)); (*window)->window = SDL_CreateWindow((const char *)info->title, info->x, info->y, info->w, info->h, info->flags); if(!(*window)->window){ arc_errno = ARC_ERRNO_NULL; ARC_DEBUG_LOG(arc_errno, "SDL_CreateWindow(%s, %d, %d, %d, %d, %x);", info->title, info->x, info->y, info->w, info->h, info->flags); + free(window); } } diff --git a/src/std/handler.c b/src/std/handler.c index 2ece4b5..2f45b4f 100644 --- a/src/std/handler.c +++ b/src/std/handler.c @@ -32,14 +32,14 @@ void ARC_Handler_Add(ARC_Handler *handler, void *data){ int8_t ARC_Handler_RemoveCompareFn(void *a, void *b){ return a == b; } void ARC_Handler_Remove(ARC_Handler *handler, void *data){ - // ARC_Vector_Add(handler->trash, data); + ARC_Vector_Add(handler->trash, data); ARC_Vector_Remove(handler->data, data, ARC_Handler_RemoveCompareFn); } void ARC_Handler_RemoveIndex(ARC_Handler *handler, uint32_t *index){ void *data = ARC_Vector_Get(handler->data, index); - ARC_Vector_RemoveIndex(handler->data, index); ARC_Vector_Add(handler->trash, data); + ARC_Vector_RemoveIndex(handler->data, index); } void ARC_Handler_Iterate(ARC_Handler *handler, ARC_Handler_DataFn datafn){ @@ -49,8 +49,9 @@ void ARC_Handler_Iterate(ARC_Handler *handler, ARC_Handler_DataFn datafn){ } void ARC_Handler_Clear(ARC_Handler *handler){ + uint32_t zeroIndex = 0; while(*ARC_Vector_Size(handler->data)){ - ARC_Handler_Remove(handler, 0); + ARC_Handler_RemoveIndex(handler, &zeroIndex); } } @@ -58,7 +59,11 @@ void ARC_Handler_Clean(ARC_Handler *handler, ARC_Handler_CleanDataFn cleanfn){ uint32_t i = 0; while(*ARC_Vector_Size(handler->trash)){ void *data = ARC_Vector_Get(handler->trash, &i); - cleanfn(data); + + if(cleanfn){ + cleanfn(data); + } + ARC_Vector_RemoveIndex(handler->trash, &i); } }