new Surface(datadir + "/images/" + images[i], true));
}
- action->frame_delay = 1000.0f/action->fps;
-
actions[action->name] = action;
}
act->x_hotspot = 0;
act->y_hotspot = 0;
act->fps = 10;
- act->frame_delay = 1000.0f/act->fps;
- time = 0;
+
+ act->animation_loops = 0;
+ last_tick = 0;
}
void
}
void
-Sprite::update(float /*delta*/)
+Sprite::start_animation(int loops)
{
- //time += 10*delta;
- //std::cout << "Delta: " << delta << std::endl;
+action->animation_loops = loops;
+reset();
}
void
-Sprite::draw(DrawingContext& context, const Vector& pos, int layer,
- Uint32 drawing_effect)
+Sprite::reset()
{
- time = SDL_GetTicks();
- unsigned int frame = get_current_frame();
+frame = 0;
+last_tick = SDL_GetTicks();
+}
- if (frame < action->surfaces.size())
+bool
+Sprite::check_animation()
+{
+return action->animation_loops;
+}
+
+void
+Sprite::update()
+{
+frame += (action->fps/1000) * (SDL_GetTicks() - last_tick);
+last_tick = SDL_GetTicks();
+
+if((unsigned int)frame >= action->surfaces.size())
{
- Surface* surface = action->surfaces[frame];
-
- context.draw_surface(surface, pos - Vector(action->x_hotspot, action->y_hotspot), layer, drawing_effect);
+ frame = 0;
+ if(action->animation_loops > 0)
+ action->animation_loops--;
}
}
+void
+Sprite::draw(DrawingContext& context, const Vector& pos, int layer,
+ Uint32 drawing_effect)
+{
+ update();
+
+ 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, action->surfaces.size()*action->frame_delay)/action->frame_delay);
- return frame % action->surfaces.size();
-}
-
int
-Sprite::get_width() const
+Sprite::get_width()
{
return action->surfaces[get_current_frame()]->w;
}
int
-Sprite::get_height() const
+Sprite::get_height()
{
return action->surfaces[get_current_frame()]->h;
}
/** Frames per second */
float fps;
- /** Number of seconds that a frame is displayed until it is switched
- to the next frame */
- float frame_delay;
+ int animation_loops;
std::vector<Surface*> surfaces;
};
Sprite(lisp_object_t* cur);
~Sprite();
- void reset();
-
- /** Update the sprite and process to the next frame */
- void update(float delta);
+ /** Draw sprite, automatically calculates next frame */
void draw(DrawingContext& context, const Vector& pos, int layer,
Uint32 drawing_effect = NONE_EFFECT);
- int get_current_frame() const;
/** Set action (or state) */
void set_action(std::string act);
+ /* Handling animations */
+ void start_animation(int loops);
+ bool check_animation();
+
float get_fps()
- {
- return action->fps;
- } ;
+ { return action->fps; }
int get_frames()
- {
- return action->surfaces.size();
- } ;
-
+ { return action->surfaces.size(); }
std::string get_name() const
- {
- return name;
- }
- int get_width() const;
- int get_height() const;
+ { return name; }
+ int get_width();
+ int get_height();
+ int get_current_frame()
+ { return (int)frame; }
Surface* get_frame(unsigned int frame)
{
if(frame < action->surfaces.size())
void init_defaults(Action* act);
void parse_action(LispReader& lispreader);
+ void update();
+ void reset();
+
std::string name;
- float time;
+ float frame;
+ float last_tick;
typedef std::map <std::string, Action*> Actions;
Actions actions;