updating Nolok contrib templates
[supertux.git] / lib / special / 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 "app/globals.h"
27 #include "app/setup.h"
28 #include "sprite.h"
29 #include "video/drawing_context.h"
30
31 namespace SuperTux
32 {
33
34 Sprite::Sprite(SpriteData& newdata)
35   : data(newdata), frame(0), animation_loops(-1)
36 {
37   action = data.get_action("normal");
38   if(!action)
39     action = data.actions.begin()->second;
40   last_ticks = SDL_GetTicks();
41 }
42
43 Sprite::Sprite(const Sprite& other)
44   : data(other.data), frame(other.frame),
45     animation_loops(other.animation_loops),
46     action(other.action)
47 {
48   last_ticks = SDL_GetTicks();
49 }
50
51 Sprite::~Sprite()
52 {
53 }
54
55 void
56 Sprite::set_action(std::string name, int loops)
57 {
58   if(action && action->name == name)
59     return;
60
61   SpriteData::Action* newaction = data.get_action(name);
62   if(!newaction) {
63 #ifdef DEBUG
64     std::cerr << "Action '" << name << "' not found.\n";
65 #endif
66     return;
67   }
68
69   action = newaction;
70   animation_loops = loops;
71   frame = 0;
72 }
73
74 bool
75 Sprite::check_animation()
76 {
77   return animation_loops == 0;
78 }
79
80 void
81 Sprite::update()
82 {
83   if(animation_loops == 0)
84     return;
85
86   Uint32 ticks = SDL_GetTicks();
87   float frame_inc = action->fps * float(ticks - last_ticks)/1000.0;
88   last_ticks = ticks;
89
90   frame += frame_inc;
91
92   if(frame >= get_frames()) {
93     frame = fmodf(frame+get_frames(), get_frames());
94     
95     animation_loops--;
96     if(animation_loops == 0)
97       frame = 0;
98   }
99 }
100
101 void
102 Sprite::draw(DrawingContext& context, const Vector& pos, int layer,
103     Uint32 drawing_effect)
104 {
105   assert(action != 0);
106   update();
107
108   if((int)frame >= get_frames() || (int)frame < 0)
109     std::cerr << "Warning: frame out of range: " << (int)frame
110               << "/" << get_frames() << " at " << get_name()
111               << "/" << get_action_name() << std::endl;
112   else
113     context.draw_surface(action->surfaces[(int)frame],
114             pos - Vector(action->x_offset, action->y_offset),
115             layer + action->z_order, drawing_effect);
116 }
117
118 void
119 Sprite::draw_part(DrawingContext& context, const Vector& source,
120     const Vector& size, const Vector& pos, int layer, Uint32 drawing_effect)
121 {
122   assert(action != 0);
123   update();
124
125   if((int)frame >= get_frames() || (int)frame < 0)
126     std::cerr << "Warning: frame out of range: " << (int)frame
127               << "/" << get_frames() << " at sprite: " << get_name()
128               << "/" << get_action_name() << std::endl;
129   else
130     context.draw_surface_part(action->surfaces[(int)frame], source, size,
131             pos - Vector(action->x_offset, action->y_offset),
132             layer + action->z_order, drawing_effect);
133 }
134
135 int
136 Sprite::get_width() const
137 {
138   return action->surfaces[get_frame()]->w;
139 }
140
141 int
142 Sprite::get_height() const
143 {
144   return action->surfaces[get_frame()]->h;
145 }
146
147 }
148