[Change] Don't always preload comms graphics for ships, load only as

needed.
This commit is contained in:
Allanis 2014-07-23 20:07:45 +01:00
parent 0faad008b5
commit 25d001d05b
3 changed files with 49 additions and 19 deletions

View File

@ -12,14 +12,17 @@
#include "rng.h"
#include "llua.h"
#include "player.h"
#include "opengl.h"
#include "comm.h"
#define BUTTON_WIDTH 80 /**< Button width. */
#define BUTTON_HEIGHT 30 /*<< Button height. */
static Pilot* comm_pilot = NULL; /**< Pilot currently talking to. */
static glTexture* comm_graphic = NULL; /**< Pilot graphic. */
/* Static. */
static void comm_close(unsigned int wid, char* unused);
static void comm_bribe(unsigned int wid, char* unused);
static unsigned int comm_getBribeAmount(void);
static char* comm_getBribeString(char* str);
@ -33,7 +36,7 @@ extern void ai_setPilot(Pilot* p); /**< From ai.c. */
*/
int comm_open(unsigned int pilot) {
int x, y, w;
glTexture* logo, *gfx_comm;
glTexture* logo;
char* name, *stand;
unsigned int wid;
glColour* c;
@ -49,7 +52,7 @@ int comm_open(unsigned int pilot) {
}
/* Get graphics and text. */
gfx_comm = comm_pilot->ship->gfx_comm;
comm_graphic = ship_loadCommGFX(comm_pilot->ship);
logo = faction_logoSmall(comm_pilot->faction);
name = comm_pilot->name;
/* Get standing colour / text. */
@ -66,36 +69,36 @@ int comm_open(unsigned int pilot) {
w += logo->w;
y = MAX(y, logo->w);
}
x = (gfx_comm->w - w) / 2;
x = (comm_graphic->w - w) / 2;
/* Create the window. */
wid = window_create("Communication Channel", -1, -1,
20 + gfx_comm->w + 20 + BUTTON_WIDTH + 20, 30 + gfx_comm->h + y + 5 + 20);
20 + comm_graphic->w + 20 + BUTTON_WIDTH + 20, 30 + comm_graphic->h + y + 5 + 20);
/* Create the ship image. */
window_addRect(wid, 20, -30, gfx_comm->w, gfx_comm->h + y + 5, "rctShip", &cGrey10, 1);
window_addImage(wid, 20, -30, "imgShip", gfx_comm, 0);
window_addRect(wid, 20, -30, comm_graphic->w, comm_graphic->h + y + 5, "rctShip", &cGrey10, 1);
window_addImage(wid, 20, -30, "imgShip", comm_graphic, 0);
/* Faction logo. */
if(logo != NULL) {
window_addImage(wid, x, -30 - gfx_comm->h - 5,
window_addImage(wid, x, -30 - comm_graphic->h - 5,
"imgFaction", logo, 0);
x += logo->w + 10;
y -= (logo->w - (gl_defFont.h*2 + 15)) / 2;
}
/* Name. */
window_addText(wid, x, -30 - gfx_comm->h - y + gl_defFont.h*2 + 10,
gfx_comm->w - x, 20, 0, "txtName",
window_addText(wid, x, -30 - comm_graphic->h - y + gl_defFont.h*2 + 10,
comm_graphic->w - x, 20, 0, "txtName",
NULL, &cDConsole, name);
/* Standing. */
window_addText(wid, x, -30 - gfx_comm->h - y + gl_defFont.h + 5,
gfx_comm->w - x, 20, 0, "txtStanding", NULL, c, stand);
window_addText(wid, x, -30 - comm_graphic->h - y + gl_defFont.h + 5,
comm_graphic->w - x, 20, 0, "txtStanding", NULL, c, stand);
/* Buttons. */
window_addButton(wid, -20, 20, BUTTON_WIDTH, BUTTON_HEIGHT,
"btnClose", "Close Channel", window_close);
"btnClose", "Close Channel", comm_close);
window_addButton(wid, -20, 20 + BUTTON_HEIGHT + 20,
BUTTON_WIDTH, BUTTON_HEIGHT, "btnGreet", "Greet", NULL);
@ -111,6 +114,22 @@ int comm_open(unsigned int pilot) {
return 0;
}
/**
* @brief Close the comm window.
* @param wid ID of window calling the function.
* @param unused Unused.
*/
static void comm_close(unsigned int wid, char* unused) {
/* Clean up a but after ourselves. */
if(comm_graphic != NULL) {
gl_freeTexture(comm_graphic);
comm_graphic = NULL;
}
comm_pilot = NULL;
/* Close the window. */
window_close(wid, unused);
}
/**
* @brief Try to bribe the pilot.
* @param wid ID of window calling the function.

View File

@ -268,6 +268,17 @@ int ship_basePrice(Ship* s) {
return price;
}
/**
* @brief Load the ships comm graphic.
*
* Must be freed afterwards.
*/
glTexture* ship_loadCommGFX(Ship* s) {
char buf[PATH_MAX];
snprintf(buf, PATH_MAX, SHIP_GFX"%s"SHIP_COMM SHIP_EXT, s->gfx_comm);
return gl_newImage(buf, 0);
}
/**
* @brief Extracts the ingame ship from an XML node.
* @param tmp Ship to load data into.
@ -308,9 +319,8 @@ static int ship_parse(Ship* tmp, xmlNodePtr parent) {
tmp->mangle = 2.*M_PI;
tmp->mangle /= tmp->gfx_space->sx * tmp->gfx_space->sy;
/* Load the comm graphic. */
tmp->gfx_comm = xml_parseTexture(node,
SHIP_GFX"%s"SHIP_COMM SHIP_EXT, 1, 1, 0);
/* Get the comm graphic for future loading. */
tmp->gfx_comm = xml_getStrd(node);
/* Load the target graphic. */
xmlr_attr(node, "target", stmp);
@ -532,8 +542,8 @@ void ships_free(void) {
/* Free graphics. */
gl_freeTexture(s->gfx_space);
gl_freeTexture(s->gfx_comm);
gl_freeTexture(ship_stack[i].gfx_target);
free(s->gfx_comm);
}
free(ship_stack);
ship_stack = NULL;

View File

@ -71,9 +71,9 @@ typedef struct Ship_ {
double thrust, turn, speed;
/* Graphics. */
glTexture* gfx_space;
glTexture* gfx_target;
glTexture* gfx_comm;
glTexture* gfx_space; /**< Space sprite sheet. */
glTexture* gfx_target; /**< Targetting window graphic. */
char* gfx_comm; /**< Name of graphic for communication. */
/* GUI interface. */
char* gui;
@ -114,6 +114,7 @@ Ship** ship_getTech(int* n, const int* tech, const int techmax);
char* ship_class(Ship* p);
ShipClass ship_classFromString(char* str);
int ship_basePrice(Ship* s);
glTexture* ship_loadCommGFX(Ship* s);
/* Toolkit. */
void ship_view(unsigned int unused, char* shipname);