From 0bae7b9c6b93aef579256034d87e8d1a5c34a578 Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Wed, 12 Feb 2014 09:49:57 +0100 Subject: [PATCH] Misc - Fixed a few errors that can occur --- .../DanBiasGame/GameClientState/LoginState.cpp | 1 - Code/Misc/EventHandler/EventButtonCollection.cpp | 11 +++++++++-- Code/Misc/EventHandler/EventButtonCollection.h | 15 ++++++++++----- Code/Misc/EventHandler/EventHandler.cpp | 9 +++++++++ Code/Misc/EventHandler/EventHandler.h | 10 ++++++++++ 5 files changed, 38 insertions(+), 8 deletions(-) diff --git a/Code/Game/DanBiasGame/GameClientState/LoginState.cpp b/Code/Game/DanBiasGame/GameClientState/LoginState.cpp index 4f148624..d94d8ed5 100644 --- a/Code/Game/DanBiasGame/GameClientState/LoginState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/LoginState.cpp @@ -257,7 +257,6 @@ bool LoginState::Release() } delete privData->collection; - //EventHandler::Instance().DeleteCollection(privData->collection); delete privData; privData = NULL; diff --git a/Code/Misc/EventHandler/EventButtonCollection.cpp b/Code/Misc/EventHandler/EventButtonCollection.cpp index cc769b5c..2bd5f09a 100644 --- a/Code/Misc/EventHandler/EventButtonCollection.cpp +++ b/Code/Misc/EventHandler/EventButtonCollection.cpp @@ -8,8 +8,8 @@ using namespace Oyster::Event; -EventButtonCollection::EventButtonCollection() - : collectionState(EventCollectionState_Enabled) +EventButtonCollection::EventButtonCollection(EventCollectionState state) + : collectionState(state) { } @@ -65,6 +65,13 @@ void EventButtonCollection::SetState(const EventCollectionState state) void EventButtonCollection::Clear() { + int size = buttons.size(); + for(int i = 0; i < size; i++) + { + delete buttons[i]; + buttons[i] = NULL; + } buttons.clear(); + collectionState = EventCollectionState_Enabled; } \ No newline at end of file diff --git a/Code/Misc/EventHandler/EventButtonCollection.h b/Code/Misc/EventHandler/EventButtonCollection.h index 56bb3d27..5bcd044a 100644 --- a/Code/Misc/EventHandler/EventButtonCollection.h +++ b/Code/Misc/EventHandler/EventButtonCollection.h @@ -28,20 +28,20 @@ namespace Oyster }; /******************************** - This EventButtonCollection will handle the destruction of the buttons when they are added to the collection. - + This EventButtonCollection will handle the destruction of the buttons when they are added to the collection ********************************/ class EventButtonCollection { public: - EventButtonCollection(); + EventButtonCollection(EventCollectionState state = EventCollectionState_Enabled); ~EventButtonCollection(); void Update(InputClass* inputObject); void Render(); - template - void AddButton(EventButton* button) + /*Add a button to the collection when a button is added to the collection you are not allowed to delete it. + */ + template void AddButton(EventButton* button) { buttons.push_back(button); } @@ -52,6 +52,11 @@ namespace Oyster //Clear all buttons and reset the state. void Clear(); + private: + //Can't copy + EventButtonCollection(const EventButtonCollection& obj); + EventButtonCollection& operator =(const EventButtonCollection& obj); + protected: std::vector buttons; EventCollectionState collectionState; diff --git a/Code/Misc/EventHandler/EventHandler.cpp b/Code/Misc/EventHandler/EventHandler.cpp index 139e0cac..4b623714 100644 --- a/Code/Misc/EventHandler/EventHandler.cpp +++ b/Code/Misc/EventHandler/EventHandler.cpp @@ -23,6 +23,7 @@ EventHandler::~EventHandler() for(int i = 0; i < size; i++) { delete collections[i]; + collections[i] = NULL; } } @@ -32,6 +33,7 @@ void EventHandler::Clean() for(int i = 0; i < size; i++) { delete collections[i]; + collections[i] = NULL; } collections.clear(); } @@ -54,6 +56,12 @@ void EventHandler::Render() void EventHandler::AddCollection(EventButtonCollection* collection) { + for(int i = 0; i < collections.size(); i++) + { + //Do not add the collection if it's already in the list. + if(collections.at(i) == collection) + return; + } collections.push_back(collection); } @@ -64,6 +72,7 @@ void EventHandler::DeleteCollection(EventButtonCollection* collection) if(collections.at(i) == collection) { delete collection; + collection = NULL; collections.erase(collections.begin() + i); break; } diff --git a/Code/Misc/EventHandler/EventHandler.h b/Code/Misc/EventHandler/EventHandler.h index 8459a77a..71a35ecd 100644 --- a/Code/Misc/EventHandler/EventHandler.h +++ b/Code/Misc/EventHandler/EventHandler.h @@ -29,11 +29,21 @@ namespace Oyster void Update(InputClass* inputObject); void Render(); + /*Add a collection to the EventHandler will only add collections not already present in the list. + + */ void AddCollection(EventButtonCollection* collection); void DeleteCollection(EventButtonCollection* collection); + private: + //Can't copy this class. + EventHandler(const EventHandler& obj); + EventHandler& operator =(const EventHandler& obj); + private: std::vector collections; + + //EventButtonCollection is a firend so it can delete it self. friend class EventButtonCollection; }; }