changeset 92:85f3b8ba3f24

Minor refactoring and optimizations. Drop stageviewer.
author Thibaut Girka <thib@sitedethib.com>
date Sun, 04 Sep 2011 17:33:40 +0200
parents f7525fa66bb0
children d167280a82fc
files eclviewer.py pytouhou/game/background.py pytouhou/game/sprite.py stageviewer.py
diffstat 4 files changed, 28 insertions(+), 160 deletions(-) [+]
line wrap: on
line diff
--- a/eclviewer.py
+++ b/eclviewer.py
@@ -154,8 +154,10 @@ def main(path, stage_num):
         glTranslatef(-x, -y, -z)
 
         glEnable(GL_DEPTH_TEST)
-        for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in background.objects_by_texture.items():
-            glBlendFunc(GL_SRC_ALPHA, {0: GL_ONE_MINUS_SRC_ALPHA, 1: GL_ONE}[blendfunc])
+        objects_by_texture = {}
+        background.get_objects_by_texture(objects_by_texture)
+        for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in objects_by_texture.items():
+            glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc])
             glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key])
             glVertexPointer(3, GL_FLOAT, 0, vertices)
             glTexCoordPointer(2, GL_FLOAT, 0, uvs)
@@ -175,12 +177,15 @@ def main(path, stage_num):
                   192., 224., 0., 0., -1., 0.)
 
         glDisable(GL_FOG)
-        for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in enemy_manager.objects_by_texture.items():
-            glBlendFunc(GL_SRC_ALPHA, {0: GL_ONE_MINUS_SRC_ALPHA, 1: GL_ONE}[blendfunc])
+        objects_by_texture = {}
+        enemy_manager.get_objects_by_texture(objects_by_texture)
+        for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in objects_by_texture.items():
+            nb_vertices = len(vertices)
+            glBlendFunc(GL_SRC_ALPHA, (GL_ONE_MINUS_SRC_ALPHA, GL_ONE)[blendfunc])
             glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key])
-            glVertexPointer(3, GL_FLOAT, 0, vertices)
-            glTexCoordPointer(2, GL_FLOAT, 0, uvs)
-            glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors)
+            glVertexPointer(3, GL_FLOAT, 0, struct.pack(str(3 * nb_vertices) + 'f', *chain(*vertices)))
+            glTexCoordPointer(2, GL_FLOAT, 0, struct.pack(str(2 * nb_vertices) + 'f', *chain(*uvs)))
+            glColorPointer(4, GL_UNSIGNED_BYTE, 0, struct.pack(str(4 * nb_vertices) + 'B', *chain(*colors)))
             glDrawArrays(GL_QUADS, 0, nb_vertices)
         glEnable(GL_FOG)
 
--- a/pytouhou/game/background.py
+++ b/pytouhou/game/background.py
@@ -85,6 +85,10 @@ class Background(object):
             self.objects.append(faces)
 
 
+    def get_objects_by_texture(self, objects_by_texture):
+        objects_by_texture.update(self.objects_by_texture)
+
+
     def update(self, frame):
         if not self.objects_by_texture:
             vertices, uvs, colors = self.object_instances_to_vertices_uvs_colors()
--- a/pytouhou/game/sprite.py
+++ b/pytouhou/game/sprite.py
@@ -160,19 +160,19 @@ class Sprite(object):
 
 
     def update(self, override_width=0, override_height=0, angle_base=0., force_rotation=False):
-        self._changed = (self._changed
-                         or override_width != self.width_override
-                         or override_height != self.height_override
-                         or self.angle != angle_base
-                         or self.force_rotation != force_rotation
-                         or self.scale_interpolator
-                         or self.fade_interpolator
-                         or self.offset_interpolator)
+        if (override_width != self.width_override
+            or override_height != self.height_override
+            or self.angle != angle_base
+            or self.force_rotation != force_rotation
+            or self.scale_interpolator
+            or self.fade_interpolator
+            or self.offset_interpolator):
 
-        self.width_override = override_width
-        self.height_override = override_height
-        self.angle = angle_base
-        self.force_rotation = force_rotation
+            self._changed = True
+            self.width_override = override_width
+            self.height_override = override_height
+            self.angle = angle_base
+            self.force_rotation = force_rotation
 
         if self.rotations_speed_3d != (0., 0., 0.) or self.scale_speed != (0., 0.):
             ax, ay, az = self.rotations_3d
