diff --git a/src/economy.c b/src/economy.c index 9c5f2b7..b7066d1 100644 --- a/src/economy.c +++ b/src/economy.c @@ -15,7 +15,7 @@ #include "cs.h" #include "lephisto.h" -#include "xml.h" +#include "lxml.h" #include "pack.h" #include "log.h" #include "spfx.h" diff --git a/src/faction.c b/src/faction.c index fc2bb2c..cb13868 100644 --- a/src/faction.c +++ b/src/faction.c @@ -11,7 +11,7 @@ #include "lephisto.h" #include "log.h" #include "pack.h" -#include "xml.h" +#include "lxml.h" #include "rng.h" #include "faction.h" diff --git a/src/hook.c b/src/hook.c index 9a2d17a..467aaa6 100644 --- a/src/hook.c +++ b/src/hook.c @@ -3,7 +3,7 @@ #include "log.h" #include "lephisto.h" -#include "xml.h" +#include "lxml.h" #include "hook.h" #define HOOK_CHUNK 32 /* Size to grow by when out of space. */ diff --git a/src/lephisto.c b/src/lephisto.c index 4534741..9a1601d 100644 --- a/src/lephisto.c +++ b/src/lephisto.c @@ -39,7 +39,7 @@ #include "pack.h" #include "weapon.h" #include "faction.h" -#include "xml.h" +#include "lxml.h" #include "pause.h" #include "toolkit.h" #include "pilot.h" diff --git a/src/llua_misn.c b/src/llua_misn.c index 6d6db59..6eeecba 100644 --- a/src/llua_misn.c +++ b/src/llua_misn.c @@ -22,7 +22,7 @@ #include "pilot.h" #include "player.h" #include "ltime.h" -#include "xml.h" +#include "lxml.h" #include "music.h" #include "unidiff.h" #include "llua_misn.h" diff --git a/src/lxml.c b/src/lxml.c new file mode 100644 index 0000000..677b3ab --- /dev/null +++ b/src/lxml.c @@ -0,0 +1,61 @@ +/** + * @file lxml.c + * + * Handles some complex xml parsing. + */ + +#include "lephisto.h" +#include "lxml.h" + +/** + * @brief Parses a texture handling the sx and sy elements. + * @param node Node to parse. + * @param path Path to get file from, should be in the format of + * "PREFIX%sSUFFIX". + * @param defsx Default X sprites. + * @param defsy Default Y sprites. + * @return The texture from the node or NULL if an error occurred. + */ +glTexture* xml_parseTexture(xmlNodePtr node, + const char* path, int defsx, int defsy) { + + int sx, sy; + char* buf, filename[PATH_MAX]; + glTexture* tex; + + /* Sane defaults. */ + sx = defsx; + sy = defsy; + + /* Read x sprites. */ + xmlr_attr(node, "sx", buf); + if(buf != NULL) { + sx = atoi(buf); + free(buf); + } + + /* Read y sprites. */ + xmlr_attr(node, "sy", buf); + if(buf != NULL) { + sy = atoi(buf); + free(buf); + } + + /* Get graphic to load. */ + buf = xml_get(node); + if(buf == NULL) + return NULL; + + /* Convert name. */ + snprintf(filename, PATH_MAX, (path != NULL) ? path : "%s", buf); + + /* Load the graphic. */ + if((sx == 1) && (sy == 1)) + tex = gl_newImage(filename); + else + tex = gl_newSprite(filename, sx, sy); + + /* Return result. */ + return tex; +} + diff --git a/src/xml.h b/src/lxml.h similarity index 94% rename from src/xml.h rename to src/lxml.h index 5c5452d..a1b7553 100644 --- a/src/xml.h +++ b/src/lxml.h @@ -4,6 +4,7 @@ #include "libxml/xmlwriter.h" #include "log.h" +#include "opengl.h" #define XML_NODE_START 1 #define XML_NODE_TEXT 3 @@ -79,3 +80,9 @@ if(xmlTextWriterEndDocument(w) < 0) { \ ERR("xmlw: Unable to end document"); return -1; } +/* + * Functions for generic complex reading. + */ +glTexture* xml_parseTexture(xmlNodePtr node, + const char* path, int defsx, int defsy); + diff --git a/src/mission.c b/src/mission.c index 46ff0c0..f471085 100644 --- a/src/mission.c +++ b/src/mission.c @@ -11,7 +11,7 @@ #include "log.h" #include "hook.h" #include "pack.h" -#include "xml.h" +#include "lxml.h" #include "faction.h" #include "player.h" #include "base64.h" diff --git a/src/outfit.c b/src/outfit.c index e8e03dd..7ba5884 100644 --- a/src/outfit.c +++ b/src/outfit.c @@ -6,7 +6,7 @@ #include "lephisto.h" #include "log.h" #include "pack.h" -#include "xml.h" +#include "lxml.h" #include "spfx.h" #include "outfit.h" diff --git a/src/pilot.c b/src/pilot.c index 6248331..b93ac5f 100644 --- a/src/pilot.c +++ b/src/pilot.c @@ -12,7 +12,7 @@ #include "log.h" #include "weapon.h" #include "pack.h" -#include "xml.h" +#include "lxml.h" #include "spfx.h" #include "rng.h" #include "hook.h" diff --git a/src/player.c b/src/player.c index d196c73..6944c0a 100644 --- a/src/player.c +++ b/src/player.c @@ -12,7 +12,7 @@ #include "opengl.h" #include "font.h" #include "pack.h" -#include "xml.h" +#include "lxml.h" #include "space.h" #include "rng.h" #include "land.h" diff --git a/src/save.c b/src/save.c index b78fce3..430df33 100644 --- a/src/save.c +++ b/src/save.c @@ -1,7 +1,7 @@ #include /* remove() */ #include "lephisto.h" #include "log.h" -#include "xml.h" +#include "lxml.h" #include "player.h" #include "toolkit.h" #include "dialogue.h" diff --git a/src/ship.c b/src/ship.c index 9b2029d..e349acc 100644 --- a/src/ship.c +++ b/src/ship.c @@ -4,7 +4,7 @@ #include "lephisto.h" #include "log.h" #include "pack.h" -#include "xml.h" +#include "lxml.h" #include "toolkit.h" #include "ship.h" @@ -246,28 +246,22 @@ static int ship_parse(Ship* tmp, xmlNodePtr parent) { if(xml_isNode(node,"GFX")) { /* Load the base graphic. */ - snprintf(str, strlen(xml_raw(node)) + - sizeof(SHIP_GFX) + sizeof(SHIP_EXT), - SHIP_GFX"%s"SHIP_EXT, xml_get(node)); - tmp->gfx_space = gl_newSprite(str, 6, 6); + tmp->gfx_space = xml_parseTexture(node, + SHIP_GFX"%s"SHIP_EXT, 6, 6); /* Load the comm graphic. */ - snprintf(str, strlen(xml_raw(node))+ - sizeof(SHIP_GFX)+sizeof(SHIP_COMM)+sizeof(SHIP_EXT), - SHIP_GFX"%s"SHIP_COMM SHIP_EXT, xml_get(node)); - tmp->gfx_comm = gl_newImage(str); + tmp->gfx_comm = xml_parseTexture(node, + SHIP_GFX"%s"SHIP_COMM SHIP_EXT, 1, 1); /* Load the target graphic. */ xmlr_attr(node, "target", stmp); if(stmp != NULL) { - snprintf(str, strlen(stmp) + - sizeof(SHIP_GFX)+sizeof(SHIP_TARGET)+sizeof(SHIP_EXT), + snprintf(str, PATH_MAX, SHIP_GFX"%s"SHIP_TARGET SHIP_EXT, stmp); tmp->gfx_target = gl_newImage(str); free(stmp); } else { /* Load standard target graphic. */ - snprintf(str, strlen(xml_raw(node)) + - sizeof(SHIP_GFX)+sizeof(SHIP_TARGET)+sizeof(SHIP_EXT), + snprintf(str, PATH_MAX, SHIP_GFX"%s"SHIP_TARGET SHIP_EXT, xml_get(node)); tmp->gfx_target = gl_newImage(str); } diff --git a/src/space.c b/src/space.c index 6085ffb..8e5ed32 100644 --- a/src/space.c +++ b/src/space.c @@ -1,7 +1,7 @@ #include #include -#include "xml.h" +#include "lxml.h" #include "lephisto.h" #include "opengl.h" #include "log.h" @@ -686,8 +686,8 @@ static int planet_parse(Planet* planet, const xmlNodePtr parent) { cur = node->children; do { if(xml_isNode(cur, "space")) { /* Load space gfx. */ - snprintf(str, PATH_MAX, PLANET_GFX_SPACE"%s", xml_get(cur)); - planet->gfx_space = gl_newImage(str); + planet->gfx_space = xml_parseTexture(cur, + PLANET_GFX_SPACE"%s", 1, 1); } else if(xml_isNode(cur, "exterior")) { /* Load land gfx. */ len = strlen(xml_raw(cur)) + sizeof(PLANET_GFX_EXTERIOR); diff --git a/src/unidiff.c b/src/unidiff.c index 66597d5..2a20eef 100644 --- a/src/unidiff.c +++ b/src/unidiff.c @@ -13,7 +13,7 @@ #include "lephisto.h" #include "log.h" -#include "xml.h" +#include "lxml.h" #include "space.h" #include "pack.h" #include "unidiff.h"