texture manager with weak ptr

This commit is contained in:
Eduardo Bart 2011-04-02 16:59:49 -03:00
parent 4fa659c26a
commit c40b827828
5 changed files with 25 additions and 14 deletions

View File

@ -20,7 +20,7 @@ MESSAGE(STATUS "BUILD TYPE: " ${CMAKE_BUILD_TYPE})
# setup compiler options # setup compiler options
IF(CMAKE_COMPILER_IS_GNUCXX) IF(CMAKE_COMPILER_IS_GNUCXX)
SET(CMAKE_CXX_FLAGS "-Wall -Wextra -Werror -Wno-unused-parameter") SET(CMAKE_CXX_FLAGS "-Wall -Wextra -Werror -Wno-unused-parameter -std=c++0x")
SET(CMAKE_CXX_FLAGS_DEBUG "-O1 -g -ggdb -fno-inline") SET(CMAKE_CXX_FLAGS_DEBUG "-O1 -g -ggdb -fno-inline")
SET(CMAKE_CXX_FLAGS_RELEASE "-O2 -Wl,-s") SET(CMAKE_CXX_FLAGS_RELEASE "-O2 -Wl,-s")
SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g") SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
@ -55,6 +55,7 @@ SET(SOURCES
# framework sources # framework sources
src/framework/framebuffer.cpp src/framework/framebuffer.cpp
src/framework/fonts.cpp
src/framework/textureloader.cpp src/framework/textureloader.cpp
src/framework/texture.cpp src/framework/texture.cpp
src/framework/texturemanager.cpp src/framework/texturemanager.cpp

View File

@ -14,8 +14,8 @@ Fonts::~Fonts()
bool Fonts::load() bool Fonts::load()
{ {
std::list<std::string> files = g_resources.getDirectoryFiles("fonts"); std::list<std::string> files = g_resources.getDirectoryFiles("fonts");
for(std::list<std::string>::iterator it = files.begin(), end = files.end(); it != end; ++it) { foreach(const std::string& file, files) {
notice("File: %s", (*it).c_str()); notice("File: %s", file.c_str());
} }
return true; return true;

View File

@ -44,8 +44,12 @@ TexturePtr TextureManager::get(const std::string& textureFile)
// check if the texture is already loaded // check if the texture is already loaded
TexturesMap::iterator it = m_texturesMap.find(textureFile); TexturesMap::iterator it = m_texturesMap.find(textureFile);
if(it != m_texturesMap.end()) if(it != m_texturesMap.end()) {
texture = it->second; if(it->second.expired())
m_texturesMap.erase(it);
else
texture = it->second.lock();
}
else { // load texture else { // load texture
// currently only png textures are supported // currently only png textures are supported
if(!boost::ends_with(textureFile, ".png")) { if(!boost::ends_with(textureFile, ".png")) {

View File

@ -28,6 +28,8 @@
#include "prerequisites.h" #include "prerequisites.h"
#include "texture.h" #include "texture.h"
typedef boost::weak_ptr<Texture> TextureWeakPtr;
class TextureManager class TextureManager
{ {
public: public:
@ -38,7 +40,7 @@ public:
TexturePtr get(const std::string& textureFile); TexturePtr get(const std::string& textureFile);
private: private:
typedef std::map<std::string, TexturePtr> TexturesMap; typedef std::map<std::string, TextureWeakPtr > TexturesMap;
TexturesMap m_texturesMap; TexturesMap m_texturesMap;
}; };

View File

@ -105,6 +105,9 @@ int main(int argc, const char *argv[])
// init engine // init engine
g_engine.init(); g_engine.init();
// state scope
{
boost::scoped_ptr<MenuState> menuState(new MenuState); boost::scoped_ptr<MenuState> menuState(new MenuState);
g_engine.changeState(menuState.get()); g_engine.changeState(menuState.get());
@ -116,6 +119,7 @@ int main(int argc, const char *argv[])
// terminate stuff // terminate stuff
g_engine.terminate(); g_engine.terminate();
}
// save configurations before exiting // save configurations before exiting
saveConfigs(); saveConfigs();