deleted file mode 100644
--- a/stageviewer.py
+++ /dev/null
@@ -1,141 +0,0 @@
-#!/usr/bin/env python
-# -*- encoding: utf-8 -*-
-##
-## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
-##
-## This program is free software; you can redistribute it and/or modify
-## it under the terms of the GNU General Public License as published
-## by the Free Software Foundation; version 3 only.
-##
-## This program is distributed in the hope that it will be useful,
-## but WITHOUT ANY WARRANTY; without even the implied warranty of
-## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-## GNU General Public License for more details.
-##
-
-import sys
-import os
-
-import struct
-from math import degrees, radians
-from io import BytesIO
-from itertools import chain
-
-import pygame
-
-from pytouhou.formats.pbg3 import PBG3
-from pytouhou.formats.std import Stage
-from pytouhou.formats.anm0 import Animations
-from pytouhou.game.sprite import AnmWrapper
-from pytouhou.game.background import Background
-from pytouhou.opengl.texture import TextureManager
-
-import OpenGL
-OpenGL.FORWARD_COMPATIBLE_ONLY = True
-from OpenGL.GL import *
-from OpenGL.GLU import *
-
-
-from reflexions.helpers import get_logger
-
-logger = get_logger(__name__)
-
-
-def main(path, stage_num):
-    # Initialize pygame
-    pygame.init()
-    window = pygame.display.set_mode((384, 448), pygame.OPENGL | pygame.DOUBLEBUF)
-
-    # Initialize OpenGL
-    glMatrixMode(GL_PROJECTION)
-    glLoadIdentity()
-    gluPerspective(30, float(window.get_width())/window.get_height(), 101010101./2010101., 101010101./10101.)
-
-    glEnable(GL_DEPTH_TEST)
-    glEnable(GL_BLEND)
-    glEnable(GL_TEXTURE_2D)
-    glEnable(GL_FOG)
-    glHint(GL_FOG_HINT, GL_NICEST)
-    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
-    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
-    glEnableClientState(GL_COLOR_ARRAY)
-    glEnableClientState(GL_VERTEX_ARRAY)
-    glEnableClientState(GL_TEXTURE_COORD_ARRAY)
-
-    # Load data
-    with open(path, 'rb') as file:
-        archive = PBG3.read(file)
-        texture_manager = TextureManager(archive)
-
-        stage = Stage.read(BytesIO(archive.extract('stage%d.std' % stage_num)), stage_num)
-        background_anim = Animations.read(BytesIO(archive.extract('stg%dbg.anm' % stage_num)))
-        background = Background(stage, AnmWrapper((background_anim,)))
-
-        print(background.stage.name)
-
-        frame = 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)
-                elif event.type == pygame.KEYDOWN:
-                    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)
-
-            fog_b, fog_g, fog_r, _, fog_start, fog_end = background.fog_interpolator.values
-            x, y, z = background.position_interpolator.values
-            dx, dy, dz = background.position2_interpolator.values
-
-            glFogi(GL_FOG_MODE, GL_LINEAR)
-            glFogf(GL_FOG_START, fog_start)
-            glFogf(GL_FOG_END,  fog_end)
-            glFogfv(GL_FOG_COLOR, (fog_r / 255., fog_g / 255., fog_b / 255., 1.))
-
-            #TODO
-            glMatrixMode(GL_MODELVIEW)
-            glLoadIdentity()
-            # Some explanations on the magic constants:
-            # 192. = 384. / 2. = width / 2.
-            # 224. = 448. / 2. = height / 2.
-            # 835.979370 = 224./math.tan(math.radians(15)) = (height/2.)/math.tan(math.radians(fov/2))
-            # This is so that objects on the (O, x, y) plane use pixel coordinates
-            gluLookAt(192., 224., - 835.979370 * dz,
-                      192. + dx, 224. - dy, 0., 0., -1., 0.)
-            glTranslatef(-x, -y, -z)
-
-            for texture_key, (nb_vertices, vertices, uvs, colors) in background.objects_by_texture.items():
-                glBindTexture(GL_TEXTURE_2D, texture_manager[texture_key])
-                glVertexPointer(3, GL_FLOAT, 0, vertices)
-                glTexCoordPointer(2, GL_FLOAT, 0, uvs)
-                glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors)
-                glDrawArrays(GL_QUADS, 0, nb_vertices)
-
-            #TODO: show the game itself
-            # It is displayed on (0, 0, 0), (0, 448, 0), (388, 448, 0), (388, 0, 0)
-            # using a camera at (192, 224, -835.979370) looking right behind itself
-            # Depth test should be disabled when rendering the game
-
-            pygame.display.flip()
-            clock.tick(120)
-            frame += 1
-
-
-
-try:
-    file_path, stage_num = sys.argv[1:]
-    stage_num = int(stage_num)
-except ValueError:
-    print('Usage: %s std_dat_path stage_num' % sys.argv[0])
-else:
-    main(file_path, stage_num)
-