# HG changeset patch # User Thibaut Girka # Date 1312543413 -7200 # Node ID 58bc264aba38f68622370af6ccc9d3849be251a0 # Parent 776453783743bb3919ce566256a09cf8b7ebd217 Refactor diff --git a/pytouhou/formats/std.py b/pytouhou/formats/std.py --- 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(' 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) + diff --git a/pytouhou/utils/interpolator.py b/pytouhou/utils/interpolator.py 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)) diff --git a/stageviewer.py b/stageviewer.py --- 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(' 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('