pingus-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Pingus-CVS] r2921 - in branches/pingus_sdl: . src src/worldobjs


From: grumbel at BerliOS
Subject: [Pingus-CVS] r2921 - in branches/pingus_sdl: . src src/worldobjs
Date: Wed, 15 Aug 2007 02:38:04 +0200

Author: grumbel
Date: 2007-08-15 02:38:00 +0200 (Wed, 15 Aug 2007)
New Revision: 2921

Modified:
   branches/pingus_sdl/TODO
   branches/pingus_sdl/src/blitter.cpp
   branches/pingus_sdl/src/blitter.hpp
   branches/pingus_sdl/src/sprite.cpp
   branches/pingus_sdl/src/sprite.hpp
   branches/pingus_sdl/src/worldobjs/surface_background.cpp
Log:
- implemented background dimming
- removed some unused code

Modified: branches/pingus_sdl/TODO
===================================================================
--- branches/pingus_sdl/TODO    2007-08-14 22:06:19 UTC (rev 2920)
+++ branches/pingus_sdl/TODO    2007-08-15 00:38:00 UTC (rev 2921)
@@ -39,15 +39,15 @@
 
 - file parsing easily crashes or asserts on incorrect files
 
+- GroundMap tiles are currently RGBA, while in reality they are just
+  RGB with a colorkey, room for optimization
+
 Roadmap for Pingus 0.7.0
 ~~~~~~~~~~~~~~~~~~~~~~~~
 
 Important:
 ==========
 
-- surface-background dimming isn't implemented (i.e. a transparent
-  fillrect over the background surface to change its color)
-
 - update INSTALL.* file
 
 - remove all the remaining unneeded debuging std::cout's

Modified: branches/pingus_sdl/src/blitter.cpp
===================================================================
--- branches/pingus_sdl/src/blitter.cpp 2007-08-14 22:06:19 UTC (rev 2920)
+++ branches/pingus_sdl/src/blitter.cpp 2007-08-15 00:38:00 UTC (rev 2921)
@@ -33,438 +33,6 @@
 /* Headers needed for i18n / gettext */
 #include "gettext.h"
 
