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