[Add] Allow for faction logo's to be displayed.

This commit is contained in:
Allanis 2013-11-05 23:50:09 +00:00
parent 946dafdf55
commit 4b64b1c185
4 changed files with 69 additions and 17 deletions

View File

@ -7,6 +7,7 @@
#include <malloc.h> #include <malloc.h>
#include <string.h> #include <string.h>
#include "opengl.h"
#include "lephisto.h" #include "lephisto.h"
#include "log.h" #include "log.h"
#include "pack.h" #include "pack.h"
@ -18,6 +19,7 @@
#define XML_FACTION_TAG "faction" #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. */ #define PLAYER_ALLY 70 /**< Above this, player is considered ally. */
@ -30,6 +32,9 @@ typedef struct Faction_ {
char* name; /**< Normal name. */ char* name; /**< Normal name. */
char* longname; /**< Long name. */ char* longname; /**< Long name. */
/* Graphics. */
glTexture* logo_small; /**< Small logo. */
/* Enemies. */ /* Enemies. */
int* enemies; /**< Enemies by ID of the faction. */ int* enemies; /**< Enemies by ID of the faction. */
int nenemies; /**< Number of enemies. */ int nenemies; /**< Number of enemies. */
@ -96,11 +101,30 @@ char* faction_name(int f) {
* @return The factions long name. * @return The factions long name.
*/ */
char* faction_longname(int f) { 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) if(faction_stack[f].longname != NULL)
return faction_stack[f].longname; return faction_stack[f].longname;
return faction_stack[f].name; 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) * @fn static void faction_sanitizePlayer(Faction* faction)
* *
@ -390,11 +414,18 @@ static int faction_isFaction(int f) {
return 1; 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) { static Faction* faction_parse(xmlNodePtr parent) {
xmlNodePtr node; xmlNodePtr node;
int player; int player;
Faction* tmp; Faction* tmp;
char buf[PATH_MAX];
tmp = CALLOC_L(Faction); tmp = CALLOC_L(Faction);
@ -405,12 +436,20 @@ static Faction* faction_parse(xmlNodePtr parent) {
player = 0; player = 0;
node = parent->xmlChildrenNode; node = parent->xmlChildrenNode;
do { do {
/* Can be 0 or negative, so we have to take that into account. */
if(xml_isNode(node, "player")) { if(xml_isNode(node, "player")) {
tmp->player = xml_getInt(node); tmp->player = xml_getInt(node);
player = 1; player = 1;
continue; continue;
} }
xmlr_strd(node, "longname", tmp->longname); 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)); } while(xml_nextNode(node));
if(player == 0) WARN("Faction '%s' missing player tag", tmp->name); if(player == 0) WARN("Faction '%s' missing player tag", tmp->name);
@ -487,10 +526,8 @@ int factions_load(void) {
/* Player faction is hardcoded. */ /* Player faction is hardcoded. */
faction_stack = malloc(sizeof(Faction)); faction_stack = malloc(sizeof(Faction));
memset(faction_stack, 0, sizeof(Faction));
faction_stack[0].name = strdup("Player"); faction_stack[0].name = strdup("Player");
faction_stack[0].longname = NULL;
faction_stack[0].nallies = 0;
faction_stack[0].nenemies = 0;
faction_nstack++; faction_nstack++;
/* First pass. */ /* First pass. */
@ -532,6 +569,7 @@ void factions_free(void) {
for(i = 0; i < faction_nstack; i++) { for(i = 0; i < faction_nstack; i++) {
free(faction_stack[i].name); free(faction_stack[i].name);
if(faction_stack[i].longname != NULL) free(faction_stack[i].longname); 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].nallies > 0) free(faction_stack[i].allies);
if(faction_stack[i].nenemies > 0) free(faction_stack[i].enemies); if(faction_stack[i].nenemies > 0) free(faction_stack[i].enemies);
} }

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include "opengl.h"
#include "colour.h" #include "colour.h"
#define FACTION_PLAYER 0 #define FACTION_PLAYER 0
@ -7,6 +8,7 @@
int faction_get(const char* name); int faction_get(const char* name);
char* faction_name(int f); char* faction_name(int f);
char* faction_longname(int f); char* faction_longname(int f);
glTexture* faction_logoSmall(int f);
/* Player stuff. */ /* Player stuff. */
void faction_modPlayer(int f, int mod); void faction_modPlayer(int f, int mod);

View File

@ -1156,6 +1156,8 @@ static void spaceport_refuel(char* str) {
/* Land the player. */ /* Land the player. */
void land(Planet* p) { void land(Planet* p) {
char buf[32], cred[16]; char buf[32], cred[16];
glTexture* logo;
int offset;
if(landed) return; if(landed) return;
@ -1164,9 +1166,21 @@ void land(Planet* p) {
gfx_exterior = gl_newImage(p->gfx_exterior); gfx_exterior = gl_newImage(p->gfx_exterior);
land_wid = window_create(p->name, -1, -1, LAND_WIDTH, LAND_HEIGHT); 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. */ /* Pretty display. */
window_addImage(land_wid, 20, -40, "imgPlanet", gfx_exterior, 1); 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); "txtPlanetDesc", &gl_smallFont, &cBlack, p->description);
/* Buttons. */ /* Buttons. */
window_addButton(land_wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT, window_addButton(land_wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT,

View File

@ -523,8 +523,7 @@ static void outfit_parseSBolt(Outfit* tmp, const xmlNodePtr parent) {
xmlr_float(node, "energy", tmp->u.blt.energy); xmlr_float(node, "energy", tmp->u.blt.energy);
if(xml_isNode(node, "gfx")) { if(xml_isNode(node, "gfx")) {
snprintf(str, strlen(xml_get(node))+sizeof(OUTFIT_GFX)+10, snprintf(str, PATH_MAX, OUTFIT_GFX"space/%s.png", xml_get(node));
OUTFIT_GFX"space/%s.png", xml_get(node));
tmp->u.blt.gfx_space = gl_newSprite(str, 6, 6); tmp->u.blt.gfx_space = gl_newSprite(str, 6, 6);
continue; continue;
} }
@ -588,8 +587,7 @@ static void outfit_parseSBeam(Outfit* tmp, const xmlNodePtr parent) {
/* Graphics stuff. */ /* Graphics stuff. */
if(xml_isNode(node, "gfx")) { if(xml_isNode(node, "gfx")) {
snprintf(str, strlen(xml_get(node))+sizeof(OUTFIT_GFX)+10, snprintf(str, PATH_MAX, OUTFIT_GFX"space/%s.png", xml_get(node));
OUTFIT_GFX"space/%s.png", xml_get(node));
tmp->u.bem.gfx = gl_newSprite(str, 1, 1); tmp->u.bem.gfx = gl_newSprite(str, 1, 1);
continue; continue;
} }