changeset 14:07a7f28c8aaa

Minor refactoring
author Thibaut Girka <thib@sitedethib.com>
date Fri, 05 Aug 2011 14:54:32 +0200
parents 58bc264aba38
children 07fba4e1da65
files pytouhou/game/background.py pytouhou/opengl/__init__.py pytouhou/opengl/texture.py stageviewer.py
diffstat 4 files changed, 49 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/game/background.py
+++ b/pytouhou/game/background.py
@@ -10,16 +10,10 @@ from pytouhou.formats.std import Stage
 from pytouhou.formats.anm0 import Animations
 
 
-
 class Background(object):
     def __init__(self, archive, stage_num):
         self.stage = Stage.read(BytesIO(archive.extract('stage%d.std' % stage_num)))
         self.anim = Animations.read(BytesIO(archive.extract('stg%dbg.anm' % stage_num)))
-        texture_components = [None, None]
-        for i, component_name in ((0, self.anim.first_name), (1, self.anim.secondary_name)):
-            if component_name:
-                texture_components[i] = BytesIO(archive.extract(os.path.basename(component_name)))
-        self.texture_components = texture_components
         self.objects = []
         self.object_instances = []
         self._uvs = b''
@@ -137,18 +131,18 @@ class Background(object):
 
         for frame_num, message_type, args in self.stage.script:
             if frame_num == frame:
-                if message_type == 1:
+                if message_type == 0:
+                    self.position_interpolator.set_interpolation_start(frame_num, args)
+                elif message_type == 1:
                     self.fog_interpolator.set_interpolation_end_values(args)
+                elif message_type == 2:
+                    self.position2_interpolator.set_interpolation_end_values(args)
                 elif message_type == 3:
                     duration, = args
                     self.position2_interpolator.set_interpolation_end_frame(frame_num + duration)
                 elif message_type == 4:
                     duration, = args
                     self.fog_interpolator.set_interpolation_end_frame(frame_num + duration)
-                elif message_type == 2:
-                    self.position2_interpolator.set_interpolation_end_values(args)
-            if frame_num <= frame and message_type == 0:
-                self.position_interpolator.set_interpolation_start(frame_num, args)
             if frame_num > frame and message_type == 0:
                 self.position_interpolator.set_interpolation_end(frame_num, args)
                 break
new file mode 100644
new file mode 100644
--- /dev/null
+++ b/pytouhou/opengl/texture.py
@@ -0,0 +1,37 @@
+import pygame
+import os
+from io import BytesIO
+
+import OpenGL
+OpenGL.FORWARD_COMPATIBLE_ONLY = True
+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()
+
+    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]))
+
+    textureData = pygame.image.tostring(textureSurface, 'RGBA', 1)
+
+    width = textureSurface.get_width()
+    height = textureSurface.get_height()
+
+    texture = glGenTextures(1)
+    glBindTexture(GL_TEXTURE_2D, texture)
+
+    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
+        GL_UNSIGNED_BYTE, textureData)
+
+    return texture, width, height
+
--- a/stageviewer.py
+++ b/stageviewer.py
@@ -12,6 +12,7 @@ import pygame
 
 from pytouhou.formats.pbg3 import PBG3
 from pytouhou.game.background import Background
+from pytouhou.opengl.texture import load_texture
 
 import OpenGL
 OpenGL.FORWARD_COMPATIBLE_ONLY = True
@@ -19,36 +20,6 @@ from OpenGL.GL import *
 from OpenGL.GLU import *
 
 
-def load_texture(image, alpha_image=None):
-    #TODO: move elsewhere
-    textureSurface = pygame.image.load(image).convert_alpha()
-
-    if alpha_image:
-        alphaSurface = pygame.image.load(alpha_image)
-        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]))
-
-    textureData = pygame.image.tostring(textureSurface, 'RGBA', 1)
-
-    width = textureSurface.get_width()
-    height = textureSurface.get_height()
-
-    texture = glGenTextures(1)
-    glBindTexture(GL_TEXTURE_2D, texture)
-
-    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
-        GL_UNSIGNED_BYTE, textureData)
-
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
-
-    return texture, width, height
-
-
 def main(path, stage_num):
     # Initialize pygame
     pygame.init()
@@ -59,13 +30,15 @@ def main(path, stage_num):
     glLoadIdentity()
     gluPerspective(30, float(window.get_width())/window.get_height(), 101010101./2010101., 101010101./10101.)
 
-    glHint(GL_FOG_HINT, GL_NICEST)
-    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
     glEnable(GL_DEPTH_TEST)
     glEnable(GL_BLEND)
-    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
     glEnable(GL_TEXTURE_2D)
     glEnable(GL_FOG)
+    glHint(GL_FOG_HINT, GL_NICEST)
+    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
+    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
     glEnableClientState(GL_VERTEX_ARRAY)
     glEnableClientState(GL_TEXTURE_COORD_ARRAY)
 
@@ -73,8 +46,7 @@ def main(path, stage_num):
     with open(path, 'rb') as file:
         archive = PBG3.read(file)
         background = Background(archive, stage_num)
-
-    texture = load_texture(*background.texture_components)
+        texture = load_texture(archive, background.anim)
 
     print(background.stage.name)