diff --git a/src/faction.c b/src/faction.c index 79ef5cc..771ba44 100644 --- a/src/faction.c +++ b/src/faction.c @@ -7,6 +7,7 @@ #include #include +#include "opengl.h" #include "lephisto.h" #include "log.h" #include "pack.h" @@ -14,10 +15,11 @@ #include "rng.h" #include "faction.h" -#define XML_FACTION_ID "Factions" /* XML section id. */ -#define XML_FACTION_TAG "faction" +#define XML_FACTION_ID "Factions" /* XML section id. */ +#define XML_FACTION_TAG "faction" -#define FACTION_DATA "../dat/faction.xml" /**< Faction xml file. */ +#define FACTION_DATA "../dat/faction.xml" /**< Faction xml file. */ +#define FACTION_LOGO_PATH "../gfx/logo/" /**< Path to logo gfx. */ #define PLAYER_ALLY 70 /**< Above this, player is considered ally. */ @@ -27,8 +29,11 @@ * @brief Represents a faction. */ typedef struct Faction_ { - char* name; /**< Normal name. */ - char* longname; /**< Long name. */ + char* name; /**< Normal name. */ + char* longname; /**< Long name. */ + + /* Graphics. */ + glTexture* logo_small; /**< Small logo. */ /* Enemies. */ int* enemies; /**< Enemies by ID of the faction. */ @@ -96,11 +101,30 @@ char* faction_name(int f) { * @return The factions long name. */ char* faction_longname(int f) { + if((f < 0) || (f >= faction_nstack)) { + WARN("Faction id '%d' is invalid.", f); + return NULL; + } if(faction_stack[f].longname != NULL) return faction_stack[f].longname; return faction_stack[f].name; } +/** + * @fn glTexture* faction_logoSmall(int f) + * + * @brief Get the factions small logo. + * @param f Faction to get the logo of. + * @return The factions small logo image. + */ +glTexture* faction_logoSmall(int f) { + if((f < 0) || (f >= faction_nstack)) { + WARN("Faction id '%d' is invalid.", f); + return NULL; + } + return faction_stack[f].logo_small; +} + /** * @fn static void faction_sanitizePlayer(Faction* faction) * @@ -390,11 +414,18 @@ static int faction_isFaction(int f) { return 1; } -/* Parses a single faction, but does not set the allies/enemies. */ +/** + * @fn static Faction* faction_parse(xmlNodePtr parent) + * + * @brief Parses a single faction, but doesn't set the allies/enemies bit. + * @param parent Parent node to extract faction from. + * @return Faction created from parent node. + */ static Faction* faction_parse(xmlNodePtr parent) { xmlNodePtr node; int player; Faction* tmp; + char buf[PATH_MAX]; tmp = CALLOC_L(Faction); @@ -405,12 +436,20 @@ static Faction* faction_parse(xmlNodePtr parent) { player = 0; node = parent->xmlChildrenNode; do { + /* Can be 0 or negative, so we have to take that into account. */ if(xml_isNode(node, "player")) { tmp->player = xml_getInt(node); player = 1; continue; } + xmlr_strd(node, "longname", tmp->longname); + + if(xml_isNode(node, "logo")) { + snprintf(buf, PATH_MAX, FACTION_LOGO_PATH"%s_small.png", xml_get(node)); + tmp->logo_small = gl_newImage(buf); + continue; + } } while(xml_nextNode(node)); if(player == 0) WARN("Faction '%s' missing player tag", tmp->name); @@ -487,10 +526,8 @@ int factions_load(void) { /* Player faction is hardcoded. */ faction_stack = malloc(sizeof(Faction)); + memset(faction_stack, 0, sizeof(Faction)); faction_stack[0].name = strdup("Player"); - faction_stack[0].longname = NULL; - faction_stack[0].nallies = 0; - faction_stack[0].nenemies = 0; faction_nstack++; /* First pass. */ @@ -531,9 +568,10 @@ void factions_free(void) { /* Free factions. */ for(i = 0; i < faction_nstack; i++) { free(faction_stack[i].name); - if(faction_stack[i].longname != NULL) free(faction_stack[i].longname); - if(faction_stack[i].nallies > 0) free(faction_stack[i].allies); - if(faction_stack[i].nenemies > 0) free(faction_stack[i].enemies); + if(faction_stack[i].longname != NULL) free(faction_stack[i].longname); + if(faction_stack[i].logo_small != NULL) gl_freeTexture(faction_stack[i].logo_small); + if(faction_stack[i].nallies > 0) free(faction_stack[i].allies); + if(faction_stack[i].nenemies > 0) free(faction_stack[i].enemies); } free(faction_stack); faction_stack = NULL; diff --git a/src/faction.h b/src/faction.h index 8f6b932..05500a1 100644 --- a/src/faction.h +++ b/src/faction.h @@ -1,4 +1,5 @@ #pragma once +#include "opengl.h" #include "colour.h" #define FACTION_PLAYER 0 @@ -7,6 +8,7 @@ int faction_get(const char* name); char* faction_name(int f); char* faction_longname(int f); +glTexture* faction_logoSmall(int f); /* Player stuff. */ void faction_modPlayer(int f, int mod); diff --git a/src/land.c b/src/land.c index 60ea90a..b0b3724 100644 --- a/src/land.c +++ b/src/land.c @@ -1156,6 +1156,8 @@ static void spaceport_refuel(char* str) { /* Land the player. */ void land(Planet* p) { char buf[32], cred[16]; + glTexture* logo; + int offset; if(landed) return; @@ -1164,9 +1166,21 @@ void land(Planet* p) { gfx_exterior = gl_newImage(p->gfx_exterior); land_wid = window_create(p->name, -1, -1, LAND_WIDTH, LAND_HEIGHT); + /* Faction logo. */ + offset = 20; + if(land_planet->faction != -1) { + logo = faction_logoSmall(land_planet->faction); + if(logo != NULL) { + window_addImage(land_wid, 440 + (LAND_WIDTH-460-logo->w)/2, -20, + "imgFaction", logo, 0); + offset = 84; + } + } + /* Pretty display. */ window_addImage(land_wid, 20, -40, "imgPlanet", gfx_exterior, 1); - window_addText(land_wid, 440, 80, LAND_WIDTH-460, 460, 0, + window_addText(land_wid, 440, -20-offset, + LAND_WIDTH-460, LAND_HEIGHT-20-offset-60-BUTTON_HEIGHT*2, 0, "txtPlanetDesc", &gl_smallFont, &cBlack, p->description); /* Buttons. */ window_addButton(land_wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT, diff --git a/src/outfit.c b/src/outfit.c index a9f940a..263badb 100644 --- a/src/outfit.c +++ b/src/outfit.c @@ -523,8 +523,7 @@ static void outfit_parseSBolt(Outfit* tmp, const xmlNodePtr parent) { xmlr_float(node, "energy", tmp->u.blt.energy); if(xml_isNode(node, "gfx")) { - snprintf(str, strlen(xml_get(node))+sizeof(OUTFIT_GFX)+10, - OUTFIT_GFX"space/%s.png", xml_get(node)); + snprintf(str, PATH_MAX, OUTFIT_GFX"space/%s.png", xml_get(node)); tmp->u.blt.gfx_space = gl_newSprite(str, 6, 6); continue; } @@ -588,8 +587,7 @@ static void outfit_parseSBeam(Outfit* tmp, const xmlNodePtr parent) { /* Graphics stuff. */ if(xml_isNode(node, "gfx")) { - snprintf(str, strlen(xml_get(node))+sizeof(OUTFIT_GFX)+10, - OUTFIT_GFX"space/%s.png", xml_get(node)); + snprintf(str, PATH_MAX, OUTFIT_GFX"space/%s.png", xml_get(node)); tmp->u.bem.gfx = gl_newSprite(str, 1, 1); continue; }