pingus-cvs
[Top][All Lists]
Advanced

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

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


From: grumbel at BerliOS
Subject: [Pingus-CVS] r2914 - in branches/pingus_sdl/src: . worldobjs
Date: Tue, 14 Aug 2007 22:19:46 +0200

Author: grumbel
Date: 2007-08-14 22:19:45 +0200 (Tue, 14 Aug 2007)
New Revision: 2914

Modified:
   branches/pingus_sdl/src/credits.cpp
   branches/pingus_sdl/src/result_screen.cpp
   branches/pingus_sdl/src/sprite.cpp
   branches/pingus_sdl/src/sprite.hpp
   branches/pingus_sdl/src/start_screen.cpp
   branches/pingus_sdl/src/worldobjs/surface_background.cpp
Log:
- cleaned up the scaling code (somewhat buggy with animated sprites)

Modified: branches/pingus_sdl/src/credits.cpp
===================================================================
--- branches/pingus_sdl/src/credits.cpp 2007-08-14 20:03:01 UTC (rev 2913)
+++ branches/pingus_sdl/src/credits.cpp 2007-08-14 20:19:45 UTC (rev 2914)
@@ -65,7 +65,7 @@
 {
   fast_scrolling = false;
   background = Resource::load_sprite("core/menu/startscreenbg");
-  background.set_surface(Blitter::scale_surface(background.get_surface(), 
Display::get_width(), Display::get_height()));
+  background.scale(Display::get_width(), Display::get_height());
   pingu = Resource::load_sprite("core/misc/creditpingu");
 
   gui_manager->add(new CreditsOkButton(this), true);

Modified: branches/pingus_sdl/src/result_screen.cpp
===================================================================
--- branches/pingus_sdl/src/result_screen.cpp   2007-08-14 20:03:01 UTC (rev 
2913)
+++ branches/pingus_sdl/src/result_screen.cpp   2007-08-14 20:19:45 UTC (rev 
2914)
@@ -153,13 +153,8 @@
   : result(arg_result)
 {
   background = Resource::load_sprite("core/menu/startscreenbg");
-  if (!(Display::get_width() == 800 && Display::get_height() == 600))
-    {
-      SDL_Surface* s = Blitter::scale_surface(background.get_surface(),
-        Display::get_width(), Display::get_height());
-      background.set_surface(s);
-    }
-       
+  background.scale(Display::get_width(), Display::get_height());
+  
   chalk_pingus.push_back(Resource::load_sprite("core/misc/chalk_pingu1"));
   chalk_pingus.push_back(Resource::load_sprite("core/misc/chalk_pingu2"));
   chalk_pingus.push_back(Resource::load_sprite("core/misc/chalk_pingu3"));

Modified: branches/pingus_sdl/src/sprite.cpp
===================================================================
--- branches/pingus_sdl/src/sprite.cpp  2007-08-14 20:03:01 UTC (rev 2913)
+++ branches/pingus_sdl/src/sprite.cpp  2007-08-14 20:19:45 UTC (rev 2914)
@@ -31,6 +31,7 @@
 #include "math/vector2i.hpp"
 #include "SDL_image.h"
 #include "sprite.hpp"
+#include "blitter.hpp"
 #include "pixel_buffer.hpp"
 #include "sprite_description.hpp"
 
@@ -46,7 +47,6 @@
   int      frame_delay;
 
   Size     array;
-  SpriteDescription* sprite_description;
 
   bool     loop;
   bool     loop_last_cycle;
@@ -56,6 +56,10 @@
   int frame; 
   int tick_count;
 
+  SpriteImpl()
+  {
+  }
+
   SpriteImpl(const SpriteDescription& desc)
     : surface(0),
       finished(false),
@@ -70,9 +74,6 @@
         assert(surface);
       }
 
-    sprite_description = new SpriteDescription();
-    *sprite_description = desc;
-
     frame_pos = desc.frame_pos;
 
     array = desc.array;
@@ -94,7 +95,6 @@
       frame_size(pixelbuffer.get_width(), pixelbuffer.get_height()),
       frame_delay(0),
       array(1,1),
-      sprite_description(NULL),
       loop(true),
       loop_last_cycle(false),
       finished(false),
@@ -115,7 +115,6 @@
   ~SpriteImpl()
   {
     SDL_FreeSurface(surface);
-    delete sprite_description;
   }
 
   void update(float delta)
@@ -175,31 +174,11 @@
   {
     finished = true;
   }
-
-  void set_surface(SDL_Surface* new_surface)
-  {
-    if (surface != new_surface)
-      {
-        SDL_FreeSurface(surface);
-        surface = new_surface;
-
-        if (sprite_description)
-          {
-            frame_size.width  = (sprite_description->frame_size.width  == -1) 
? surface->w : sprite_description->frame_size.width;
-            frame_size.height = (sprite_description->frame_size.height == -1) 
? surface->h : sprite_description->frame_size.height;
-            offset = calc_origin(sprite_description->origin, frame_size) - 
sprite_description->offset;
-          }
-        else
-          {
-            frame_size.width = surface->w;
-            frame_size.height = surface->h;
-          }
-      }
-  }
 };
 
 Sprite::Sprite()
 {
+  
 }
 
 Sprite::Sprite(const PixelBuffer& pixelbuffer)
