Optimized gradient when top color is the same as the bottom one.
[supertux.git] / src / screen / texture.cpp
index d841754..e92ad61 100644 (file)
@@ -366,28 +366,20 @@ sdl_surface_from_gradient(Color top, Color bottom, int w, int h)
 {
   SDL_Surface* sdl_surface;
 
-  Uint32 rmask, gmask, bmask, amask;
+  sdl_surface = SDL_CreateRGBSurface(screen->flags, w, h,
+                    screen->format->BitsPerPixel, screen->format->Rmask,
+                    screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
 
-  /* SDL interprets each pixel as a 32-bit number, so our masks must depend
-       on the endianness (byte order) of the machine */
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
-    rmask = 0xff000000;
-    gmask = 0x00ff0000;
-    bmask = 0x0000ff00;
-    amask = 0x000000ff;
-#else
-    rmask = 0x000000ff;
-    gmask = 0x0000ff00;
-    bmask = 0x00ff0000;
-    amask = 0xff000000;
-#endif
-
-    sdl_surface = SDL_CreateRGBSurface(screen->flags, w, h,
-                      screen->format->BitsPerPixel, rmask, gmask, bmask, amask);
-
-    if(sdl_surface == NULL)
-        st_abort("Cannot create surface for the gradient", "SURFACE");
+  if(sdl_surface == NULL)
+      st_abort("Cannot create surface for the gradient", "SURFACE");
 
+  if(top == bottom)
+    {
+    SDL_FillRect(sdl_surface, NULL, SDL_MapRGB(sdl_surface->format,
+        top.red, top.green, top.blue));
+    }
+  else
+    {
     float redstep = (float(bottom.red)-float(top.red)) / float(h);
     float greenstep = (float(bottom.green)-float(top.green)) / float(h);
     float bluestep = (float(bottom.blue) - float(top.blue)) / float(h);
@@ -404,6 +396,7 @@ sdl_surface_from_gradient(Color top, Color bottom, int w, int h)
           int(float(top.green) + greenstep * y),
           int(float(top.blue) + bluestep * y)));
       }
+    }
 
   return sdl_surface;
 }