#include <iostream>
#include <cmath>
-#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 "../special/sprite.h"
+#include "../video/drawing_context.h"
+
+using namespace SuperTux;
Sprite::Sprite(lisp_object_t* cur)
{
- init_defaults();
+ 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()
+{
+ 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;
+ }
+}
+
+void
+Sprite::parse_action(LispReader& lispreader)
+{
+ action = new Action;
- LispReader reader(cur);
+ init_defaults(action);
- if(!reader.read_string("name", name))
- st_abort("Sprite wihtout name", "");
- reader.read_int("x-hotspot", x_hotspot);
- reader.read_int("y-hotspot", y_hotspot);
- reader.read_float("fps", fps);
+ 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(!reader.read_string_vector("images", images))
- st_abort("Sprite contains no images: ", name.c_str());
+ 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)
{
- surfaces.push_back(
+ action->surfaces.push_back(
new Surface(datadir + "/images/" + images[i], true));
}
- frame_delay = 1000.0f/fps;
+ actions[action->name] = action;
}
-Sprite::~Sprite()
+void
+Sprite::init_defaults(Action* act)
+{
+ act->x_hotspot = 0;
+ act->y_hotspot = 0;
+ act->fps = 10;
+
+ animation_loops = -1;
+ last_tick = 0;
+}
+
+void
+Sprite::set_action(std::string act)
{
- for(std::vector<Surface*>::iterator i = surfaces.begin(); i != surfaces.end();
- ++i)
- delete *i;
+Actions::iterator i = actions.find(act);
+action = i->second;
}
void
-Sprite::init_defaults()
+Sprite::start_animation(int loops)
{
- x_hotspot = 0;
- y_hotspot = 0;
- fps = 10;
- time = 0;
- frame_delay = 1000.0f/fps;
+animation_loops = loops;
+reset();
}
void
-Sprite::update(float /*delta*/)
+Sprite::reset()
+{
+frame = 0;
+last_tick = SDL_GetTicks();
+animation_reversed = false;
+}
+
+bool
+Sprite::check_animation()
{
- //time += 10*delta;
- //std::cout << "Delta: " << delta << std::endl;
+return animation_loops;
}
void
-Sprite::draw(DrawingContext& context, const Vector& pos, int layer,
- Uint32 drawing_effect)
+Sprite::reverse_animation()
{
- time = SDL_GetTicks();
- unsigned int frame = get_current_frame();
+animation_reversed = !animation_reversed;
- if (frame < surfaces.size())
+if(animation_reversed)
+ frame = get_frames()-1;
+else
+ frame = 0;
+}
+
+void
+Sprite::update()
+{
+if(animation_loops == 0)
+ return;
+
+float inc_frame = (action->fps/1000) * (SDL_GetTicks() - last_tick);
+
+if(animation_reversed)
+ frame -= inc_frame;
+else
+ frame += inc_frame;
+
+last_tick = SDL_GetTicks();
+
+if(!animation_reversed)
{
- Surface* surface = surfaces[frame];
-
- context.draw_surface(surface, pos - Vector(x_hotspot, y_hotspot), layer, drawing_effect);
+ if((unsigned int)frame >= action->surfaces.size())
+ {
+ frame = 0;
+ if(animation_loops > 0)
+ animation_loops--;
+ }
+ }
+else
+ {
+ if((unsigned int)frame < 0)
+ {
+ frame = get_frames()-1;
+ if(animation_loops > 0)
+ animation_loops--;
+ }
}
}
+void
+Sprite::draw(DrawingContext& context, const Vector& pos, int layer,
+ Uint32 drawing_effect)
+{
+ update();
+
+ if((int)frame >= get_frames())
+ std::cerr << "Warning: frame higher than total frames!\n";
+ else
+ context.draw_surface(action->surfaces[(int)frame],
+ pos - Vector(action->x_hotspot, action->y_hotspot), layer, drawing_effect);
+}
+
#if 0
void
Sprite::draw_part(float sx, float sy, float x, float y, float w, float h)
}
#endif
-void
-Sprite::reset()
-{
- time = 0;
-}
-
-int
-Sprite::get_current_frame() const
-{
- unsigned int frame = static_cast<int>(fmodf(time, surfaces.size()*frame_delay)/frame_delay);
- return frame % surfaces.size();
-}
-
int
-Sprite::get_width() const
+Sprite::get_width()
{
- return surfaces[get_current_frame()]->w;
+ return action->surfaces[get_frame()]->w;
}
int
-Sprite::get_height() const
+Sprite::get_height()
{
- return surfaces[get_current_frame()]->h;
+ return action->surfaces[get_frame()]->h;
}
/* EOF */