Author: alink
Date: Tue Apr 29 04:50:15 2008
New Revision: 26224

URL: http://svn.gna.org/viewcvs/wesnoth?rev=26224&view=rev
Log:
Finish to replace all the remaining slow SDL_*RGBA calls by faster bitwise
operations.

Modified:
    trunk/src/sdl_utils.cpp

Modified: trunk/src/sdl_utils.cpp
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/src/sdl_utils.cpp?rev=26224&r1=26223&r2=26224&view=diff
==============================================================================
--- trunk/src/sdl_utils.cpp (original)
+++ trunk/src/sdl_utils.cpp Tue Apr 29 04:50:15 2008
@@ -564,8 +564,11 @@
                Uint32* end = beg + nsurf->w*surf->h;
 
                while(beg != end) {
-                       Uint8 red, green, blue, alpha;
-                       
SDL_GetRGBA(*beg,nsurf->format,&red,&green,&blue,&alpha);
+                       Uint8 r, g, b, a;
+                       a = (*beg) >> 24;
+                       r = (*beg) >> 16;
+                       g = (*beg) >> 8;
+                       b = (*beg);
 
                        //const Uint8 avg = (red+green+blue)/3;
 
@@ -574,12 +577,11 @@
                        // The correct formula being:
                        // gray=0.299red+0.587green+0.114blue
                        const Uint8 avg = static_cast<Uint8>((
-                               77 * static_cast<Uint16>(red) +
-                               150 * static_cast<Uint16>(green) +
-                               29 * static_cast<Uint16>(blue)) / 256);
-
-
-                       *beg = SDL_MapRGBA(nsurf->format,avg,avg,avg,alpha);
+                               77  * static_cast<Uint16>(r) +
+                               150 * static_cast<Uint16>(g) +
+                               29  * static_cast<Uint16>(b)  ) / 256);
+
+                       *beg = (a << 24) | (avg << 16) | (avg << 8) | avg;
 
                        ++beg;
                }
@@ -605,8 +607,11 @@
                Uint32* end = beg + nsurf->w*surf->h;
 
                while(beg != end) {
-                       Uint8 red, green, blue, alpha;
-                       
SDL_GetRGBA(*beg,nsurf->format,&red,&green,&blue,&alpha);
+                       Uint8 r, g, b, a;
+                       a = (*beg) >> 24;
+                       r = (*beg) >> 16;
+                       g = (*beg) >> 8;
+                       b = (*beg);
 
                        //const Uint8 avg = (red+green+blue)/3;
 
@@ -615,16 +620,16 @@
                        // The correct formula being:
                        // gray=0.299red+0.587green+0.114blue
                        const Uint8 avg = static_cast<Uint8>((
-                               77 * static_cast<Uint16>(red) +
-                               150 * static_cast<Uint16>(green) +
-                               29 * static_cast<Uint16>(blue)) / 256);
+                               77  * static_cast<Uint16>(r) +
+                               150 * static_cast<Uint16>(g) +
+                               29  * static_cast<Uint16>(b)  ) / 256);
                        // then tint 77%, 67%, 72%
-                       const Uint8 r= ((avg * 196) >> 8);
-                       const Uint8 g= ((avg * 171) >> 8);
-                       const Uint8 b= ((avg * 184) >> 8);
-
-
-                       *beg = SDL_MapRGBA(nsurf->format,r,g,b,alpha);
+                       r = ((avg * 196) >> 8);
+                       g = ((avg * 171) >> 8);
+                       b = ((avg * 184) >> 8);
+
+
+                       *beg = (a << 24) | (r << 16) | (g << 8) | b;
 
                        ++beg;
                }
@@ -1153,24 +1158,27 @@
                Uint32* beg = lock.pixels();
                Uint32* end = beg + nsurf->w*surf->h;
 
-               Uint8 red2, green2, blue2, alpha2;
-               SDL_GetRGBA(colour,nsurf->format,&red2,&green2,&blue2,&alpha2);
-
-               red2 = Uint8(red2*amount);
-               green2 = Uint8(green2*amount);
-               blue2 = Uint8(blue2*amount);
+               Uint8 red, green, blue, alpha;
+               SDL_GetRGBA(colour,nsurf->format,&red,&green,&blue,&alpha);
+
+               red   = Uint8(red   * amount);
+               green = Uint8(green * amount);
+               blue  = Uint8(blue  * amount);
 
                amount = 1.0 - amount;
 
                while(beg != end) {
-                       Uint8 red, green, blue, alpha;
-                       
SDL_GetRGBA(*beg,nsurf->format,&red,&green,&blue,&alpha);
-
-                       red = Uint8(red*amount) + red2;
-                       green = Uint8(green*amount) + green2;
-                       blue = Uint8(blue*amount) + blue2;
-
-                       *beg = SDL_MapRGBA(nsurf->format,red,green,blue,alpha);
+                       Uint8 r, g, b, a;
+                       a = (*beg) >> 24;
+                       r = (*beg) >> 16;
+                       g = (*beg) >> 8;
+                       b = (*beg);
+
+                       r = Uint8(r * amount) + red;
+                       g = Uint8(g * amount) + green;
+                       b = Uint8(b * amount) + blue;
+
+                       *beg = (a << 24) | (r << 16) | (g << 8) | b;
 
                        ++beg;
                }
@@ -1470,16 +1478,13 @@
 
 struct not_alpha
 {
-       not_alpha(SDL_PixelFormat& format) : fmt_(format) {}
-
+       not_alpha() {}
+
+       // we assume neutral format
        bool operator()(Uint32 pixel) const {
-               Uint8 r, g, b, a;
-               SDL_GetRGBA(pixel,&fmt_,&r,&g,&b,&a);
-               return a != 0x00;
-       }
-
-private:
-       SDL_PixelFormat& fmt_;
+               Uint8 alpha = pixel >> 24;
+               return alpha != 0x00;
+       }
 };
 
 }
@@ -1493,7 +1498,7 @@
                return res;
        }
 
-       const not_alpha calc(*(nsurf->format));
+       const not_alpha calc;
 
        surface_lock lock(nsurf);
        const Uint32* const pixels = lock.pixels();


_______________________________________________
Wesnoth-commits mailing list
[email protected]
https://mail.gna.org/listinfo/wesnoth-commits

Reply via email to