diff options
| author | Andrew Guschin <saintruler@gmail.com> | 2021-09-11 13:48:08 +0400 |
|---|---|---|
| committer | Andrew Guschin <saintruler@gmail.com> | 2021-09-11 13:48:08 +0400 |
| commit | 259c4e23a136d2077c4083c4bbbad55a2a31bcef (patch) | |
| tree | ee3670ece2d0b655d7167c4d3e5883ff895cfc5b | |
initial commit
| -rw-r--r-- | .ccls | 2 | ||||
| -rw-r--r-- | .gitignore | 6 | ||||
| -rw-r--r-- | Makefile | 32 | ||||
| -rw-r--r-- | src/main.c | 64 | ||||
| -rw-r--r-- | src/storage.c | 69 | ||||
| -rw-r--r-- | src/storage.h | 53 |
6 files changed, 226 insertions, 0 deletions
@@ -0,0 +1,2 @@ +tcc +-Iinclude diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8452281 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.ccls-cache/ +build/*.o +build/*.d +package/*.out +package/*.exe +vendor/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..74fcad5 --- /dev/null +++ b/Makefile @@ -0,0 +1,32 @@ +CC=tcc +CFLAGS=-Wall -Wpedantic -ggdb -std=c99 + +INCLUDE_DIR=include +BUILD_DIR=build +PACKAGE_DIR=package +LIBS_DIR=libs +LIBS=-lXi -ldl -lm + +SOURCES := $(wildcard src/*.c) +OBJECTS := $(patsubst src/%.c,$(BUILD_DIR)/%.o,$(SOURCES)) +DEPENDS := $(patsubst src/%.c,$(BUILD_DIR)/%.d,$(SOURCES)) +EXE_NAME=app.out +TARGET=$(PACKAGE_DIR)/$(EXE_NAME) + +all: $(TARGET) +$(TARGET): $(OBJECTS) + $(CC) $(CFLAGS) $(OBJECTS) -L$(LIBS_DIR) $(LIBS) -o $@ + +-include $(DEPENDS) + +$(BUILD_DIR)/%.o: src/%.c Makefile + $(CC) $(CFLAGS) -I$(INCLUDE_DIR) -MD -c $< -o $@ + +.PHONY: clean +clean: + rm -rf $(BUILD_DIR)/* $(TARGET) + +.PHONY: run +run: $(TARGET) + @cd $(PACKAGE_DIR) && echo '==== PROGRAM ====' && ./$(EXE_NAME) + diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..2285802 --- /dev/null +++ b/src/main.c @@ -0,0 +1,64 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "storage.h" + +typedef struct +{ + int x; + int y; +} Transform; + +void +printTransform(Transform *t) +{ + printf("Transform { .x = %i, .y = %i }\n", t->x, t->y); +} + +typedef struct +{ + float width; + float height; + int collisionsCount; +} Collider; + +void +initStorage(storage *storage) +{ + storage->componentsStored = 0; + storage->objectsStored = 0; + storage->objects = (component_pair *) malloc(MAX_COMPONENTS * sizeof(component_pair)); + + REGISTER_COMPONENT(storage, Transform); + REGISTER_COMPONENT(storage, Collider); +} + +void +loadScene(storage *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); + } +} + +int +main() +{ + storage storage; + initStorage(&storage); + loadScene(&storage); + return 0; +} diff --git a/src/storage.c b/src/storage.c new file mode 100644 index 0000000..2fd2601 --- /dev/null +++ b/src/storage.c @@ -0,0 +1,69 @@ +#include "storage.h" + +component_pair * +findPair(storage *this, const char *name) +{ + for (int i = 0; i < this->componentsStored; ++i) + { + component_pair *pair = &this->objects[i]; + if (strcmp(name, pair->name) == 0) + { + return pair; + } + } + return NULL; +} + +Entity +createEntity(storage *this) +{ + Entity idx = this->objectsStored++; + for (int i = 0; i < this->componentsStored; ++i) + { + this->objects[i].objects[idx] = NULL; + } + return idx; +} + +// Not intended for use outside of macros +void +internal_registerComponent(storage *storage, char *name, size_t size) +{ + unsigned int componentIdx = storage->componentsStored++; + storage->objects[componentIdx] = (component_pair) { + .name = name, + .objectSize = size, + .objects = malloc(sizeof(void*) * MAX_OBJECTS) + }; +} + +void * // NULL if component with such name is not found +internal_addComponent(storage *this, Entity idx, const char *name) +{ + component_pair *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); + + return component; +} + +// Not intended for use outside of macros +void * // NULL if component is not found +internal_getComponent(storage *this, Entity idx, const char *name) +{ + for (int i = 0; i < this->componentsStored; ++i) + { + component_pair *pair = &this->objects[i]; + if (strcmp(name, pair->name) == 0) + { + return pair->objects[idx]; + } + } + return NULL; +} diff --git a/src/storage.h b/src/storage.h new file mode 100644 index 0000000..44fe5fb --- /dev/null +++ b/src/storage.h @@ -0,0 +1,53 @@ +#ifndef storage_h_INCLUDED +#define storage_h_INCLUDED + +#include <stdlib.h> +#include <string.h> + +#ifndef MAX_OBJECTS +#define MAX_OBJECTS 4096 +#endif + +#ifndef MAX_COMPONENTS +#define MAX_COMPONENTS 32 +#endif + +#define GET_COMPONENT(storage, componentType, idx) (componentType *) internal_getComponent(storage, idx, #componentType) +#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 struct +{ + char *name; + size_t objectSize; + void **objects; +} component_pair; + +typedef struct +{ + unsigned int componentsStored; + unsigned int objectsStored; + component_pair *objects; +} storage; + +component_pair * // NULL if pair is not found +findPair(storage *this, const char *name); + +Entity +createEntity(storage *this); + +// Not intended for use outside of macros +void +internal_registerComponent(storage *storage, char *name, size_t size); + +void * // NULL if component with such name is not found +internal_addComponent(storage *this, Entity 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); + +#endif // storage_h_INCLUDED + |