Compare commits
No commits in common. "0d916b56f2eec30030d999c94325e8e86d7d4244" and "44ec7317a134e84c948f439903c83ada3857ffc4" have entirely different histories.
0d916b56f2
...
44ec7317a1
@ -12,9 +12,9 @@ Testing some (potentially stupid 😅) ideas for a generic event system implment
|
||||
- [x] ~~Register a event new when not out of capacity and no free events exist~~
|
||||
- [x] ~~Re-allocate to add more events when out of capacity~~
|
||||
- [x] ~~Implement removing a previously registered event~~
|
||||
- [x] ~~Implement adding new event listeners~~
|
||||
- [ ] Implement adding new event listeners
|
||||
- [x] ~~Re-use a free slot if one exists~~
|
||||
- [x] ~~Register a new listeners when not out of capacity and no free listeners exist~~
|
||||
- [x] ~~Re-allocate to add more listeners when out of capacity~~
|
||||
- [ ] Re-allocate to add more listeners when out of capacity
|
||||
- [x] ~~Implement removing an existing listener~~
|
||||
- [x] ~~Implement emitting an event~~
|
||||
|
@ -54,7 +54,6 @@ internal void *alloc(u64 size);
|
||||
internal ESInternal *get_event_system_internal(EventSystem event_system);
|
||||
internal EventSlot *get_event_slots_array(const ESInternal *es);
|
||||
internal u64 *get_free_array(const ESInternal *es);
|
||||
internal void reallocate_system(EventSystem *event_system, ESInternal *es);
|
||||
internal void copy_event_data(const ESInternal *src, ESInternal *dst);
|
||||
internal EventInternal *get_event(const ESInternal *es, Event event);
|
||||
internal EventInternal *get_event_from_offset(const ESInternal *es, u64 offset);
|
||||
@ -173,7 +172,18 @@ Event es_register_event(EventSystem *event_system) {
|
||||
}
|
||||
|
||||
if (es->event_count >= es->event_capacity) {
|
||||
reallocate_system(event_system, es);
|
||||
u64 new_callback_capacity = get_largest_capacity(es);
|
||||
EventSystem event_system_new = es_init(es->event_capacity, new_callback_capacity);
|
||||
ESInternal *es_new = get_event_system_internal(event_system_new);
|
||||
|
||||
es_new->id = es->id;
|
||||
es_new->event_count = es->event_count;
|
||||
|
||||
copy_event_data(es, es_new);
|
||||
|
||||
es_deinit(event_system);
|
||||
event_system->id = (u64)es_new;
|
||||
es = es_new;
|
||||
}
|
||||
|
||||
event.id = ++(es->event_count);
|
||||
@ -197,13 +207,9 @@ void es_deregister_event(EventSystem event_system, Event *event) {
|
||||
|
||||
EventSlot *event_slots_array = get_event_slots_array(es);
|
||||
u64 *free_array = get_free_array(es);
|
||||
EventInternal *ev = get_event(es, *event);
|
||||
|
||||
event_slots_array[event->id].registered = false;
|
||||
free_array[(es->free_count)++] = event->id;
|
||||
|
||||
ev->count = 0;
|
||||
ev->free_count = 0;
|
||||
free_array[(es->free_count)++] = event->id;
|
||||
|
||||
if (event->id == es->event_count) {
|
||||
es->event_count -= 1;
|
||||
@ -239,8 +245,8 @@ EventListener es_add_event_listener(EventSystem *event_system, Event event, Even
|
||||
}
|
||||
|
||||
if (ev->count >= ev->capacity) {
|
||||
reallocate_system(event_system, es);
|
||||
ev = get_event(es, event);
|
||||
// TODO (Abdelrahman): Handle reallocating when out of callbacks
|
||||
goto RETURN_LISTENER;
|
||||
}
|
||||
|
||||
u64 id = ++(ev->count);
|
||||
@ -321,21 +327,6 @@ internal u64 *get_free_array(const ESInternal *es) {
|
||||
return (u64 *)((uptr)es + es->free_offset);
|
||||
}
|
||||
|
||||
internal void reallocate_system(EventSystem *event_system, ESInternal *es) {
|
||||
u64 new_callback_capacity = get_largest_capacity(es);
|
||||
EventSystem event_system_new = es_init(es->event_capacity, new_callback_capacity);
|
||||
ESInternal *es_new = get_event_system_internal(event_system_new);
|
||||
|
||||
es_new->id = es->id;
|
||||
es_new->event_count = es->event_count;
|
||||
|
||||
copy_event_data(es, es_new);
|
||||
|
||||
es_deinit(event_system);
|
||||
event_system->id = event_system_new.id;
|
||||
es = es_new;
|
||||
}
|
||||
|
||||
internal void copy_event_data(const ESInternal *src, ESInternal *dst) {
|
||||
EventSlot *slots_src = get_event_slots_array(src);
|
||||
EventSlot *slots_dst = get_event_slots_array(dst);
|
||||
|
10
src/main.c
10
src/main.c
@ -37,7 +37,7 @@ int main(void) {
|
||||
EventListener listeners[COUNT_EVENTS] = {0};
|
||||
EventCallback callbacks[COUNT_EVENTS] = {window_event_handler, keyboard_event_handler, mouse_event_handler};
|
||||
|
||||
EventSystem es = es_init(COUNT_EVENTS, 4);
|
||||
EventSystem es = es_init(COUNT_EVENTS, 64);
|
||||
|
||||
for (int i = 0; i < COUNT_EVENTS; ++i) {
|
||||
events[i] = es_register_event(&es);
|
||||
@ -71,18 +71,10 @@ int main(void) {
|
||||
es_register_event(&es);
|
||||
}
|
||||
|
||||
printf("AFTER REALLOCATION WHEN OUT OF EVENTS\n\n");
|
||||
es_emit_event(es, events[EVENT_WINDOW], (void *)&window_event);
|
||||
es_emit_event(es, events[EVENT_KEYBOARD], (void *)&keyboard_event);
|
||||
es_emit_event(es, events[EVENT_MOUSE], (void *)&mouse_event);
|
||||
|
||||
for (int i = 0; i < 20; ++i) {
|
||||
es_add_event_listener(&es, events[EVENT_WINDOW], extra_event_handler);
|
||||
}
|
||||
|
||||
printf("AFTER REALLOCATION WHEN OUT OF CALLBACKS\n\n");
|
||||
es_emit_event(es, events[EVENT_WINDOW], (void *)&window_event);
|
||||
|
||||
for (int i = 0; i < COUNT_EVENTS; ++i) {
|
||||
es_remove_event_listener(es, listeners[i]);
|
||||
es_deregister_event(es, &events[i]);
|
||||
|
Loading…
Reference in New Issue
Block a user