Fumofumotris/source/fumoengine/include/event.c

52 lines
1.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
bool CreateEvent(struct Event *event)
2024-04-03 23:31:47 +00:00
{
2024-05-07 22:10:15 +00:00
struct Method *methods = malloc(16 * sizeof(struct Method));
2024-04-09 22:10:30 +00:00
2024-05-07 22:10:15 +00:00
if (methods == 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 22:10:15 +00:00
.methods = methods,
2024-04-03 23:31:47 +00:00
.len = 0,
2024-05-07 22:10:15 +00:00
.capacity = 16
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 22:10:15 +00:00
free(event->methods);
2024-04-22 22:13:13 +00:00
}
2024-05-08 12:47:46 +00:00
bool EventAdd(struct Event *event, handler callback, void *instance)
2024-04-03 23:31:47 +00:00
{
2024-04-22 22:13:13 +00:00
if (event->len == event->capacity) {
2024-05-07 22:10:15 +00:00
usize new_size = event->capacity * 2 * sizeof(struct Method);
struct Method *new_methods = realloc(event->methods, new_size);
2024-04-22 22:13:13 +00:00
2024-05-07 22:10:15 +00:00
if (new_methods == nullptr)
2024-04-22 22:13:13 +00:00
return false;
2024-05-07 22:10:15 +00:00
event->methods = new_methods;
2024-05-07 03:29:10 +00:00
event->capacity = new_size;
2024-04-03 23:31:47 +00:00
}
2024-05-07 22:10:15 +00:00
event->methods[event->len++] = (struct Method) {
2024-05-08 12:47:46 +00:00
.callback = callback,
2024-05-07 22:10:15 +00:00
.instance = instance
};
2024-05-07 03:29:10 +00:00
2024-04-22 22:13:13 +00:00
return true;
}
2024-05-07 22:10:15 +00:00
void EventInvoke(struct Event *event, void *state)
2024-04-03 23:31:47 +00:00
{
2024-05-07 03:29:10 +00:00
for (usize i = 0; i < event->len; i++) {
2024-05-07 22:10:15 +00:00
struct Method *method = event->methods + i;
2024-05-08 12:47:46 +00:00
method->callback(state, method->instance);
2024-04-03 23:31:47 +00:00
}
}