- Reworked Surface class and drawing stuff:
[supertux.git] / src / sprite / sprite.hpp
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
20 #ifndef SUPERTUX_SPRITE_H
21 #define SUPERTUX_SPRITE_H
22
23 #include <string>
24 #include <assert.h>
25 #include <SDL.h>
26
27 #include "video/surface.hpp"
28 #include "math/vector.hpp"
29 #include "sprite_data.hpp"
30
31 class DrawingContext;
32
33 class Sprite
34 {
35 public:
36   Sprite(SpriteData& data);
37   Sprite(const Sprite& other);
38   ~Sprite();
39
40   /** Draw sprite, automatically calculates next frame */
41   void draw(DrawingContext& context, const Vector& pos, int layer);
42
43   void draw_part(DrawingContext& context, const Vector& source,
44       const Vector& size, const Vector& pos, int layer);
45
46   /** Set action (or state) */
47   void set_action(const std::string& act, int loops = -1);
48
49   /* Stop animation */
50   void stop_animation()
51   { animation_loops = 0; }
52   /** Check if animation is stopped or not */
53   bool check_animation();
54
55   float get_fps() const
56   { return action->fps; }
57   /** Get current action total frames */
58   int get_frames() const
59   { return action->surfaces.size(); }
60   /** Get sprite's name */
61   const std::string& get_name() const
62   { return data.name; }
63   /** Get current action name */
64   const std::string& get_action_name() const
65   { return action->name; }
66
67   int get_width() const;
68   int get_height() const;
69
70   /** Get current frame */
71   int get_frame() const
72   { return (int)frame; }
73   /** Set current frame */
74   void set_frame(int frame_)
75   { if(frame_ > get_frames()) frame = 0; else frame = frame_; }
76   Surface* get_frame(unsigned int frame)
77   {
78     assert(frame < action->surfaces.size());
79     return action->surfaces[frame];
80   }    
81
82 private:
83   void update();
84
85   SpriteData& data;
86
87   float frame;
88   int animation_loops;
89   Uint32 last_ticks;
90
91   SpriteData::Action* action;
92 };
93
94 #endif
95