@@ -328,10 +307,34 @@
 }
 
 void
-Sprite::set_surface(SDL_Surface* surface)
+Sprite::scale(int w, int h)
 {
-  if (impl.get())
-    impl->set_surface(surface);
+  if (impl->frame_size.width != w || impl->frame_size.height != h)
+    {
+      boost::shared_ptr<SpriteImpl> new_impl(new SpriteImpl()); 
+
+      float scale_x = float(w) / float(impl->frame_size.width);
+      float scale_y = float(h) / float(impl->frame_size.height);
+      
+      new_impl->surface = Blitter::scale_surface(impl->surface, 
+                                                 w * impl->array.width,
+                                                 h * impl->array.height);
+      
+      new_impl->offset      = Vector2i(int(impl->offset.x * scale_x),
+                                       int(impl->offset.y * scale_y));
+      new_impl->frame_pos   = Vector2i(int(impl->frame_pos.x * scale_x),
+                                       int(impl->frame_pos.y * scale_y));
+      new_impl->frame_size      = Size(w, h);
+      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 20:03:01 UTC (rev 2913)
+++ branches/pingus_sdl/src/sprite.hpp  2007-08-14 20:19:45 UTC (rev 2914)
@@ -61,6 +61,10 @@
   SDL_Surface* get_surface() const;
   void set_surface(SDL_Surface* surface);
 
+  /** Resizes the Sprite to the given size. This is a destructive
+      operation. The original Surface will be lost. */
+  void scale(int w, int h);
+
 private:
   boost::shared_ptr<SpriteImpl> impl;
 };

Modified: branches/pingus_sdl/src/start_screen.cpp
===================================================================
--- branches/pingus_sdl/src/start_screen.cpp    2007-08-14 20:03:01 UTC (rev 
2913)
+++ branches/pingus_sdl/src/start_screen.cpp    2007-08-14 20:19:45 UTC (rev 
2914)
@@ -135,12 +135,7 @@
   : plf(p)
 {
   background = Resource::load_sprite("core/menu/startscreenbg");
-  if (!(Display::get_width() == 800 && Display::get_height() == 600))
-    {
-      SDL_Surface* s = Blitter::scale_surface(background.get_surface(),
-        Display::get_width(), Display::get_height());
-      background.set_surface(s);
-    }
+  background.scale(Display::get_width(), Display::get_height());
   time_str = GameTime::ticks_to_realtime_string(plf.get_time());
 }
 

Modified: branches/pingus_sdl/src/worldobjs/surface_background.cpp
===================================================================
--- branches/pingus_sdl/src/worldobjs/surface_background.cpp    2007-08-14 
20:03:01 UTC (rev 2913)
+++ branches/pingus_sdl/src/worldobjs/surface_background.cpp    2007-08-14 
20:19:45 UTC (rev 2914)
@@ -60,54 +60,43 @@
 
   reader.read_bool("keep-aspect", keep_aspect);
 
-
   Timer timer("Background creation");
 
   if (color.a > 1.0)
     std::cout << "Background: Warning dim larger than 1.0 are no longer 
supported" << std::endl;
 
-  PixelBuffer canvas = Resource::load_pixelbuffer(desc);
-  SDL_Surface* s = canvas.get_surface();
-  SDL_Surface* new_surface = NULL;
+  bg_surface = Resource::load_sprite(desc);
 
   // Scaling Code
   if (stretch_x && stretch_y)
     {
-      new_surface = Blitter::scale_surface(s, world->get_width(), 
world->get_height());
+      bg_surface.scale(world->get_width(), world->get_height());
     }
   else if (stretch_x && !stretch_y)
     {
       if (keep_aspect)
         {
-          float aspect = canvas.get_height()/float(canvas.get_width());
-          new_surface = Blitter::scale_surface(s,
-            world->get_width(), int(world->get_width()*aspect));
+          float aspect = bg_surface.get_height()/float(bg_surface.get_width());
+          bg_surface.scale(world->get_width(), int(world->get_width()*aspect));
         }
       else
         {
-          new_surface = Blitter::scale_surface(s, canvas.get_width(), 
world->get_height());
+          bg_surface.scale(world->get_width(), bg_surface.get_height());
         }
     }
   else if (!stretch_x && stretch_y)
     {
       if (keep_aspect)
         {
-          float aspect = float(canvas.get_width())/canvas.get_height();
-          new_surface = Blitter::scale_surface(s,
-            int(world->get_height() * aspect), world->get_height());
+          float aspect = float(bg_surface.get_width())/bg_surface.get_height();
+          bg_surface.scale(int(world->get_height() * aspect), 
world->get_height());
         }
       else
         {
-          new_surface = Blitter::scale_surface(s, canvas.get_width(), 
world->get_height());
+          bg_surface.scale(bg_surface.get_width(), world->get_height());
         }
     }
 
-  bg_surface = Sprite(canvas);
-  if (new_surface)
-    {
-      bg_surface.set_surface(new_surface);
-    }
-
   timer.stop();
 }
 





reply via email to

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