changeset 504:69c73023f7a0

Make ANM garbage collectable.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 23 Oct 2013 18:24:17 +0200
parents c622eaf64428
children bfea9e9a6845
files pytouhou/resource/loader.py pytouhou/ui/gamerunner.pyx pytouhou/ui/texture.pyx
diffstat 3 files changed, 13 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/resource/loader.py
+++ b/pytouhou/resource/loader.py
@@ -101,7 +101,8 @@ class Loader(object):
         self.exe_files = []
         self.game_dir = game_dir
         self.known_files = {}
-        self.instanced_anms = {} #TODO: remove it someday.
+        self.instanced_anms = {}  # Cache for the textures.
+        self.loaded_anms = []  # For the double loading warnings.
 
 
     def scan_archives(self, paths_lists):
@@ -136,10 +137,13 @@ class Loader(object):
 
 
     def get_anm(self, name):
-        if name not in self.instanced_anms:
-            file = self.get_file(name)
-            self.instanced_anms[name] = ANM0.read(file)
-        return self.instanced_anms[name]
+        if name in self.loaded_anms:
+            logger.warn('ANM0 %s already loaded', name)
+        file = self.get_file(name)
+        anm = ANM0.read(file)
+        self.instanced_anms[name] = anm
+        self.loaded_anms.append(name)
+        return anm
 
 
     def get_stage(self, name):
--- a/pytouhou/ui/gamerunner.pyx
+++ b/pytouhou/ui/gamerunner.pyx
@@ -49,7 +49,7 @@ cdef class GameRunner(Runner):
         self.game = game
         self.background = background
 
-        self.renderer.texture_manager.load(self.resource_loader.instanced_anms.values())
+        self.renderer.texture_manager.load(self.resource_loader.instanced_anms)
         self.renderer.load_background(background)
 
         self.set_input(replay)
--- a/pytouhou/ui/texture.pyx
+++ b/pytouhou/ui/texture.pyx
@@ -39,8 +39,8 @@ class TextureManager(object):
         self.renderer = renderer
 
 
-    def load(self, anm_list):
-        for anm in sorted(anm_list, key=lambda x: x[0].first_name.endswith('ascii.png')):
+    def load(self, anms):
+        for anm in sorted(anms.values(), key=lambda x: x[0].first_name.endswith('ascii.png')):
             for entry in anm:
                 if not hasattr(entry, 'texture'):
                     texture = decode_png(self.loader, entry.first_name, entry.secondary_name)
@@ -49,6 +49,7 @@ class TextureManager(object):
                     entry.texture = load_texture(entry.texture)
                 self.renderer.add_texture(entry.texture)
                 entry.texture.renderer = self.renderer
+        anms.clear()
 
 
 cdef class FontManager: