Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / supertux / game_object.hpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #ifndef HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_HPP
18 #define HEADER_SUPERTUX_SUPERTUX_GAME_OBJECT_HPP
19
20 #include <string>
21
22 #include "util/refcounter.hpp"
23
24 class DrawingContext;
25 class ObjectRemoveListener;
26
27 /**
28  * Base class for all the things that make up Levels' Sectors.
29  *
30  * Each sector of a level will hold a list of active GameObject while the 
31  * game is played.
32  *
33  * This class is responsible for:
34  *  - Updating and Drawing the object. This should happen in the update() and
35  *    draw() functions. Both are called once per frame.
36  *  - Providing a safe way to remove the object by calling the remove_me
37  *    functions.
38  */
39 class GameObject : public RefCounter
40 {
41 public:
42   GameObject();
43   GameObject(const GameObject& rhs);
44   virtual ~GameObject();
45
46   /** This function is called once per frame and allows the object to update
47    * it's state. The elapsed_time is the time since the last frame in
48    * seconds and should be the base for all timed calculations (don't use
49    * SDL_GetTicks directly as this will fail in pause mode)
50    */
51   virtual void update(float elapsed_time) = 0;
52
53   /** The GameObject should draw itself onto the provided DrawingContext if this
54    * function is called.
55    */
56   virtual void draw(DrawingContext& context) = 0;
57
58   /** returns true if the object is not scheduled to be removed yet */
59   bool is_valid() const
60   {
61     return !wants_to_die;
62   }
63
64   /** schedules this object to be removed at the end of the frame */
65   void remove_me()
66   {
67     wants_to_die = true;
68   }
69
70   /** registers a remove listener which will be called if the object
71    * gets removed/destroyed
72    */
73   void add_remove_listener(ObjectRemoveListener* listener);
74   
75   /** 
76    * unregisters a remove listener, so it will no longer be called if the object
77    * gets removed/destroyed
78    */
79   void del_remove_listener(ObjectRemoveListener* listener);
80
81   const std::string& get_name() const
82   {
83     return name;
84   }
85
86 private:
87   /** this flag indicates if the object should be removed at the end of the
88    * frame
89    */
90   bool wants_to_die;
91
92   struct RemoveListenerListEntry
93   {
94     RemoveListenerListEntry* next;
95     ObjectRemoveListener* listener;
96   };
97   RemoveListenerListEntry* remove_listeners;
98
99 protected:
100   /**
101    * a name for the gameobject, this is mostly a hint for scripts and for
102    * debugging, don't rely on names being set or being unique
103    */
104   std::string name;
105
106 private:
107   GameObject& operator=(const GameObject&);
108 };
109
110 #endif /*SUPERTUX_GAMEOBJECT_H*/
111
112 /* EOF */