From 10d320b29de903117d04a87d0d6c77bcf7405329 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 5 Feb 2023 18:35:26 +0100 Subject: [PATCH] =?UTF-8?q?kack=20dict=20ausm=20internet=20um=20gro=C3=9Fe?= =?UTF-8?q?=20switch=20cases=20zu=20vermeiden=20vllt=20keine=20Ahnung?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dict/dict.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Dict/dict.h | 21 +++++++++++++++++ Makefile | 5 +++- main.c | 16 ++++++++++++- 4 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 Dict/dict.c create mode 100644 Dict/dict.h diff --git a/Dict/dict.c b/Dict/dict.c new file mode 100644 index 0000000..c5da224 --- /dev/null +++ b/Dict/dict.c @@ -0,0 +1,66 @@ +#include "dict.h" +#include +#include +#include + +// dict_t **dict = dict_alloc(); +// dict_addItem(dict, "foo", "bar"); +// (char *)dict_getItem(*dict, "foo") +// dict_size(*dict) +// dict_delItem(dict, "foo"); +// dict_dealloc(*dict); + + +Dict ** dict_alloc() { + return malloc(sizeof(Dict)); +} + +void dict_dealloc(Dict *dict) { + Dict *ptr; + for (ptr = dict; ptr != NULL; ptr = ptr->next) { + free(ptr); + } +} + +void *dict_getItem(Dict *dict, char *key) { + Dict *ptr; + for (ptr = dict; ptr != NULL; ptr = ptr->next) { + if (strcmp(ptr->key, key) == 0) { + return ptr->value; + } + } + + return NULL; +} + +void dict_addItem(Dict **dict, char *key, void *value) { + Dict *d = malloc(sizeof(Dict)); + d->key = malloc(strlen(key)+1); + strcpy(d->key, key); + d->value = value; + d->next = *dict; + *dict = d; +} + +int dict_size(Dict *dict) { + int size = 0; + + Dict *ptr; + for (ptr = dict; ptr != NULL; ptr = ptr->next) { + size++; + } + + return size; +} + +char *sprintf_id(char *str, int id){ + if(strlen(str) > 50){ + printf("WARNING: char '%s' ist relativ lang (%ld Zeichen)! Evtl sprintf_id malloc anpassen!\n", str, strlen(str)); + } + if(strlen(str) >= 63){ + printf("Ok der war bisschen sehr lang, SIGSEV incoming :O :O :O, es ist alles kaputt, mach tmp lieber mal länger\n"); + } + char *tmp = malloc(sizeof(char) * 64); + sprintf(tmp, "%s%d", str, id); + return tmp; +} diff --git a/Dict/dict.h b/Dict/dict.h new file mode 100644 index 0000000..2c255b1 --- /dev/null +++ b/Dict/dict.h @@ -0,0 +1,21 @@ +#ifndef DICT_H_ +#define DICT_H_ + +typedef struct Dict { + char *key; + void *value; + struct Dict *next; +} Dict; + +char *sprintf_id(char *str, int id); +Dict **dict_alloc(); +void dict_dealloc(Dict *dict); +void *dict_getItem(Dict *dict, char *key); +void dict_delItem(Dict **dict, char *key); +void dict_addItem(Dict **dict, char *key, void *value); +int dict_size(Dict *dict); + + + + +#endif \ No newline at end of file diff --git a/Makefile b/Makefile index e9479bf..fcf8aeb 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC = gcc FLAGS = -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 -g -OBJS = main.o sprite.o inputHandler.o isometricMap.o game.o textureatlas.o animation.o animationHandler.o button.o uiContainer.o debug.o building.o entity.o selectable.o task.o entityacts.o onClickFunctions.o staticobjects.o screen.o +OBJS = main.o sprite.o inputHandler.o isometricMap.o game.o textureatlas.o animation.o animationHandler.o button.o uiContainer.o debug.o building.o entity.o selectable.o task.o entityacts.o onClickFunctions.o staticobjects.o screen.o dict.o spiel: $(OBJS) $(CC) -o spiel $(OBJS) $(FLAGS) @@ -62,5 +62,8 @@ staticobjects.o: MapObject/staticobjects.c screen.o: Screen/screen.c $(CC) -c Screen/screen.c $(FLAGS) +dict.o: Dict/dict.c + $(CC) -c Dict/dict.c $(FLAGS) + clean: rm *.o spiel diff --git a/main.c b/main.c index f07ad54..4a8ce40 100644 --- a/main.c +++ b/main.c @@ -13,15 +13,28 @@ #include "Ui/debug.h" #include "definitions.h" #include "Screen/screen.h" +#include "Dict/dict.h" +#include "Ui/onClickFunctions.h" int main(){ + InitWindow(1280, 720, "basic window"); Game *game = GameInit(); // Hides the operating systems own cursor HideCursor(); + + //Dict **functions = dict_alloc(); + //dict_addItem(functions, sprintf_id("OnSelected", SELECTABLE_ID_SPAWN_BUILDING), (void *)&OnSelectedSpawnBuilding); + //dict_addItem(functions, sprintf_id("OnSelected", SELECTABLE_ID_SPAWN_LUMBERJACK), (void *) &OnSelectedSpawnLumberjack); + //dict_addItem(functions, sprintf_id( "OnSelected", SELECTABLE_ID_SPAWN_WORKER), (void *) &OnSelectedSpawnWorker); + //dict_addItem(functions, sprintf_id("OnClick", BUTTON_ID_CONTINUE), (void *)&OnClickContinueButton); + //dict_addItem(functions, sprintf_id("OnClick", BUTTON_ID_EXIT), (void *)&OnClickExitButton); + //dict_addItem(functions, sprintf_id("OnClick", BUTTON_ID_START_GAME), (void *)&OnClickStartButton); + //printf("%p\n", dict_getItem(*functions, sprintf_id("OnClick", BUTTON_ID_START_GAME))); + //dict_dealloc(*functions); //SetTargetFPS(60); @@ -43,8 +56,9 @@ int main(){ EndDrawing(); if(game->currentScreen == SCREEN_EXIT){ break; + } + } - } CloseWindow(); return 0;