Fumofumotris/source/fumoengine/include/event.c

50 lines
1 KiB
C
Raw Normal View History

2024-04-22 22:13:13 +00:00
#include "event.h"
2024-04-03 23:31:47 +00:00
2024-04-22 22:13:13 +00:00
#define INIT_CAPACITY 16
2024-04-03 23:31:47 +00:00
2024-04-22 22:13:13 +00:00
bool CreateEvent(struct Event *event)
2024-04-03 23:31:47 +00:00
{
2024-05-07 03:29:10 +00:00
void_func *callbacks = malloc(INIT_CAPACITY * sizeof(void_func));
2024-04-09 22:10:30 +00:00
2024-04-22 22:13:13 +00:00
if (callbacks == nullptr)
2024-04-09 22:10:30 +00:00
return false;
2024-04-22 22:13:13 +00:00
*event = (struct Event) {
2024-05-07 03:29:10 +00:00
.callbacks = callbacks,
2024-04-03 23:31:47 +00:00
.len = 0,
2024-05-07 03:29:10 +00:00
.capacity = INIT_CAPACITY
2024-04-03 23:31:47 +00:00
};
2024-05-07 03:29:10 +00:00
2024-04-09 22:10:30 +00:00
return true;
2024-04-03 23:31:47 +00:00
}
2024-04-22 22:13:13 +00:00
void FreeEvent(struct Event *event)
{
2024-05-07 03:29:10 +00:00
free(event->callbacks);
2024-04-22 22:13:13 +00:00
}
2024-05-07 03:29:10 +00:00
bool EventRegister(struct Event *event, void_func callback)
2024-04-03 23:31:47 +00:00
{
2024-04-22 22:13:13 +00:00
if (event->len == event->capacity) {
2024-05-07 03:29:10 +00:00
usize new_size = event->capacity * 2 * sizeof(void_func);
void_func *new_callbacks = realloc(event->callbacks, new_size);
2024-04-22 22:13:13 +00:00
2024-05-07 03:29:10 +00:00
if (new_callbacks == nullptr)
2024-04-22 22:13:13 +00:00
return false;
2024-05-07 03:29:10 +00:00
event->callbacks = new_callbacks;
event->capacity = new_size;
2024-04-03 23:31:47 +00:00
}
2024-05-07 03:29:10 +00:00
event->callbacks[event->len++] = callback;
2024-04-22 22:13:13 +00:00
return true;
}
2024-05-07 03:29:10 +00:00
void EventInvoke(struct Event *event, void *args)
2024-04-03 23:31:47 +00:00
{
2024-05-07 03:29:10 +00:00
for (usize i = 0; i < event->len; i++) {
event->callbacks[i](args);
2024-04-03 23:31:47 +00:00
}
}