Prepare for event system

Moved stuff around.
Create an entry point for RSE.
Created base for events module

Signed-off-by: Piotr Krygier <piotrkrygier@everyonecancode@xyz>
This commit is contained in:
Piotr Krygier
2026-03-03 11:26:43 +01:00
parent 7d6156be01
commit bbbe69d73c
13 changed files with 245 additions and 53 deletions
+17
View File
@@ -0,0 +1,17 @@
rse_events_sources = [
]
sdl3_dep = dependency('sdl3',
version : '>=3.4.0')
rse_events_lib = shared_library(
'rse_events',
rse_events_sources,
include_directories : [
'../'
],
dependencies : [
],
link_with: rse_utilities_lib,
install : true
)
+37
View File
@@ -0,0 +1,37 @@
/**
* @file events_context.h
* @author Piotr Krygier (piotrkrygier@everyonencancode.xyz)
* @brief Events context for Red Scarf Engine
* @version 0.1
* @date 2026-03-02
*
* @copyright Copyright (c) 2026
*
*/
#ifndef EVENT_CONTEXT_H_
#define EVENT_CONTEXT_H_
#include "SDL3/SDL_mouse.h"
#include "utilities/commons.h"
struct mouse_event_data_t
{
float x_pos;
float y_pos;
SDL_MouseButtonFlags mouse_buttons;
};
typedef rse_err_t (*rse_event_handler_t)(void*);
struct event_handlers
{
rse_event_handler_t mouse_event;
};
struct events_context
{
struct event_handlers event_handlers;
};
#endif /* EVENT_CONTEXT_H_ */
+81
View File
@@ -0,0 +1,81 @@
/**
* @file events_manager.c
* @author Piotr Krygier (piotrkrygier@everyonencancode.xyz)
* @brief Events manager for Red Scarf Engine
* @version 0.1
* @date 2026-03-02
*
* @copyright Copyright (c) 2026
*
*/
#include "events_manager.h"
#include "SDL3/SDL_events.h"
#include "SDL3/SDL_mouse.h"
#include "events_context.h"
#include "utilities/commons.h"
#include "utilities/errors_common.h"
static rse_err_t default_event(void* args)
{
(void)args;
return RSE_ERROR_NO_ERROR;
}
static rse_err_t quit_event(void)
{
return RSE_ERROR_NO_ERROR;
}
static rse_err_t mouse_event(struct events_context* events_context)
{
struct mouse_event_data_t mouse_data = {0};
mouse_data.mouse_buttons = SDL_GetMouseState(&mouse_data.x_pos, &mouse_data.y_pos);
return events_context->event_handlers.mouse_event((void*)&mouse_data);
}
rse_err_t events_init(struct events_context* events_context)
{
events_context->event_handlers.mouse_event = default_event;
return RSE_ERROR_NO_ERROR;
}
rse_err_t events_set_event_handler(struct events_context* events_context,
enum RSE_EVENT_TYPE event_type,
rse_event_handler_t event_handler)
{
switch (event_type) {
case RSE_EVENT_TYPE_MOUSE:
events_context->event_handlers.mouse_event = event_handler;
break;
default:
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "Invalid event type provided");
return RSE_ERROR_INVALID_PARAM;
}
return RSE_ERROR_NO_ERROR;
}
rse_err_t events_run(struct events_context* context)
{
SDL_Event event;
rse_err_t status = RSE_ERROR_NO_ERROR;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_QUIT:
STATUS_CHECK(quit_event());
break;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
STATUS_CHECK(mouse_event(context));
break;
}
}
return status;
}
+20
View File
@@ -0,0 +1,20 @@
/**
* @file events_manager.h
* @author Piotr Krygier (piotrkrygier@everyonencancode.xyz)
* @brief Events manager for Red Scarf Engine
* @version 0.1
* @date 2026-03-02
*
* @copyright Copyright (c) 2026
*
*/
#ifndef EVENTS_MANAGER_H_
#define EVENTS_MANAGER_H_
enum RSE_EVENT_TYPE {
RSE_EVENT_TYPE_MOUSE,
};
#endif /* EVENTS_MANAGER_H_ */
+4 -5
View File
@@ -1,10 +1,9 @@
#ifndef RSE_GRAPHICS_H #ifndef RSE_GRAPHICS_H
#define RSE_GRAPHICS_H #define RSE_GRAPHICS_H
#include "src/graphics_context.h"
#include "utilities/commons.h" #include "utilities/commons.h"
struct rse_graphics_context_t;
/** /**
* @brief Initializes graphics backend. This has to be called before any new mesh, texture or any other object is * @brief Initializes graphics backend. This has to be called before any new mesh, texture or any other object is
* added * added
@@ -12,20 +11,20 @@ struct rse_graphics_context_t;
* @param context Graphics context handle. Must be NULL! * @param context Graphics context handle. Must be NULL!
* @return rse_err_t RSE_ERROR_NO_ERROR on success * @return rse_err_t RSE_ERROR_NO_ERROR on success
*/ */
rse_err_t rse_graphics_init(struct rse_graphics_context_t** context); rse_err_t rse_graphics_init(struct graphics_context_t* context);
/** /**
* @brief Runs graphics engine. This has to be called after all objects are added to the scene * @brief Runs graphics engine. This has to be called after all objects are added to the scene
* *
* @return rse_err_t RSE_ERROR_NO_ERROR on success * @return rse_err_t RSE_ERROR_NO_ERROR on success
*/ */
int rse_graphics_run(void* arg); rse_err_t rse_graphics_main_loop(struct graphics_context_t* context);
/** /**
* @brief Custom function for testing engine. TODO: Remove it whe releasing * @brief Custom function for testing engine. TODO: Remove it whe releasing
* *
*/ */
rse_err_t rse_graphics_test_function(struct rse_graphics_context_t* context); rse_err_t rse_graphics_test_function(struct graphics_context_t* context);
#endif /* RSE_GRAPHICS_H */ #endif /* RSE_GRAPHICS_H */
+5 -21
View File
@@ -106,32 +106,20 @@ static rse_err_t render_static_mesh(struct graphics_context_t* context, uint32_t
return RSE_ERROR_NO_ERROR; return RSE_ERROR_NO_ERROR;
} }
rse_err_t rse_graphics_init(struct rse_graphics_context_t** context) rse_err_t rse_graphics_init(struct graphics_context_t* context)
{ {
rse_err_t status = RSE_ERROR_NO_ERROR; rse_err_t status = RSE_ERROR_NO_ERROR;
struct rse_graphics_context_t* ctx_ptr = NULL;
if (*context != NULL) { STATUS_CHECK(window_init(&context->window_handle, &context->is_framebuffer_resized));
SDL_LogCritical(SDL_LOG_CATEGORY_GPU, "Graphics already initialized!"); STATUS_CHECK(vulkan_init(context));
return RSE_ERROR_ALREADY_INITIALIZED;
}
rse_malloc(*context, sizeof(struct rse_graphics_context_t));
ctx_ptr = *context;
rse_malloc(ctx_ptr->context, sizeof(struct graphics_context_t));
rse_memset(ctx_ptr->context, 0, sizeof(struct graphics_context_t));
STATUS_CHECK(window_init(&ctx_ptr->context->window_handle, &ctx_ptr->context->is_framebuffer_resized));
STATUS_CHECK(vulkan_init(ctx_ptr->context));
return status; return status;
} }
#define TEXTURES_COUNT (2U) #define TEXTURES_COUNT (2U)
rse_err_t rse_graphics_test_function(struct rse_graphics_context_t* rse_context) rse_err_t rse_graphics_test_function(struct graphics_context_t* context)
{ {
rse_err_t status = RSE_ERROR_NO_ERROR; rse_err_t status = RSE_ERROR_NO_ERROR;
struct graphics_context_t* context = rse_context->context;
rse_id_t textures[TEXTURES_COUNT] = {0}; rse_id_t textures[TEXTURES_COUNT] = {0};
struct pipeline_t pipeline; struct pipeline_t pipeline;
rse_id_t descriptor_set_handle = 0; rse_id_t descriptor_set_handle = 0;
@@ -257,11 +245,9 @@ rse_err_t rse_graphics_test_function(struct rse_graphics_context_t* rse_context)
return RSE_ERROR_NO_ERROR; return RSE_ERROR_NO_ERROR;
} }
int rse_graphics_run(void* arg) rse_err_t rse_graphics_main_loop(struct graphics_context_t* context)
{ {
rse_err_t status = RSE_ERROR_NO_ERROR; rse_err_t status = RSE_ERROR_NO_ERROR;
struct rse_graphics_context_t* rse_context = (struct rse_graphics_context_t*)arg;
struct graphics_context_t* context = rse_context->context;
STATUS_CHECK(vulkan_run(context)); STATUS_CHECK(vulkan_run(context));
STATUS_CHECK(window_loop(context)); STATUS_CHECK(window_loop(context));
@@ -270,8 +256,6 @@ int rse_graphics_run(void* arg)
window_terminate(context->window_handle); window_terminate(context->window_handle);
rse_free(context->debug_overlay.pixels); rse_free(context->debug_overlay.pixels);
rse_free(context);
rse_free(rse_context);
return status; return status;
} }
+9 -3
View File
@@ -18,6 +18,7 @@
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include "SDL3/SDL_log.h"
#include "utilities/commons.h" #include "utilities/commons.h"
#include "utilities/errors_common.h" #include "utilities/errors_common.h"
#include "utilities/time_utils.h" #include "utilities/time_utils.h"
@@ -43,7 +44,8 @@ rse_err_t window_init(SDL_Window** window_handle, bool* is_framebuffer_resized)
{ {
SDL_Init(SDL_INIT_VIDEO); // TODO: Move to initial phase of RSE initialization SDL_Init(SDL_INIT_VIDEO); // TODO: Move to initial phase of RSE initialization
(void)is_framebuffer_resized;; // TODO: Actually use this parameter? (void)is_framebuffer_resized;
// TODO: Actually use this parameter?
*window_handle = SDL_CreateWindow("RedScarfEngine", 1024, 768, SDL_WINDOW_VULKAN); *window_handle = SDL_CreateWindow("RedScarfEngine", 1024, 768, SDL_WINDOW_VULKAN);
@@ -65,8 +67,12 @@ rse_err_t window_loop(struct graphics_context_t* context)
while (0 == done) { while (0 == done) {
while (SDL_PollEvent(&event)) { while (SDL_PollEvent(&event)) {
if (SDL_EVENT_QUIT == event.type) { switch (event.type) {
done = 1; case SDL_EVENT_QUIT:
done = 1;
break;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
break;
} }
} }
+2
View File
@@ -8,5 +8,7 @@ project('RedScarfEngine',
subdir('utilities') subdir('utilities')
subdir('graphics') subdir('graphics')
subdir('events')
subdir('red_scarf_engine') subdir('red_scarf_engine')
+1
View File
@@ -1,4 +1,5 @@
red_scarf_engine_srcs = [ red_scarf_engine_srcs = [
'src/red_scarf_engine.c',
'src/main.c' 'src/main.c'
] ]
+23
View File
@@ -0,0 +1,23 @@
/**
* @file red_scarf_engine.h
* @author Piotr Krygier (piotrkrygier@everyonencancode.xyz)
* @brief Red Scarf Engine main entry point
* @version 0.1
* @date 2026-03-03
*
* @copyright Copyright (c) 2026
*
*/
#ifndef RED_SCARF_ENGINE_H_
#define RED_SCARF_ENGINE_H_
#include "utilities/commons.h"
#include "utilities/errors_common.h"
typedef struct rse_context_t* rse_context;
rse_err_t rse_init(rse_context* context);
rse_err_t rse_run(rse_context context);
#endif /* RED_SCARF_ENGINE_H_ */
+5 -23
View File
@@ -1,33 +1,15 @@
#include <SDL3/SDL_init.h> #include "red_scarf_engine.h"
#include <SDL3/SDL_log.h>
#include "graphics/rse_graphics.h"
#include "utilities/errors_common.h"
#include "SDL3/SDL_thread.h"
#include "utilities/time_utils.h"
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
rse_err_t status = RSE_ERROR_NO_ERROR; rse_err_t status = RSE_ERROR_NO_ERROR;
struct rse_graphics_context_t* context = NULL; rse_context context;
SDL_Thread* graphics_task = NULL;
(void)argc; (void)argc;
(void)argv; (void)argv;
SDL_SetLogPriorities(SDL_LOG_PRIORITY_VERBOSE); STATUS_CHECK(rse_init(&context));
STATUS_CHECK(rse_run(context));
STATUS_CHECK(time_init());
STATUS_CHECK(rse_graphics_init(&context));
STATUS_CHECK(rse_graphics_test_function(context));
graphics_task = SDL_CreateThread(rse_graphics_run, "graphics_task", (void*) context);
if (NULL == graphics_task) {
SDL_LogCritical(SDL_LOG_CATEGORY_GPU, "Failed to create graphics task");
exit(1);
}
SDL_WaitThread(graphics_task, NULL);
return 0; return 0;
} }
+40
View File
@@ -0,0 +1,40 @@
#include "red_scarf_engine.h"
#include "graphics/rse_graphics.h"
#include "graphics/src/graphics_context.h"
#include "utilities/commons.h"
#include "utilities/errors_common.h"
#include "utilities/time_utils.h"
struct rse_context_t {
struct graphics_context_t graphics_context;
};
rse_err_t rse_init(rse_context* context)
{
rse_err_t status = RSE_ERROR_NO_ERROR;
struct rse_context_t* rse_context = NULL;
rse_malloc(*context, sizeof(struct rse_context_t));
rse_context = *context;
SDL_SetLogPriorities(SDL_LOG_PRIORITY_VERBOSE);
STATUS_CHECK(time_init());
rse_graphics_init(&(rse_context->graphics_context));
return RSE_ERROR_NO_ERROR;
}
rse_err_t rse_run(rse_context context)
{
rse_err_t status = RSE_ERROR_NO_ERROR;
STATUS_CHECK(rse_graphics_test_function(&context->graphics_context));
STATUS_CHECK(rse_graphics_main_loop(&context->graphics_context));
rse_free(context);
return status;
}
+1 -1
View File
@@ -22,7 +22,7 @@
*/ */
#ifndef RSE_TEST #ifndef RSE_TEST
#define rse_malloc(ptr, size) \ #define rse_malloc(ptr, size) \
ptr = malloc(size); \ ptr = calloc(1, size); \
if (ptr == NULL) { \ if (ptr == NULL) { \
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "Memory allocation failure"); \ SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "Memory allocation failure"); \
exit(1); \ exit(1); \