-#if 0
-void
-Blitter::put_surface(PixelBuffer canvas, PixelBuffer provider,
-                    int x, int y)
-{
-  switch(provider.get_format().get_depth())
-    {
-    case  8:
-      put_surface_8bit(canvas, provider, x, y);
-      break;
-    case 32:
-      put_surface_32bit(canvas, provider, x, y);
-      break;
-    default:
-      PingusError::raise("Blitter:put_surface:Unknown color depth: " + 
StringUtil::to_string(provider.get_format().get_depth()));
-      break;
-    }
-}
-
-void
-Blitter::put_surface_8bit(PixelBuffer target, PixelBuffer source,
-                          int x_pos, int y_pos)
-{
-  //std::cout << "8bit blit" << std::endl;
-
-  assert(target.get_format().get_depth() == 32);
-
-  target.lock();
-  source.lock();
-
-  int swidth  = source.get_width();
-  int twidth  = target.get_width();
-
-  int start_x = std::max(0, -x_pos);
-  int start_y = std::max(0, -y_pos);
-
-  int end_x = std::min(swidth,  twidth  - x_pos);
-  int end_y = std::min(source.get_height(), target.get_height() - y_pos);
-
-  if (end_x - start_x <= 0 || end_y - start_y <= 0)
-    return;
-
-  cl_uint8* target_buf = static_cast<cl_uint8*>(target.get_data());
-  cl_uint8* source_buf = static_cast<cl_uint8*>(source.get_data());
-
-  CL_Palette palette = source.get_palette();
-
-  if (source.get_format().has_colorkey())
-    {
-      unsigned int colorkey = source.get_format().get_colorkey();
-
-      for (int y = start_y; y < end_y; ++y)
-        {
-          cl_uint8* tptr = target_buf + 4*((twidth*(y+y_pos)) + x_pos + 
start_x);
-          cl_uint8* sptr = source_buf + swidth*y + start_x;
-
-          for (int x = start_x; x < end_x; ++x)
-            { 
-              if (*sptr != colorkey)
-                {
-                                 if (!CL_Endian::is_system_big())
-                                 {
-                    tptr[0] = 255;
-                    tptr[1] = palette.colors[*sptr].get_blue();
-                    tptr[2] = palette.colors[*sptr].get_green();
-                    tptr[3] = palette.colors[*sptr].get_red();
-                                 }
-                                 else
-                                 {
-                    tptr[3] = 255;
-                    tptr[2] = palette.colors[*sptr].get_blue();
-                    tptr[1] = palette.colors[*sptr].get_green();
-                    tptr[0] = palette.colors[*sptr].get_red();
-                                 }
-                }
-
-              tptr += 4;
-              sptr += 1;
-            }
-        }
-    }
-  else
-    {
-      for (int y = start_y; y < end_y; ++y)
-        {
-          cl_uint8* tptr = target_buf + 4*((twidth*(y+y_pos)) + x_pos + 
start_x);
-          cl_uint8* sptr = source_buf + swidth*y + start_x;
-
-          for (int x = start_x; x < end_x; ++x)
-            {
-              if (!CL_Endian::is_system_big())
-              {
-                tptr[0] = 255;
-                tptr[1] = palette.colors[*sptr].get_blue();
-                tptr[2] = palette.colors[*sptr].get_green();
-                tptr[3] = palette.colors[*sptr].get_red();
-              }
-              else
-              {
-                tptr[3] = 255;
-                tptr[2] = palette.colors[*sptr].get_blue();
-                tptr[1] = palette.colors[*sptr].get_green();
-                tptr[0] = palette.colors[*sptr].get_red();
-              }
-
-              tptr += 4;
-              sptr += 1;
-            }
-        }
-    }
-  
-  source.unlock();
-  target.unlock();
-}
-
-void
-Blitter::put_surface_32bit(PixelBuffer target, PixelBuffer source,
-                          const int x_pos, const int y_pos)
-{
-  //std::cout << "32bit blit" << std::endl;
-
-  target.lock();
-  source.lock();
-
-  int swidth  = source.get_width();
-  int sheight = source.get_height();
-
-  int twidth  = target.get_width();
-  int theight = target.get_height();
-
-  int start_x = std::max(0, -x_pos);
-  int start_y = std::max(0, -y_pos);
-
-  int end_x = std::min(swidth,  twidth  - x_pos);
-  int end_y = std::min(sheight, theight - y_pos);
-
-  if (end_x - start_x <= 0
-      || end_y - start_y <= 0)
-    return;
-
-  /* Benchmarks: 
-   * ===========
-   * 6msec with memcpy
-   * 10msec with uint32
-   * 17msec with uint8
-   */
-      
-  cl_uint8* target_buf = static_cast<cl_uint8*>(target.get_data());
-  cl_uint8* source_buf = static_cast<cl_uint8*>(source.get_data());
-
-  for (int y = start_y; y < end_y; ++y)
-    {
-      int tidx = 4*(twidth * (y_pos + y) + x_pos);
-      int sidx = 4*(swidth * y);
-      
-                       /*
-      if (0)
-        { // Fast but doesn't handle masks
-          memcpy(target_buf + tidx + 4*start_x, source_buf + sidx + 4*start_x, 
-                 sizeof(cl_uint32)*(end_x - start_x));
-        }
-      else if (0)
-        { // doesn't handle masks either, but looks half correct
-          cl_uint8* tptr = target_buf + tidx + 4*start_x;
-          cl_uint8* sptr = source_buf + sidx + 4*start_x;
-
-          for (int x = start_x; x < end_x; ++x)
-            {
-              if (!CL_Endian::is_system_big())
-              {
-                tptr[0] = sptr[3];
-                tptr[1] = sptr[0];
-                tptr[2] = sptr[1];
-                tptr[3] = sptr[2];
-              }
-              else
-              {
-                tptr[3] = sptr[3];
-                tptr[2] = sptr[0];
-                tptr[1] = sptr[1];
-                tptr[0] = sptr[2];
-              }
-
-              tptr += 4;
-              sptr += 4;
-            }
-        }
-      else */
-        {
-          // doesn't handle masks either, but looks half correct
-          cl_uint8* tptr = target_buf + tidx + 4*start_x;
-          cl_uint8* sptr = source_buf + sidx + 4*start_x;
-
-          for (int x = start_x; x < end_x; ++x)
-            {
-              if (!CL_Endian::is_system_big())
-              {
-                                                               float a = 
sptr[0]/255.0f;
-                tptr[0] = Math::mid(0, int((1.0f - a) * tptr[0] + a * 
sptr[0]), 255);
-                tptr[1] = Math::mid(0, int((1.0f - a) * tptr[1] + a * 
sptr[1]), 255);
-                tptr[2] = Math::mid(0, int((1.0f - a) * tptr[2] + a * 
sptr[2]), 255);
-                tptr[3] = Math::mid(0, int((1.0f - a) * tptr[3] + a * 
sptr[3]), 255);
-              }
-              else
-              {
-                float a = sptr[3]/255.0f;
-                tptr[3] = Math::mid(0, int((1.0f - a) * tptr[3] + a * 
sptr[3]), 255);
-                tptr[2] = Math::mid(0, int((1.0f - a) * tptr[2] + a * 
sptr[0]), 255);
-                tptr[1] = Math::mid(0, int((1.0f - a) * tptr[1] + a * 
sptr[1]), 255);
-                tptr[0] = Math::mid(0, int((1.0f - a) * tptr[0] + a * 
sptr[2]), 255);
-              }
-
-              tptr += 4;
-              sptr += 4;
-            }
-        }
-    }
-  
-  source.unlock();
-  target.unlock();
-}
-
-void
-Blitter::fill_rect(PixelBuffer target, const CL_Rect& rect, const Color& color)
-{
-  if (target.get_format().get_depth() != 32
-      && target.get_format().get_depth() != 24)
-    {
-      std::cout << "Blitter::fill_rect: depth must be 32 but is " << 
target.get_format().get_depth() << std::endl;
-      return;
-    }
-
-  target.lock();
-  
-  int twidth  = target.get_width();
-  int swidth  = rect.get_width();
-
-  int start_x = std::max(0, -rect.left);
-  int start_y = std::max(0, -rect.top);
-
-  int end_x = std::min(swidth,  twidth  - rect.left);
-  int end_y = std::min(rect.get_height(), target.get_height() - rect.top);
-
-  if (end_x - start_x <= 0 || end_y - start_y <= 0)
-    return;
-
-  cl_uint8* target_buf = static_cast<cl_uint8*>(target.get_data());
-
-  if (target.get_format().get_depth() == 24)
-    {
-      if (color.get_alpha() == 255)
-        {
-          for (int y = start_y; y < end_y; ++y)
-            {
-              cl_uint8* tptr = target_buf + 3*((twidth*(y + rect.top)) + 
rect.left + start_x);
-
-              for (int x = start_x; x < end_x; ++x)
-                { 
-                  if (!CL_Endian::is_system_big())
-                  {
-                    tptr[0] = color.get_red();
-                    tptr[1] = color.get_green();
-                    tptr[2] = color.get_blue();
-                  }
-                  else
-                  {
-                    tptr[2] = color.get_red();
-                    tptr[1] = color.get_green();
-                    tptr[0] = color.get_blue();
-                  }
-                  tptr += 3;
-                }
-            }
-        }
-      else
-        {
-          float a = color.get_alpha()/255.0f;
-
-          for (int y = start_y; y < end_y; ++y)
-            {
-              cl_uint8* tptr = target_buf + 3*((twidth*(y + rect.top)) + 
rect.left + start_x);
-
-              for (int x = start_x; x < end_x; ++x)
-                { 
-                  if (!CL_Endian::is_system_big())
-                  {
-                    tptr[0] = Math::mid(0, int(((1.0f - a) * (tptr[0])) + a * 
color.get_blue()) , 255); //blue
-                    tptr[1] = Math::mid(0, int(((1.0f - a) * (tptr[1])) + a * 
color.get_green()), 255); //green
-                    tptr[2] = Math::mid(0, int(((1.0f - a) * (tptr[2])) + a * 
color.get_red()), 255); //red
-                  }
-                  else
-                  {
-                    tptr[2] = Math::mid(0, int(((1.0f - a) * (tptr[2])) + a * 
color.get_blue()) , 255); //blue
-                    tptr[1] = Math::mid(0, int(((1.0f - a) * (tptr[1])) + a * 
color.get_green()), 255); //green
-                    tptr[0] = Math::mid(0, int(((1.0f - a) * (tptr[0])) + a * 
color.get_red()), 255); //red
-                  }
-
-                  tptr += 3;
-                }
-            }
-        }
-    }
-  else if (target.get_format().get_depth() == 32)
-    {
-      if (color.get_alpha() == 255)
-        {
-          for (int y = start_y; y < end_y; ++y)
-            {
-              cl_uint8* tptr = target_buf + 4*((twidth*(y + rect.top)) + 
rect.left + start_x);
-
-              for (int x = start_x; x < end_x; ++x)
-                { 
-                  if (!CL_Endian::is_system_big())
-                  {
-                    tptr[0] = 255;
-                    tptr[1] = color.get_blue();
-                    tptr[2] = color.get_green();
-                    tptr[3] = color.get_red();
-                  }
-                  else
-                  {
-                    tptr[3] = 255;
-                    tptr[2] = color.get_blue();
-                    tptr[1] = color.get_green();
-                    tptr[0] = color.get_red();
-                  }
-                  tptr += 4;
-                }
-            }
-        }
-      else
-        {
-          for (int y = start_y; y < end_y; ++y)
-            {
-              cl_uint8* tptr = target_buf + 4*((twidth*(y + rect.top)) + 
rect.left + start_x);
-
-              for (int x = start_x; x < end_x; ++x)
-                { 
-                  float a = color.get_alpha()/255.0f;
-
-                  if (!CL_Endian::is_system_big())
-                  {
-                    tptr[0] = Math::mid(0, int(tptr[0] + a * 
color.get_alpha()), 255);
-                    tptr[1] = Math::mid(0, int((1.0f - a) * tptr[1] + a * 
color.get_blue()) , 255);
-                    tptr[2] = Math::mid(0, int((1.0f - a) * tptr[2] + a * 
color.get_green()), 255);
-                    tptr[3] = Math::mid(0, int((1.0f - a) * tptr[3] + a * 
color.get_red())  , 255);
-                  }
-                  else
-                  {
-                    tptr[3] = Math::mid(0, int(tptr[3] + a * 
color.get_alpha()), 255);
-                    tptr[2] = Math::mid(0, int((1.0f - a) * tptr[2] + a * 
color.get_blue()) , 255);
-                    tptr[1] = Math::mid(0, int((1.0f - a) * tptr[1] + a * 
color.get_green()), 255);
-                    tptr[0] = Math::mid(0, int((1.0f - a) * tptr[0] + a * 
color.get_red())  , 255);
-                  }
-                }
-            }
-        }
-    }
-  
-  target.unlock();
-}
-
-void
-Blitter::clear_canvas(PixelBuffer canvas, Color color)
-{
-  unsigned char* buffer;
-
-  canvas.lock();
-  buffer = static_cast<unsigned char*>(canvas.get_data());
-  memset(buffer, color.color, sizeof(unsigned char) * canvas.get_pitch() * 
canvas.get_height());
-  canvas.unlock();
-}
-
-PixelBuffer
-Blitter::create_canvas(PixelBuffer prov)
-{
-  PixelBuffer canvas(prov.get_width(), prov.get_height(), prov.get_width()*4, 
CL_PixelFormat::rgba8888);
-
-  switch (prov.get_format().get_depth())
-    {
-      // RGB888
-    case 24:
-      {
-       canvas.lock();
-       prov.lock();
-
-       int buffer_size = prov.get_pitch () * prov.get_height ();
-       unsigned char* sbuffer = static_cast<unsigned char*>(prov.get_data ());
-       unsigned char* tbuffer = static_cast<unsigned char*>(canvas.get_data 
());
-
-       for (int si = 0, ti = 0; si < buffer_size; si += 3, ti += 4)
-         {
-           if (!CL_Endian::is_system_big())
-        {
-          tbuffer[ti + 0] = 255; // Alpha
-          tbuffer[ti + 1] = sbuffer[si + 0];
-          tbuffer[ti + 2] = sbuffer[si + 1];
-          tbuffer[ti + 3] = sbuffer[si + 2];
-        }
-        else
-        {
-          tbuffer[ti + 3] = 255; // Alpha
-          tbuffer[ti + 2] = sbuffer[si + 0];
-          tbuffer[ti + 1] = sbuffer[si + 1];
-          tbuffer[ti + 0] = sbuffer[si + 2];
-        }
-         }
-
-       // -FIXME: memory hole
-       prov.unlock();
-       canvas.unlock();
-      }
-      break;
-
-      // RGBA8888
-    case 32:
-      canvas.lock();
-      prov.lock();
-      memcpy(canvas.get_data(), prov.get_data(),
-            sizeof(unsigned char) * prov.get_height() * prov.get_pitch());
-      prov.unlock();
-      canvas.unlock();
-      break;
-
-    default:
-      put_surface(canvas, prov, 0, 0);
-      break;
-    }
-  return canvas;
-}
-#endif
-
 SDL_Surface*
 Blitter::scale_surface(SDL_Surface* surface, int width, int height)
 {

Modified: branches/pingus_sdl/src/blitter.hpp
===================================================================
--- branches/pingus_sdl/src/blitter.hpp 2007-08-14 22:06:19 UTC (rev 2920)
+++ branches/pingus_sdl/src/blitter.hpp 2007-08-15 00:38:00 UTC (rev 2921)
@@ -31,31 +31,7 @@
     this are slower and work. */
 class Blitter
 {
-private:
-  ///
-  static void put_surface_8bit(PixelBuffer target, PixelBuffer source,
-                                   int x, int y);
-  ///
-  static void put_surface_32bit(PixelBuffer target, PixelBuffer source,
-                              int x, int y);
 public:
-  /** Puts a given surface to a given canvas at position x, y. */
-  ////static void put_surface(PixelBuffer target, const CL_Surface& source,
-  ////                   int x, int y);
-
-  /** Puts a given surface provider to a given canvas at position x, y. */
-  static void put_surface(PixelBuffer target, PixelBuffer source,
-                         int x, int y);
-
-  /** Returns a newly allocated canvas. The canvas contains the same
-      image as the given surface provider */
-  static PixelBuffer create_canvas(PixelBuffer );
-
-  /** Sets all pixels of a canvas to zero */
-  static void clear_canvas(PixelBuffer, Color color = Color(0, 0, 0, 0));
-
-  static void fill_rect(PixelBuffer target, const Rect& rect, const Color& 
color);
-
   /** Flip a surface horizontal */
   static PixelBuffer flip_horizontal (PixelBuffer sur);
 

Modified: branches/pingus_sdl/src/sprite.cpp
===================================================================
--- branches/pingus_sdl/src/sprite.cpp  2007-08-14 22:06:19 UTC (rev 2920)
+++ branches/pingus_sdl/src/sprite.cpp  2007-08-15 00:38:00 UTC (rev 2921)
@@ -37,7 +37,9 @@
 
 class SpriteImpl
 {
-public:
+private:
+  friend class Sprite;
+
   SDL_Surface* surface;
 
   Vector2i offset;
@@ -56,6 +58,7 @@
   int frame; 
   int tick_count;
 
+public:
   SpriteImpl()
   {
   }
@@ -369,4 +372,68 @@
     }
 }
 
+void
+Sprite::fill(const Color& color)
+{
+  if (color.a != 0)
+    {
+      boost::shared_ptr<SpriteImpl> new_impl(new SpriteImpl()); 
+
+      SDL_Surface* new_surface = SDL_CreateRGBSurface(SDL_SWSURFACE, 
impl->surface->w, impl->surface->h,
+                                                   32,
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+                                                   0xff000000, 0x00ff0000, 
0x0000ff00, 0x000000ff
+#else
+                                                   0x000000ff, 0x0000ff00, 
0x00ff0000, 0xff000000
+#endif
+                                                   );
+      SDL_BlitSurface(impl->surface, NULL, new_surface, NULL);
+
+      SDL_LockSurface(new_surface);
+      uint8_t* pixels = static_cast<uint8_t*>(new_surface->pixels);
+      int width  = new_surface->w;
+      int height = new_surface->h;
+      int pitch  = new_surface->pitch;
+
+      for(int y = 0; y < height; ++y)
+        for(int x = 0; x < width; ++x)
+          {
+            uint8_t* pixel = pixels + y * pitch + 
new_surface->format->BytesPerPixel * x;
+
+            if (pixel[3])
+              {
+                pixel[0] = ((pixel[0] * (255 - color.a)) + (color.r * 
color.a)) / 255;
+                pixel[1] = ((pixel[1] * (255 - color.a)) + (color.g * 
color.a)) / 255;
+                pixel[2] = ((pixel[2] * (255 - color.a)) + (color.b * 
color.a)) / 255;
+                // alpha stays the same
+              }
+          }
+      SDL_UnlockSurface(new_surface);
+
+      if (impl->surface->format->Amask == 0)
+        new_impl->surface = SDL_DisplayFormat(new_surface);
+      else
+        new_impl->surface = SDL_DisplayFormatAlpha(new_surface);
+
+      SDL_FreeSurface(new_surface);
+
+      new_impl->offset = impl->offset;
+
+      new_impl->frame_pos   = impl->frame_pos;
+      new_impl->frame_size  = impl->frame_size;
+      new_impl->frame_delay = impl->frame_delay;
+
+      new_impl->array = impl->array;
+
+      new_impl->loop            = impl->loop;
+      new_impl->loop_last_cycle = impl->loop_last_cycle;
+      new_impl->finished        = impl->finished;
+
+      new_impl->frame      = impl->frame; 
+      new_impl->tick_count = impl->tick_count;
+
+      impl = new_impl;
+    }
+}
+
 /* EOF */

Modified: branches/pingus_sdl/src/sprite.hpp
===================================================================
--- branches/pingus_sdl/src/sprite.hpp  2007-08-14 22:06:19 UTC (rev 2920)
+++ branches/pingus_sdl/src/sprite.hpp  2007-08-15 00:38:00 UTC (rev 2921)
@@ -30,6 +30,7 @@
 #include <boost/shared_ptr.hpp>
 #include "SDL.h"
 
+class Color;
 class PixelBuffer;
 class SpriteImpl;
 class SpriteDescription;
@@ -65,6 +66,11 @@
       operation. The original Surface will be lost. */
   void scale(int w, int h);
 
+  /** Fills the Sprite with the given color. Transparent areas aren't
+      touched.  This is a destructive operation. The original Surface
+      will be lost. */
+  void fill(const Color& color);
+
 private:
   boost::shared_ptr<SpriteImpl> impl;
 };

Modified: branches/pingus_sdl/src/worldobjs/surface_background.cpp
===================================================================
--- branches/pingus_sdl/src/worldobjs/surface_background.cpp    2007-08-14 
22:06:19 UTC (rev 2920)
+++ branches/pingus_sdl/src/worldobjs/surface_background.cpp    2007-08-15 
00:38:00 UTC (rev 2921)
@@ -62,10 +62,8 @@
 
   Timer timer("Background creation");
 
-  if (color.a > 1.0)
-    std::cout << "Background: Warning dim larger than 1.0 are no longer 
supported" << std::endl;
-
   bg_surface = Resource::load_sprite(desc);
+  bg_surface.fill(color);
 
   // Scaling Code
   if (stretch_x && stretch_y)





reply via email to

[Prev in Thread] Current Thread [Next in Thread]