[Super-tux-commit] supertux/lib/special object_remove_listener.h,NONE,1.1 game_object.cpp,1.5,1.6 ga
Brought to you by:
wkendrick
From: Matze B. <mat...@us...> - 2005-01-16 12:11:39
|
Update of /cvsroot/super-tux/supertux/lib/special In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18853/lib/special Modified Files: game_object.cpp game_object.h Added Files: object_remove_listener.h Log Message: added object remove_listener so that you can get a message if some objects are removed (not tested yet though) Index: game_object.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/game_object.cpp,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- game_object.cpp 24 Nov 2004 14:10:23 -0000 1.5 +++ game_object.cpp 16 Jan 2005 12:11:22 -0000 1.6 @@ -19,18 +19,27 @@ #include <config.h> -#include "special/game_object.h" +#include "game_object.h" +#include "object_remove_listener.h" namespace SuperTux { GameObject::GameObject() - : wants_to_die(false), flags(0) + : wants_to_die(false), remove_listeners(0), flags(0) { } GameObject::~GameObject() { + // call remove listeners (and remove them from the list) + RemoveListenerListEntry* entry = remove_listeners; + while(entry != 0) { + RemoveListenerListEntry* next = entry->next; + entry->listener->object_removed(this); + delete entry; + entry = next; + } } } Index: game_object.h =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/special/game_object.h,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- game_object.h 20 Nov 2004 22:14:36 -0000 1.4 +++ game_object.h 16 Jan 2005 12:11:22 -0000 1.5 @@ -27,6 +27,8 @@ class DrawingContext; + class ObjectRemoveListener; + /** * Base class for all game objects. This contains functions for: * -querying the actual type of the object @@ -67,6 +69,18 @@ { wants_to_die = true; } + /** registers a remove listener which will be called if the object + * gets removed/destroyed + */ + void add_remove_listener(ObjectRemoveListener* listener) + { + RemoveListenerListEntry* entry = new RemoveListenerListEntry(); + entry->next = remove_listeners; + entry->listener = listener; + + remove_listeners = entry; + } + // flags enum { @@ -87,6 +101,13 @@ */ bool wants_to_die; + struct RemoveListenerListEntry + { + RemoveListenerListEntry* next; + ObjectRemoveListener* listener; + }; + RemoveListenerListEntry* remove_listeners; + protected: int flags; }; --- NEW FILE: object_remove_listener.h --- #ifndef __OBJECT_REMOVE_LISTENER_H__ #define __OBJECT_REMOVE_LISTENER_H__ namespace SuperTux { class GameObject; class ObjectRemoveListener { public: virtual void object_removed(GameObject* object) = 0; }; } #endif |