From 2c3304134495753bf18015bd9a625ac77e3a0691 Mon Sep 17 00:00:00 2001 From: Steve Date: Sat, 27 Feb 2016 16:14:14 +0000 Subject: [PATCH] Load data from files found in directory, rather than using a text file. --- data/capitalShips/list.json | 8 -------- data/craft/list.json | 7 ------- data/fighters/list.json | 26 -------------------------- data/turrets/list.json | 4 ---- data/widgets/list.json | 12 ------------ src/battle/capitalShips.c | 21 ++++++++++++--------- src/battle/capitalShips.h | 1 + src/battle/fighters.c | 27 +++++++++++++++------------ src/battle/fighters.h | 1 + src/system/widgets.c | 25 ++++++++++++++----------- src/system/widgets.h | 1 + 11 files changed, 44 insertions(+), 89 deletions(-) delete mode 100644 data/capitalShips/list.json delete mode 100644 data/craft/list.json delete mode 100644 data/fighters/list.json delete mode 100644 data/turrets/list.json delete mode 100644 data/widgets/list.json diff --git a/data/capitalShips/list.json b/data/capitalShips/list.json deleted file mode 100644 index 088bb3a..0000000 --- a/data/capitalShips/list.json +++ /dev/null @@ -1,8 +0,0 @@ -[ - "data/capitalShips/csnCorvette01.json", - "data/capitalShips/csnCorvette02.json", - "data/capitalShips/unfCorvette01.json", - "data/capitalShips/unfCorvette02.json", - "data/capitalShips/infCorvette01.json", - "data/capitalShips/infCorvette02.json" -] diff --git a/data/craft/list.json b/data/craft/list.json deleted file mode 100644 index f2f3af9..0000000 --- a/data/craft/list.json +++ /dev/null @@ -1,7 +0,0 @@ -[ - "data/craft/civilian.json", - "data/craft/tug.json", - "data/craft/shuttle.json", - "data/craft/supplyShip.json", - "data/craft/munitionsTransport.json" -] diff --git a/data/fighters/list.json b/data/fighters/list.json deleted file mode 100644 index 8b1d93f..0000000 --- a/data/fighters/list.json +++ /dev/null @@ -1,26 +0,0 @@ -[ - "data/fighters/ataf.json", - "data/fighters/dart.json", - "data/fighters/jackal.json", - "data/fighters/simpleDart.json", - "data/fighters/sphinx.json", - "data/fighters/staticDart.json", - "data/fighters/taf.json", - "data/fighters/unarmedDart.json", - "data/fighters/ray.json", - "data/fighters/nymph.json", - "data/fighters/firefly.json", - "data/fighters/hyenaA.json", - "data/fighters/hyenaB.json", - "data/fighters/leopard.json", - "data/fighters/kingfisher.json", - "data/fighters/mantis.json", - "data/fighters/rook.json", - "data/fighters/thunderhead.json", - "data/fighters/scarab.json", - "data/fighters/hammerhead.json", - "data/fighters/khepri.json", - "data/fighters/cannonDart.json", - "data/fighters/missileDart.json", - "data/fighters/lynx.json" -] diff --git a/data/turrets/list.json b/data/turrets/list.json deleted file mode 100644 index 8012f8d..0000000 --- a/data/turrets/list.json +++ /dev/null @@ -1,4 +0,0 @@ -[ - "data/turrets/rocketTurret.json", - "data/turrets/plasmaTurret.json" -] diff --git a/data/widgets/list.json b/data/widgets/list.json deleted file mode 100644 index 0c3a1c8..0000000 --- a/data/widgets/list.json +++ /dev/null @@ -1,12 +0,0 @@ -[ - "data/widgets/galacticMap.json", - "data/widgets/modal.json", - "data/widgets/startBattle.json", - "data/widgets/inBattle.json", - "data/widgets/battleWon.json", - "data/widgets/battleLost.json", - "data/widgets/title.json", - "data/widgets/options.json", - "data/widgets/stats.json", - "data/widgets/starSystem.json" -] diff --git a/src/battle/capitalShips.c b/src/battle/capitalShips.c index 53a59a2..4e5cb21 100644 --- a/src/battle/capitalShips.c +++ b/src/battle/capitalShips.c @@ -310,22 +310,25 @@ static void die(void) void loadCapitalShipDefs(void) { - cJSON *root, *node; - char *text; - - text = readFile(getFileLocation("data/capitalShips/list.json")); - root = cJSON_Parse(text); + char **filenames; + char path[MAX_FILENAME_LENGTH]; + int count, i; memset(&defHead, 0, sizeof(Entity)); defTail = &defHead; - for (node = root->child ; node != NULL ; node = node->next) + filenames = getFileList("data/capitalShips", &count); + + for (i = 0 ; i < count ; i++) { - loadCapitalShipDef(node->valuestring); + sprintf(path, "data/capitalShips/%s", filenames[i]); + + loadCapitalShipDef(path); + + free(filenames[i]); } - cJSON_Delete(root); - free(text); + free(filenames); } static void loadCapitalShipDef(char *filename) diff --git a/src/battle/capitalShips.h b/src/battle/capitalShips.h index d9a91e7..fe5ba42 100644 --- a/src/battle/capitalShips.h +++ b/src/battle/capitalShips.h @@ -46,6 +46,7 @@ extern Entity **getAllEntsWithin(int x, int y, int w, int h, Entity *ignore); extern void addDebris(int x, int y, int amount); extern void runScriptFunction(char *format, ...); extern void updateObjective(char *name, int type); +extern char **getFileList(char *dir, int *count); extern Battle battle; extern Entity *self; diff --git a/src/battle/fighters.c b/src/battle/fighters.c index 724e36e..f12daab 100644 --- a/src/battle/fighters.c +++ b/src/battle/fighters.c @@ -611,26 +611,29 @@ void loadFighterDefs(void) memset(&defHead, 0, sizeof(Entity)); defTail = &defHead; - loadFighterDefList("data/fighters/list.json"); - loadFighterDefList("data/craft/list.json"); - loadFighterDefList("data/turrets/list.json"); + loadFighterDefList("data/fighters"); + loadFighterDefList("data/craft"); + loadFighterDefList("data/turrets"); } -static void loadFighterDefList(char *filename) +static void loadFighterDefList(char *dir) { - cJSON *root, *node; - char *text; + char **filenames; + char path[MAX_FILENAME_LENGTH]; + int count, i; - text = readFile(getFileLocation(filename)); - root = cJSON_Parse(text); + filenames = getFileList(dir, &count); - for (node = root->child ; node != NULL ; node = node->next) + for (i = 0 ; i < count ; i++) { - loadFighterDef(node->valuestring); + sprintf(path, "%s/%s", dir, filenames[i]); + + loadFighterDef(path); + + free(filenames[i]); } - cJSON_Delete(root); - free(text); + free(filenames); } static void loadFighterDef(char *filename) diff --git a/src/battle/fighters.h b/src/battle/fighters.h index a0bdbeb..dba1439 100644 --- a/src/battle/fighters.h +++ b/src/battle/fighters.h @@ -45,6 +45,7 @@ extern void completeMission(void); extern void runScriptFunction(char *format, ...); extern char *getFileLocation(char *filename); extern void addDebris(int x, int y, int amount); +extern char **getFileList(char *dir, int *count); extern App app; extern Battle battle; diff --git a/src/system/widgets.c b/src/system/widgets.c index 22312b0..aa5131f 100644 --- a/src/system/widgets.c +++ b/src/system/widgets.c @@ -20,7 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "widgets.h" -static void loadWidgets(char *filename); +static void loadWidgets(void); static void loadWidgetSet(char *filename); static void handleMouse(void); static void handleKeyboard(void); @@ -46,7 +46,7 @@ void initWidgets(void) optionsLeft = getTexture("gfx/widgets/optionsLeft.png"); optionsRight = getTexture("gfx/widgets/optionsRight.png"); - loadWidgets("data/widgets/list.json"); + loadWidgets(); drawingWidgets = 0; } @@ -249,21 +249,24 @@ static void handleKeyboard(void) } } -static void loadWidgets(char *filename) +static void loadWidgets() { - cJSON *root, *node; - char *text; + char **filenames; + char path[MAX_FILENAME_LENGTH]; + int count, i; - text = readFile(getFileLocation(filename)); - root = cJSON_Parse(text); + filenames = getFileList("data/widgets", &count); - for (node = root->child ; node != NULL ; node = node->next) + for (i = 0 ; i < count ; i++) { - loadWidgetSet(node->valuestring); + sprintf(path, "data/widgets/%s", filenames[i]); + + loadWidgetSet(path); + + free(filenames[i]); } - cJSON_Delete(root); - free(text); + free(filenames); } static void loadWidgetSet(char *filename) diff --git a/src/system/widgets.h b/src/system/widgets.h index 8e3d3d6..84d6678 100644 --- a/src/system/widgets.h +++ b/src/system/widgets.h @@ -31,6 +31,7 @@ extern SDL_Texture *getTexture(char *filename); extern int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2); extern void playSound(int id); extern char *getFileLocation(char *filename); +extern char **getFileList(char *dir, int *count); extern App app; extern Colors colors;