input and handler possibly fixed

This commit is contained in:
herbglitch 2022-11-11 01:15:33 -07:00
parent 31b8730a61
commit d6281e8eac
11 changed files with 84 additions and 59 deletions

View file

@ -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 <SDL.h>
#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);

View file

@ -1,11 +1,10 @@
#include "arc/engine/state.h"
#include <stddef.h>
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);
}

View file

@ -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);
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}