Commit 7082dc52 authored by Dennis Payne's avatar Dennis Payne

Add animation back in.

parent 70af0f03
......@@ -174,6 +174,20 @@ void ResourceXMLParser::startElement(const XML_Char *name, const XML_Char **atts
}
}
}
else if (strcmp(name, "animation") == 0)
{
if (_cur_sprite.get())
{
for (int i = 0; atts[i]; i += 2)
{
if (strcmp(atts[i], "speed") == 0)
{
_cur_sprite->_animation_speed = atoi(atts[i + 1]);
break;
}
}
}
}
else if (strcmp(name, "font") == 0)
{
_cur_font = std::shared_ptr<SDL_CL_Font_Impl>(new SDL_CL_Font_Impl);
......
......@@ -200,11 +200,28 @@ int SDL_CL_Sprite::get_width()
bool SDL_CL_Sprite::is_finished()
{
if (_impl.get() && _impl->_animation_speed > 0)
{
if (_impl->_cur_frame + 1 < _impl->frame.size())
return false;
if (_impl->_start_time != -1)
{
int cur_time = SDL_GetTicks();
if (cur_time - _impl->_start_time > _impl->_animation_speed)
return true;
}
return false;
}
return true;
}
void SDL_CL_Sprite::restart()
{
if (_impl.get())
{
_impl->_start_time = -1;
_impl->_cur_frame = 0;
}
}
void SDL_CL_Sprite::set_alpha(float a)
......@@ -220,6 +237,20 @@ void SDL_CL_Sprite::set_play_loop(bool loop)
/************************************************************************/
void SDL_CL_Sprite::update()
{
if (_impl.get() && _impl->_animation_speed > 0)
{
if (_impl->_start_time == -1)
_impl->_start_time = SDL_GetTicks();
else if (_impl->_cur_frame + 1 < _impl->frame.size())
{
int cur_time = SDL_GetTicks();
if (cur_time - _impl->_start_time > _impl->_animation_speed)
{
_impl->_start_time += _impl->_animation_speed;
_impl->_cur_frame++;
}
}
}
}
SDL_Texture *SDL_CL_Font_Impl::get_colored_font(SDL_Renderer *gc, SDL_Color c)
......
......@@ -65,6 +65,8 @@ protected:
class SDL_CL_Sprite_Impl
{
public:
SDL_CL_Sprite_Impl() : _animation_speed(0), _start_time(-1), _cur_frame(0) {}
void add_frame(const std::string &filename, int x = -1, int y = -1, int w = -1, int h = -1);
void load_frames(SDL_Renderer *r);
......@@ -72,6 +74,9 @@ public:
/** Animation speed */
int _animation_speed;
int _cur_frame;
int _start_time;
std::vector<std::shared_ptr<SDL_CL_Image_Impl> > frame;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment