From 1f2c2082a03079286d8802af812d02324efdb2dc Mon Sep 17 00:00:00 2001 From: Doyle Thai Date: Tue, 23 Aug 2016 18:20:20 +1000 Subject: [PATCH] Add framework to parse xml tree into game data --- src/Common.c | 6 ++ src/WorldTraveller.c | 130 ++++++++++++++++++++++++++++++++--- src/include/Dengine/Common.h | 2 +- 3 files changed, 127 insertions(+), 11 deletions(-) diff --git a/src/Common.c b/src/Common.c index fb66c51..826c07c 100644 --- a/src/Common.c +++ b/src/Common.c @@ -73,3 +73,9 @@ void common_itoa(i32 value, char *buf, i32 bufSize) // NOTE(doyle): The actual string length may differ from the bufSize reverseString(buf, common_strlen(buf)); } + +i32 common_atoi(char *string, i32 len) +{ + // TODO(doyle): Implement ATOI + return 0; +} diff --git a/src/WorldTraveller.c b/src/WorldTraveller.c index 70a93f5..dbb4a48 100644 --- a/src/WorldTraveller.c +++ b/src/WorldTraveller.c @@ -130,7 +130,8 @@ typedef struct XmlToken i32 len; } XmlToken; -INTERNAL void debug_recursivePrintXmlTree(XmlNode *root) +#define DEBUG_RECURSIVE_PRINT_XML_TREE(sig) debug_recursivePrintXmlTree(sig, 1) +INTERNAL void debug_recursivePrintXmlTree(XmlNode *root, i32 levelsDeep) { if (!root) { @@ -138,6 +139,11 @@ INTERNAL void debug_recursivePrintXmlTree(XmlNode *root) } else { + for (i32 i = 0; i < levelsDeep; i++) + { + printf("-"); + } + printf("%s ", root->name); XmlAttribute *attribute = &root->attribute; @@ -150,8 +156,8 @@ INTERNAL void debug_recursivePrintXmlTree(XmlNode *root) } printf("\n"); - debug_recursivePrintXmlTree(root->child); - debug_recursivePrintXmlTree(root->next); + debug_recursivePrintXmlTree(root->child, levelsDeep+1); + debug_recursivePrintXmlTree(root->next, levelsDeep); } } @@ -256,7 +262,8 @@ INTERNAL void assetInit(GameState *state) { char c = (CAST(char *) xmlFileRead.buffer)[j]; - if (c == ' ' || c == '=') + if (c == ' ' || c == '=' || c == '>' || c == '<' || + c == '\\') { break; } @@ -290,9 +297,36 @@ INTERNAL void assetInit(GameState *state) case xmltokentype_openArrow: { - /* Open arrows are followed by the node name */ - token = &xmlTokens[++i]; - node->name = token->string; + /* Open arrows indicate closing parent node or new node name */ + XmlToken *nextToken = &xmlTokens[++i]; + if (nextToken->type == xmltokentype_backslash) + { + nextToken = &xmlTokens[++i]; + if (common_strcmp(nextToken->string, node->parent->name) == 0) + { + node->parent->isClosed = TRUE; + node = node->parent; + } + else + { +#ifdef DENGINE_DEBUG + DEBUG_LOG( + "Closing xml node name does not match parent name"); +#endif + } + } + else if (nextToken->type == xmltokentype_name) + { + node->name = nextToken->string; + } + else + { +#ifdef DENGINE_DEBUG + DEBUG_LOG("Unexpected token type after open arrow"); +#endif + } + + token = nextToken; break; } @@ -332,7 +366,6 @@ INTERNAL void assetInit(GameState *state) { node->isClosed = TRUE; node = node->parent; - } if (!node->isClosed) @@ -367,10 +400,87 @@ INTERNAL void assetInit(GameState *state) } } - } - debug_recursivePrintXmlTree(&root); + DEBUG_RECURSIVE_PRINT_XML_TREE(&root); + + node = &root; + while (node) + { + if(common_strcmp(node->name, "TextureAtlas") == 0) + { + XmlNode *atlasXmlNode = node; + XmlNode *atlasChildNode = atlasXmlNode->child; + while (atlasChildNode) + { + if (common_strcmp(atlasChildNode->name, "SubTexture") == 0) + { + XmlAttribute *subTextureAttrib = &atlasChildNode->attribute; + while (subTextureAttrib) + { + // TODO(doyle): Fill in details properly + Rect rect = {0}; + if (common_strcmp(subTextureAttrib->name, "name") == 0) + { + } + else if (common_strcmp(subTextureAttrib->name, "x") == + 0) + { + char *name = subTextureAttrib->name; + i32 nameLen = common_strlen(name); + rect.pos.x = CAST(f32) common_atoi(name, nameLen); + } + else if (common_strcmp(subTextureAttrib->name, "y") == + 0) + { + char *name = subTextureAttrib->name; + i32 nameLen = common_strlen(name); + rect.pos.y = CAST(f32) common_atoi(name, nameLen); + } + else if (common_strcmp(subTextureAttrib->name, + "width") == 0) + { + char *name = subTextureAttrib->name; + i32 nameLen = common_strlen(name); + rect.size.w = CAST(f32) common_atoi(name, nameLen); + } + else if (common_strcmp(subTextureAttrib->name, + "height") == 0) + { + char *name = subTextureAttrib->name; + i32 nameLen = common_strlen(name); + rect.size.h = CAST(f32) common_atoi(name, nameLen); + } + else + { +#ifdef DENGINE_DEBUG + DEBUG_LOG( + "Unsupported xml attribute in SubTexture"); +#endif + } + + subTextureAttrib = subTextureAttrib->next; + } + } + else + { +#ifdef DENGINE_DEBUG + DEBUG_LOG("Unsupported xml node name not parsed"); +#endif + } + + atlasChildNode = atlasChildNode->next; + } + } + else + { +#ifdef DENGINE_DEBUG + DEBUG_LOG("Unsupported xml node name not parsed"); +#endif + } + + node = node->next; + } /* Load textures */ asset_loadTextureImage(assetManager, diff --git a/src/include/Dengine/Common.h b/src/include/Dengine/Common.h index f26b75e..ec95796 100644 --- a/src/include/Dengine/Common.h +++ b/src/include/Dengine/Common.h @@ -35,6 +35,6 @@ char *common_memset(char *const ptr, const i32 value, const i32 numBytes); // Max buffer size should be 11 for 32 bit integers void common_itoa(i32 value, char *buf, i32 bufSize); -i32 common_atoi(char *buf, i32 bufSize); +i32 common_atoi(char *string, i32 len); #endif