texture manager with weak ptr

master
Eduardo Bart 13 years ago
parent 4fa659c26a
commit c40b827828

@ -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

@ -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;

@ -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")) {

@ -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;
}; };

@ -105,17 +105,21 @@ int main(int argc, const char *argv[])
// init engine // init engine
g_engine.init(); g_engine.init();
boost::scoped_ptr<MenuState> menuState(new MenuState);
g_engine.changeState(menuState.get());
Platform::showWindow(); // state scope
//Platform::hideMouseCursor(); {
boost::scoped_ptr<MenuState> menuState(new MenuState);
g_engine.changeState(menuState.get());
// main loop, run everything Platform::showWindow();
g_engine.run(); //Platform::hideMouseCursor();
// terminate stuff // main loop, run everything
g_engine.terminate(); g_engine.run();
// terminate stuff
g_engine.terminate();
}
// save configurations before exiting // save configurations before exiting
saveConfigs(); saveConfigs();

Loading…
Cancel
Save