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

@ -7,12 +7,16 @@ extern "C" {
#include "arc/graphics/window.h" #include "arc/graphics/window.h"
#include "arc/graphics/renderer.h" #include "arc/graphics/renderer.h"
#include "arc/input/mouse.h"
#include "arc/input/keyboard.h"
#include "arc/std/handler.h" #include "arc/std/handler.h"
typedef struct ARC_EngineData { typedef struct ARC_EngineData {
ARC_Window *window; ARC_Window *window;
ARC_Renderer *renderer; ARC_Renderer *renderer;
ARC_Handler *state; ARC_Handler *state;
ARC_Mouse *mouse;
ARC_Keyboard *keyboard;
double dt; double dt;
} ARC_EngineData; } ARC_EngineData;

View file

@ -5,9 +5,12 @@
extern "C" { extern "C" {
#endif #endif
typedef void (* ARC_State_UpdateFn)(void *data);
typedef void (* ARC_State_RenderFn)(void *data);
typedef struct ARC_State { typedef struct ARC_State {
void (* updateFn)(); ARC_State_UpdateFn updateFn;
void (* renderFn)(); ARC_State_RenderFn renderFn;
void *data;
} ARC_State; } ARC_State;
void ARC_State_Update(void *data); void ARC_State_Update(void *data);

View file

@ -18,7 +18,7 @@ typedef struct ARC_RenderInfo ARC_RenderInfo;
* @param renderer ARC_Renderer to initialize * @param renderer ARC_Renderer to initialize
* @param info Info on how to create ARC_Window * @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 * @brief destroys ARC_Renderer type

View file

@ -1,30 +1,12 @@
#ifndef ARC_MATH_VECTOR2_H_ #ifndef ARC_MATH_VECTOR2_H_
#define ARC_MATH_VECTOR2_H_ #define ARC_MATH_VECTOR2_H_
#include <stdint.h>
typedef struct ARC_Vector2 { 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; float x, y;
} ARC_FVector2; } ARC_Vector2;
typedef struct ARC_DVector2 { typedef struct ARC_DVector2 {
double x, y; double x, y;
} ARC_DVector2; } 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_ #endif // ARC_MATH_VECTOR2_H_

View file

@ -8,12 +8,12 @@
#define ARC_ERRNO_COPY -0x03 #define ARC_ERRNO_COPY -0x03
#define ARC_ERRNO_EXISTS -0x04 #define ARC_ERRNO_EXISTS -0x04
#define ARC_ERRNO_OVERFLOW -0x05 #define ARC_ERRNO_OVERFLOW -0x05
#define ARC_ERRNO_INIT -0x06
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable" #pragma GCC diagnostic ignored "-Wunused-variable"
static int32_t arc_errno = 0; static int32_t arc_errno = 0;
// #pragma GCC diagnostic pop #pragma GCC diagnostic pop
#ifdef __cplusplus #ifdef __cplusplus

View file

@ -4,76 +4,101 @@
#include "arc/engine/state.h" #include "arc/engine/state.h"
#include "arc/graphics/window.h" #include "arc/graphics/window.h"
#include "arc/graphics/renderer.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" #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 #ifdef ARC_SDL
#include <SDL.h> #include <SDL.h>
#include "arc/graphics/sdl/window.h" #include "arc/graphics/sdl/window.h"
#include "arc/graphics/sdl/renderer.h" #include "arc/graphics/sdl/renderer.h"
#include "arc/input/sdl/mouse.h"
#include "arc/input/sdl/keyboard.h"
#endif // ARC_SDL #endif // ARC_SDL
void ARC_EngineData_Create(ARC_EngineData **data){ void ARC_EngineData_Create(ARC_EngineData **data){
*data = (ARC_EngineData *)malloc(sizeof(ARC_EngineData)); *data = (ARC_EngineData *)malloc(sizeof(ARC_EngineData));
(*data)->window = NULL; (*data)->window = NULL;
(*data)->renderer = NULL; (*data)->renderer = NULL;
(*data)->mouse = NULL;
ARC_Handler_Create(&((*data)->state), sizeof(ARC_State)); ARC_Handler_Create(&((*data)->state), sizeof(ARC_State));
ARC_WindowInfo windowInfo; ARC_WindowInfo windowInfo;
ARC_RenderInfo renderInfo; ARC_RenderInfo renderInfo;
ARC_MouseInfo mouseInfo;
ARC_KeyboardInfo keyboardInfo;
#ifdef ARC_SDL #ifdef ARC_SDL
if(SDL_Init(SDL_INIT_VIDEO) < 0){ windowInfo = (ARC_WindowInfo){ "title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 2560, 1440, 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 };
#endif // ARC_SDL #endif // ARC_SDL
ARC_Window_Create(&((*data)->window), &windowInfo); ARC_Window_Create(&((*data)->window), &windowInfo);
//TODO: handle arc_errno errors here if(arc_errno){
free(data);
return;
}
#ifdef ARC_SDL #ifdef ARC_SDL
renderInfo = (ARC_RenderInfo){ (SDL_Window *)(*data)->window->window, -1, SDL_RENDERER_ACCELERATED }; renderInfo = (ARC_RenderInfo){ (SDL_Window *)(*data)->window->window, -1, SDL_RENDERER_ACCELERATED };
#endif // ARC_SDL #endif // ARC_SDL
ARC_Renderer_Create(&((*data)->renderer), &renderInfo); 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){ void ARC_EngineData_Destroy(ARC_EngineData *data){
#ifdef ARC_SDL #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_Renderer_Destroy(data->renderer);
ARC_Window_Destroy(data->window); 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){ void ARC_Engine_Run(ARC_EngineData *data){
double lastTime = 0, currentTime; if(arc_errno){
return;
}
#ifdef ARC_SDL #ifdef ARC_SDL
SDL_Event event; SDL_Event *event = data->mouse->event;
#endif // ARC_SDL #endif // ARC_SDL
double lastTime = 0, currentTime;
while(1){ while(1){
#ifdef ARC_SDL #ifdef ARC_SDL
currentTime = SDL_GetTicks(); currentTime = SDL_GetTicks();
data->dt = currentTime - lastTime; data->dt = currentTime - lastTime;
lastTime = currentTime; lastTime = currentTime;
while(SDL_PollEvent(&event)){ SDL_PollEvent(data->mouse->event);
if(event.type == SDL_QUIT){ return; } if(event->type == SDL_QUIT){ return; }
if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE){ return; } if(event->type == SDL_KEYDOWN && event->key.keysym.sym == SDLK_ESCAPE){ return; }
}
#endif // ARC_SDL #endif // ARC_SDL
// data->mouse.update(data->event); ARC_Mouse_Update(data->mouse);
// data->keyboard.update(data->event);
ARC_Keyboard_Update(data->keyboard);
// data->state.update();
ARC_Handler_Iterate(data->state, ARC_State_Update); ARC_Handler_Iterate(data->state, ARC_State_Update);
ARC_Renderer_Clear(data->renderer); ARC_Renderer_Clear(data->renderer);

View file

@ -1,11 +1,10 @@
#include "arc/engine/state.h" #include "arc/engine/state.h"
#include <stddef.h>
void ARC_State_Update(void *data){ void ARC_State_Update(void *data){
ARC_State *temp = (ARC_State *) data; ((ARC_State *)data)->updateFn(((ARC_State *)data)->data);
((ARC_State *)data)->updateFn();
} }
void ARC_State_Render(void *data){ void ARC_State_Render(void *data){
ARC_State *temp = (ARC_State *) data; ((ARC_State *)data)->renderFn(((ARC_State *)data)->data);
((ARC_State *)data)->renderFn();
} }

View file

@ -17,9 +17,10 @@ void ARC_Renderer_Create(ARC_Renderer **renderer, ARC_RenderInfo *info){
*renderer = (ARC_Renderer *)malloc(sizeof(ARC_Renderer)); *renderer = (ARC_Renderer *)malloc(sizeof(ARC_Renderer));
(*renderer)->renderer = SDL_CreateRenderer((SDL_Window *)info->window, info->index, info->flags); (*renderer)->renderer = SDL_CreateRenderer((SDL_Window *)info->window, info->index, info->flags);
if(!renderer){ if(!(*renderer)->renderer){
arc_errno = ARC_ERRNO_NULL; arc_errno = ARC_ERRNO_NULL;
ARC_DEBUG_LOG(arc_errno, "SDL_CreateRenderer(%p, %d, %u);", info->window, info->index, info->flags); 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; 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 = (ARC_Window *)malloc(sizeof(ARC_Window));
(*window)->window = SDL_CreateWindow((const char *)info->title, info->x, info->y, info->w, info->h, info->flags); (*window)->window = SDL_CreateWindow((const char *)info->title, info->x, info->y, info->w, info->h, info->flags);
if(!(*window)->window){ if(!(*window)->window){
arc_errno = ARC_ERRNO_NULL; 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); 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; } int8_t ARC_Handler_RemoveCompareFn(void *a, void *b){ return a == b; }
void ARC_Handler_Remove(ARC_Handler *handler, void *data){ 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); ARC_Vector_Remove(handler->data, data, ARC_Handler_RemoveCompareFn);
} }
void ARC_Handler_RemoveIndex(ARC_Handler *handler, uint32_t *index){ void ARC_Handler_RemoveIndex(ARC_Handler *handler, uint32_t *index){
void *data = ARC_Vector_Get(handler->data, index); void *data = ARC_Vector_Get(handler->data, index);
ARC_Vector_RemoveIndex(handler->data, index);
ARC_Vector_Add(handler->trash, data); ARC_Vector_Add(handler->trash, data);
ARC_Vector_RemoveIndex(handler->data, index);
} }
void ARC_Handler_Iterate(ARC_Handler *handler, ARC_Handler_DataFn datafn){ 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){ void ARC_Handler_Clear(ARC_Handler *handler){
uint32_t zeroIndex = 0;
while(*ARC_Vector_Size(handler->data)){ 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; uint32_t i = 0;
while(*ARC_Vector_Size(handler->trash)){ while(*ARC_Vector_Size(handler->trash)){
void *data = ARC_Vector_Get(handler->trash, &i); void *data = ARC_Vector_Get(handler->trash, &i);
if(cleanfn){
cleanfn(data); cleanfn(data);
}
ARC_Vector_RemoveIndex(handler->trash, &i); ARC_Vector_RemoveIndex(handler->trash, &i);
} }
} }