changeset 13:58bc264aba38

Refactor
author Thibaut Girka <thib@sitedethib.com>
date Fri, 05 Aug 2011 13:23:33 +0200
parents 776453783743
children 07a7f28c8aaa
files pytouhou/formats/std.py pytouhou/game/__init__.py pytouhou/game/background.py pytouhou/utils/interpolator.py stageviewer.py
diffstat 5 files changed, 230 insertions(+), 172 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/formats/std.py
+++ b/pytouhou/formats/std.py
@@ -79,8 +79,21 @@ class Stage(object):
             if size != 0x0c:
                 raise Exception #TODO
             data = file.read(12)
-            #TODO: maybe add another class for instructions...
-            stage.script.append((frame, message_type, data))
+            #TODO: maybe add a name somewhere
+            if message_type == 0: # ViewPos
+                args = unpack('<fff', data)
+            elif message_type == 1: # Color
+                args = unpack('<BBBBff', data)
+            elif message_type == 2: # ViewPos2
+                args = unpack('<Iff', data)
+            elif message_type == 3:  # StartInterpolatingViewPos2
+                args = tuple(unpack('<III', data)[:1])
+            elif message_type == 4: # StartInterpolatingFog
+                args = tuple(unpack('<III', data)[:1])
+            else:
+                args = (data,)
+                print('Warning: unknown opcode %d' % message_type) #TODO
+            stage.script.append((frame, message_type, args))
 
         return stage
 
