Hey guys im using the
https://github.com/SFML/SFML/wiki/TutorialImageManager for my test code, now at the moment I have it in my game while loop, this works fine if I use all the loading and stuff in my main loop, but I want to make my game code abit more tidy, how can i pass the ImageManager via parameter?
Here is the ImageManager.h
//Image manager header
#ifndef gIMAGEMANAGER_H
#define gIMAGEMANAGER_H
//Includes
#include <SFML/Graphics.hpp>
//Body
class gImageManager
{
public:
gImageManager();
~gImageManager();
private:
gImageManager( const gImageManager& );
gImageManager& operator =( const gImageManager& );
public:
const sf::Texture& get_image( const std::string& filename );
void delete_image( const sf::Texture& image );
void delete_image( const std::string& filename );
void add_resource_directory( const std::string& directory );
void remove_resource_directory( const std::string& directory );
private:
std::map< std::string, sf::Texture > images_;
std::vector< std::string > resource_directories_;
};
//end body
#endif
ImageManager.cpp
#include <map>
#include <iostream>
#include <SFML/Graphics.hpp>
//#include <boost/filesystem.hpp>
#include "gImageManager.h"
gImageManager::gImageManager() : images_(), resource_directories_()
{
}
gImageManager::~gImageManager()
{
images_.clear();
resource_directories_.clear();
}
const sf::Texture& gImageManager::get_image( const std::string& filename )
{
// Check, whether the image already exists
for( std::map<std::string, sf::Texture>::const_iterator it = images_.begin();
it != images_.end();
++it)
{
if( filename == it->first )
{
std::cout << "DEBUG_MESSAGE: " << filename << " using existing image.\n";
return it->second;
}
}
// The image doesen't exists. Create it and save it.
sf::Texture image;
// Search project's main directory
if( image.loadFromFile( filename ) )
{
images_[filename] = image;
std::cout << "DEBUG_MESSAGE: " << filename << " loading image.\n";
return images_[filename];
}
// If the image has still not been found, search all registered directories
for( std::vector< std::string >::iterator it = resource_directories_.begin();
it != resource_directories_.end();
++it )
{
if( image.loadFromFile( (*it) + filename ) )
{
images_[filename] = image;
std::cout << "DEBUG_MESSAGE: " << filename << " loading image.\n";
return images_[filename];
}
}
std::cout << "GAME_ERROR: Image was not found. It is filled with an empty image.\n";
images_[filename] = image;
return images_[filename];
}
void gImageManager::delete_image( const sf::Texture& image )
{
for( std::map<std::string, sf::Texture>::const_iterator it = images_.begin();
it != images_.end();
++it)
{
if( &image == &it->second )
{
images_.erase( it );
return;
}
}
}
void gImageManager::delete_image( const std::string& filename )
{
std::map<std::string, sf::Texture>::const_iterator it = images_.find( filename );
if( it != images_.end() )
images_.erase( it );
}
void gImageManager::add_resource_directory( const std::string& directory )
{
// Check whether the path already exists
for( std::vector<std::string>::const_iterator it = resource_directories_.begin();
it != resource_directories_.end();
++it )
{
// The path exists. So it isn't necessary to add id once more.
if( directory == (*it) )
return;
}
// insert the directory
resource_directories_.push_back( directory );
}
void gImageManager::remove_resource_directory( const std::string& directory )
{
for( std::vector<std::string>::const_iterator it = resource_directories_.begin();
it != resource_directories_.end(); )
{
// The path exists. So it isn't necessary to add id once more.
if( directory == (*it) )
it = resource_directories_.erase( it );
else
++it;
}
}
Here is my while statement, also I tried to declare one in the header, but it seems it goes out of scope once again.
void gState::update()
{
gImageManager imgManage;
while (rtnQuit() == false)
{
currentState += 1;
//Event checker
while(screen.pollEvent(iEvent))
{
if(iEvent.type == sf::Event::Closed)
{
quit = true;
}
//Events
//stateEvents();
}
//State checker
if(currentState == 1)
{
//if(buttons.at(0).rtnState() == 1 && buttons.at(0).rtnID() == 1)
//{
// currentState = 2;
// changeState();
//}
}
//Collision
//stateCollision();
//Update
//stateUpdate();
if(fps.getElapsedTime().asMilliseconds() > frameRate)
{
fps.restart();
}
screen.clear(sf::Color(currentState,currentState,currentState));
screen.draw(button.sprite);
screen.display();
//DRAW
//stateDraw();
}
}
What i want to do is a method will be called, which is changeState and that will be given a reference, so then depending on the state the correct images can be loaded, I could do this in the while loop as i have stated, but It feels to me wrong and well not professional.
Canvas
Its ok, i declared a imgManager in the header and not any where else and it seems to work fine
cheers