Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Hmm Where to start up again I wonder.  (Read 2705 times)

0 Members and 1 Guest are viewing this topic.

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Hmm Where to start up again I wonder.
« on: October 26, 2012, 07:32:23 pm »
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.
I have many ideas but need the help of others to find way to make use of them.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Hmm Where to start up again I wonder.
« Reply #1 on: October 26, 2012, 07:54:25 pm »
Okay, some points:
  • Does your compiler support C++11? If yes, replace your raw owning pointers with std::unique_ptr, then you can also get rid of the deallocation functor. And you can use auto for the iterator declarations.
  • Get() is const, but returns a pointer to a mutable GameObject.
  • You iterate using const_iterator, but change the elements?
  • GetObjectCount() should return an unsigned type.
  • You don't need the whole <SFML/Graphics.hpp> header, it just increases compile time. The only headers you need to include in your header are <map> and <string>, which you ironically have forgotten. For everything else, forward declarations are enough.
Apart from that, one could argue how meaningful it is to store everything in one object... It depends on the game, sometimes separate containers for each game object allow more flexibility. And it could also be worth a thought if graphic and logic updates are decoupled, maybe work at different framerates. But those are just some thoughts...
« Last Edit: October 26, 2012, 08:16:59 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: Hmm Where to start up again I wonder.
« Reply #2 on: October 26, 2012, 08:46:34 pm »
On your first point I have no idea how much if any C++11 Visual Studio 2010 supports but I'll look.
Next much of my current code for this has up and disappeared so I'm starting from this code here yet again. :(
The Consts where they shouldn't be will vanish after this post. Thanks for finding the ones I missed. :)
lol funny how the get is contradicting itself. Simple fix of removing the const or just changing to a reference since I want the original object as the target of any changes.

Also many of my includes are in either the header or the stdafx.h file so that's way you don't see them.
The main reason some of this code looks bad is because of it being from a tutorial I was looking through that was quite helpful.  Too bad I lost my version I had fixed up after finishing the tutorial game. :( Oh well been worse off. :)

I'll post up the fixed up version here in a bit. :)
I have many ideas but need the help of others to find way to make use of them.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Hmm Where to start up again I wonder.
« Reply #3 on: October 26, 2012, 09:04:29 pm »
Visual Studio supports C++11 partially.

std::unique_ptr is available in the header <memory>, I strongly recommend to use it where you have new/delete :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: Hmm Where to start up again I wonder.
« Reply #4 on: October 26, 2012, 10:32:49 pm »
If only I knew how to use the std::unique_ptr in this case for object creating and destroying.  Say for example if I want to get rid of a sprite right after it's hit and its bool isdead  is set to false. I still want to be able to create and destroy sprites whenever I need to. 

As you can tell I'm a bit out of it today due to having HTML/CSS and VB codes floating around in my head. (dang College classes :( )
I have many ideas but need the help of others to find way to make use of them.

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: Hmm Where to start up again I wonder.
« Reply #5 on: October 27, 2012, 05:08:58 am »
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.
I have many ideas but need the help of others to find way to make use of them.

 

anything