new file mode 100644
new file mode 100644
--- /dev/null
+++ b/pytouhou/game/background.py
@@ -0,0 +1,159 @@
+from io import BytesIO
+import os
+import struct
+from itertools import chain
+
+from pytouhou.utils.matrix import Matrix
+from pytouhou.utils.interpolator import Interpolator
+
+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''
+        self._vertices = b''
+        self.build_objects()
+        self.build_object_instances()
+
+
+    def build_object_instances(self):
+        self.object_instances = []
+        for obj, ox, oy, oz in self.stage.object_instances:
+            obj_id = self.stage.objects.index(obj)
+
+            obj_instance = []
+            for face_vertices, face_uvs in self.objects[obj_id]:
+                obj_instance.append((tuple((x + ox, y + oy, z + oz)
+                                        for x, y, z in face_vertices),
+                                    face_uvs))
+            self.object_instances.append(obj_instance)
+        # Z-sorting
+        def keyfunc(obj):
+            return min(z for face in obj for x, y, z in face[0])
+        self.object_instances.sort(key=keyfunc, reverse=True)
+
+
+    def object_instances_to_vertices_uvs(self):
+        vertices = tuple(vertex for obj in self.object_instances
+                            for face in obj for vertex in face[0])
+        uvs = tuple(uv for obj in self.object_instances
+                            for face in obj for uv in face[1])
+        return vertices, uvs
+
+
+    def build_objects(self):
+        self.objects = []
+        for i, obj in enumerate(self.stage.objects):
+            faces = []
+            for script_index, x, y, z, width_override, height_override in obj.quads:
+                #TODO: refactor
+                vertices = []
+                uvs = []
+                vertmat = Matrix()
+                vertmat.data[0][0] = -.5
+                vertmat.data[1][0] = -.5
+
+                vertmat.data[0][1] = .5
+                vertmat.data[1][1] = -.5
+
+                vertmat.data[0][2] = .5
+                vertmat.data[1][2] = .5
+
+                vertmat.data[0][3] = -.5
+                vertmat.data[1][3] = .5
+
+                for i in range(4):
+                    vertmat.data[2][i] = 0.
+                    vertmat.data[3][i] = 1.
+
+                properties = {}
+                for time, instr_type, data in self.anim.scripts[script_index]:
+                    if instr_type == 15:
+                        properties[15] = b''
+                        break
+                    elif time == 0: #TODO
+                        properties[instr_type] = data
+                #if 15 not in properties: #TODO: Skip properties
+                #    continue
+
+                #TODO: properties 3 and 4
+                if 1 in properties:
+                    tx, ty, tw, th = self.anim.sprites[struct.unpack('<I', properties[1])[0]]
+                width, height = 1., 1.
+                if 2 in properties:
+                    width, height = struct.unpack('<ff', properties[2])
+                width = width_override or width * tw
+                height = height_override or height * th
+                transform = Matrix.get_scaling_matrix(width, height, 1.)
+                if 7 in properties:
+                    transform = Matrix.get_scaling_matrix(-1., 1., 1.).mult(transform)
+                if 9 in properties:
+                    rx, ry, rz = struct.unpack('<fff', properties[9])
+                    transform = Matrix.get_rotation_matrix(-rx, 'x').mult(transform)
+                    transform = Matrix.get_rotation_matrix(ry, 'y').mult(transform)
+                    transform = Matrix.get_rotation_matrix(-rz, 'z').mult(transform) #TODO: minus, really?
+                if 23 in properties: # Reposition
+                    transform = Matrix.get_translation_matrix(width / 2., height / 2., 0.).mult(transform)
+                vertmat = transform.mult(vertmat)
+
+                uvs = [(tx / self.anim.size[0],         1. - (ty / self.anim.size[1])),
+                       ((tx + tw) / self.anim.size[0],  1. - (ty / self.anim.size[1])),
+                       ((tx + tw) / self.anim.size[0],  1. - ((ty + th) / self.anim.size[1])),
+                       (tx / self.anim.size[0],         1. - ((ty + th) / self.anim.size[1]))]
+
+                for i in xrange(4):
+                    w = vertmat.data[3][i]
+                    vertices.append((vertmat.data[0][i] / w + x,
+                                     vertmat.data[1][i] / w + y,
+                                     vertmat.data[2][i] / w + z))
+                faces.append((vertices, uvs))
+            self.objects.append(faces)
+
+
+    def update(self, frame):
+        if not self._uvs or not self._vertices:
+            vertices, uvs = self.object_instances_to_vertices_uvs()
+            self.nb_vertices = len(vertices)
+            vertices_format = 'f' * (3 * self.nb_vertices)
+            uvs_format = 'f' * (2 * self.nb_vertices)
+            self._vertices = struct.pack(vertices_format, *chain(*vertices))
+            self._uvs = struct.pack(uvs_format, *chain(*uvs))
+
+            self.position_interpolator = Interpolator((0, 0, 0))
+            self.fog_interpolator = Interpolator((0, 0, 0, 0, 0))
+            self.position2_interpolator = Interpolator((0, 0, 0))
+
+        for frame_num, message_type, args in self.stage.script:
+            if frame_num == frame:
+                if message_type == 1:
+                    self.fog_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
+
+        self.position2_interpolator.update(frame)
+        self.fog_interpolator.update(frame)
+        self.position_interpolator.update(frame)
+
new file mode 100644
--- /dev/null
+++ b/pytouhou/utils/interpolator.py
@@ -0,0 +1,36 @@
+class Interpolator(object):
+    def __init__(self, values=()):
+        self.values = tuple(values)
+        self.start_values = tuple(values)
+        self.end_values = tuple(values)
+        self.start_frame = 0
+        self.end_frame = 0
+
+
+    def set_interpolation_start(self, frame, values):
+        self.start_values = tuple(values)
+        self.start_frame = frame
+
+
+    def set_interpolation_end(self, frame, values):
+        self.end_values = tuple(values)
+        self.end_frame = frame
+
+
+    def set_interpolation_end_frame(self, end_frame):
+        self.end_frame = end_frame
+
+
+    def set_interpolation_end_values(self, values):
+        self.end_values = tuple(values)
+
+
+    def update(self, frame):
+        if frame >= self.end_frame:
+            self.values = tuple(self.end_values)
+            self.start_values = tuple(self.end_values)
+            self.start_frame = frame
+        else:
+            truc = float(frame - self.start_frame) / float(self.end_frame - self.start_frame)
+            self.values = tuple(start_value + truc * (end_value - start_value)
+                                for (start_value, end_value) in zip(self.start_values, self.end_values))
--- a/stageviewer.py
+++ b/stageviewer.py
@@ -10,19 +10,17 @@ from itertools import chain
 
 import pygame
 
+from pytouhou.formats.pbg3 import PBG3
+from pytouhou.game.background import Background
+
 import OpenGL
 OpenGL.FORWARD_COMPATIBLE_ONLY = True
 from OpenGL.GL import *
 from OpenGL.GLU import *
 
-from pytouhou.formats.pbg3 import PBG3
-from pytouhou.formats.std import Stage
-from pytouhou.formats.anm0 import Animations
-
-from pytouhou.utils.matrix import Matrix
-
 
 def load_texture(image, alpha_image=None):
+    #TODO: move elsewhere
     textureSurface = pygame.image.load(image).convert_alpha()
 
     if alpha_image:
@@ -51,85 +49,6 @@ def load_texture(image, alpha_image=None
     return texture, width, height
 
 
-
-def build_objects_faces(stage, anim):
-    objects_faces = []
-    for i, obj in enumerate(stage.objects):
-        faces = []
-        for script_index, x, y, z, width_override, height_override in obj.quads:
-            #TODO: move mof of it elsewhere
-            vertices = []
-            uvs = []
-            vertmat = Matrix()
-            vertmat.data[0][0] = -.5
-            vertmat.data[1][0] = -.5
-
-            vertmat.data[0][1] = .5
-            vertmat.data[1][1] = -.5
-
-            vertmat.data[0][2] = .5
-            vertmat.data[1][2] = .5
-
-            vertmat.data[0][3] = -.5
-            vertmat.data[1][3] = .5
-
-            for i in range(4):
-                vertmat.data[2][i] = 0.
-                vertmat.data[3][i] = 1.
-
-            properties = {}
-            for time, instr_type, data in anim.scripts[script_index]:
-                if instr_type == 15:
-                    properties[15] = b''
-                    break
-                elif time == 0: #TODO
-                    properties[instr_type] = data
-            #if 15 not in properties: #TODO: Skip properties
-            #    continue
-
-            #TODO: properties 3 and 4
-            if 1 in properties:
-                tx, ty, tw, th = anim.sprites[struct.unpack('<I', properties[1])[0]]
-            width, height = 1., 1.
-            if 2 in properties:
-                width, height = struct.unpack('<ff', properties[2])
-            width = width_override or width * tw
-            height = height_override or height * th
-            transform = Matrix.get_scaling_matrix(width, height, 1.)
-            if 7 in properties:
-                transform = Matrix.get_scaling_matrix(-1., 1., 1.).mult(transform)
-            if 9 in properties:
-                rx, ry, rz = struct.unpack('<fff', properties[9])
-                transform = Matrix.get_rotation_matrix(-rx, 'x').mult(transform)
-                transform = Matrix.get_rotation_matrix(ry, 'y').mult(transform)
-                transform = Matrix.get_rotation_matrix(-rz, 'z').mult(transform) #TODO: minus, really?
-            if 23 in properties: # Reposition
-                transform = Matrix.get_translation_matrix(width / 2., height / 2., 0.).mult(transform)
-
-            transform = Matrix.get_translation_matrix(x, y, z).mult(transform)
-            vertmat = transform.mult(vertmat)
-
-            uvs = [(tx / anim.size[0],         1. - (ty / anim.size[1])),
-                   ((tx + tw) / anim.size[0],  1. - (ty / anim.size[1])),
-                   ((tx + tw) / anim.size[0],  1. - ((ty + th) / anim.size[1])),
-                   (tx / anim.size[0],         1. - ((ty + th) / anim.size[1]))]
-
-            for i in xrange(4):
-                w = vertmat.data[3][i]
-                vertices.append((vertmat.data[0][i] / w, vertmat.data[1][i] / w, vertmat.data[2][i] / w))
-            faces.append((vertices, uvs))
-        objects_faces.append(faces)
-    return objects_faces
-
-
-
-def objects_faces_to_vertices_uvs(objects):
-    vertices = tuple(vertex for obj in objects for face in obj for vertex in face[0]) #TODO: check
-    uvs = tuple(uv for obj in objects for face in obj for uv in face[1]) #TODO: check
-    return vertices, uvs
-
-
-
 def main(path, stage_num):
     # Initialize pygame
     pygame.init()
@@ -153,49 +72,18 @@ def main(path, stage_num):
     # Load data
     with open(path, 'rb') as file:
         archive = PBG3.read(file)
-        stage = Stage.read(BytesIO(archive.extract('stage%d.std' % stage_num)))
-        anim = Animations.read(BytesIO(archive.extract('stg%dbg.anm' % stage_num)))
-        textures_components = [None, None]
-        for i, component_name in enumerate((anim.first_name, anim.secondary_name)):
-            if component_name:
-                textures_components[i] = BytesIO(archive.extract(os.path.basename(component_name)))
-        texture = load_texture(*textures_components)
+        background = Background(archive, stage_num)
 
-    print(stage.name)
-
-    uvs = []
-    vertices = []
-    objects_faces = build_objects_faces(stage, anim)
-    objects_instances_faces = []
-    for obj, ox, oy, oz in stage.object_instances:
-        obj_id = stage.objects.index(obj)
+    texture = load_texture(*background.texture_components)
 
-        obj_instance = []
-        for face_vertices, face_uvs in objects_faces[obj_id]:
-            obj_instance.append((tuple((x + ox, y + oy, z + oz) for x, y, z in face_vertices),
-                                face_uvs))
-        objects_instances_faces.append(obj_instance)
-
-    def keyfunc(obj):
-        return min(z for face in obj for x, y, z in face[0])
-    objects_instances_faces.sort(key=keyfunc, reverse=True)
+    print(background.stage.name)
 
-    vertices, uvs = objects_faces_to_vertices_uvs(objects_instances_faces)
-    nb_vertices = len(vertices)
-    vertices_format = 'f' * (3 * nb_vertices)
-    uvs_format = 'f' * (2 * nb_vertices)
-    vertices, uvs = objects_faces_to_vertices_uvs(objects_instances_faces)
-    glVertexPointer(3, GL_FLOAT, 0, struct.pack(vertices_format, *chain(*vertices)))
-    glTexCoordPointer(2, GL_FLOAT, 0, struct.pack(uvs_format, *chain(*uvs)))
-
-    x, y, z = 0, 0, 0
     frame = 0
-    interpolation = 0, 0, 0
-    interpolation2 = 0, 0, 0
 
     # Main loop
     clock = pygame.time.Clock()
     while True:
+        # Check events
         for event in pygame.event.get():
             if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key in (pygame.K_ESCAPE, pygame.K_q)):
                 sys.exit(0)
