# HG changeset patch # User Thibaut Girka # Date 1312548872 -7200 # Node ID 07a7f28c8aaa4a8577edee8df36aed6cb33e4833 # Parent 58bc264aba38f68622370af6ccc9d3849be251a0 Minor refactoring diff --git a/pytouhou/game/background.py b/pytouhou/game/background.py --- 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 diff --git a/pytouhou/opengl/__init__.py b/pytouhou/opengl/__init__.py new file mode 100644 diff --git a/pytouhou/opengl/texture.py b/pytouhou/opengl/texture.py 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 + diff --git a/stageviewer.py b/stageviewer.py --- 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)