diff pytouhou/opengl/texture.py @ 16:66ce9bb440ac

Refactor in order to support multiple textures
author Thibaut Girka <thib@sitedethib.com>
date Sat, 06 Aug 2011 12:36:25 +0200
parents 07fba4e1da65
children cc864aadc733
line wrap: on
line diff
--- a/pytouhou/opengl/texture.py
+++ b/pytouhou/opengl/texture.py
@@ -8,33 +8,50 @@ from OpenGL.GL import *
 from OpenGL.GLU import *
 
 
-def load_texture(archive, anim):
-    image_file = BytesIO(archive.extract(os.path.basename(anim.first_name)))
-    textureSurface = pygame.image.load(image_file).convert_alpha()
+class TextureManager(object):
+    def __init__(self, archive):
+        self.archive = archive
+        self.textures = {}
+
+    def __getitem__(self, key):
+        if not key in self.textures:
+            self.textures[key] = self.load_texture(key)
+        return self.textures[key]
 
-    if anim.secondary_name:
-        alpha_image_file = BytesIO(archive.extract(os.path.basename(anim.secondary_name)))
-        alphaSurface = pygame.image.load(alpha_image_file)
-        assert textureSurface.get_size() == alphaSurface.get_size()
-        for x in range(alphaSurface.get_width()):
-            for y in range(alphaSurface.get_height()):
-                r, g, b, a = textureSurface.get_at((x, y))
-                color2 = alphaSurface.get_at((x, y))
-                textureSurface.set_at((x, y), (r, g, b, color2[0]))
+
+    def set_archive(self, archive):
+        self.archive = archive
+
+
+    def load_texture(self, key):
+        first_name, secondary_name = key
+
+        image_file = BytesIO(self.archive.extract(os.path.basename(first_name)))
+        textureSurface = pygame.image.load(image_file).convert_alpha()
 
-    textureData = pygame.image.tostring(textureSurface, 'RGBA', 1)
+        if secondary_name:
+            alpha_image_file = BytesIO(self.archive.extract(os.path.basename(secondary_name)))
+            alphaSurface = pygame.image.load(alpha_image_file)
+            assert textureSurface.get_size() == alphaSurface.get_size()
+            for x in range(alphaSurface.get_width()):
+                for y in range(alphaSurface.get_height()):
+                    r, g, b, a = textureSurface.get_at((x, y))
+                    color2 = alphaSurface.get_at((x, y))
+                    textureSurface.set_at((x, y), (r, g, b, color2[0]))
 
-    width = textureSurface.get_width()
-    height = textureSurface.get_height()
-
-    texture = glGenTextures(1)
-    glBindTexture(GL_TEXTURE_2D, texture)
+        textureData = pygame.image.tostring(textureSurface, 'RGBA', 1)
 
-    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
-        GL_UNSIGNED_BYTE, textureData)
+        width = textureSurface.get_width()
+        height = textureSurface.get_height()
+
+        texture = glGenTextures(1)
+        glBindTexture(GL_TEXTURE_2D, texture)
 
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
+        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
+            GL_UNSIGNED_BYTE, textureData)
 
-    return texture, width, height
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
+        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
 
+        return texture
+