[Add] Added flags to image creation to control transparency mapping.

Should speed up the loading and reduce memory usage.
This commit is contained in:
Allanis 2014-05-17 11:19:15 +01:00
parent a14034411e
commit 1abb2c117d
13 changed files with 63 additions and 44 deletions

View File

@ -462,7 +462,7 @@ static int faction_parse(Faction* tmp, xmlNodePtr parent) {
if(xml_isNode(node, "logo")) { if(xml_isNode(node, "logo")) {
snprintf(buf, PATH_MAX, FACTION_LOGO_PATH"%s_small.png", xml_get(node)); snprintf(buf, PATH_MAX, FACTION_LOGO_PATH"%s_small.png", xml_get(node));
tmp->logo_small = gl_newImage(buf); tmp->logo_small = gl_newImage(buf, 0);
continue; continue;
} }
} while(xml_nextNode(node)); } while(xml_nextNode(node));

View File

@ -1354,7 +1354,7 @@ void land(Planet* p) {
/* Load stuff. */ /* Load stuff. */
land_planet = p; land_planet = p;
gfx_exterior = gl_newImage(p->gfx_exterior); gfx_exterior = gl_newImage(p->gfx_exterior, 0);
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. */ /* Faction logo. */

View File

@ -311,7 +311,7 @@ void loadscreen_load(void) {
/* Load the texture. */ /* Load the texture. */
strncpy(file_path, loadscreens[RNG(0, nload-1)], PATH_MAX); strncpy(file_path, loadscreens[RNG(0, nload-1)], PATH_MAX);
loading = gl_newImage(file_path); loading = gl_newImage(file_path, 0);
/* Clean up. */ /* Clean up. */
free(loadscreens); free(loadscreens);

View File

@ -14,10 +14,12 @@
* "PREFIX%sSUFFIX". * "PREFIX%sSUFFIX".
* @param defsx Default X sprites. * @param defsx Default X sprites.
* @param defsy Default Y sprites. * @param defsy Default Y sprites.
* @param flags Image parameter control flags.
* @return The texture from the node or NULL if an error occurred. * @return The texture from the node or NULL if an error occurred.
*/ */
glTexture* xml_parseTexture(xmlNodePtr node, glTexture* xml_parseTexture(xmlNodePtr node,
const char* path, int defsx, int defsy) { const char* path, int defsx, int defsy,
const unsigned int flags) {
int sx, sy; int sx, sy;
char* buf, filename[PATH_MAX]; char* buf, filename[PATH_MAX];
@ -51,9 +53,9 @@ glTexture* xml_parseTexture(xmlNodePtr node,
/* Load the graphic. */ /* Load the graphic. */
if((sx == 1) && (sy == 1)) if((sx == 1) && (sy == 1))
tex = gl_newImage(filename); tex = gl_newImage(filename, flags);
else else
tex = gl_newSprite(filename, sx, sy); tex = gl_newSprite(filename, sx, sy, flags);
/* Return result. */ /* Return result. */
return tex; return tex;

View File

@ -84,5 +84,6 @@
* Functions for generic complex reading. * Functions for generic complex reading.
*/ */
glTexture* xml_parseTexture(xmlNodePtr node, glTexture* xml_parseTexture(xmlNodePtr node,
const char* path, int defsx, int defsy); const char* path, int defsx, int defsy,
const unsigned int flags);

View File

@ -96,7 +96,7 @@ void menu_main(void) {
unsigned int bwid, wid; unsigned int bwid, wid;
glTexture* tex; glTexture* tex;
tex = gl_newImage("../gfx/saracraft_logo1.png"); tex = gl_newImage("../gfx/saracraft_logo1.png", 0);
nebu_prep(300., 0.); /* Needed for nebuale to not spaz out. */ nebu_prep(300., 0.); /* Needed for nebuale to not spaz out. */
/* Create background image window. */ /* Create background image window. */

View File

@ -48,10 +48,12 @@
glInfo gl_screen; /**< Give data of current opengl settings. */ glInfo gl_screen; /**< Give data of current opengl settings. */
Vec2* gl_camera; /**< Camera we are using. */ Vec2* gl_camera; /**< Camera we are using. */
/* offsets to Adjust the pilot's place onscreen */ /*
/*to be in the middle, even with the GUI. */ * Used to adjust the pilots place onscreen to be in the middle
extern double gui_xoff; * even with the GUI.
extern double gui_yoff; */
extern double gui_xoff; /**< GUI X offset. */
extern double gui_yoff; /**< GUI Y offset. */
/* Graphic list. */ /* Graphic list. */
/** /**
@ -70,7 +72,7 @@ static int SDL_IsTrans(SDL_Surface* s, int x, int y);
static uint8_t* SDL_MapTrans(SDL_Surface* s); static uint8_t* SDL_MapTrans(SDL_Surface* s);
/* glTexture. */ /* glTexture. */
static GLuint gl_loadSurface(SDL_Surface* surface, int* rw, int* rh); static GLuint gl_loadSurface(SDL_Surface* surface, int* rw, int* rh);
static glTexture* gl_loadNewImage(const char* path); static glTexture* gl_loadNewImage(const char* path, unsigned int flags);
static void gl_blitTexture(const glTexture* texture, static void gl_blitTexture(const glTexture* texture,
const double x, const double y, const double x, const double y,
const double tx, const double ty, const glColour* c); const double tx, const double ty, const glColour* c);
@ -427,9 +429,10 @@ glTexture* gl_loadImage(SDL_Surface* surface) {
* *
* May not necessarily load the image but use one of it's already open. * May not necessarily load the image but use one of it's already open.
* @param path Image to load. * @param path Image to load.
* @param flags Flags to control image parameters.
* @return Texture loaded from image. * @return Texture loaded from image.
*/ */
glTexture* gl_newImage(const char* path) { glTexture* gl_newImage(const char* path, const unsigned int flags) {
glTexList* cur, *last; glTexList* cur, *last;
/* Check to see if it already exists. */ /* Check to see if it already exists. */
@ -450,7 +453,7 @@ glTexture* gl_newImage(const char* path) {
cur->used = 1; cur->used = 1;
/* Load the image. */ /* Load the image. */
cur->tex = gl_loadNewImage(path); cur->tex = gl_loadNewImage(path, flags);
if(texture_list == NULL) /* Special condition - creating new list. */ if(texture_list == NULL) /* Special condition - creating new list. */
texture_list = cur; texture_list = cur;
@ -463,9 +466,10 @@ glTexture* gl_newImage(const char* path) {
/** /**
* @brief Only loads the image, does not add to stack unlike gl_newImage. * @brief Only loads the image, does not add to stack unlike gl_newImage.
* @param path Image to load. * @param path Image to load.
* @param flags Flags to control image parameters.
* @return Texture loaded from image. * @return Texture loaded from image.
*/ */
static glTexture* gl_loadNewImage(const char* path) { static glTexture* gl_loadNewImage(const char* path, const unsigned int flags) {
SDL_Surface* tmp, *surface; SDL_Surface* tmp, *surface;
glTexture* t; glTexture* t;
uint8_t* trans; uint8_t* trans;
@ -502,9 +506,12 @@ static glTexture* gl_loadNewImage(const char* path) {
} }
/* Do after flipping for collision detection. */ /* Do after flipping for collision detection. */
if(flags & OPENGL_TEX_MAPTRANS) {
SDL_LockSurface(surface); SDL_LockSurface(surface);
trans = SDL_MapTrans(surface); trans = SDL_MapTrans(surface);
SDL_UnlockSurface(surface); SDL_UnlockSurface(surface);
} else
trans = NULL;
/* Set the texture. */ /* Set the texture. */
t = gl_loadImage(surface); t = gl_loadImage(surface);
@ -518,11 +525,14 @@ static glTexture* gl_loadNewImage(const char* path) {
* @param path Image to load. * @param path Image to load.
* @param sx Number of X sprites in image. * @param sx Number of X sprites in image.
* @param sy Number of Y sprites in image. * @param sy Number of Y sprites in image.
* @param flags Flags to control image parameters.
* @return Texture loaded. * @return Texture loaded.
*/ */
glTexture* gl_newSprite(const char* path, const int sx, const int sy) { glTexture* gl_newSprite(const char* path, const int sx, const int sy,
const unsigned int flags) {
glTexture* texture; glTexture* texture;
if((texture = gl_newImage(path)) == NULL) if((texture = gl_newImage(path, flags)) == NULL)
return NULL; return NULL;
/* /*

View File

@ -61,6 +61,9 @@ extern glInfo gl_screen; /* Local structure set with gl_init etc. */
#define COLOUR(x) glColor4d((x).r, (x).g, (x).b, (x).a) /**< Change colour. */ #define COLOUR(x) glColor4d((x).r, (x).g, (x).b, (x).a) /**< Change colour. */
#define ACOLOUR(x,a) glColor4d((x).r, (x).g, (x).b, a) /**< Change colour and override alpha. */ #define ACOLOUR(x,a) glColor4d((x).r, (x).g, (x).b, a) /**< Change colour and override alpha. */
/* Texture Flags. */
#define OPENGL_TEX_MAPTRANS (1<<0) /**< Create a transparent map. */
/** /**
* @brief Abstraction for rendering spritesheets. * @brief Abstraction for rendering spritesheets.
* *
@ -92,8 +95,9 @@ typedef struct glTexture_ {
/* glTexture loading/freeing. */ /* glTexture loading/freeing. */
SDL_Surface* gl_prepareSurface(SDL_Surface* surface); /* Only preps it. */ SDL_Surface* gl_prepareSurface(SDL_Surface* surface); /* Only preps it. */
glTexture* gl_loadImage(SDL_Surface* surface); /* Frees the surface. */ glTexture* gl_loadImage(SDL_Surface* surface); /* Frees the surface. */
glTexture* gl_newImage(const char* path); glTexture* gl_newImage(const char* path, const unsigned int flags);
glTexture* gl_newSprite(const char* path, const int sx, const int sy); glTexture* gl_newSprite(const char* path, const int sx, const int sy,
const unsigned int flags);
void gl_freeTexture(glTexture* texture); void gl_freeTexture(glTexture* texture);
/* Rendering. */ /* Rendering. */

View File

@ -620,7 +620,8 @@ static void outfit_parseSBolt(Outfit* tmp, const xmlNodePtr parent) {
if(xml_isNode(node, "gfx")) { if(xml_isNode(node, "gfx")) {
tmp->u.blt.gfx_space = xml_parseTexture(node, tmp->u.blt.gfx_space = xml_parseTexture(node,
OUTFIT_GFX"space/%s.png", 6, 6); OUTFIT_GFX"space/%s.png", 6, 6,
OPENGL_TEX_MAPTRANS);
xmlr_attr(node, "spin", buf); xmlr_attr(node, "spin", buf);
if(buf != NULL) { if(buf != NULL) {
outfit_setProp(tmp, OUTFIT_PROP_WEAP_SPIN); outfit_setProp(tmp, OUTFIT_PROP_WEAP_SPIN);
@ -694,7 +695,7 @@ static void outfit_parseSBeam(Outfit* tmp, const xmlNodePtr parent) {
/* Graphics stuff. */ /* Graphics stuff. */
if(xml_isNode(node, "gfx")) { if(xml_isNode(node, "gfx")) {
tmp->u.bem.gfx = xml_parseTexture(node, tmp->u.bem.gfx = xml_parseTexture(node,
OUTFIT_GFX"space/%s.png", 1, 1); OUTFIT_GFX"space/%s.png", 1, 1, 0);
continue; continue;
} }
@ -785,7 +786,8 @@ static void outfit_parseSAmmo(Outfit* tmp, const xmlNodePtr parent) {
xmlr_float(node, "energy", tmp->u.amm.energy); xmlr_float(node, "energy", tmp->u.amm.energy);
if(xml_isNode(node, "gfx")) { if(xml_isNode(node, "gfx")) {
tmp->u.amm.gfx_space = xml_parseTexture(node, tmp->u.amm.gfx_space = xml_parseTexture(node,
OUTFIT_GFX"space/%s.png", 6, 6); OUTFIT_GFX"space/%s.png", 6, 6,
OPENGL_TEX_MAPTRANS);
xmlr_attr(node, "spin", buf); xmlr_attr(node, "spin", buf);
if(buf != NULL) { if(buf != NULL) {
outfit_setProp(tmp, OUTFIT_PROP_WEAP_SPIN); outfit_setProp(tmp, OUTFIT_PROP_WEAP_SPIN);
@ -969,7 +971,6 @@ static void outfit_parseSJammer(Outfit* tmp, const xmlNodePtr parent) {
static int outfit_parse(Outfit* tmp, const xmlNodePtr parent) { static int outfit_parse(Outfit* tmp, const xmlNodePtr parent) {
xmlNodePtr cur, node; xmlNodePtr cur, node;
char* prop; char* prop;
char str[PATH_MAX] = "\0";
/* Clear data. */ /* Clear data. */
memset(tmp, 0, sizeof(Outfit)); memset(tmp, 0, sizeof(Outfit));
@ -990,10 +991,10 @@ static int outfit_parse(Outfit* tmp, const xmlNodePtr parent) {
xmlr_int(cur, "price", tmp->price); xmlr_int(cur, "price", tmp->price);
xmlr_strd(cur, "description", tmp->description); xmlr_strd(cur, "description", tmp->description);
if(xml_isNode(cur, "gfx_store")) { if(xml_isNode(cur, "gfx_store")) {
snprintf(str, PATH_MAX, OUTFIT_GFX"store/%s.png", xml_get(cur)); tmp->gfx_store = xml_parseTexture(cur,
tmp->gfx_store = gl_newImage(str); OUTFIT_GFX"store/%s.png", 1, 1, 0);
} }
} while((cur = cur->next)); } while(xml_nextNode(cur));
} }
else if(xml_isNode(node, "specific")) { else if(xml_isNode(node, "specific")) {
/* Has to be processed seperately. */ /* Has to be processed seperately. */

View File

@ -1536,17 +1536,17 @@ static int gui_parse(const xmlNodePtr parent, const char* name) {
ERR("GUI '%s' has no gfx property", name); ERR("GUI '%s' has no gfx property", name);
return -1; return -1;
} }
/* Load gfx. */
/* Load gfx. */
/* Frame. */ /* Frame. */
snprintf(buf, PATH_MAX, GUI_GFX"%s.png", tmp); snprintf(buf, PATH_MAX, GUI_GFX"%s.png", tmp);
gui.gfx_frame = gl_newImage(buf); gui.gfx_frame = gl_newImage(buf, 0);
/* Pilot. */ /* Pilot. */
snprintf(buf, PATH_MAX, GUI_GFX"%s_pilot.png", tmp); snprintf(buf, PATH_MAX, GUI_GFX"%s_pilot.png", tmp);
gui.gfx_targetPilot = gl_newSprite(buf, 2, 2); gui.gfx_targetPilot = gl_newSprite(buf, 2, 2, 0);
/* Planet. */ /* Planet. */
snprintf(buf, PATH_MAX, GUI_GFX"%s_planet.png", tmp); snprintf(buf, PATH_MAX, GUI_GFX"%s_planet.png", tmp);
gui.gfx_targetPlanet = gl_newSprite(buf, 2, 2); gui.gfx_targetPlanet = gl_newSprite(buf, 2, 2, 0);
free(tmp); free(tmp);
/* Frame (based on gfx). */ /* Frame (based on gfx). */
@ -1595,7 +1595,7 @@ static int gui_parse(const xmlNodePtr parent, const char* name) {
tmp = xml_get(cur); tmp = xml_get(cur);
if(tmp != NULL) { if(tmp != NULL) {
snprintf(buf, PATH_MAX, GUI_GFX"%s.png", tmp); snprintf(buf, PATH_MAX, GUI_GFX"%s.png", tmp);
gui.gfx_shield = gl_newImage(buf); gui.gfx_shield = gl_newImage(buf, 0);
} }
RELATIVIZE(gui.shield); RELATIVIZE(gui.shield);
} }
@ -1606,7 +1606,7 @@ static int gui_parse(const xmlNodePtr parent, const char* name) {
tmp = xml_get(cur); tmp = xml_get(cur);
if(tmp != NULL) { if(tmp != NULL) {
snprintf(buf, PATH_MAX, GUI_GFX"%s.png", tmp); snprintf(buf, PATH_MAX, GUI_GFX"%s.png", tmp);
gui.gfx_armour = gl_newImage(buf); gui.gfx_armour = gl_newImage(buf, 0);
} }
RELATIVIZE(gui.armour); RELATIVIZE(gui.armour);
} }
@ -1617,7 +1617,7 @@ static int gui_parse(const xmlNodePtr parent, const char* name) {
tmp = xml_get(cur); tmp = xml_get(cur);
if(tmp != NULL) { if(tmp != NULL) {
snprintf(buf, PATH_MAX, GUI_GFX"%s.png", tmp); snprintf(buf, PATH_MAX, GUI_GFX"%s.png", tmp);
gui.gfx_energy = gl_newImage(buf); gui.gfx_energy = gl_newImage(buf, 0);
} }
RELATIVIZE(gui.energy); RELATIVIZE(gui.energy);
} }
@ -1627,7 +1627,7 @@ static int gui_parse(const xmlNodePtr parent, const char* name) {
tmp = xml_get(cur); tmp = xml_get(cur);
if(tmp != NULL) { if(tmp != NULL) {
snprintf(buf, PATH_MAX, GUI_GFX"%s.png", tmp); snprintf(buf, PATH_MAX, GUI_GFX"%s.png", tmp);
gui.gfx_fuel = gl_newImage(buf); gui.gfx_fuel = gl_newImage(buf, 0);
} }
RELATIVIZE(gui.fuel); RELATIVIZE(gui.fuel);
} }

View File

@ -247,23 +247,24 @@ static int ship_parse(Ship* tmp, xmlNodePtr parent) {
/* Load the base graphic. */ /* Load the base graphic. */
tmp->gfx_space = xml_parseTexture(node, tmp->gfx_space = xml_parseTexture(node,
SHIP_GFX"%s"SHIP_EXT, 6, 6); SHIP_GFX"%s"SHIP_EXT, 6, 6,
OPENGL_TEX_MAPTRANS);
/* Load the comm graphic. */ /* Load the comm graphic. */
tmp->gfx_comm = xml_parseTexture(node, tmp->gfx_comm = xml_parseTexture(node,
SHIP_GFX"%s"SHIP_COMM SHIP_EXT, 1, 1); SHIP_GFX"%s"SHIP_COMM SHIP_EXT, 1, 1, 0);
/* Load the target graphic. */ /* Load the target graphic. */
xmlr_attr(node, "target", stmp); xmlr_attr(node, "target", stmp);
if(stmp != NULL) { if(stmp != NULL) {
snprintf(str, PATH_MAX, snprintf(str, PATH_MAX,
SHIP_GFX"%s"SHIP_TARGET SHIP_EXT, stmp); SHIP_GFX"%s"SHIP_TARGET SHIP_EXT, stmp);
tmp->gfx_target = gl_newImage(str); tmp->gfx_target = gl_newImage(str, 0);
free(stmp); free(stmp);
} else { /* Load standard target graphic. */ } else { /* Load standard target graphic. */
snprintf(str, PATH_MAX, snprintf(str, PATH_MAX,
SHIP_GFX"%s"SHIP_TARGET SHIP_EXT, xml_get(node)); SHIP_GFX"%s"SHIP_TARGET SHIP_EXT, xml_get(node));
tmp->gfx_target = gl_newImage(str); tmp->gfx_target = gl_newImage(str, 0);
} }
} }
xmlr_strd(node, "GUI", tmp->gui); xmlr_strd(node, "GUI", tmp->gui);

View File

@ -718,7 +718,7 @@ static int planet_parse(Planet* planet, const xmlNodePtr parent) {
do { do {
if(xml_isNode(cur, "space")) { /* Load space gfx. */ if(xml_isNode(cur, "space")) { /* Load space gfx. */
planet->gfx_space = xml_parseTexture(cur, planet->gfx_space = xml_parseTexture(cur,
PLANET_GFX_SPACE"%s", 1, 1); PLANET_GFX_SPACE"%s", 1, 1, 0);
} }
else if(xml_isNode(cur, "exterior")) { /* Load land gfx. */ else if(xml_isNode(cur, "exterior")) { /* Load land gfx. */
len = strlen(xml_raw(cur)) + sizeof(PLANET_GFX_EXTERIOR); len = strlen(xml_raw(cur)) + sizeof(PLANET_GFX_EXTERIOR);

View File

@ -97,7 +97,7 @@ static int spfx_base_load(char* name, int ttl, int anim, char* gfx, int sx, int
cur->anim = (double)anim / 1000.; cur->anim = (double)anim / 1000.;
cur->ttl = (double)ttl / 1000.; cur->ttl = (double)ttl / 1000.;
sprintf(buf, SPFX_GFX"%s", gfx); sprintf(buf, SPFX_GFX"%s", gfx);
cur->gfx = gl_newSprite(buf, sx, sy); cur->gfx = gl_newSprite(buf, sx, sy, 0);
return 0; return 0;
} }