Unified Messaging Subsystem
[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.");
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
103               << "/" << get_frames() << " at " << get_name()
104               << "/" << get_action_name());
105   else
106     context.draw_surface(action->surfaces[(int)frame],
107             pos - Vector(action->x_offset, action->y_offset),
108             layer + action->z_order);
109 }
110
111 void
112 Sprite::draw_part(DrawingContext& context, const Vector& source,
113     const Vector& size, const Vector& pos, int layer)
114 {
115   assert(action != 0);
116   update();
117
118   if((int)frame >= get_frames() || (int)frame < 0)
119     msg_warning("frame out of range: " << (int)frame
120               << "/" << get_frames() << " at sprite: " << get_name()
121               << "/" << get_action_name());
122   else
123     context.draw_surface_part(action->surfaces[(int)frame], source, size,
124             pos - Vector(action->x_offset, action->y_offset),
125             layer + action->z_order);
126 }
127
128 int
129 Sprite::get_width() const
130 {
131   return (int) action->surfaces[get_frame()]->get_width();
132 }
133
134 int
135 Sprite::get_height() const
136 {
137   return (int) action->surfaces[get_frame()]->get_height();
138 }
139
140 void
141 Sprite::set_fps(float new_fps)
142 {
143   action->fps = new_fps;
144 }
145
146