Modified the ObjectStore and the Hashmap and added the ObjectStore to
the GameManager
This commit is contained in:
1 parent
a0e76bc1fc
commit
79f28100bd
8 files changed
+181
-73
No files matched your search
+33
-17
@@ -5,9 +5,12 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
struct object_store {
|
||||
size_t count;
|
||||
int32_t active_indices[MAX_OBJ_COUNT];
|
||||
Hashmap *map;
|
||||
void **object_ptrs;
|
||||
EventHandlerPtr *event_handlers;
|
||||
UpdateFuncPtr *update_funcs;
|
||||
@@ -15,19 +18,20 @@ struct object_store {
|
||||
};
|
||||
|
||||
ObjectStore *create_object_store() {
|
||||
init_hashmap();
|
||||
|
||||
bool allocation_complete = true;
|
||||
|
||||
ObjectStore *os = (ObjectStore *)malloc(sizeof(ObjectStore));
|
||||
if (!os) {
|
||||
// If allocating memory to the store fails, we still need to call the
|
||||
// destroy function to make sure that the memory allocated for the
|
||||
// hashmap is freed.
|
||||
destroy_object_store(os);
|
||||
// If allocating memory to the store fails, we just return NULL. No need to
|
||||
// continue with the rest of the function or to call any cleanup functions
|
||||
return NULL;
|
||||
}
|
||||
|
||||
os->map = create_hashmap();
|
||||
if (!(os->map)) {
|
||||
allocation_complete = false;
|
||||
}
|
||||
|
||||
os->count = 0;
|
||||
|
||||
os->object_ptrs = malloc(sizeof(void *) * MAX_OBJ_COUNT);
|
||||
@@ -70,7 +74,7 @@ ObjectStore *create_object_store() {
|
||||
void add_object_to_store(ObjectStore *os, void *object, const char *object_tag,
|
||||
EventHandlerPtr eh, UpdateFuncPtr uf,
|
||||
RenderFuncPtr rf) {
|
||||
int32_t index = add_object_to_map(object_tag);
|
||||
int32_t index = add_object_to_map(os->map, object_tag);
|
||||
|
||||
if (index < 0) {
|
||||
return;
|
||||
@@ -89,7 +93,7 @@ void remove_object_from_store(ObjectStore *os, const char *object_tag) {
|
||||
return;
|
||||
}
|
||||
|
||||
int32_t index = delete_object_from_map(object_tag);
|
||||
int32_t index = delete_object_from_map(os->map, object_tag);
|
||||
|
||||
if (index < 0) {
|
||||
return;
|
||||
@@ -132,8 +136,11 @@ void handle_all_objects_events(ObjectStore *os, const SDL_Event *event) {
|
||||
|
||||
for (int i = 0; i < os->count; ++i) {
|
||||
int32_t index = os->active_indices[i];
|
||||
void *object = os->object_ptrs[index];
|
||||
(os->event_handlers[index])(event, object);
|
||||
|
||||
if (os->event_handlers[index]) {
|
||||
void *object = os->object_ptrs[index];
|
||||
(os->event_handlers[index])(event, object);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,8 +151,11 @@ void update_all_objects(ObjectStore *os, float delta) {
|
||||
|
||||
for (int i = 0; i < os->count; ++i) {
|
||||
int32_t index = os->active_indices[i];
|
||||
void *object = os->object_ptrs[index];
|
||||
(os->update_funcs[index])(delta, object);
|
||||
|
||||
if (os->update_funcs[index]) {
|
||||
void *object = os->object_ptrs[index];
|
||||
(os->update_funcs[index])(delta, object);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,14 +166,15 @@ void render_all_objects(ObjectStore *os, Renderer *renderer) {
|
||||
|
||||
for (int i = 0; i < os->count; ++i) {
|
||||
int32_t index = os->active_indices[i];
|
||||
void *object = os->object_ptrs[index];
|
||||
(os->render_funcs[index])(renderer, object);
|
||||
|
||||
if (os->render_funcs[index]) {
|
||||
void *object = os->object_ptrs[index];
|
||||
(os->render_funcs[index])(renderer, object);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void destroy_object_store(ObjectStore *os) {
|
||||
destroy_hashmap();
|
||||
|
||||
if (!os) {
|
||||
return;
|
||||
}
|
||||
@@ -194,10 +205,15 @@ void destroy_object_store(ObjectStore *os) {
|
||||
os->object_ptrs = NULL;
|
||||
}
|
||||
|
||||
if (os->map) {
|
||||
destroy_hashmap(os->map);
|
||||
os->map = NULL;
|
||||
}
|
||||
|
||||
free(os);
|
||||
os = NULL;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_ENGINE
|
||||
void print_objects() { print_hashmap(); }
|
||||
void print_objects(ObjectStore *os) { print_hashmap(os->map); }
|
||||
#endif /* DEBUG_ENGINE */
|
||||
Reference in new issue
Block a user