@@ -203,63 +91,25 @@ def main(path, stage_num):
                 if event.key == pygame.K_RETURN and event.mod & pygame.KMOD_ALT:
                     pygame.display.toggle_fullscreen()
 
+        # Update game
+        background.update(frame)
+
+        # Draw everything
         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
 
-        for frame_num, message_type, data in stage.script:
-            if frame_num == frame and message_type == 1:
-                #TODO: move interpolation elsewhere
-                next_fog_b, next_fog_g, next_fog_r, _, next_fog_start, next_fog_end = struct.unpack('<BBBBff', data)
-            if frame_num == frame and message_type == 3:
-                duration, junk1, junk2 = struct.unpack('<III', data)
-                interpolation = frame_num, duration, frame_num + duration
-                old_unknownx, old_dy, old_dz = unknownx, dy, dz
-            if frame_num == frame and message_type == 4:
-                duration, junk1, junk2 = struct.unpack('<III', data)
-                interpolation2 = frame_num, duration, frame_num + duration
-                old_fog_b, old_fog_g, old_fog_r, old_fog_start, old_fog_end = fog_b, fog_g, fog_r, fog_start, fog_end
-            if frame_num <= frame and message_type == 0:
-                last_message = frame_num, message_type, data
-            if frame_num <= frame and message_type == 2:
-                next_unknownx, next_dy, next_dz = struct.unpack('<fff', data)
-            if frame_num > frame and message_type == 0:
-                next_message = frame_num, message_type, data
-                break
+        glVertexPointer(3, GL_FLOAT, 0, background._vertices)
+        glTexCoordPointer(2, GL_FLOAT, 0, background._uvs)
 
-        if frame < interpolation[2]:
-            truc = float(frame - interpolation[0]) / interpolation[1]
-            unknownx = old_unknownx + (next_unknownx - old_unknownx) * truc
-            dy = old_dy + (next_dy - old_dy) * truc
-            dz = old_dz + (next_dz - old_dz) * truc
-        else:
-            unknownx, dy, dz = next_unknownx, next_dy, next_dz
-
-        if frame < interpolation2[2]:
-            truc = float(frame - interpolation2[0]) / interpolation2[1]
-            fog_b = old_fog_b + (next_fog_b - old_fog_b) * truc
-            fog_g = old_fog_g + (next_fog_g - old_fog_g) * truc
-            fog_r = old_fog_r + (next_fog_r - old_fog_r) * truc
-            fog_start = old_fog_start + (next_fog_start - old_fog_start) * truc
-            fog_end = old_fog_end + (next_fog_end - old_fog_end) * truc
-        else:
-            fog_r, fog_g, fog_b, fog_start, fog_end = next_fog_r, next_fog_g, next_fog_b, next_fog_start, next_fog_end
-
+        fog_b, fog_g, fog_r, _, fog_start, fog_end = background.fog_interpolator.values
+        x, y, z = background.position_interpolator.values
+        unknownx, dy, dz = background.position2_interpolator.values
 
         glFogi(GL_FOG_MODE, GL_LINEAR)
         glFogf(GL_FOG_START, fog_start)
-        glFogf(GL_FOG_END, fog_end)
+        glFogf(GL_FOG_END,  fog_end)
         glFogfv(GL_FOG_COLOR, (fog_r / 255., fog_g / 255., fog_b / 255., 1.))
 
-
-        x1, y1, z1 = struct.unpack('<fff', last_message[2])
-        x2, y2, z2 = struct.unpack('<fff', next_message[2])
-
-        truc = (float(frame) - last_message[0]) / (next_message[0] - last_message[0])
-
-        x = x1 + (x2 - x1) * truc
-        y = y1 + (y2 - y1) * truc
-        z = z1 + (z2 - z1) * truc
-
-
+        #TODO
         glMatrixMode(GL_MODELVIEW)
         glLoadIdentity()
         # Some explanations on the magic constants:
@@ -272,7 +122,7 @@ def main(path, stage_num):
         #print(glGetFloat(GL_MODELVIEW_MATRIX))
         glTranslatef(-x, -y, -z)
 
-        glDrawArrays(GL_QUADS, 0, nb_vertices)
+        glDrawArrays(GL_QUADS, 0, background.nb_vertices)
 
         #TODO: show the game itself
         # It is displayed on (0, 0, 0), (0, 448, 0), (388, 448, 0), (388, 0, 0)