some cleanups in the sprite class, increased delta for collision response
[supertux.git] / lib / special / sprite.cpp
index be2b81f..18c6067 100644 (file)
 //  You should have received a copy of the GNU General Public License
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+#include <config.h>
 
 #include <iostream>
 #include <cmath>
+#include <cassert>
+#include <stdexcept>
 
-#include "../app/globals.h"
-#include "../app/setup.h"
-#include "../special/sprite.h"
-#include "../video/drawing_context.h"
+#include "app/globals.h"
+#include "app/setup.h"
+#include "sprite.h"
+#include "video/drawing_context.h"
 
-using namespace SuperTux;
-
-Sprite::Sprite(lisp_object_t* cur)
+namespace SuperTux
 {
-  for(; !lisp_nil_p(cur); cur = lisp_cdr(cur))
-    {
-    std::string token = lisp_symbol(lisp_car(lisp_car(cur)));
-    lisp_object_t* data = lisp_car(lisp_cdr(lisp_car(cur)));
-    LispReader reader(lisp_cdr(lisp_car(cur)));
-
-    if(token == "name")
-      name = lisp_string(data);
-    else if(token == "action")
-      parse_action(reader);
-    }
-
-  if(name.empty())
-    Termination::abort("Error: Sprite wihtout name.", "");
-  if(actions.empty())
-    Termination::abort("Error: Sprite wihtout actions.", "");
-}
 
-Sprite::~Sprite()
+Sprite::Sprite(SpriteData& newdata)
+  : data(newdata), frame(0), animation_loops(-1)
 {
-  for(Actions::iterator i_act = actions.begin(); i_act != actions.end(); ++i_act)
-    {
-    for(std::vector<Surface*>::iterator i_sur = i_act->second->surfaces.begin();
-        i_sur != i_act->second->surfaces.end(); ++i_sur)
-      delete *i_sur;
-    delete i_act->second;
-    }
+  action = data.actions.begin()->second;
+  last_ticks = SDL_GetTicks();
 }
 
-void
-Sprite::parse_action(LispReader& lispreader)
+Sprite::Sprite(const Sprite& other)
+  : data(other.data), frame(other.frame),
+    animation_loops(other.animation_loops),
+    action(other.action)
 {
-  action = new Action;
-
-  init_defaults(action);
-
-  if(!lispreader.read_string("name", action->name))
-    if(!actions.empty())
-      Termination::abort("Error: If there are more than one action, they need names!", "");
-  lispreader.read_int("x-hotspot", action->x_hotspot);
-  lispreader.read_int("y-hotspot", action->y_hotspot);
-  lispreader.read_float("fps",     action->fps);
-
-  std::vector<std::string> images;
-  if(!lispreader.read_string_vector("images", images))
-    Termination::abort("Sprite contains no images: ", action->name.c_str());
-
-  for(std::vector<std::string>::size_type i = 0; i < images.size(); ++i)
-    {
-      action->surfaces.push_back(
-          new Surface(datadir + "/images/" + images[i], true));
-    }        
-
-  actions[action->name] = action;
+  last_ticks = SDL_GetTicks();
 }
 
-void
-Sprite::init_defaults(Action* act)
+Sprite::~Sprite()
 {
-  act->x_hotspot = 0;
-  act->y_hotspot = 0;
-  act->fps = 10;
-
-  start_animation(-1);
 }
 
 void
-Sprite::set_action(std::string act)
+Sprite::set_action(std::string name, int loops)
 {
-Actions::iterator i = actions.find(act);
-action = i->second;
-}
+  if(action && action->name == name)
+    return;
 
-void
-Sprite::start_animation(int loops, std::string next_act)
-{
-reset();
-animation_loops = loops;
-if(!next_act.empty())
-  next_action = next_act;
-}
+  SpriteData::Action* newaction = data.get_action(name);
+  if(!action) {
+#ifdef DEBUG
+    std::cerr << "Action '" << name << "' not found.\n";
+#endif
+    return;
+  }
 
