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.