From 1abb2c117dc67d77f290cce320fc38130f4c0954 Mon Sep 17 00:00:00 2001 From: Allanis Date: Sat, 17 May 2014 11:19:15 +0100 Subject: [PATCH] [Add] Added flags to image creation to control transparency mapping. Should speed up the loading and reduce memory usage. --- src/faction.c | 2 +- src/land.c | 2 +- src/lephisto.c | 2 +- src/lxml.c | 8 +++++--- src/lxml.h | 3 ++- src/menu.c | 2 +- src/opengl.c | 36 +++++++++++++++++++++++------------- src/opengl.h | 8 ++++++-- src/outfit.c | 15 ++++++++------- src/player.c | 16 ++++++++-------- src/ship.c | 9 +++++---- src/space.c | 2 +- src/spfx.c | 2 +- 13 files changed, 63 insertions(+), 44 deletions(-) diff --git a/src/faction.c b/src/faction.c index 6fc79b6..1545f8c 100644 --- a/src/faction.c +++ b/src/faction.c @@ -462,7 +462,7 @@ static int faction_parse(Faction* tmp, xmlNodePtr parent) { if(xml_isNode(node, "logo")) { 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; } } while(xml_nextNode(node)); diff --git a/src/land.c b/src/land.c index 30823f0..a450068 100644 --- a/src/land.c +++ b/src/land.c @@ -1354,7 +1354,7 @@ void land(Planet* p) { /* Load stuff. */ 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); /* Faction logo. */ diff --git a/src/lephisto.c b/src/lephisto.c index 8f914a8..120919f 100644 --- a/src/lephisto.c +++ b/src/lephisto.c @@ -311,7 +311,7 @@ void loadscreen_load(void) { /* Load the texture. */ strncpy(file_path, loadscreens[RNG(0, nload-1)], PATH_MAX); - loading = gl_newImage(file_path); + loading = gl_newImage(file_path, 0); /* Clean up. */ free(loadscreens); diff --git a/src/lxml.c b/src/lxml.c index 677b3ab..dfc6259 100644 --- a/src/lxml.c +++ b/src/lxml.c @@ -14,10 +14,12 @@ * "PREFIX%sSUFFIX". * @param defsx Default X sprites. * @param defsy Default Y sprites. + * @param flags Image parameter control flags. * @return The texture from the node or NULL if an error occurred. */ 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; char* buf, filename[PATH_MAX]; @@ -51,9 +53,9 @@ glTexture* xml_parseTexture(xmlNodePtr node, /* Load the graphic. */ if((sx == 1) && (sy == 1)) - tex = gl_newImage(filename); + tex = gl_newImage(filename, flags); else - tex = gl_newSprite(filename, sx, sy); + tex = gl_newSprite(filename, sx, sy, flags); /* Return result. */ return tex; diff --git a/src/lxml.h b/src/lxml.h index a1b7553..60184ff 100644 --- a/src/lxml.h +++ b/src/lxml.h @@ -84,5 +84,6 @@ * Functions for generic complex reading. */ glTexture* xml_parseTexture(xmlNodePtr node, - const char* path, int defsx, int defsy); + const char* path, int defsx, int defsy, + const unsigned int flags); diff --git a/src/menu.c b/src/menu.c index e8f21f1..5940610 100644 --- a/src/menu.c +++ b/src/menu.c @@ -96,7 +96,7 @@ void menu_main(void) { unsigned int bwid, wid; 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. */ /* Create background image window. */ diff --git a/src/opengl.c b/src/opengl.c index 9a0bf70..2fcabb5 100644 --- a/src/opengl.c +++ b/src/opengl.c @@ -48,10 +48,12 @@ glInfo gl_screen; /**< Give data of current opengl settings. */ Vec2* gl_camera; /**< Camera we are using. */ -/* offsets to Adjust the pilot's place onscreen */ -/*to be in the middle, even with the GUI. */ -extern double gui_xoff; -extern double gui_yoff; +/* + * Used to adjust the pilots place onscreen to be in the middle + * even with the GUI. + */ +extern double gui_xoff; /**< GUI X offset. */ +extern double gui_yoff; /**< GUI Y offset. */ /* 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); /* glTexture. */ 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, const double x, const double y, 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. * @param path Image to load. + * @param flags Flags to control image parameters. * @return Texture loaded from image. */ -glTexture* gl_newImage(const char* path) { +glTexture* gl_newImage(const char* path, const unsigned int flags) { glTexList* cur, *last; /* Check to see if it already exists. */ @@ -450,7 +453,7 @@ glTexture* gl_newImage(const char* path) { cur->used = 1; /* Load the image. */ - cur->tex = gl_loadNewImage(path); + cur->tex = gl_loadNewImage(path, flags); if(texture_list == NULL) /* Special condition - creating new list. */ 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. * @param path Image to load. + * @param flags Flags to control image parameters. * @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; glTexture* t; uint8_t* trans; @@ -502,9 +506,12 @@ static glTexture* gl_loadNewImage(const char* path) { } /* Do after flipping for collision detection. */ - SDL_LockSurface(surface); - trans = SDL_MapTrans(surface); - SDL_UnlockSurface(surface); + if(flags & OPENGL_TEX_MAPTRANS) { + SDL_LockSurface(surface); + trans = SDL_MapTrans(surface); + SDL_UnlockSurface(surface); + } else + trans = NULL; /* Set the texture. */ t = gl_loadImage(surface); @@ -518,11 +525,14 @@ static glTexture* gl_loadNewImage(const char* path) { * @param path Image to load. * @param sx Number of X sprites in image. * @param sy Number of Y sprites in image. + * @param flags Flags to control image parameters. * @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; - if((texture = gl_newImage(path)) == NULL) + if((texture = gl_newImage(path, flags)) == NULL) return NULL; /* diff --git a/src/opengl.h b/src/opengl.h index d38b2b4..cd7a4f7 100644 --- a/src/opengl.h +++ b/src/opengl.h @@ -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 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. * @@ -92,8 +95,9 @@ typedef struct glTexture_ { /* glTexture loading/freeing. */ SDL_Surface* gl_prepareSurface(SDL_Surface* surface); /* Only preps it. */ glTexture* gl_loadImage(SDL_Surface* surface); /* Frees the surface. */ -glTexture* gl_newImage(const char* path); -glTexture* gl_newSprite(const char* path, const int sx, const int sy); +glTexture* gl_newImage(const char* path, const unsigned int flags); +glTexture* gl_newSprite(const char* path, const int sx, const int sy, + const unsigned int flags); void gl_freeTexture(glTexture* texture); /* Rendering. */ diff --git a/src/outfit.c b/src/outfit.c index 190ecab..d27f85b 100644 --- a/src/outfit.c +++ b/src/outfit.c @@ -620,7 +620,8 @@ static void outfit_parseSBolt(Outfit* tmp, const xmlNodePtr parent) { if(xml_isNode(node, "gfx")) { 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); if(buf != NULL) { outfit_setProp(tmp, OUTFIT_PROP_WEAP_SPIN); @@ -694,7 +695,7 @@ static void outfit_parseSBeam(Outfit* tmp, const xmlNodePtr parent) { /* Graphics stuff. */ if(xml_isNode(node, "gfx")) { tmp->u.bem.gfx = xml_parseTexture(node, - OUTFIT_GFX"space/%s.png", 1, 1); + OUTFIT_GFX"space/%s.png", 1, 1, 0); continue; } @@ -785,7 +786,8 @@ static void outfit_parseSAmmo(Outfit* tmp, const xmlNodePtr parent) { xmlr_float(node, "energy", tmp->u.amm.energy); if(xml_isNode(node, "gfx")) { 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); if(buf != NULL) { 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) { xmlNodePtr cur, node; char* prop; - char str[PATH_MAX] = "\0"; /* Clear data. */ 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_strd(cur, "description", tmp->description); if(xml_isNode(cur, "gfx_store")) { - snprintf(str, PATH_MAX, OUTFIT_GFX"store/%s.png", xml_get(cur)); - tmp->gfx_store = gl_newImage(str); + tmp->gfx_store = xml_parseTexture(cur, + OUTFIT_GFX"store/%s.png", 1, 1, 0); } - } while((cur = cur->next)); + } while(xml_nextNode(cur)); } else if(xml_isNode(node, "specific")) { /* Has to be processed seperately. */ diff --git a/src/player.c b/src/player.c index e324cb6..78b98e6 100644 --- a/src/player.c +++ b/src/player.c @@ -1536,17 +1536,17 @@ static int gui_parse(const xmlNodePtr parent, const char* name) { ERR("GUI '%s' has no gfx property", name); return -1; } + /* Load gfx. */ - /* Frame. */ snprintf(buf, PATH_MAX, GUI_GFX"%s.png", tmp); - gui.gfx_frame = gl_newImage(buf); + gui.gfx_frame = gl_newImage(buf, 0); /* Pilot. */ 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. */ 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); /* Frame (based on gfx). */ @@ -1595,7 +1595,7 @@ static int gui_parse(const xmlNodePtr parent, const char* name) { tmp = xml_get(cur); if(tmp != NULL) { 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); } @@ -1606,7 +1606,7 @@ static int gui_parse(const xmlNodePtr parent, const char* name) { tmp = xml_get(cur); if(tmp != NULL) { 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); } @@ -1617,7 +1617,7 @@ static int gui_parse(const xmlNodePtr parent, const char* name) { tmp = xml_get(cur); if(tmp != NULL) { 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); } @@ -1627,7 +1627,7 @@ static int gui_parse(const xmlNodePtr parent, const char* name) { tmp = xml_get(cur); if(tmp != NULL) { 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); } diff --git a/src/ship.c b/src/ship.c index 0485822..99dde95 100644 --- a/src/ship.c +++ b/src/ship.c @@ -247,23 +247,24 @@ static int ship_parse(Ship* tmp, xmlNodePtr parent) { /* Load the base graphic. */ 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. */ 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. */ xmlr_attr(node, "target", stmp); if(stmp != NULL) { snprintf(str, PATH_MAX, SHIP_GFX"%s"SHIP_TARGET SHIP_EXT, stmp); - tmp->gfx_target = gl_newImage(str); + tmp->gfx_target = gl_newImage(str, 0); free(stmp); } else { /* Load standard target graphic. */ snprintf(str, PATH_MAX, 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); diff --git a/src/space.c b/src/space.c index 2664716..0e7e66b 100644 --- a/src/space.c +++ b/src/space.c @@ -718,7 +718,7 @@ static int planet_parse(Planet* planet, const xmlNodePtr parent) { do { if(xml_isNode(cur, "space")) { /* Load space gfx. */ 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. */ len = strlen(xml_raw(cur)) + sizeof(PLANET_GFX_EXTERIOR); diff --git a/src/spfx.c b/src/spfx.c index 99886b2..ba462b8 100644 --- a/src/spfx.c +++ b/src/spfx.c @@ -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->ttl = (double)ttl / 1000.; sprintf(buf, SPFX_GFX"%s", gfx); - cur->gfx = gl_newSprite(buf, sx, sy); + cur->gfx = gl_newSprite(buf, sx, sy, 0); return 0; }