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