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
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_RELEASE "-O2 -Wl,-s")
SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O2 -g")
@ -55,6 +55,7 @@ SET(SOURCES
# framework sources
src/framework/framebuffer.cpp
src/framework/fonts.cpp
src/framework/textureloader.cpp
src/framework/texture.cpp
src/framework/texturemanager.cpp

View File

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

View File

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

View File

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

View File

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