Compare commits
3 Commits
2179c7eb94
...
571bb9699d
| Author | SHA1 | Date |
|---|---|---|
|
|
571bb9699d | |
|
|
c57cfea7f7 | |
|
|
abf1231aac |
|
|
@ -4,6 +4,7 @@ CORE_SOURCES := \
|
|||
vector2d.c \
|
||||
inputs.c \
|
||||
asset_manager.c \
|
||||
event_bus.c \
|
||||
ecs.c \
|
||||
display.c \
|
||||
main.c
|
||||
|
|
|
|||
|
|
@ -10,12 +10,6 @@
|
|||
|
||||
int create_asset(asset_t *asset, va_list args)
|
||||
{
|
||||
if(!asset)
|
||||
{
|
||||
error_printf("Failed to allocate memory : %d", errno);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
asset->type = va_arg(args, asset_type_t);
|
||||
asset->nb_refs = 0;
|
||||
|
||||
|
|
|
|||
27
src/ecs.c
27
src/ecs.c
|
|
@ -6,12 +6,6 @@
|
|||
|
||||
int create_component(component_t *component, va_list args)
|
||||
{
|
||||
if(!component)
|
||||
{
|
||||
error_printf("Component cannot be NULL");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
component->type = va_arg(args, component_type_t);
|
||||
|
||||
switch(component->type)
|
||||
|
|
@ -41,12 +35,6 @@ int create_component(component_t *component, va_list args)
|
|||
|
||||
inline int destroy_component(component_t *component)
|
||||
{
|
||||
if(!component)
|
||||
{
|
||||
error_printf("Component cannot be NULL");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if(component->deleter(component)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
|
@ -54,12 +42,6 @@ inline int destroy_component(component_t *component)
|
|||
|
||||
int create_entity(entity_t *entity, va_list args)
|
||||
{
|
||||
if(!entity)
|
||||
{
|
||||
error_printf("Entity cannot be NULL");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
entity->id = va_arg(args, size_t);
|
||||
entity->draw_priority = 0;
|
||||
|
||||
|
|
@ -70,12 +52,6 @@ int create_entity(entity_t *entity, va_list args)
|
|||
|
||||
inline int destroy_entity(entity_t *entity)
|
||||
{
|
||||
if(!entity)
|
||||
{
|
||||
error_printf("Entity cannot be NULL");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if(linked_list_exit(&entity->components)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
|
@ -85,6 +61,7 @@ component_t *entity_new_component(entity_t *entity, component_type_t component_t
|
|||
{
|
||||
elem_t *component_elem = create_elem(&entity->components, component_type);
|
||||
if(!component_elem) return NULL;
|
||||
|
||||
component_t *component = component_elem->data;
|
||||
|
||||
component->entity = entity;
|
||||
|
|
@ -118,6 +95,7 @@ inline component_t *entity_get_component(const entity_t *entity, component_type_
|
|||
static inline int action_update_component(elem_t *elem, va_list args __attribute__((unused)))
|
||||
{
|
||||
component_t *component = elem->data;
|
||||
|
||||
if(component->update(component)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
|
@ -135,6 +113,7 @@ inline int update_entity(entity_t *entity)
|
|||
static inline int action_draw_component(elem_t *elem, va_list args __attribute__((unused)))
|
||||
{
|
||||
component_t *component = elem->data;
|
||||
|
||||
if(component->draw) if(component->draw(component)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,118 @@
|
|||
#include "event_bus.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "linked_list.h"
|
||||
#include "memory_alloc.h"
|
||||
|
||||
// Args : void *payload
|
||||
int create_event(event_t *event, va_list args)
|
||||
{
|
||||
event->payload = va_arg(args, void *);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int destroy_event(event_t *event)
|
||||
{
|
||||
free(event->payload);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// Args : callback_t callback
|
||||
int create_subscription(subscription_t *subscription, va_list args)
|
||||
{
|
||||
subscription->callback = va_arg(args, callback_t);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// Args : size_t id, size_t max_history_size
|
||||
int create_topic(topic_t *topic, va_list args)
|
||||
{
|
||||
topic->id = va_arg(args, size_t);
|
||||
topic->max_history_size = va_arg(args, size_t);
|
||||
|
||||
linked_list_init(&topic->events, sizeof(event_t), (allocator_t)create_event, (deleter_t)destroy_event);
|
||||
linked_list_init(&topic->subscriptions, sizeof(subscription_t), (allocator_t)create_subscription, NULL);
|
||||
|
||||
if(linked_list_reserve(&topic->events, topic->max_history_size)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int destroy_topic(topic_t *topic)
|
||||
{
|
||||
if(linked_list_exit(&topic->events)) return EXIT_FAILURE;
|
||||
if(linked_list_exit(&topic->subscriptions)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int event_bus_init(event_bus_t *event_bus)
|
||||
{
|
||||
linked_list_init(&event_bus->topics, sizeof(topic_t), (allocator_t)create_topic, (deleter_t)destroy_topic);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int event_bus_exit(event_bus_t *event_bus)
|
||||
{
|
||||
if(linked_list_exit(&event_bus->topics)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int event_bus_new_topic(event_bus_t *event_bus, size_t id, size_t max_history_size)
|
||||
{
|
||||
if(!create_elem(&event_bus->topics, id, max_history_size)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// Args : size_t id
|
||||
static inline bool condition_is_topic_id(elem_t *elem, va_list args)
|
||||
{
|
||||
return ((topic_t *)elem->data)->id == va_arg(args, size_t);
|
||||
}
|
||||
|
||||
int event_bus_remove_topic(event_bus_t *event_bus, size_t id)
|
||||
{
|
||||
if(linked_list_remove_if(&event_bus->topics, condition_is_topic_id, id)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// Args : void *payload
|
||||
static inline int action_execute_callback(elem_t *elem, va_list args)
|
||||
{
|
||||
subscription_t *subscription = elem->data;
|
||||
|
||||
if(subscription->callback(va_arg(args, void *))) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int event_bus_publish(event_bus_t *event_bus, size_t id, void *payload)
|
||||
{
|
||||
elem_t *topic_elem = linked_list_get_if(&event_bus->topics, condition_is_topic_id, id);
|
||||
if(!topic_elem) return EXIT_FAILURE;
|
||||
|
||||
topic_t *topic = topic_elem->data;
|
||||
if(!create_elem(&topic->events, payload)) return EXIT_FAILURE;
|
||||
|
||||
if(linked_list_for_each(&topic->subscriptions, action_execute_callback, payload)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int event_bus_subscribe(event_bus_t *event_bus, size_t id, callback_t callback)
|
||||
{
|
||||
elem_t *topic_elem = linked_list_get_if(&event_bus->topics, condition_is_topic_id, id);
|
||||
if(!topic_elem) return EXIT_FAILURE;
|
||||
|
||||
topic_t *topic = topic_elem->data;
|
||||
if(!create_elem(&topic->subscriptions, callback)) return EXIT_FAILURE;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
#ifndef EVENT_BUS_H
|
||||
#define EVENT_BUS_H
|
||||
|
||||
#include <stdarg.h>
|
||||
#include "linked_list.h"
|
||||
|
||||
typedef int (*callback_t)(void *payload);
|
||||
|
||||
typedef struct event_t {
|
||||
void *payload;
|
||||
} event_t;
|
||||
|
||||
int create_event(event_t *event, va_list args);
|
||||
int destroy_event(event_t *event);
|
||||
|
||||
typedef struct subscription_t {
|
||||
callback_t callback;
|
||||
} subscription_t;
|
||||
|
||||
int create_subscription(subscription_t *subscription, va_list args);
|
||||
|
||||
typedef struct topic_t {
|
||||
size_t id;
|
||||
linked_list_t events;
|
||||
linked_list_t subscriptions;
|
||||
size_t max_history_size;
|
||||
} topic_t;
|
||||
|
||||
int create_topic(topic_t *topic, va_list args);
|
||||
int destroy_topic(topic_t *topic);
|
||||
|
||||
typedef struct event_bus_t {
|
||||
linked_list_t topics;
|
||||
} event_bus_t;
|
||||
|
||||
int event_bus_init(event_bus_t *event_bus);
|
||||
int event_bus_exit(event_bus_t *event_bus);
|
||||
|
||||
int event_bus_new_topic(event_bus_t *event_bus, size_t id, size_t max_history_size);
|
||||
int event_bus_remove_topic(event_bus_t *event_bus, size_t id);
|
||||
|
||||
int event_bus_publish(event_bus_t *event_bus, size_t id, void *payload);
|
||||
int event_bus_subscribe(event_bus_t *event_bus, size_t id, callback_t callback);
|
||||
|
||||
#endif // EVENT_BUS_H
|
||||
|
|
@ -19,7 +19,7 @@
|
|||
typedef struct game_t {
|
||||
asset_manager_t asset_manager;
|
||||
entity_manager_t entity_manager;
|
||||
events_t events;
|
||||
inputs_t events;
|
||||
|
||||
float delta_time_ms;
|
||||
bool is_running;
|
||||
|
|
|
|||
|
|
@ -5,20 +5,19 @@
|
|||
#include "vector2d.h"
|
||||
#include "keycodes.h"
|
||||
|
||||
#define EVENT_KEY_UP SDL_EVENT_KEY_UP
|
||||
#define EVENT_KEY_DOWN SDL_EVENT_KEY_DOWN
|
||||
#define EVENT_QUIT SDL_EVENT_QUIT
|
||||
#define INPUT_EVENT_KEY_UP SDL_EVENT_KEY_UP
|
||||
#define INPUT_EVENT_KEY_DOWN SDL_EVENT_KEY_DOWN
|
||||
#define INPUT_EVENT_QUIT SDL_EVENT_QUIT
|
||||
|
||||
typedef struct event_t {
|
||||
typedef struct input_event_t {
|
||||
uint32_t type;
|
||||
SDL_Scancode key;
|
||||
} event_t;
|
||||
} input_event_t;
|
||||
|
||||
int pollevent(event_t *event);
|
||||
int poll_event(input_event_t *event);
|
||||
|
||||
typedef struct events_t {
|
||||
vector2d_t cursor_pos;
|
||||
typedef struct inputs_t {
|
||||
bool keys[MAX_KEYCODES];
|
||||
} events_t;
|
||||
} inputs_t;
|
||||
|
||||
#endif // KEYBOARD_H
|
||||
|
|
@ -258,7 +258,7 @@ elem_t *linked_list_get_if(const linked_list_t *linked_list, const condition_t c
|
|||
* @param condition Condition to verify
|
||||
* @param ... Argument to pass to the condition
|
||||
*
|
||||
* @return Pointer to element, NULL if no element verify the condition or if an error occurred.
|
||||
* @return EXIT_SUCCESS, EXIT_FAILURE on error.
|
||||
*/
|
||||
int linked_list_remove_if(linked_list_t *linked_list, const condition_t condition, ...);
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
#include <SDL3/SDL_events.h>
|
||||
|
||||
inline int pollevent(event_t *event)
|
||||
inline int poll_event(input_event_t *event)
|
||||
{
|
||||
SDL_Event sdl_event;
|
||||
int ret = SDL_PollEvent(&sdl_event);
|
||||
|
|
|
|||
Loading…
Reference in New Issue