From ca21f8c63a806278f3386b2c1f61d5c216fd3169 Mon Sep 17 00:00:00 2001 From: "Martin Felis (berta)" Date: Wed, 14 Apr 2010 22:01:45 +0200 Subject: [PATCH] hunted some memory leaks (with success) --- CMakeLists.txt | 6 ++++-- asteroids/EntityFactory.cc | 10 ---------- asteroids/Model.h | 5 +++++ asteroids/View.cc | 16 ++++++++++++---- asteroids/View.h | 15 ++++++++++++--- engine/EntityBase.cc | 2 -- engine/EntityFactoryBase.cc | 8 -------- engine/Logging.cc | 9 ++++++++- engine/Logging.h | 1 + engine/LoggingGlobal.h | 5 ++++- engine/ModelBase.cc | 15 +++++---------- engine/OverlayBase.h | 4 ++++ engine/PhysicsBase.cc | 26 +++++++------------------- engine/ViewBase.cc | 21 ++++++++------------- engine/ViewBase.h | 12 +++++++----- 15 files changed, 77 insertions(+), 78 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 62b6b65..cff7bee 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,5 @@ +PROJECT ( Asteroids ) + CMAKE_MINIMUM_REQUIRED(VERSION 2.6) LIST( APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMake ) @@ -36,8 +38,8 @@ SET ( ASTEROIDS_SOURCES asteroids/MenuOverlay.cc ) -ADD_EXECUTABLE ( hasteroids ${ASTEROIDS_SOURCES} ) +ADD_EXECUTABLE ( run_asteroids ${ASTEROIDS_SOURCES} ) -TARGET_LINK_LIBRARIES ( hasteroids +TARGET_LINK_LIBRARIES ( run_asteroids Engine ) diff --git a/asteroids/EntityFactory.cc b/asteroids/EntityFactory.cc index 3b5e165..33a7f6d 100644 --- a/asteroids/EntityFactory.cc +++ b/asteroids/EntityFactory.cc @@ -21,7 +21,6 @@ Engine::EntityPhysicState* EntityFactory::CreateEntityPhysicState (int type) { // However to prevent errors we do a simple check vor validity. if (type < 0 || type > GameEntityTypeLast ) { Engine::LogError ("Cannot create Entity with type %d: invalid type!", type); - assert (0); } // Create the Entity @@ -32,7 +31,6 @@ Engine::EntityPhysicState* EntityFactory::CreateEntityPhysicState (int type) { entity_physics = new ShipEntityPhysicState (); if (!entity_physics) { Engine::LogError ("Could not allocate enough memory for EntityPhysicState of type '%d'", type); - assert (0); } entity_physics->mRadius = 0.5; entity_physics->mShape = new coll2d::Sphere (entity_physics->mRadius); @@ -42,7 +40,6 @@ Engine::EntityPhysicState* EntityFactory::CreateEntityPhysicState (int type) { entity_physics = new AsteroidEntityPhysicState (); if (!entity_physics) { Engine::LogError ("Could not allocate enough memory for EntityPhysicState of type '%d'", type); - assert (0); } entity_physics->mRadius = 1.; @@ -53,7 +50,6 @@ Engine::EntityPhysicState* EntityFactory::CreateEntityPhysicState (int type) { entity_physics = new RocketEntityPhysicState(); if (!entity_physics) { Engine::LogError ("Could not allocate enough memory for EntityPhysicState of type '%d'", type); - assert (0); } entity_physics->mRadius = 0.1; entity_physics->mShape = new coll2d::Sphere (entity_physics->mRadius); @@ -62,13 +58,11 @@ Engine::EntityPhysicState* EntityFactory::CreateEntityPhysicState (int type) { entity_physics->mBaseType = Engine::EntityBaseTypeBlock; if (!entity_physics) { Engine::LogError ("Could not allocate enough memory for EntityPhysicState of type '%d'", type); - assert (0); } entity_physics->mRadius = 0.1; entity_physics->mShape = new coll2d::Sphere (entity_physics->mRadius); } else { Engine::LogError ("No EntityPhysicState defined for GameEntity type '%d'", type); - assert (0); } entity_physics->mType = type; @@ -81,7 +75,6 @@ Engine::EntityControllerState* EntityFactory::CreateEntityControllerState (int t // However to prevent errors we do a simple check vor validity. if (type < 0 || type >> Engine::EntityBaseTypeLast ) { Engine::LogError ("Cannot create Entity with type %d: invalid type!", type); - assert (0); } // Create the Entity @@ -92,7 +85,6 @@ Engine::EntityControllerState* EntityFactory::CreateEntityControllerState (int t entity_controller = new Engine::EntityControllerState (); if (!entity_controller) { Engine::LogError ("Could not allocate enough memory for EntityControllerState of type '%d'", type); - assert (0); } } @@ -104,7 +96,6 @@ Engine::EntityBase* EntityFactory::CreateEntity (int type) { // However to prevent errors we do a simple check vor validity. if (type < 0 || type > GameEntityTypeLast ) { Engine::LogError ("Cannot create Entity with type %d: invalid type!", type); - assert (0); } // Create the Entity @@ -125,7 +116,6 @@ Engine::EntityBase* EntityFactory::CreateEntity (int type) { if (!entity) { Engine::LogError ("Could not allocate enough memory for EntityVisualState of type '%d'", type); - assert (0); } entity->mPhysicState = CreateEntityPhysicState (type); diff --git a/asteroids/Model.h b/asteroids/Model.h index 45c83ce..0051cd1 100644 --- a/asteroids/Model.h +++ b/asteroids/Model.h @@ -8,6 +8,7 @@ namespace asteroids { class Model : public Engine::ModelBase { public: + virtual ~Model() {}; virtual void Process(); int DoLoadLevel (const char* filename); int DoSaveLevel (const char* filename); @@ -26,6 +27,10 @@ class Model : public Engine::ModelBase { protected: /** \brief Initializes the system */ virtual int OnInit (int argc, char* argv[]); + virtual void OnDestroy() { + Engine::ModelBase::OnDestroy(); + mAsteroids.clear(); + }; virtual void OnRegisterCommands (); virtual void OnCreateEntity (const int type, const unsigned int id); diff --git a/asteroids/View.cc b/asteroids/View.cc index dcc9458..e8c8974 100644 --- a/asteroids/View.cc +++ b/asteroids/View.cc @@ -30,16 +30,16 @@ int View::OnInit (int argc, char* argv[]) { ViewBase::OnInit (argc, argv); // We want menu - mMenuOverlay = new MenuOverlay; + mMenuOverlay = boost::shared_ptr (new MenuOverlay); mMenuOverlay->SetModel ((Model*) mModel); mMenuOverlay->Init(); AddOverlay (mMenuOverlay); // We want the console - Engine::SimpleConsoleOverlay *console = new Engine::SimpleConsoleOverlay; + mConsoleOverlay = boost::shared_ptr (new Engine::SimpleConsoleOverlay); // We also want to display the log bar - console->SetDrawLogBar (true); - AddOverlay (console); + mConsoleOverlay->SetDrawLogBar (true); + AddOverlay (mConsoleOverlay); // This is a simple star field that makes the game so spacy int i; @@ -73,6 +73,14 @@ int View::OnInit (int argc, char* argv[]) { void View::OnDestroy() { delete mAccelerateEventHandler; + delete mShipExplodeEventHandler; + + mBackgroundStars.clear(); + mShipPartsEntityIds.clear(); + + mMenuOverlay.reset(); + + Engine::ViewBase::OnDestroy(); } /* diff --git a/asteroids/View.h b/asteroids/View.h index 485eae8..1a9d8d4 100644 --- a/asteroids/View.h +++ b/asteroids/View.h @@ -2,9 +2,11 @@ #define _VIEW_H #include "ViewBase.h" +#include "OverlayBase.h" #include "mathlib.h" #include "Sprite.h" #include "EntityBase.h" +#include "SimpleConsoleOverlay.h" namespace asteroids { @@ -20,10 +22,13 @@ struct BackgroundStar { /** \brief Performs the actual drawing based on Camera and Model */ class View : public Engine::ViewBase { + public: + virtual ~View() {}; + protected: /** \brief Initializes the system */ - int OnInit (int argc, char* argv[]); - void OnDestroy (); + virtual int OnInit (int argc, char* argv[]); + virtual void OnDestroy (); /** \brief Updates the camera for further drawing */ virtual void UpdateCamera (); @@ -38,7 +43,11 @@ class View : public Engine::ViewBase { void DrawRocket (RocketEntity *asteroid); void DrawShipPart (Engine::EntityBase *entity); - MenuOverlay *mMenuOverlay; +// Engine::OverlayBasePtr mMenuOverlay; +// Engine::OverlayBasePtr mConsoleOverlay; + boost::shared_ptr mMenuOverlay; + boost::shared_ptr mConsoleOverlay; + std::vector mBackgroundStars; std::vector mShipPartsEntityIds; diff --git a/engine/EntityBase.cc b/engine/EntityBase.cc index a1fa0d0..bb3d39a 100644 --- a/engine/EntityBase.cc +++ b/engine/EntityBase.cc @@ -27,7 +27,6 @@ void EntityControllerState::UnsetKey (int state) { void EntityBase::SetControllerKeyState (int state) { if (!mControllerState) { LogError ("Error when trying to send a KeyState to an Entity that has no EntityControllerState!"); - assert (0); } mControllerState->SetKey (state); @@ -36,7 +35,6 @@ void EntityBase::SetControllerKeyState (int state) { void EntityBase::UnsetControllerKeyState (int state) { if (!mControllerState) { LogError ("Error when trying to send a KeyState to an Entity that has no EntityControllerState!"); - assert (0); } mControllerState->UnsetKey (state); diff --git a/engine/EntityFactoryBase.cc b/engine/EntityFactoryBase.cc index 4cf1c82..493eafb 100644 --- a/engine/EntityFactoryBase.cc +++ b/engine/EntityFactoryBase.cc @@ -18,7 +18,6 @@ EntityPhysicState* EntityFactoryBase::CreateEntityPhysicState (int type) { // However to prevent errors we do a simple check vor validity. if (type < 0 || type >> EntityBaseTypeLast ) { LogError ("Cannot create Entity with type %d: invalid type!", type); - assert (0); } EntityBaseType base_type = (EntityBaseType) type; @@ -26,7 +25,6 @@ EntityPhysicState* EntityFactoryBase::CreateEntityPhysicState (int type) { EntityPhysicState* entity_physics = new EntityPhysicState (); if (!entity_physics) { LogError ("Could not allocate enough memory for EntityPhysicState of type '%d'", type); - assert (0); } // default values for all entities @@ -52,7 +50,6 @@ EntityPhysicState* EntityFactoryBase::CreateEntityPhysicState (int type) { assert (entity_physics->mShape); } else { LogError ("No EntityPhysicState defined for Entity base_type '%d'", base_type); - assert (0); } return entity_physics; @@ -63,7 +60,6 @@ EntityControllerState* EntityFactoryBase::CreateEntityControllerState (int type) // However to prevent errors we do a simple check vor validity. if (type < 0 || type >> EntityBaseTypeLast ) { LogError ("Cannot create Entity with type %d: invalid type!", type); - assert (0); } EntityBaseType base_type = (EntityBaseType) type; @@ -76,13 +72,11 @@ EntityControllerState* EntityFactoryBase::CreateEntityControllerState (int type) entity_controller = new EntityControllerState (); if (!entity_controller) { LogError ("Could not allocate enough memory for EntityControllerState of type '%d'", type); - assert (0); } } else if (base_type == EntityBaseTypeBlock) { } else if (base_type == EntityBaseTypeParticle) { } else { LogError ("No EntityPhysicState defined for Entity base_type '%d'", base_type); - assert (0); } return entity_controller; @@ -93,7 +87,6 @@ EntityBase* EntityFactoryBase::CreateEntity (int type) { // However to prevent errors we do a simple check vor validity. if (type < 0 || type >> EntityBaseTypeLast ) { LogError ("Cannot create Entity with type %d: invalid type!", type); - assert (0); } EntityBaseType base_type = (EntityBaseType) type; @@ -103,7 +96,6 @@ EntityBase* EntityFactoryBase::CreateEntity (int type) { if (!entity) { LogError ("Could not allocate enough memory for EntityVisualState of type '%d'", type); - assert (0); } entity->mPhysicState = CreateEntityPhysicState (type); diff --git a/engine/Logging.cc b/engine/Logging.cc index a008579..98e2da5 100644 --- a/engine/Logging.cc +++ b/engine/Logging.cc @@ -74,6 +74,14 @@ void Logging::Log (LogLevel level, const char *str, ...) { << ": " << msg << std::endl; mLogFileOut.flush(); } + + if (level == LogLevelError) { + std::cerr << "Error occured: Aborting!" << std::endl; + mLogFileOut << "Error occured: Aborting!" << std::endl; + mLogFileOut.flush(); + mLogFileOut.close(); + exit (-1); + } } void Logging::SetLogPrintLevel (LogLevel print_level) { @@ -85,7 +93,6 @@ void Logging::SetLogFilename (const char *filename) { if (!mLogFileOut) { LogError ("Could not open logfile %s for writing!", filename); - assert (0); } requested_logfilename = ""; diff --git a/engine/Logging.h b/engine/Logging.h index b519027..abaab06 100644 --- a/engine/Logging.h +++ b/engine/Logging.h @@ -13,6 +13,7 @@ class Module; * a log filename was specified by Logging::SetLogFilename() then all messages * sent to the Logging class are written to the file. * + * \note The program automatically abortst when reporting an error * \TODO Add log level for files separately */ class Logging : public Module { diff --git a/engine/LoggingGlobal.h b/engine/LoggingGlobal.h index 75c2d3a..96e3187 100644 --- a/engine/LoggingGlobal.h +++ b/engine/LoggingGlobal.h @@ -26,7 +26,10 @@ void SetLogPrintLevel (LogLevel print_level); /** \brief Sets the filename to which all the logging is sent, set to NULL to disable logging */ void SetLogFilename (const char *filename); -/** \brief Sends the Message to the Logging system */ +/** \brief Sends the Message to the Logging system + * + * \note The program automatically abortst when reporting an error + */ void LogError (const char* str, ...); /** \brief Sends the Message to the Logging system */ void LogWarning (const char* str, ...); diff --git a/engine/ModelBase.cc b/engine/ModelBase.cc index 62cc529..d5ee02c 100644 --- a/engine/ModelBase.cc +++ b/engine/ModelBase.cc @@ -53,7 +53,6 @@ void ModelBase::Process () { do { if (entity_iter->second == NULL) { LogError ("Entity with id %d does not exist!", entity_iter->first); - assert (0); } entity_iter->second->Update(mDeltaSec); entity_iter++; @@ -93,7 +92,6 @@ void ModelBase::RegisterEntity (EntityBase* entity) { if (mEntities.find(id) != mEntities.end ()) { LogError ("Replacing Entity with id '%d'", id); - assert (0); } if (entity->mPhysicState) @@ -107,7 +105,6 @@ void ModelBase::KillEntity (const unsigned int id) { if (iter == mEntities.end ()) { LogError ("Could not kill Entity with id '%d': Entity not found!", id); - assert (0); return; } else { EntityBase *entity = iter->second; @@ -128,7 +125,6 @@ void ModelBase::UnregisterEntity (const unsigned int id) { if (iter == mEntities.end ()) { LogError ("Could not unregister Entity with id '%d': Entity not found!", id); - assert (0); return; } else { EntityBase *entity = iter->second; @@ -137,6 +133,11 @@ void ModelBase::UnregisterEntity (const unsigned int id) { entity->mPhysicState = NULL; } + if (entity->mControllerState) { + delete entity->mControllerState; + entity->mControllerState = NULL; + } + delete entity; mEntities.erase (iter); @@ -228,7 +229,6 @@ void ModelBase::SendEntityCollisionEvent (const unsigned int reference_entity_id unsigned int GetPlayerEntityId () { if (!ModelInstance) { LogError ("Couldn't create Entity: Model not initialized!"); - assert (0); } return ModelInstance->GetPlayerEntityId (); @@ -237,7 +237,6 @@ unsigned int GetPlayerEntityId () { float GetFrameDuration () { if (!ModelInstance) { LogError ("Couldn't create Entity: Model not initialized!"); - assert (0); } return ModelInstance->GetFrameDuration (); @@ -246,7 +245,6 @@ float GetFrameDuration () { EntityBase * CreateEntity (int type) { if (!ModelInstance) { LogError ("Couldn't create Entity: Model not initialized!"); - assert (0); } EntityBase *result = ModelInstance->CreateEntity (type); @@ -257,7 +255,6 @@ EntityBase * CreateEntity (int type) { void DestroyEntity (unsigned int id) { if (!ModelInstance) { LogError ("Couldn't destroy Entity: Model not initialized!"); - assert (0); } ModelInstance->UnregisterEntity (id); @@ -266,7 +263,6 @@ void DestroyEntity (unsigned int id) { void KillEntity (unsigned int id) { if (!ModelInstance) { LogError ("Couldn't kill Entity: Model not initialized!"); - assert (0); } ModelInstance->KillEntity (id); @@ -275,7 +271,6 @@ void KillEntity (unsigned int id) { EntityBase * GetEntity (unsigned int id) { if (!ModelInstance) { LogError ("Couldn't execute GetEntity(): Model not initialized!"); - assert (0); } return ModelInstance->GetEntity (id); diff --git a/engine/OverlayBase.h b/engine/OverlayBase.h index b2644e5..46fb544 100644 --- a/engine/OverlayBase.h +++ b/engine/OverlayBase.h @@ -1,6 +1,8 @@ #ifndef OVERLAY #define OVERLAY +#include + #include namespace Engine { @@ -20,6 +22,8 @@ class OverlayBase { void _strange_function_for_vtable () {}; }; +typedef boost::shared_ptr OverlayBasePtr; + } #endif /* OVERLAY */ diff --git a/engine/PhysicsBase.cc b/engine/PhysicsBase.cc index 6a4a64f..28faac0 100644 --- a/engine/PhysicsBase.cc +++ b/engine/PhysicsBase.cc @@ -144,13 +144,12 @@ void PhysicsBase::RegisterEntity (EntityPhysicState* entity) { mEntities[id] = entity; else { LogError ("Physics already has registered an Entity with id '%d'", id); - assert (0); } } void PhysicsBase::UnregisterEntity (const unsigned int id) { std::map::iterator iter = mEntities.find(id); - LogDebug ("Unegistering EntityPhysicState with id '%d'", id); + LogDebug ("Unregistering EntityPhysicState with id '%d'", id); if (iter != mEntities.end ()) { // Remove all the references of existing contacts to the Entity that is @@ -175,7 +174,6 @@ void PhysicsBase::UnregisterEntity (const unsigned int id) { delete entity_physic_state; } else { LogError ("Could not unegister EntityPhysicState with id '%d': Entity not found!", id); - assert (0); } } @@ -254,7 +252,6 @@ bool PhysicsBase::CalcNextCollision ( if (!HandleColl2dError (coll2d_result, stepsize, entity_a, entity_b, temp_info)) { LogError ("Could not handle coll2d error: %d\n", coll2d_result); - assert (0); } if (coll2d_result > 0 && temp_info.time < info.time ) { @@ -267,7 +264,6 @@ bool PhysicsBase::CalcNextCollision ( } else { reference_entity_id = collision_iter->first; incidence_entity_id = collision_ref->first; - //assert (0); } } } @@ -410,8 +406,6 @@ void PhysicsBase::ContactCacheAdd (EntityPhysicState* incidence_entity, EntityPh void PhysicsBase::ContactCacheRemove (unsigned int entity_a_id, unsigned int entity_b_id) { assert (entity_a_id != entity_b_id); -// LogDebug ("Removing start %d and %d", entity_a_id, entity_b_id); - EntityPhysicState *entity_a, *entity_b; #ifdef WIN32 @@ -447,8 +441,6 @@ void PhysicsBase::ContactCacheRemove (unsigned int entity_a_id, unsigned int ent contact_normal[0], contact_normal[1], contact_normal[2], entity_b_id, entity_a_id); entity_b->mContactNormals.erase (entity_b->mContactNormals.find(entity_a_id)); - -// LogDebug ("Removing done!"); } /** \brief Checks whether we are still in contact with the entities stored in mContactNormals @@ -471,12 +463,12 @@ void PhysicsBase::CheckContactCache (EntityPhysicState* entity) { // * Attention! * // current_iter can be used throughout this environment and contacts_iter // can already now be increased as it *must not* be used! This is due to - // the nature of ContactCachRemove() which might make contacts_iter + // the nature of ContactCacheRemove() which might make contacts_iter // invalid if we were using that. current_iter = contacts_iter; contacts_iter ++; - unsigned int contact_entity_id = contacts_iter->first; + unsigned int contact_entity_id = current_iter->first; #ifdef WIN32 EntityPhysicState* contact_entity = mEntities[contact_entity_id]; @@ -484,7 +476,7 @@ void PhysicsBase::CheckContactCache (EntityPhysicState* entity) { EntityPhysicState* contact_entity = mEntities.at (contact_entity_id); #endif - vector3d normal = contacts_iter->second; + vector3d normal = current_iter->second; vector3d old_velocity = entity->GetVelocity(); // If we already move away from the normal, we delete the contact. @@ -508,21 +500,19 @@ void PhysicsBase::CheckContactCache (EntityPhysicState* entity) { if (!HandleColl2dError (coll2d_result, 1.0, entity, contact_entity, info)) { // error LogError ("Error when performing collision check: %s\n", __FUNCTION__); - assert (0); } - if (coll2d_result == 0) { + if (coll2d_result > 0) { // no contact, so delete it: LogDebug ("Lost Contact with entity %d!", current_iter->first); ContactCacheRemove (entity->mId, current_iter->first); entity->SetVelocity (old_velocity); continue; - } else if ( coll2d_result > 0){ + } else if ( coll2d_result < 0){ // contact if (info.time != 0. || normal != info.normal) { - LogError ("Something strange happened when checking for contacts in %s\n", __FUNCTION__); - assert (0); + LogError ("Something strange happened when checking for contacts in %s", __FUNCTION__); } } @@ -567,7 +557,6 @@ EntityPhysicState* CreateEntityPhysicState (EntityBaseType type, unsigned int id EntityPhysicState* entity_physics = new EntityPhysicState (); if (!entity_physics) { LogError ("Could not allocate enough memory for EntityPhysicState of type '%d'", type); - assert (0); } // default values for all Entities entity_physics->mId = id; @@ -593,7 +582,6 @@ EntityPhysicState* CreateEntityPhysicState (EntityBaseType type, unsigned int id assert (entity_physics->mShape); } else { LogError ("No EntityPhysicState defined for Entity type '%d'", type); - assert (0); } return entity_physics; diff --git a/engine/ViewBase.cc b/engine/ViewBase.cc index 0f83f00..23384bb 100644 --- a/engine/ViewBase.cc +++ b/engine/ViewBase.cc @@ -60,7 +60,7 @@ int ViewBase::OnInit (int argc, char* argv[]) { exit (-1); } - SimpleConsoleOverlay* console_overlay = new SimpleConsoleOverlay; + OverlayBasePtr console_overlay(new SimpleConsoleOverlay); AddOverlay (console_overlay); mConsoleFont->setForegroundColor (1., 1., 1.); @@ -81,13 +81,8 @@ void ViewBase::OnDestroy () { delete mConsoleFont; mConsoleFont = NULL; - std::vector::iterator overlay_iter = mOverlays.begin(), overlay_temp; - while (overlay_iter != mOverlays.end()) { - overlay_temp = overlay_iter; - delete *overlay_temp; - - overlay_iter++; - } + while (mOverlays.size() > 0) + mOverlays.pop_back(); ViewInstance = NULL; @@ -185,7 +180,7 @@ void ViewBase::Draw () { DrawWorld (); - std::vector::iterator overlay_iter; + std::vector::iterator overlay_iter; for (overlay_iter = mOverlays.begin(); overlay_iter != mOverlays.end(); overlay_iter++) { (*overlay_iter)->Draw(); } @@ -265,7 +260,7 @@ void ViewBase::Resize (int width, int height) { } bool ViewBase::SendKeyDown (const SDL_keysym &keysym) { - std::vector::iterator overlay_iter; + std::vector::iterator overlay_iter; for (overlay_iter = mOverlays.begin(); overlay_iter != mOverlays.end(); overlay_iter++) { if ( (*overlay_iter)->OnKeyDown (keysym)) return true; @@ -275,7 +270,7 @@ bool ViewBase::SendKeyDown (const SDL_keysym &keysym) { } bool ViewBase::SendKeyUp (const SDL_keysym &keysym) { - std::vector::iterator overlay_iter; + std::vector::iterator overlay_iter; for (overlay_iter = mOverlays.begin(); overlay_iter != mOverlays.end(); overlay_iter++) { if ( (*overlay_iter)->OnKeyUp (keysym)) return true; @@ -285,7 +280,7 @@ bool ViewBase::SendKeyUp (const SDL_keysym &keysym) { } bool ViewBase::SendMouseButtonUp (Uint8 button, Uint16 xpos, Uint16 ypos) { - std::vector::iterator overlay_iter; + std::vector::iterator overlay_iter; for (overlay_iter = mOverlays.begin(); overlay_iter != mOverlays.end(); overlay_iter++) { if ( (*overlay_iter)->OnMouseButtonUp (button, xpos, ypos)) return true; @@ -295,7 +290,7 @@ bool ViewBase::SendMouseButtonUp (Uint8 button, Uint16 xpos, Uint16 ypos) { } bool ViewBase::SendMouseButtonDown (Uint8 button, Uint16 xpos, Uint16 ypos) { - std::vector::iterator overlay_iter; + std::vector::iterator overlay_iter; for (overlay_iter = mOverlays.begin(); overlay_iter != mOverlays.end(); overlay_iter++) { if ( (*overlay_iter)->OnMouseButtonDown (button, xpos, ypos)) return true; diff --git a/engine/ViewBase.h b/engine/ViewBase.h index a6a2bf1..b62083f 100644 --- a/engine/ViewBase.h +++ b/engine/ViewBase.h @@ -2,6 +2,7 @@ #define _VIEWBASE_H #include "Engine.h" +#include "OverlayBase.h" // forward declarations for the OGLFT fonts namespace OGLFT { @@ -13,13 +14,14 @@ namespace Engine { class Module; class ModelBase; class CameraBase; -class OverlayBase; /** \brief Performs the actual drawing based on Camera and Model */ class ViewBase : public Module{ public: + virtual ~ViewBase() {}; + /** \brief Resizes the View */ void Resize (int width, int height); @@ -45,7 +47,7 @@ class ViewBase : public Module{ bool GetDrawGrid () { return mDrawGrid; }; void SetGridSize (int x, int z) { mGridSizeX = x; mGridSizeZ = z; } - void AddOverlay (OverlayBase *overlay) { mOverlays.push_back (overlay); }; + void AddOverlay (OverlayBasePtr overlay) { mOverlays.push_back (overlay); }; /* Input forwarding for the overlays */ bool SendKeyDown (const SDL_keysym &keysym); @@ -55,9 +57,9 @@ class ViewBase : public Module{ protected: /** \brief Initializes the system */ - int OnInit (int argc, char* argv[]); + virtual int OnInit (int argc, char* argv[]); /** \brief Destroys the system (must be called!) */ - void OnDestroy (); + virtual void OnDestroy (); /** \brief Updates the camera for further drawing */ virtual void UpdateCamera (); @@ -71,7 +73,7 @@ class ViewBase : public Module{ ModelBase *mModel; CameraBase *mCamera; - std::vector mOverlays; + std::vector mOverlays; /** \brief The height of the canvas we're drawing on */ unsigned int mWindowHeight;