Browse Source

texture manager with weak ptr

Eduardo Bart 10 years ago
parent
commit
c40b827828
5 changed files with 25 additions and 14 deletions
  1. 2
    1
      CMakeLists.txt
  2. 2
    2
      src/framework/fonts.cpp
  3. 6
    2
      src/framework/texturemanager.cpp
  4. 3
    1
      src/framework/texturemanager.h
  5. 12
    8
      src/main.cpp

+ 2
- 1
CMakeLists.txt View File

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

+ 2
- 2
src/framework/fonts.cpp View File

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

+ 6
- 2
src/framework/texturemanager.cpp View File

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

+ 3
- 1
src/framework/texturemanager.h View File

@@ -28,6 +28,8 @@
28 28
 #include "prerequisites.h"
29 29
 #include "texture.h"
30 30
 
31
+typedef boost::weak_ptr<Texture> TextureWeakPtr;
32
+
31 33
 class TextureManager
32 34
 {
33 35
 public:
@@ -38,7 +40,7 @@ public:
38 40
     TexturePtr get(const std::string& textureFile);
39 41
 
40 42
 private:
41
-    typedef std::map<std::string, TexturePtr> TexturesMap;
43
+    typedef std::map<std::string, TextureWeakPtr > TexturesMap;
42 44
     TexturesMap m_texturesMap;
43 45
 };
44 46
 

+ 12
- 8
src/main.cpp View File

@@ -105,17 +105,21 @@ int main(int argc, const char *argv[])
105 105
 
106 106
     // init engine
107 107
     g_engine.init();
108
-    boost::scoped_ptr<MenuState> menuState(new MenuState);
109
-    g_engine.changeState(menuState.get());
110 108
 
111
-    Platform::showWindow();
112
-    //Platform::hideMouseCursor();
109
+    // state scope
110
+    {
111
+        boost::scoped_ptr<MenuState> menuState(new MenuState);
112
+        g_engine.changeState(menuState.get());
113 113
 
114
-    // main loop, run everything
115
-    g_engine.run();
114
+        Platform::showWindow();
115
+        //Platform::hideMouseCursor();
116 116
 
117
-    // terminate stuff
118
-    g_engine.terminate();
117
+        // main loop, run everything
118
+        g_engine.run();
119
+
120
+        // terminate stuff
121
+        g_engine.terminate();
122
+    }
119 123
 
120 124
     // save configurations before exiting
121 125
     saveConfigs();

Loading…
Cancel
Save