The first one here is a version of Serapth's Game From Scratch tutorial.
Would be nice to put in std::unique_ptr but unsure how to an where.
GameObjectManager.h#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;
}
};
};
GameObjectManager.cpp#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++;
}
}
Meanwhile this next one is something I was working on but couldn't get some annoying error to go away in.
Was trying to see if vector was any better but I'm having issues scanning through the list and getting things by anything other than a number.
BaseObjectManager.h#pragma once
#include <vector>
#include <string>
#include "BlankSprite.h"
class BaseObjectManager
{
public:
BaseObjectManager();
~BaseObjectManager();
//void AddSprite(BlankSprite &s);
void AddSprite(BlankSprite *s);
//void AddSprite(BlankSprite s);
//void RemoveSprite(BlankSprite &s);
void RemoveSprite(BlankSprite *s);
void RemoveSprite(std::string name);
void RemoveSprite(int element);
void RemoveDeadSprites();
void ClearAllSprites();
BlankSprite *getSpriteByPtr(std::string name);
BlankSprite &getSpriteByRef(std::string name);
BlankSprite getSpriteCopy(std::string name);
unsigned int getSpriteListSize();
protected:
private:
std::vector<BlankSprite*> spriteList;
//std::vector<std::unique_ptr<BlankSprite*>> spriteList;
};
BaseObjectManager.cpp#include "StdAfx.h"
#include "BaseObjectManager.h"
#include <iostream>
BaseObjectManager::BaseObjectManager()
{
}
BaseObjectManager::~BaseObjectManager()
{
}
/*void BaseObjectManager::AddSprite(BlankSprite &s)
{
spriteList.push_back(s);
}*/
void BaseObjectManager::AddSprite(BlankSprite *s)
{
spriteList.push_back(s);
}
/*void BaseObjectManager::AddSprite(BlankSprite s)
{
spriteList.push_back(s);
}*/
/*void BaseObjectManager::RemoveSprite(BlankSprite &s)
{
}*/
void BaseObjectManager::RemoveSprite(BlankSprite *s)
{
//meant to remove a sprite by its pointer
}
void BaseObjectManager::RemoveSprite(std::string name)
{
//meant to remove a sprite by its name
std::vector<BlankSprite*>::iterator scanList;
unsigned int i = 0;
for(scanList = spriteList.begin(); scanList != spriteList.end(); scanList++)
{
//BlankSprite hold = spriteList.at( std::distance(spriteList.begin(),scanList));
if(i < (spriteList.size() -1))
{
if(spriteList.at(i)->getName().compare(name) == 0)
{
spriteList.erase(spriteList.begin() + i);
}
else
{
std::cout << "No sprite with this name exists!" << std::endl;
}
}
i++;
}
}
void BaseObjectManager::RemoveSprite(int element)
{
spriteList.erase(spriteList.begin() + element);
}
void BaseObjectManager::ClearAllSprites()
{
spriteList.clear();
}
void BaseObjectManager::RemoveDeadSprites()
{
std::vector<BlankSprite*>::iterator scanList;
unsigned int i = 0;
for(scanList = spriteList.begin(); scanList != spriteList.end(); scanList++)
{
//BlankSprite hold = spriteList.at( std::distance(spriteList.begin(),scanList));
if(i < (spriteList.size() -1))
{
if(spriteList.at(i)->getDead() == true)
{
spriteList.erase(spriteList.begin() + i);
//spriteList.pop_back(spriteList.at(i));
}
else
{
//std::cout << "No sprite with this name exists!" << std::endl;
continue;
}
}
i++;
}
}
BlankSprite *BaseObjectManager::getSpriteByPtr(std::string name)
{
std::vector<BlankSprite*>::iterator scanList;
unsigned int i = 0;
for(scanList = spriteList.begin(); scanList != spriteList.end(); scanList++ )
{
//BlankSprite hold = spriteList.at( std::distance(spriteList.begin(),scanList));
if(i < (spriteList.size() -1) )
{
if(spriteList.at(i)->getName().compare(name) == 0)
{
return spriteList.at(i);
}
else
{
//std::cout << "No sprite with this name exists!" << std::endl;
//return nullptr;
continue;
}
}
i++;
}
}
/*
BlankSprite &BaseObjectManager::getSpriteByRef(std::string name)
{
}
BlankSprite BaseObjectManager::getSpriteCopy(std::string name)
{
}*/
unsigned int BaseObjectManager::getSpriteListSize()
{
return spriteList.size();
}
I keep getting some annoying excpetion error anytime I try to use the methods meant for targeting a specific sprite/object.
Any ideas? I know I'm missing something but can't pin down what.