pingus-cvs
[Top][All Lists]
Advanced

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

[Pingus-CVS] r2733 - branches/pingus_sdl/src


From: jsalmon3
Subject: [Pingus-CVS] r2733 - branches/pingus_sdl/src
Date: Sat, 14 Jul 2007 19:27:56 +0200

Author: jsalmon3
Date: 2007-07-14 19:27:49 +0200 (Sat, 14 Jul 2007)
New Revision: 2733

Modified:
   branches/pingus_sdl/src/sprite.cpp
   branches/pingus_sdl/src/sprite.hpp
Log:
Implemented sprite looping

Modified: branches/pingus_sdl/src/sprite.cpp
===================================================================
--- branches/pingus_sdl/src/sprite.cpp  2007-07-14 06:27:13 UTC (rev 2732)
+++ branches/pingus_sdl/src/sprite.cpp  2007-07-14 17:27:49 UTC (rev 2733)
@@ -47,6 +47,10 @@
 
   Size     array;
 
+  bool     loop;
+  bool     loop_last_cycle;
+  bool     finished;
+
   /** Current frame */
   int frame; 
   int tick_count;
@@ -77,6 +81,10 @@
 
     array = desc.array;
 
+    loop = desc.loop;
+    loop_last_cycle = false;
+    finished = false;
+
     offset = calc_origin(desc.origin, frame_size) - desc.offset;
   }
 
@@ -108,10 +116,29 @@
 
   void update(float delta)
   {
-      tick_count += int(delta * 1000.0f);
-      tick_count = tick_count % (frame_delay * (array.width * array.height));
-        
-      frame = tick_count / frame_delay;
+    if (finished)
+      return;
+
+    int total_time = frame_delay * (array.width * array.height);
+    tick_count += int(delta * 1000.0f);
+    if (tick_count >= total_time)
+      {
+       if (loop)
+         {
+           loop_last_cycle = true;
+           tick_count = tick_count % total_time;
+           frame = tick_count / frame_delay;
+         }
+       else
+         {
+           finished = true;
+         }
+      }
+    else
+      {
+       loop_last_cycle = false;
+       frame = tick_count / frame_delay;
+      }
   }
 
   void draw(float x, float y, SDL_Surface* dst)
@@ -131,6 +158,19 @@
 
     SDL_BlitSurface(surface, &srcrect, dst, &dstrect);
   }
+
+  void restart()
+  {
+    finished = false;
+    loop_last_cycle = false;
+    frame = 0;
+    tick_count = 0;
+  }
+
+  void finish()
+  {
+    finished = true;
+  }
 };
 
 Sprite::Sprite()
@@ -155,7 +195,6 @@
 void
 Sprite::draw(float x, float y, SDL_Surface* target)
 {
-  //std::cout << "Sprite: draw; " << x << ", " << y << std::endl;
   if (impl.get())
     impl->draw(x, y, target);
 }
@@ -184,7 +223,7 @@
 }
 
 void
-Sprite:: update(float delta)
+Sprite::update(float delta)
 {
   if (impl.get())
     impl->update(delta);
@@ -200,15 +239,37 @@
 int
 Sprite::get_frame_count() const
 {
-  return (impl->array.width * impl->array.height);
+  if (impl.get())
+    return (impl->array.width * impl->array.height);
+  else
+    return 0;
 }
 
 bool
 Sprite::is_finished() const
 {
-  return true;
+  if (impl.get())
+    return impl->finished;
+  else
+    return true;
 }
 
+bool
+Sprite::is_looping() const
+{
+  if (impl.get())
+    return impl->loop_last_cycle;
+  else
+    return false;
+}
+
+void
+Sprite::set_play_loop(bool loop)
+{
+  if (impl.get())
+    impl->loop = loop;
+}
+
 int
 Sprite::get_current_frame() const
 {
@@ -221,7 +282,15 @@
 void
 Sprite::restart()
 {
-  
+  if (impl.get())
+    impl->restart();
 }
 
+void
+Sprite::finish()
+{
+  if (impl.get())
+    impl->finish();
+}
+
 /* EOF */

Modified: branches/pingus_sdl/src/sprite.hpp
===================================================================
--- branches/pingus_sdl/src/sprite.hpp  2007-07-14 06:27:13 UTC (rev 2732)
+++ branches/pingus_sdl/src/sprite.hpp  2007-07-14 17:27:49 UTC (rev 2733)
@@ -53,7 +53,10 @@
   int  get_frame_count() const;
   int  get_current_frame() const;
   bool is_finished() const;
-  void  restart();
+  bool is_looping() const;
+  void set_play_loop(bool loop = true);
+  void restart();
+  void finish();
   operator bool();
 
 private:





reply via email to

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