It's been a while since programming in C/C++ and I still have the sprite/object manager from my the pong game I was working on.
Also found a nice idea for a game to use it for but first I want to see if anyone can spot any issues with it before I go and build up a new game.
Yes this code is in SFML1.6 Couldn't find my SFML2.0RC version of it after changing computers.
Here's the header File of it.
#pragma once
#include "VisibleGameObject.h"
#include "SFML/Graphics.hpp"
class GameObjectManager
{
public:
GameObjectManager();
~GameObjectManager();
void Add(std::string name, VisibleGameObject* gameObject);
void Remove(std::string name);
int GetObjectCount() const;
VisibleGameObject* Get(std::string name) const;
void DrawAll(sf::RenderWindow& renderWindow);
void UpdateAll();
private:
std::map<std::string, VisibleGameObject*> _gameObjects;
struct GameObjectDeallocator
{
void operator()(const std::pair<std::string,VisibleGameObject*> & p) const
{
delete p.second;
}
};
};
Here's the Source File
#include "stdafx.h"
#include "GameObjectManager.h"
#include "Game.h"
GameObjectManager::GameObjectManager()
{
}
GameObjectManager::~GameObjectManager()
{
std::for_each(_gameObjects.begin(),_gameObjects.end(),GameObjectDeallocator());
}
void GameObjectManager::Add(std::string name, VisibleGameObject* gameObject)
{
_gameObjects.insert(std::pair<std::string,VisibleGameObject*>(name,gameObject));
}
void GameObjectManager::Remove(std::string name)
{
std::map<std::string, VisibleGameObject*>::iterator results = _gameObjects.find(name);
if(results != _gameObjects.end() )
{
delete results->second;
_gameObjects.erase(results);
}
}
VisibleGameObject* GameObjectManager::Get(std::string name) const
{
std::map<std::string, VisibleGameObject*>::const_iterator results = _gameObjects.find(name);
if(results == _gameObjects.end() )
return NULL;
return results->second;
}
int GameObjectManager::GetObjectCount() const
{
return _gameObjects.size();
}
void GameObjectManager::DrawAll(sf::RenderWindow& renderWindow)
{
std::map<std::string,VisibleGameObject*>::const_iterator itr = _gameObjects.begin();
while(itr != _gameObjects.end())
{
itr->second->Draw(renderWindow);
itr++;
}
}
void GameObjectManager::UpdateAll()
{
std::map<std::string,VisibleGameObject*>::const_iterator itr = _gameObjects.begin();
float timeDelta = Game::GetWindow().GetFrameTime();
while(itr != _gameObjects.end())
{
itr->second->Update(timeDelta);
itr++;
}
}
I could include the rest of the connecting file for the project if needed.
Finally got around to posting the mostly fixed up version of my code.
The header all be it a bit unneeded.
#pragma once
#include "VisibleGameObject.h"
//#include "SFML/Graphics.hpp"
class GameObjectManager
{
public:
GameObjectManager();
~GameObjectManager();
void Add(std::string name, VisibleGameObject* gameObject);
void Remove(std::string name);
unsigned int GetObjectCount();
VisibleGameObject* Get(std::string name);
void DrawAll(sf::RenderWindow& renderWindow);
void UpdateAll();
private:
std::map<std::string, VisibleGameObject*> _gameObjects;
struct GameObjectDeallocator
{
void operator()(const std::pair<std::string,VisibleGameObject*> & p) const
{
delete p.second;
}
};
};
and the cpp file that I made my best attempt to not leave riddled with errors.
#include "stdafx.h"
#include "GameObjectManager.h"
#include "Game.h"
#include <memory>
GameObjectManager::GameObjectManager()
{
}
GameObjectManager::~GameObjectManager()
{
std::for_each(_gameObjects.begin(),_gameObjects.end(),GameObjectDeallocator());
}
void GameObjectManager::Add(std::string name, VisibleGameObject* gameObject)
{
_gameObjects.insert(std::pair<std::string,VisibleGameObject*>(name,gameObject));
}
void GameObjectManager::Remove(std::string name)
{
std::map<std::string, VisibleGameObject*>::iterator results = _gameObjects.find(name);
if(results != _gameObjects.end() )
{
delete results->second;
//std::unique_ptr<VisibleGameObject*> temp(results->second); //Have no clue how to use this for here???????
_gameObjects.erase(results);
}
}
VisibleGameObject* GameObjectManager::Get(std::string name)
{
std::map<std::string, VisibleGameObject*>::const_iterator results = _gameObjects.find(name);
if(results == _gameObjects.end() )
{
return NULL;
}
else
{
return results->second;
}
}
unsigned int GameObjectManager::GetObjectCount()
{
return _gameObjects.size();
}
void GameObjectManager::DrawAll(sf::RenderWindow& renderWindow)
{
std::map<std::string,VisibleGameObject*>::iterator itr = _gameObjects.begin();
while(itr != _gameObjects.end())
{
itr->second->Draw(renderWindow);
itr++;
}
}
void GameObjectManager::UpdateAll()
{
std::map<std::string,VisibleGameObject*>::iterator itr = _gameObjects.begin();
float timeDelta = Game::GetWindow().GetFrameTime();
while(itr != _gameObjects.end())
{
itr->second->Update(timeDelta);
itr++;
}
}
Still has a few things that need fixing but they don't interfere with the rest of the game yet.
In any event I've still got no clue how to use unique_ptr for this.