1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-22 11:46:14 +02:00

Fix issues with loading images with alpha disabled.

These issues are fixed:
* loadPNG gave RGBA surfaces unconditionally. Now it gives RGB surfaces if
  its second parameter, defaulting to true, is false. This fixes adding per-
  surface alpha values, because an RGBA surface ignores its per-surface value.
* Surface::loadImage is updated to receive a third parameter, defaulting to
  true, to determine whether alpha is loaded.
* SurfaceCollection::defaultAlpha was never used.
* SurfaceCollection::defaultAlpha defaulted to false, so even if it were used,
  it would have loaded images without alpha.
This commit is contained in:
Nebuleon Fumika 2014-07-13 20:47:45 +00:00 committed by Maarten ter Huurne
parent bb7a30d697
commit 7284104fd1
6 changed files with 9 additions and 9 deletions

View File

@ -24,7 +24,7 @@ static void __readFromOpk(png_structp png_ptr, png_bytep ptr, png_size_t length)
}
#endif
SDL_Surface *loadPNG(const std::string &path) {
SDL_Surface *loadPNG(const std::string &path, bool loadAlpha) {
// Declare these with function scope and initialize them to NULL,
// so we can use a single cleanup block at the end of the function.
SDL_Surface *surface = NULL;
@ -134,10 +134,10 @@ SDL_Surface *loadPNG(const std::string &path) {
goto cleanup;
}
// Allocate ARGB surface to hold the image.
// Allocate [A]RGB surface to hold the image.
surface = SDL_CreateRGBSurface(
SDL_SWSURFACE | SDL_SRCALPHA, width, height, 32,
0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000
0x00FF0000, 0x0000FF00, 0x000000FF, loadAlpha ? 0xFF000000 : 0x00000000
);
if (!surface) {
// Failed to create surface, probably out of memory.

View File

@ -7,6 +7,6 @@ struct SDL_Surface;
/** Loads an image from a PNG file into a newly allocated 32bpp RGBA surface.
*/
SDL_Surface *loadPNG(const std::string &path);
SDL_Surface *loadPNG(const std::string &path, bool loadAlpha = true);
#endif

View File

@ -55,14 +55,14 @@ Surface *Surface::emptySurface(int width, int height) {
return new Surface(raw, true);
}
Surface *Surface::loadImage(const string &img, const string &skin) {
Surface *Surface::loadImage(const string &img, const string &skin, bool loadAlpha) {
string skinpath;
if (!skin.empty() && !img.empty() && img[0]!='/')
skinpath = SurfaceCollection::getSkinFilePath(skin, img);
else
skinpath = img;
SDL_Surface *raw = loadPNG(skinpath);
SDL_Surface *raw = loadPNG(skinpath, loadAlpha);
if (!raw) {
ERROR("Couldn't load surface '%s'\n", img.c_str());
return NULL;

View File

@ -41,7 +41,7 @@ public:
static Surface *openOutputSurface(int width, int height, int bitsperpixel);
static Surface *emptySurface(int width, int height);
static Surface *loadImage(const std::string &img,
const std::string &skin="");
const std::string &skin="", bool loadAlpha=true);
Surface(Surface *s);
~Surface();

View File

@ -119,7 +119,7 @@ Surface *SurfaceCollection::add(const string &path) {
}
DEBUG("Adding surface: '%s'\n", path.c_str());
Surface *s = Surface::loadImage(filePath);
Surface *s = Surface::loadImage(filePath, "", defaultAlpha);
if (s != NULL) {
surfaces[path] = s;
}

View File

@ -42,7 +42,7 @@ public:
static std::string getSkinFilePath(const std::string &skin, const std::string &file, bool useDefault = true);
static std::string getSkinPath(const std::string &skin);
bool defaultAlpha;
bool defaultAlpha = true;
void debug();
Surface *add(Surface *s, const std::string &path);