sw-battle-test-c

Interesting test task in C instead of C++
git clone git://bsandro.tech/sw-battle-test-c
Log | Files | Refs | README

commit c60256ea518d42ca0a2247b3c229becf58efaa1a
parent abd873fd4c8212343659f4a5879d87e818c7f653
Author: bsandro <email@bsandro.tech>
Date:   Fri, 21 Aug 2026 02:07:21 +0300

commands registry

Diffstat:
MMakefile | 7+++++--
Acommands/CREATE_MAP.c | 5+++++
Acommands/MARCH.c | 5+++++
Acommands/SPAWN_HUNTER.c | 5+++++
Acommands/SPAWN_SWORDSMAN.c | 5+++++
Acommands/gen_registry.sh | 25+++++++++++++++++++++++++
Acommands/registry.h | 14++++++++++++++
Acore/command.h | 10++++++++++
Mmain.c | 15+++++++++++++--
9 files changed, 87 insertions(+), 4 deletions(-)

diff --git a/Makefile b/Makefile @@ -15,7 +15,10 @@ LDFLAGS=-lc -lm -flto $(SDL_LDFLAGS) all: $(NAME) -.PHONY: clean +.PHONY: clean gen_commands + +gen_commands: + ./commands/gen_registry.sh > ./commands/registry.h clean: rm -f $(OBJ) $(NAME) @@ -23,7 +26,7 @@ clean: %.o : %.c $(DEPS) $(CC) $(CFLAGS) -c $< -o $@ -$(NAME): $(OBJ) +$(NAME): gen_commands $(OBJ) $(CC) $(OBJ) -o $@ $(LDFLAGS) run: $(NAME) diff --git a/commands/CREATE_MAP.c b/commands/CREATE_MAP.c @@ -0,0 +1,5 @@ +#include "core/command.h" + +void cmd_create_map() { + puts("create map"); +} diff --git a/commands/MARCH.c b/commands/MARCH.c @@ -0,0 +1,5 @@ +#include "core/command.h" + +void cmd_march() { + puts("march"); +} diff --git a/commands/SPAWN_HUNTER.c b/commands/SPAWN_HUNTER.c @@ -0,0 +1,5 @@ +#include "core/command.h" + +void cmd_spawn_hunter() { + puts("spawn hunter"); +} diff --git a/commands/SPAWN_SWORDSMAN.c b/commands/SPAWN_SWORDSMAN.c @@ -0,0 +1,5 @@ +#include "core/command.h" + +void cmd_spawn_swordsman() { + puts("spawn swordsman"); +} diff --git a/commands/gen_registry.sh b/commands/gen_registry.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash + +cd $(dirname "$0") + +echo '#pragma once' +echo '/* DO NOT MODIFY! REGENERATED WITH gen_registry.sh */' +echo '#include "core/command.h"' + +for f in *.c; do + str="$(basename -s.c $f)"; + fun="cmd_${str,,}"; + echo "Command_Func $fun;"; +done + +echo 'static const Command COMMANDS[] = {' + +for f in *.c; do + str="$(basename -s.c $f)"; + fun="cmd_${str,,}"; + echo ' { "'$str'", '$fun' },'; +done + +echo '};' + +echo 'static const int COMMANDS_COUNT = (sizeof(COMMANDS) / sizeof(Command));' diff --git a/commands/registry.h b/commands/registry.h @@ -0,0 +1,14 @@ +#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/core/command.h b/core/command.h @@ -0,0 +1,10 @@ +#pragma once + +#include <stdio.h> + +typedef void Command_Func(void); + +typedef struct Command { + const char *name; + Command_Func *func; +} Command; diff --git a/main.c b/main.c @@ -1,9 +1,20 @@ #include <stdio.h> +#include <string.h> #include "core/world.h" +#include "commands/registry.h" -int main(void) { +int main(int argc, char **argv) { World w; Map_init(&w.map, 3, 4); - puts("ok"); + puts("world init ok"); + + if (argc>1) { + for (int i=0;i<COMMANDS_COUNT;++i) { + if (strcmp(COMMANDS[i].name, argv[1])==0) { + COMMANDS[i].func(); + break; + } + } + } return 0; }