summaryrefslogtreecommitdiff
path: root/src/resources
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2011-07-20 02:57:47 +0300
committerAndrei Karas <akaras@inbox.ru>2011-07-20 03:41:35 +0300
commite645959d2dc85ee6a50d9927df3d9f6e374c8317 (patch)
tree6e5c348a7c89c4c864bab299fc9989fc0b0a5791 /src/resources
parentca945c9bd62318109246443c6d43091885e817b1 (diff)
downloadplus-e645959d2dc85ee6a50d9927df3d9f6e374c8317.tar.gz
plus-e645959d2dc85ee6a50d9927df3d9f6e374c8317.tar.bz2
plus-e645959d2dc85ee6a50d9927df3d9f6e374c8317.tar.xz
plus-e645959d2dc85ee6a50d9927df3d9f6e374c8317.zip
Improve perfomance in creation SDL font surfaces.
Diffstat (limited to 'src/resources')
-rw-r--r--src/resources/image.cpp77
-rw-r--r--src/resources/image.h2
2 files changed, 79 insertions, 0 deletions
diff --git a/src/resources/image.cpp b/src/resources/image.cpp
index 29e89d26e..583cf7343 100644
--- a/src/resources/image.cpp
+++ b/src/resources/image.cpp
@@ -189,6 +189,83 @@ Image *Image::load(SDL_Surface *tmpImage)
return _SDLload(tmpImage);
}
+Image *Image::createTextSurface(SDL_Surface *tmpImage, float alpha)
+{
+ if (!tmpImage)
+ return NULL;
+
+ Image *img;
+#ifdef USE_OPENGL
+ if (mUseOpenGL)
+ {
+ img = _GLload(tmpImage);
+ img->setAlpha(alpha);
+ return img;
+ }
+#endif
+
+ bool hasAlpha = false;
+ bool converted = false;
+
+ // The alpha channel to be filled with alpha values
+ Uint8 *alphaChannel = new Uint8[tmpImage->w * tmpImage->h];
+
+ const int sz = tmpImage->w * tmpImage->h;
+ const SDL_PixelFormat * const fmt = tmpImage->format;
+ if (fmt->Amask)
+ {
+ for (int i = 0; i < sz; ++ i)
+ {
+ unsigned v = ((static_cast<Uint32*>(tmpImage->pixels))[i]
+ & fmt->Amask) >> fmt->Ashift;
+ Uint8 a = (v << fmt->Aloss) + (v >> (8 - (fmt->Aloss << 1)));
+
+ Uint8 a2 = static_cast<Uint8>(static_cast<float>(a) * alpha);
+
+ (static_cast<Uint32*>(tmpImage->pixels))[i] &= ~fmt->Amask;
+ (static_cast<Uint32*>(tmpImage->pixels))[i] |=
+ ((a2 >> fmt->Aloss) << fmt->Ashift & fmt->Amask);
+
+ if (a != 255)
+ hasAlpha = true;
+
+ alphaChannel[i] = a;
+
+ }
+ }
+
+ SDL_Surface *image;
+
+ // Convert the surface to the current display format
+ if (hasAlpha)
+ {
+ image = SDL_DisplayFormatAlpha(tmpImage);
+ }
+ else
+ {
+ image = SDL_DisplayFormat(tmpImage);
+
+ // We also delete the alpha channel since
+ // it's not used.
+ delete[] alphaChannel;
+ alphaChannel = 0;
+ }
+
+ if (!image)
+ {
+ logger->log1("Error: Image convert failed.");
+ delete[] alphaChannel;
+ return 0;
+ }
+
+ if (converted)
+ SDL_FreeSurface(tmpImage);
+
+ img = new Image(image, hasAlpha, alphaChannel);
+ img->mAlpha = alpha;
+ return img;
+}
+
void Image::SDLCleanCache()
{
ResourceManager *resman = ResourceManager::getInstance();
diff --git a/src/resources/image.h b/src/resources/image.h
index 718301f81..9ac2b163e 100644
--- a/src/resources/image.h
+++ b/src/resources/image.h
@@ -95,6 +95,8 @@ class Image : public Resource
static SDL_Surface* convertTo32Bit(SDL_Surface* tmpImage);
+ static Image *createTextSurface(SDL_Surface *tmpImage, float alpha);
+
/**
* Frees the resources created by SDL.
*/