From b4ec5a83cbb42213aef9874fb62bd0911ccbb5a0 Mon Sep 17 00:00:00 2001
From: Andrei Karas <akaras@inbox.ru>
Date: Sun, 26 May 2013 23:48:03 +0300
Subject: improve temp fix for alpha channel in cached strings in software
 renderer.

---
 src/gui/sdlfont.cpp | 71 +++++++++++++++++------------------------------------
 src/gui/sdlfont.h   |  3 ++-
 2 files changed, 25 insertions(+), 49 deletions(-)

diff --git a/src/gui/sdlfont.cpp b/src/gui/sdlfont.cpp
index 0d285ad1d..d0e1fc043 100644
--- a/src/gui/sdlfont.cpp
+++ b/src/gui/sdlfont.cpp
@@ -44,6 +44,8 @@ const unsigned int CACHE_SIZE_SMALL3 = 170;
 const unsigned int CLEAN_TIME = 7;
 const int OUTLINE_SIZE = 1;
 
+bool SDLFont::mOpengl(true);
+
 char *strBuf;
 
 #ifdef UNITTESTS
@@ -92,6 +94,10 @@ bool SDLTextChunkSmall::operator<(const SDLTextChunkSmall &chunk) const
         return c2.g > color2.g;
     if (c2.b != color2.b)
         return c2.b > color2.b;
+
+    if (c.a != color.a && !SDLFont::mOpengl)
+        return c.a > color.a;
+
     return false;
 }
 
@@ -318,15 +324,18 @@ SDLFont::SDLFont(std::string filename,
     mFont(nullptr),
     mCreateCounter(0),
     mDeleteCounter(0),
-    mOpengl(imageHelper->useOpenGL()),
     mCleanTime(cur_time + CLEAN_TIME)
 {
     const ResourceManager *const resman = ResourceManager::getInstance();
 
-    if (fontCounter == 0 && TTF_Init() == -1)
+    if (fontCounter == 0)
     {
-        throw GCN_EXCEPTION("Unable to initialize SDL_ttf: " +
-            std::string(TTF_GetError()));
+        mOpengl = imageHelper->useOpenGL();
+        if (TTF_Init() == -1)
+        {
+            throw GCN_EXCEPTION("Unable to initialize SDL_ttf: " +
+                std::string(TTF_GetError()));
+        }
     }
 
     if (!fontCounter)
@@ -435,55 +444,21 @@ void SDLFont::drawString(gcn::Graphics *const graphics,
     const unsigned char chr = text[0];
     TextChunkList *const cache = &mCache[chr];
 
-    if (!mOpengl)
+    std::map<SDLTextChunkSmall, SDLTextChunk*> &search = cache->search;
+    std::map<SDLTextChunkSmall, SDLTextChunk*>::iterator i
+        = search.find(SDLTextChunkSmall(text, col, col2));
+    if (i != search.end())
     {
-        bool found(false);
-        SDLTextChunk chunk(text, col, col2);
-        SDLTextChunk *i1 = cache->start;
-        while (i1)
-        {
-            if (*i1 == chunk)
-            {
-                cache->moveToFirst(i1);
-                found = true;
-                break;
-            }
-            i1 = i1->next;
-        }
-
-        if (found)
+        SDLTextChunk *const chunk2 = (*i).second;
+        cache->moveToFirst(chunk2);
+        Image *const image = chunk2->img;
+        if (image)
         {
-            Image *const image = cache->start->img;
-            if (image)
-            {
-                image->setAlpha(alpha);
-                g->drawImage(image, x, y);
-            }
-            BLOCK_END("SDLFont::drawString")
-            return;
+            image->setAlpha(alpha);
+            g->drawImage(image, x, y);
         }
     }
     else
-    {
-        std::map<SDLTextChunkSmall, SDLTextChunk*> &search = cache->search;
-        std::map<SDLTextChunkSmall, SDLTextChunk*>::iterator i
-            = search.find(SDLTextChunkSmall(text, col, col2));
-        if (i != search.end())
-        {
-            SDLTextChunk *const chunk2 = (*i).second;
-
-            cache->moveToFirst(chunk2);
-            Image *const image = chunk2->img;
-            if (image)
-            {
-                image->setAlpha(alpha);
-                g->drawImage(image, x, y);
-            }
-            BLOCK_END("SDLFont::drawString")
-            return;
-        }
-    }
-//    else
     {
         if (cache->size >= CACHE_SIZE)
         {
diff --git a/src/gui/sdlfont.h b/src/gui/sdlfont.h
index 52d617518..30b4d2fa4 100644
--- a/src/gui/sdlfont.h
+++ b/src/gui/sdlfont.h
@@ -151,11 +151,12 @@ class SDLFont final : public gcn::Font
         int getDeleteCounter() const A_WARN_UNUSED
         { return mDeleteCounter; }
 
+        static bool mOpengl;
+
     private:
         TTF_Font *mFont;
         unsigned mCreateCounter;
         unsigned mDeleteCounter;
-        bool mOpengl;
 
         // Word surfaces cache
         int mCleanTime;
-- 
cgit v1.2.3-70-g09d2