189f60ef9a4f590d8aee1151a26a8c87d91af00d
[supertux.git] / src / sprite / sprite.cpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2004 Ingo Ruhnke <grumbel@gmx.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #include <config.h>
20
21 #include <iostream>
22 #include <cmath>
23 #include <cassert>
24 #include <stdexcept>
25
26 #include "sprite.hpp"
27 #include "video/drawing_context.hpp"
28 #include "msg.hpp"
29
30 Sprite::Sprite(SpriteData& newdata)
31   : data(newdata), frame(0), animation_loops(-1)
32 {
33   action = data.get_action("normal");
34   if(!action)
35     action = data.actions.begin()->second;
36   last_ticks = SDL_GetTicks();
37 }
38
39 Sprite::Sprite(const Sprite& other)
40   : data(other.data), frame(other.frame),
41     animation_loops(other.animation_loops),
42     action(other.action)
43 {
44   last_ticks = SDL_GetTicks();
45 }
46
47 Sprite::~Sprite()
48 {
49 }
50
51 void
52 Sprite::set_action(const std::string& name, int loops)
53 {
54   if(action && action->name == name)
55     return;
56
57   SpriteData::Action* newaction = data.get_action(name);
58   if(!newaction) {
59     msg_debug << "Action '" << name << "' not found." << std::endl;
60     return;
61   }
62
63   action = newaction;
64   animation_loops = loops;
65   frame = 0;
66 }
67
68 bool
69 Sprite::animation_done()
70 {
71   return animation_loops == 0;
72 }
73
74 void
75 Sprite::update()
76 {
77   if(animation_done())
78     return;
79
80   Uint32 ticks = SDL_GetTicks();
81   float frame_inc = action->fps * float(ticks - last_ticks)/1000.0;
82   last_ticks = ticks;
83
84   frame += frame_inc;
85
86   if(frame >= get_frames()) {
87     frame = fmodf(frame, get_frames());
88       
89     animation_loops--;
90     if(animation_done())
91       frame = get_frames()-1;
92   }
93 }
94
95 void
96 Sprite::draw(DrawingContext& context, const Vector& pos, int layer)
97 {
98   assert(action != 0);
99   update();
100
101   if((int)frame >= get_frames() || (int)frame < 0)
102     msg_warning << "frame out of range: " << (int)frame << "/" << get_frames() << " at " << get_name() << "/" << get_action_name() << std::endl;
103   else
104     context.draw_surface(action->surfaces[(int)frame],
105             pos - Vector(action->x_offset, action->y_offset),
106             layer + action->z_order);
107 }
108
109 void
110 Sprite::draw_part(DrawingContext& context, const Vector& source,
111     const Vector& size, const Vector& pos, int layer)
112 {
113   assert(action != 0);
114   update();
115
116   if((int)frame >= get_frames() || (int)frame < 0)
117     msg_warning << "frame out of range: " << (int)frame << "/" << get_frames() << " at sprite: " << get_name() << "/" << get_action_name() << std::endl;
118   else
119     context.draw_surface_part(action->surfaces[(int)frame], source, size,
120             pos - Vector(action->x_offset, action->y_offset),
121             layer + action->z_order);
122 }
123
124 int
125 Sprite::get_width() const
126 {
127   return (int) action->surfaces[get_frame()]->get_width();
128 }
129
130 int
131 Sprite::get_height() const
132 {
133   return (int) action->surfaces[get_frame()]->get_height();
134 }
135
136 void
137 Sprite::set_fps(float new_fps)
138 {
139   action->fps = new_fps;
140 }
141
142