diff options
| author | Andrew Guschin <saintruler@gmail.com> | 2021-09-12 20:21:28 +0400 |
|---|---|---|
| committer | Andrew Guschin <saintruler@gmail.com> | 2021-09-12 20:21:28 +0400 |
| commit | 5438fe938f6a15f4eadcc0bb2fd09a02130aafb4 (patch) | |
| tree | f05d70112e40c255c225cb29d0d08853df633ae2 | |
| parent | 259c4e23a136d2077c4083c4bbbad55a2a31bcef (diff) | |
Renamed most of structs and changed allocation strategy for storage
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | src/main.c | 44 | ||||
| -rw-r--r-- | src/storage.c | 37 | ||||
| -rw-r--r-- | src/storage.h | 27 |
4 files changed, 54 insertions, 58 deletions
@@ -1,11 +1,11 @@ -CC=tcc +CC=gcc CFLAGS=-Wall -Wpedantic -ggdb -std=c99 INCLUDE_DIR=include BUILD_DIR=build PACKAGE_DIR=package LIBS_DIR=libs -LIBS=-lXi -ldl -lm +LIBS=-ldl -lm SOURCES := $(wildcard src/*.c) OBJECTS := $(patsubst src/%.c,$(BUILD_DIR)/%.o,$(SOURCES)) @@ -8,10 +8,10 @@ typedef struct { int x; int y; -} Transform; +} transform_t; void -printTransform(Transform *t) +printTransform(transform_t *t) { printf("Transform { .x = %i, .y = %i }\n", t->x, t->y); } @@ -21,43 +21,39 @@ typedef struct float width; float height; int collisionsCount; -} Collider; +} collider_t; void -initStorage(storage *storage) +initStorage(storage_t *storage) { storage->componentsStored = 0; storage->objectsStored = 0; - storage->objects = (component_pair *) malloc(MAX_COMPONENTS * sizeof(component_pair)); + storage->objects = (component_pair_t *) malloc(MAX_COMPONENTS * sizeof(component_pair_t)); - REGISTER_COMPONENT(storage, Transform); - REGISTER_COMPONENT(storage, Collider); + REGISTER_COMPONENT(storage, transform_t); + REGISTER_COMPONENT(storage, collider_t); } void -loadScene(storage *storage) +loadScene(storage_t *storage) { - Entity e1 = createEntity(storage); - ADD_COMPONENT(storage, e1, Transform); - - Transform *t1 = GET_COMPONENT(storage, Transform, e1); - if (t1 == NULL) - { - printf("t1 is NULL\n"); - } - else - { - printf("t1 is NOT NULL\n"); - t1->x = 13; - t1->y = 37; - printTransform(t1); - } + entity_t e1 = createEntity(storage); + transform_t *t1 = GET_COMPONENT(storage, transform_t, e1); + t1->x = 13; + t1->y = 37; + printTransform(t1); + + entity_t e2 = createEntity(storage); + transform_t *t2 = GET_COMPONENT(storage, transform_t, e2); + t2->x = 14; + t2->y = 88; + printTransform(t2); } int main() { - storage storage; + storage_t storage; initStorage(&storage); loadScene(&storage); return 0; diff --git a/src/storage.c b/src/storage.c index 2fd2601..3b715ee 100644 --- a/src/storage.c +++ b/src/storage.c @@ -1,11 +1,11 @@ #include "storage.h" -component_pair * -findPair(storage *this, const char *name) +component_pair_t * +findPair(storage_t *this, const char *name) { for (int i = 0; i < this->componentsStored; ++i) { - component_pair *pair = &this->objects[i]; + component_pair_t *pair = &this->objects[i]; if (strcmp(name, pair->name) == 0) { return pair; @@ -14,55 +14,54 @@ findPair(storage *this, const char *name) return NULL; } -Entity -createEntity(storage *this) +entity_t +createEntity(storage_t *this) { - Entity idx = this->objectsStored++; + entity_t idx = this->objectsStored++; for (int i = 0; i < this->componentsStored; ++i) { - this->objects[i].objects[idx] = NULL; + component_pair_t pair = this->objects[i]; + memset(&pair.objects[idx], 0, pair.objectSize); } return idx; } // Not intended for use outside of macros void -internal_registerComponent(storage *storage, char *name, size_t size) +internal_registerComponent(storage_t *storage, const char *name, size_t size) { unsigned int componentIdx = storage->componentsStored++; - storage->objects[componentIdx] = (component_pair) { + storage->objects[componentIdx] = (component_pair_t) { .name = name, .objectSize = size, - .objects = malloc(sizeof(void*) * MAX_OBJECTS) + .objects = malloc(sizeof(char) * size * MAX_OBJECTS) }; } void * // NULL if component with such name is not found -internal_addComponent(storage *this, Entity idx, const char *name) +internal_addComponent(storage_t *this, entity_t idx, const char *name) { - component_pair *pair = findPair(this, name); + component_pair_t *pair = findPair(this, name); if (pair == NULL) { return NULL; } - pair->objects[idx] = (void *) malloc(pair->objectSize); - void *component = pair->objects[idx]; - // component = (void *) malloc(pair->objectSize); + // TODO: add bitset implementation - return component; + return &pair->objects[idx]; } // Not intended for use outside of macros void * // NULL if component is not found -internal_getComponent(storage *this, Entity idx, const char *name) +internal_getComponent(storage_t *this, entity_t idx, const char *name) { for (int i = 0; i < this->componentsStored; ++i) { - component_pair *pair = &this->objects[i]; + component_pair_t *pair = &this->objects[i]; if (strcmp(name, pair->name) == 0) { - return pair->objects[idx]; + return &pair->objects[idx]; } } return NULL; diff --git a/src/storage.h b/src/storage.h index 44fe5fb..3989c4a 100644 --- a/src/storage.h +++ b/src/storage.h @@ -16,38 +16,39 @@ #define REGISTER_COMPONENT(storage, componentType) internal_registerComponent(storage, #componentType, sizeof(componentType)) #define ADD_COMPONENT(storage, idx, componentType) internal_addComponent(storage, idx, #componentType) -typedef unsigned int Entity; +typedef unsigned int entity_t; typedef struct { - char *name; + const char *name; size_t objectSize; - void **objects; -} component_pair; + char *objects; +} component_pair_t; typedef struct { unsigned int componentsStored; unsigned int objectsStored; - component_pair *objects; -} storage; + component_pair_t *objects; +} storage_t; -component_pair * // NULL if pair is not found -findPair(storage *this, const char *name); +component_pair_t * // NULL if pair is not found +findPair(storage_t *this, const char *name); -Entity -createEntity(storage *this); +entity_t +createEntity(storage_t *this); // Not intended for use outside of macros void -internal_registerComponent(storage *storage, char *name, size_t size); +internal_registerComponent(storage_t *storage, const char *name, size_t size); +// Not intended for use outside of macros void * // NULL if component with such name is not found -internal_addComponent(storage *this, Entity idx, const char *name); +internal_addComponent(storage_t *this, entity_t idx, const char *name); // Not intended for use outside of macros void * // NULL if component is not found -internal_getComponent(storage *this, Entity idx, const char *name); +internal_getComponent(storage_t *this, entity_t idx, const char *name); #endif // storage_h_INCLUDED |