-void
-Sprite::reset()
-{
-frame = 0;
-last_tick = SDL_GetTicks();
-animation_reversed = true;
-next_action.clear();
+  action = newaction;
+  animation_loops = loops;
+  frame = 0;
 }
 
 bool
 Sprite::check_animation()
 {
-return animation_loops;
-}
-
-void
-Sprite::reverse_animation()
-{
-animation_reversed = !animation_reversed;
-
-if(animation_reversed)
-  frame = get_frames()-1;
-else
-  frame = 0;
+  return animation_loops;
 }
 
 void
 Sprite::update()
 {
-if(animation_loops == 0)
-  return;
+  if(animation_loops == 0)
+    return;
 
-float frame_inc = (action->fps/1000.0) * (SDL_GetTicks() - last_tick);
+  Uint32 ticks = SDL_GetTicks();
+  float frame_inc = action->fps * float(ticks - last_ticks)/1000.0;
+  last_ticks = ticks;
 
-if(animation_reversed)
-  frame -= frame_inc;
-else
   frame += frame_inc;
 
-last_tick = SDL_GetTicks();
-
-if(animation_reversed)
-  {
-  float excedent = frame - 0;
-  if(excedent < 0 || excedent >= get_frames())
-    {  // last case can happen when not used reverse_animation()
-    frame = get_frames() - 1;
-    if(animation_loops > 0)
-      {
-      animation_loops--;
-      if(animation_loops == 0)
-        {
-        set_action(next_action);
-        start_animation(-1);
-        }
-      }
-
-    if(fabsf(excedent) < get_frames())
-      frame += excedent;
-    }
-  }
-else
-  {
-  float excedent = frame - action->surfaces.size();
-  if(excedent >= 0)
-    {
-    frame = 0;
-    if(animation_loops > 0)
-      {
+  float lastframe = frame;
+  frame = fmodf(frame+get_frames(), get_frames());
+  if(frame != lastframe) {
+    if(animation_loops > 0) {
       animation_loops--;
       if(animation_loops == 0)
-        {
-        set_action(next_action);
-        start_animation(-1);
-        }
-      }
-
-    if(excedent < get_frames())
-      frame += excedent;
+        frame = 0;
     }
   }
 }
@@ -198,39 +102,48 @@ void
 Sprite::draw(DrawingContext& context, const Vector& pos, int layer,
     Uint32 drawing_effect)
 {
+  assert(action != 0);
   update();
 
   if((int)frame >= get_frames() || (int)frame < 0)
     std::cerr << "Warning: frame out of range: " << (int)frame
-              << "/" << get_frames() << " at sprite: " << get_name()
+              << "/" << get_frames() << " at " << get_name()
               << "/" << get_action_name() << std::endl;
   else
     context.draw_surface(action->surfaces[(int)frame],
-            pos - Vector(action->x_hotspot, action->y_hotspot), layer, drawing_effect);
+            pos - Vector(action->x_offset, action->y_offset),
+            layer + action->z_order, drawing_effect);
 }
 
-#if 0
 void
-Sprite::draw_part(float sx, float sy, float x, float y, float w, float h)
+Sprite::draw_part(DrawingContext& context, const Vector& source,
+    const Vector& size, const Vector& pos, int layer, Uint32 drawing_effect)
 {
-  time = SDL_GetTicks();
-  unsigned int frame = get_current_frame();
+  assert(action != 0);
+  update();
 
-  if (frame < surfaces.size())
-    surfaces[frame]->draw_part(sx, sy, x - x_hotspot, y - y_hotspot, w, h);
+  if((int)frame >= get_frames() || (int)frame < 0)
+    std::cerr << "Warning: frame out of range: " << (int)frame
+              << "/" << get_frames() << " at sprite: " << get_name()
+              << "/" << get_action_name() << std::endl;
+  else
+    context.draw_surface_part(action->surfaces[(int)frame], source, size,
+            pos - Vector(action->x_offset, action->y_offset),
+            layer + action->z_order, drawing_effect);
 }
-#endif
 
 int
-Sprite::get_width()
+Sprite::get_width() const
 {
   return action->surfaces[get_frame()]->w;
 }
 
 int
-Sprite::get_height()
+Sprite::get_height() const
 {
   return action->surfaces[get_frame()]->h;
 }
 
+}
+
 /* EOF */