commit 2da61694a72f77885c8d1518362e674694d186f1
parent ec938771d719ce1249c4bd76ae740f0b13146c70
Author: bsandro <email@bsandro.tech>
Date: Wed, 2 Sep 2026 01:16:26 +0300
populating components directory and generating neccessary files for their usage
Diffstat:
11 files changed, 44 insertions(+), 31 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1,2 +1,3 @@
*.o
sw-battle-test-c
+_*
diff --git a/Makefile b/Makefile
@@ -4,7 +4,7 @@ SDL_LDFLAGS=$(shell pkg-config --libs sdl2 SDL2_image)
SRC:=$(shell find . -type f -iname '*.c')
DEPS:=$(shell find . -type f -iname '*.h')
OBJ:=$(SRC:.c=.o)
-CFLAGS=-std=c99 -Wpedantic -Wall -Wextra -Wno-unused -ffast-math -I. $(SDL_CFLAGS)
+CFLAGS=-std=c11 -Wpedantic -Wall -Wextra -Wno-unused -ffast-math -I. $(SDL_CFLAGS)
ifeq (${DEBUG},1)
CFLAGS+=-O0 -g
else
@@ -17,9 +17,12 @@ all: $(NAME)
.PHONY: clean gen_commands
-gen_commands:
+gen_commands: gen_components
./commands/gen_registry.sh > ./commands/_registry.h
+gen_components:
+ ./components/populate.sh
+
clean:
rm -f $(OBJ) $(NAME)
diff --git a/commands/registry.h b/commands/registry.h
@@ -1,14 +0,0 @@
-#pragma once
-/* DO NOT MODIFY! REGENERATED WITH gen_registry.sh */
-#include "core/command.h"
-Command_Func cmd_create_map;
-Command_Func cmd_march;
-Command_Func cmd_spawn_hunter;
-Command_Func cmd_spawn_swordsman;
-static const Command COMMANDS[] = {
- { "CREATE_MAP", cmd_create_map },
- { "MARCH", cmd_march },
- { "SPAWN_HUNTER", cmd_spawn_hunter },
- { "SPAWN_SWORDSMAN", cmd_spawn_swordsman },
-};
-static const int COMMANDS_COUNT = (sizeof(COMMANDS) / sizeof(Command));
diff --git a/components/health.h b/components/health.h
@@ -1,8 +1,5 @@
#pragma once
-#include "ecs/component.h"
-
typedef struct Health {
- Component c;
int hp;
} Health;
diff --git a/components/melee.h b/components/melee.h
@@ -1,8 +1,5 @@
#pragma once
-#include "ecs/component.h"
-
typedef struct Melee {
- Component c;
int strength;
} Melee;
diff --git a/components/populate.sh b/components/populate.sh
@@ -0,0 +1,20 @@
+#!/usr/bin/env bash
+
+cd $(dirname "$0")
+
+rm -f _registry.inc
+rm -f _types.inc
+rm -f _include.h
+
+echo '#pragma once' > _include.h
+
+for f in *.h; do
+ if [[ "$f" == _* ]]; then continue; fi
+ echo "Components: populating $f..."
+ struct=`sed -n 's/^typedef struct \([A-Za-z_]*\?\).*/\1/p' $f`
+ str="$(basename -s.h $f)";
+ comp="COMPONENT_${str^^}";
+ echo "$struct $str;" >> _registry.inc
+ echo "$comp," >> _types.inc
+ echo "#include \"components/$f\"" >> _include.h
+done
diff --git a/components/unit.h b/components/unit.h
@@ -1,9 +1,6 @@
#pragma once
-#include "ecs/component.h"
-
typedef struct Unit {
- Component c;
int x;
int y;
} Unit;
diff --git a/core/ecs/component.h b/core/ecs/component.h
@@ -1,7 +1,14 @@
#pragma once
+#include "components/_include.h"
+
+typedef enum ComponentType {
+#include "components/_types.inc"
+} ComponentType;
+
typedef struct Component {
- int id;
- struct Component *prev;
- struct Component *next;
+ ComponentType type;
+ union {
+ #include "components/_registry.inc"
+ };
} Component;
diff --git a/core/ecs/entity.c b/core/ecs/entity.c
@@ -1,5 +1,9 @@
+#define STB_DS_IMPLEMENTATION
+
#include <string.h>
#include <stdlib.h>
+#include <stdio.h>
+#include "stb_ds.h"
#include "core/ecs/entity.h"
Entity * Entity_New(int id) {
@@ -10,7 +14,8 @@ Entity * Entity_New(int id) {
}
bool Entity_Add_Component(Entity *e, Component *c) {
- if (e->components!=NULL) return false;
+ printf("%d add component %d\n", e->id, c->type);
+ arrput(e->components, *c);
return true;
}
diff --git a/core/ecs/entity.h b/core/ecs/entity.h
@@ -1,14 +1,13 @@
#pragma once
+#include <stdbool.h>
#include "core/ecs/component.h"
#include "core/ecs/system.h"
typedef struct Entity {
int id;
Component *components;
- System *systems;
} Entity;
Entity * Entity_New(int id);
bool Entity_Add_Component(Entity *e, Component *c);
-bool Entity_Add_System(Entity *e, System *s);
diff --git a/core/ecs/system.h b/core/ecs/system.h
@@ -7,4 +7,5 @@ typedef bool (System_Run)(World *w);
typedef struct System {
System_Run *run;
+ struct System *next;
} System;