Removed a few things from svn:ignore that are no longer needed to to switch to Cmake
[supertux.git] / src / video / sdl_renderer.cpp
index 90ee2a1..dede6c8 100644 (file)
 #include "texture_manager.hpp"
 #include "obstack/obstackpp.hpp"
 
+namespace
+{
+  SDL_Surface *apply_alpha(SDL_Surface *src, float alpha_factor)
+  {
+    // FIXME: This is really slow
+    assert(src->format->Amask);
+    int alpha = (int) (alpha_factor * 256);
+    SDL_Surface *dst = SDL_CreateRGBSurface(src->flags, src->w, src->h, src->format->BitsPerPixel, src->format->Rmask,  src->format->Gmask, src->format->Bmask, src->format->Amask);
+    int bpp = dst->format->BytesPerPixel;
+    if(SDL_MUSTLOCK(src))
+    {
+      SDL_LockSurface(src);
+    }
+    if(SDL_MUSTLOCK(dst))
+    {
+      SDL_LockSurface(dst);
+    }
+    for(int y = 0;y < dst->h;y++) {
+      for(int x = 0;x < dst->w;x++) {
+        Uint8 *srcpixel = (Uint8 *) src->pixels + y * src->pitch + x * bpp;
+        Uint8 *dstpixel = (Uint8 *) dst->pixels + y * dst->pitch + x * bpp;
+        Uint32 mapped = 0;
+        switch(bpp) {
+          case 1:
+            mapped = *srcpixel;
+            break;
+          case 2:
+            mapped = *(Uint16 *)srcpixel;
+            break;
+          case 3:
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+            mapped |= srcpixel[0] << 16;
+            mapped |= srcpixel[1] << 8;
+            mapped |= srcpixel[2] << 0;
+#else
+            mapped |= srcpixel[0] << 0;
+            mapped |= srcpixel[1] << 8;
+            mapped |= srcpixel[2] << 16;
+#endif
+            break;
+          case 4:
+            mapped = *(Uint32 *)srcpixel;
+            break;
+        }
+        Uint8 r, g, b, a;
+        SDL_GetRGBA(mapped, src->format, &r, &g, &b, &a);
+        mapped = SDL_MapRGBA(dst->format, r, g, b, (a * alpha) >> 8);
+        switch(bpp) {
+          case 1:
+            *dstpixel = mapped;
+            break;
+          case 2:
+            *(Uint16 *)dstpixel = mapped;
+            break;
+          case 3:
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+            dstpixel[0] = (mapped >> 16) & 0xff;
+            dstpixel[1] = (mapped >> 8) & 0xff;
+            dstpixel[2] = (mapped >> 0) & 0xff;
+#else
+            dstpixel[0] = (mapped >> 0) & 0xff;
+            dstpixel[1] = (mapped >> 8) & 0xff;
+            dstpixel[2] = (mapped >> 16) & 0xff;
+#endif
+            break;
+          case 4:
+            *(Uint32 *)dstpixel = mapped;
+            break;
+        }
+      }
+    }
+    if(SDL_MUSTLOCK(dst))
+    {
+      SDL_UnlockSurface(dst);
+    }
+    if(SDL_MUSTLOCK(src))
+    {
+      SDL_UnlockSurface(src);
+    }
+    return dst;
+  }
+}
+
 namespace SDL
 {
   Renderer::Renderer()
@@ -116,7 +199,47 @@ namespace SDL
     dst_rect.x = (int) request.pos.x * numerator / denominator;
     dst_rect.y = (int) request.pos.y * numerator / denominator;
 
+    Uint8 alpha = 0;
+    if(request.alpha != 1.0)
+    {
+      if(!transform->format->Amask)
+      {
+        if(transform->flags & SDL_SRCALPHA)
+        {
+          alpha = transform->format->alpha;
+        }
+        else
+        {
+          alpha = 255;
+        }
+        SDL_SetAlpha(transform, SDL_SRCALPHA, (Uint8) (request.alpha * alpha));
+      }
+      /*else
+      {
+        transform = apply_alpha(transform, request.alpha);
+      }*/
+    }
+
     SDL_BlitSurface(transform, src_rect, screen, &dst_rect);
+
+    if(request.alpha != 1.0)
+    {
+      if(!transform->format->Amask)
+      {
+        if(alpha == 255)
+        {
+          SDL_SetAlpha(transform, SDL_RLEACCEL, 0);
+        }
+        else
+        {
+          SDL_SetAlpha(transform, SDL_SRCALPHA | SDL_RLEACCEL, alpha);
+        }
+      }
+      /*else
+      {
+        SDL_FreeSurface(transform);
+      }*/
+    }
   }
 
   void
@@ -167,7 +290,47 @@ namespace SDL
     dst_rect.x = (int) request.pos.x * numerator / denominator;
     dst_rect.y = (int) request.pos.y * numerator / denominator;
 
+    Uint8 alpha = 0;
+    if(request.alpha != 1.0)
+    {
+      if(!transform->format->Amask)
+      {
+        if(transform->flags & SDL_SRCALPHA)
+        {
+          alpha = transform->format->alpha;
+        }
+        else
+        {
+          alpha = 255;
+        }
+        SDL_SetAlpha(transform, SDL_SRCALPHA, (Uint8) (request.alpha * alpha));
+      }
+      /*else
+      {
+        transform = apply_alpha(transform, request.alpha);
+      }*/
+    }
+
     SDL_BlitSurface(transform, &src_rect, screen, &dst_rect);
+
+    if(request.alpha != 1.0)
+    {
+      if(!transform->format->Amask)
+      {
+        if(alpha == 255)
+        {
+          SDL_SetAlpha(transform, SDL_RLEACCEL, 0);
+        }
+        else
+        {
+          SDL_SetAlpha(transform, SDL_SRCALPHA | SDL_RLEACCEL, alpha);
+        }
+      }
+      /*else
+      {
+        SDL_FreeSurface(transform);
+      }*/
+    }
   }
 
   void
@@ -198,7 +361,7 @@ namespace SDL
         SDL_Surface *temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
 
         SDL_FillRect(temp, 0, color);
-        SDL_SetAlpha(temp, SDL_SRCALPHA, a);
+        SDL_SetAlpha(temp, SDL_SRCALPHA | SDL_RLEACCEL, a);
         SDL_BlitSurface(temp, 0, screen, &rect);
         SDL_FreeSurface(temp);
       }
@@ -206,15 +369,6 @@ namespace SDL
   }
 
   void
-  Renderer::draw_text(const DrawingRequest& request)
-  {
-    const TextRequest* textrequest = (TextRequest*) request.request_data;
-
-    textrequest->font->draw(this, textrequest->text, request.pos,
-        textrequest->alignment, request.drawing_effect, request.alpha);
-  }
-
-  void
   Renderer::draw_filled_rect(const DrawingRequest& request)
   {
     const FillRectRequest* fillrectrequest
@@ -236,7 +390,7 @@ namespace SDL
       SDL_Surface *temp = SDL_CreateRGBSurface(screen->flags, rect.w, rect.h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
 
       SDL_FillRect(temp, 0, color);
-      SDL_SetAlpha(temp, SDL_SRCALPHA, a);
+      SDL_SetAlpha(temp, SDL_SRCALPHA | SDL_RLEACCEL, a);
       SDL_BlitSurface(temp, 0, screen, &rect);
       SDL_FreeSurface(temp);
     }