From c87b9a6986644c43e2ef85740b701d780c4daed9 Mon Sep 17 00:00:00 2001 From: Steve Date: Wed, 14 Feb 2018 08:32:09 +0000 Subject: [PATCH] Start of widget loading. --- src/defs.h | 4 +-- src/system/init.c | 1 + src/system/init.h | 2 +- src/system/lookup.c | 4 +++ src/system/sprites.h | 2 +- src/system/widgets.c | 66 +++++++++++++++++++++++++++++++++++++++++++- src/system/widgets.h | 4 +++ src/test/atlasTest.c | 2 +- 8 files changed, 79 insertions(+), 6 deletions(-) diff --git a/src/defs.h b/src/defs.h index 6b2e611..e198b58 100644 --- a/src/defs.h +++ b/src/defs.h @@ -335,6 +335,8 @@ enum STAT_KEYS_FOUND, STAT_CELLS_FOUND, STAT_HEARTS_FOUND, + STAT_TARGETS_DEFEATED, + STAT_MIAS_RESCUED, STAT_DEATHS, STAT_SHOTS_FIRED, STAT_SHOTS_HIT, @@ -345,8 +347,6 @@ enum STAT_BATTERIES_PICKED_UP, STAT_WEAPONS_PICKED_UP, STAT_ENEMIES_KILLED, - STAT_TARGETS_DEFEATED, - STAT_MIAS_RESCUED, STAT_MISSIONS_PLAYED, STAT_TIME_PLAYED, STAT_MAX diff --git a/src/system/init.c b/src/system/init.c index 862f880..9365162 100644 --- a/src/system/init.c +++ b/src/system/init.c @@ -134,6 +134,7 @@ void initGameSystem(void) initGraphics, initFonts, initAtlas, + initWidgets, initSounds, initSprites, initEntityFactory diff --git a/src/system/init.h b/src/system/init.h index 1bbf7bb..c1daa19 100644 --- a/src/system/init.h +++ b/src/system/init.h @@ -29,12 +29,12 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. extern void createSaveFolder(void); extern void setLanguage(char *applicationName, char *languageCode); -extern void initLookups(void); extern void initGraphics(void); extern void initFonts(void); extern void initAtlas(void); extern void initSounds(void); extern void initSprites(void); +extern void initWidgets(void); extern void initEntityFactory(void); extern void destroyLookups(void); extern void destroyFonts(void); diff --git a/src/system/lookup.c b/src/system/lookup.c index 3701846..a6ac5e1 100644 --- a/src/system/lookup.c +++ b/src/system/lookup.c @@ -48,6 +48,10 @@ void initLookups(void) addLookup("CONTROL_JETPACK", CONTROL_JETPACK); addLookup("CONTROL_PAUSE", CONTROL_PAUSE); addLookup("CONTROL_MAP", CONTROL_MAP); + + addLookup("WT_BUTTON", WT_BUTTON); + addLookup("WT_SPINNER", WT_SPINNER); + addLookup("WT_PLAIN_BUTTON", WT_PLAIN_BUTTON); } static void addLookup(const char *name, long value) diff --git a/src/system/sprites.h b/src/system/sprites.h index fca0ddb..d56b7a1 100644 --- a/src/system/sprites.h +++ b/src/system/sprites.h @@ -23,5 +23,5 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. extern float wrap(float value, float low, float high); extern char *readFile(const char *filename); -char **getFileList(const char *dir, int *count); +extern char **getFileList(const char *dir, int *count); extern Atlas *getImageFromAtlas(char *filename); diff --git a/src/system/widgets.c b/src/system/widgets.c index 0c3e3e9..4bddd5e 100644 --- a/src/system/widgets.c +++ b/src/system/widgets.c @@ -21,6 +21,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "widgets.h" Widget *getWidgetAt(int x, int y); +static void loadWidgetGroup(char *filename); +static void loadWidgets(void); static Widget widgetHead; static Widget *widgetTail; @@ -30,6 +32,8 @@ void initWidgets(void) { memset(&widgetHead, 0, sizeof(Widget)); widgetTail = &widgetHead; + + loadWidgets(); } Widget *getWidget(char *name, char *group) @@ -139,9 +143,69 @@ int wasWidgetClicked(Widget *w) return wasClicked; } -void loadWidgets(char *filename) +static void loadWidgets(void) { + char **filenames; + char path[MAX_FILENAME_LENGTH]; + int count, i; + + filenames = getFileList("data/widgets", &count); + + for (i = 0 ; i < count ; i++) + { + sprintf(path, "data/widgets/%s", filenames[i]); + + loadWidgetGroup(path); + + free(filenames[i]); + } + + free(filenames); +} + +static void loadWidgetGroup(char *filename) +{ + cJSON *root, *node; + char *text; + Widget *w; + + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", filename); + + text = readFile(filename); + root = cJSON_Parse(text); + for (node = root->child ; node != NULL ; node = node->next) + { + w = malloc(sizeof(Widget)); + memset(w, 0, sizeof(Widget)); + widgetTail->next = w; + widgetTail = w; + + STRNCPY(w->name, cJSON_GetObjectItem(node, "name")->valuestring, MAX_NAME_LENGTH); + STRNCPY(w->group, cJSON_GetObjectItem(node, "group")->valuestring, MAX_NAME_LENGTH); + STRNCPY(w->label, cJSON_GetObjectItem(node, "label")->valuestring, MAX_NAME_LENGTH); + w->x = cJSON_GetObjectItem(node, "x")->valueint; + w->y = cJSON_GetObjectItem(node, "y")->valueint; + w->w = cJSON_GetObjectItem(node, "w")->valueint; + w->h = cJSON_GetObjectItem(node, "h")->valueint; + w->type = lookup(cJSON_GetObjectItem(node, "type")->valuestring); + + switch (w->type) + { + case WT_BUTTON: + break; + + case WT_SPINNER: + break; + + case WT_PLAIN_BUTTON: + break; + } + } + + cJSON_Delete(root); + + free(text); } void destroy(void) diff --git a/src/system/widgets.h b/src/system/widgets.h index ad8a06b..f9cdc32 100644 --- a/src/system/widgets.h +++ b/src/system/widgets.h @@ -19,5 +19,9 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "../common.h" +#include "../json/cJSON.h" extern int collision(int x1, int y1, int w1, int h1, int x2, int y2, int w2, int h2); +extern char *readFile(const char *filename); +extern char **getFileList(const char *dir, int *count); +extern long lookup(const char *name); diff --git a/src/test/atlasTest.c b/src/test/atlasTest.c index cad3697..68a2169 100644 --- a/src/test/atlasTest.c +++ b/src/test/atlasTest.c @@ -33,7 +33,7 @@ void initAtlasTest(void) initHub(); - loadWorld("greenlands4"); + loadWorld("greenlands5"); initWorld();