pingus-cvs
[Top][All Lists]
Advanced

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

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


From: grumbel at BerliOS
Subject: [Pingus-CVS] r2938 - in branches/pingus_sdl/src: . worldobjs
Date: Wed, 15 Aug 2007 22:53:39 +0200

Author: grumbel
Date: 2007-08-15 22:53:38 +0200 (Wed, 15 Aug 2007)
New Revision: 2938

Modified:
   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:
- some cleanup in the Sprite class

Modified: branches/pingus_sdl/src/blitter.cpp
===================================================================
--- branches/pingus_sdl/src/blitter.cpp 2007-08-15 19:34:34 UTC (rev 2937)
+++ branches/pingus_sdl/src/blitter.cpp 2007-08-15 20:53:38 UTC (rev 2938)
@@ -214,4 +214,28 @@
   return BlitterImpl::modify(sur, BlitterImpl::transform_rot270_flip());
 }
 
+SDL_Surface*
+Blitter::create_surface_rgba(int w, int h)
+{
+  return SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, w, h, 32,
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+                              0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff
+#else
+                              0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
+#endif
+                              );
+}
+
+SDL_Surface*
+Blitter::create_surface_rgb(int w, int h)
+{
+  return SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 24,
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+                              0xff000000, 0x00ff0000, 0x0000ff00, 0x00000000
+#else
+                              0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000
+#endif
+                              );
+}
+
 /* EOF */

Modified: branches/pingus_sdl/src/blitter.hpp
===================================================================
--- branches/pingus_sdl/src/blitter.hpp 2007-08-15 19:34:34 UTC (rev 2937)
+++ branches/pingus_sdl/src/blitter.hpp 2007-08-15 20:53:38 UTC (rev 2938)
@@ -20,6 +20,7 @@
 #ifndef HEADER_PINGUS_BLITTER_HXX
 #define HEADER_PINGUS_BLITTER_HXX
 
+#include "SDL.h"
 #include "pingus.hpp"
 #include "math/color.hpp"
 #include "math/rect.hpp"
@@ -32,6 +33,9 @@
 class Blitter
 {
 public:
+  static SDL_Surface* create_surface_rgba(int w, int h);
+  static SDL_Surface* create_surface_rgb(int w, int h);
+
   /** 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-15 19:34:34 UTC (rev 2937)
+++ branches/pingus_sdl/src/sprite.cpp  2007-08-15 20:53:38 UTC (rev 2938)
@@ -375,19 +375,64 @@
 void
 Sprite::fill(const Color& color)
 {
-       if (color.a != 0) {
-               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_FillRect(new_surface, NULL, 
SDL_MapRGBA(new_surface->format, color.r, color.g, color.b, color.a));
-               SDL_BlitSurface(new_surface, NULL, this->get_surface(), NULL);
-               SDL_FreeSurface(new_surface);
-       }
+  if (color.a != 0) 
+    {
+      make_single_user();
+
+      // FIXME: Couldn't get this to work with a RGBA surface for some
+      // reason, something to do with tmp format and impl->surface
+      // matching up maybe, anyway with RGB it works and it saves a
+      // little bit of space to
+      SDL_Surface* tmp = Blitter::create_surface_rgb(impl->surface->w, 
impl->surface->h);
+      SDL_FillRect(tmp, NULL, SDL_MapRGBA(tmp->format, color.r, color.g, 
color.b, 255));
+      SDL_SetAlpha(tmp, SDL_SRCALPHA, color.a);
+          
+      SDL_BlitSurface(tmp, NULL, impl->surface, NULL);
+
+      SDL_FreeSurface(tmp);
+    }
 }
 
+void
+Sprite::make_single_user()
+{
+  boost::shared_ptr<SpriteImpl> new_impl(new SpriteImpl());
+  
+  if (impl->surface->format->Amask == 0)
+    new_impl->surface         = Blitter::create_surface_rgb(impl->surface->w, 
impl->surface->h);
+  else
+    new_impl->surface         = Blitter::create_surface_rgba(impl->surface->w, 
impl->surface->h);
+
+  SDL_BlitSurface(impl->surface, NULL, new_impl->surface, NULL);
+
+  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;  
+}
+
+void
+Sprite::optimize()
+{
+  // FIXME: Could add a check to check if the surface is already optimized
+  SDL_Surface* new_surface;
+
+  if (impl->surface->format->Amask == 0)
+    new_surface = SDL_DisplayFormat(impl->surface);
+  else
+    new_surface = SDL_DisplayFormatAlpha(impl->surface);
+  
+  SDL_FreeSurface(impl->surface);
+
+  impl->surface = new_surface;
+}
+
 /* EOF */

Modified: branches/pingus_sdl/src/sprite.hpp
===================================================================
--- branches/pingus_sdl/src/sprite.hpp  2007-08-15 19:34:34 UTC (rev 2937)
+++ branches/pingus_sdl/src/sprite.hpp  2007-08-15 20:53:38 UTC (rev 2938)
@@ -71,6 +71,14 @@
       will be lost. */
   void fill(const Color& color);
 
+  /** Call SDL_DisplayFormatAlpha() or SDL_DisplayFormat() to speed up
+      rendering of this sprite */
+  void optimize();
+
+  /** Duplicate the underlying SDL_Surface to allow manipulation
+      without affecting other references to it */
+  void make_single_user();
+
 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-15 
19:34:34 UTC (rev 2937)
+++ branches/pingus_sdl/src/worldobjs/surface_background.cpp    2007-08-15 
20:53:38 UTC (rev 2938)
@@ -95,6 +95,8 @@
         }
     }
 
+  bg_surface.optimize();
+
   timer.stop();
 }
 





reply via email to

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