changeset 94:ca571697ec83

Various minor optimisations and refactoring
author Thibaut Girka <thib@sitedethib.com>
date Sun, 04 Sep 2011 20:04:00 +0200
parents d167280a82fc
children e2d8f2a56ea4
files eclviewer.py pytouhou/formats/std.py pytouhou/game/background.py pytouhou/game/bullet.py pytouhou/game/enemymanager.py pytouhou/game/sprite.py
diffstat 6 files changed, 48 insertions(+), 48 deletions(-) [+]
line wrap: on
line diff
--- a/eclviewer.py
+++ b/eclviewer.py
@@ -154,9 +154,7 @@ def main(path, stage_num):
         glTranslatef(-x, -y, -z)
 
         glEnable(GL_DEPTH_TEST)
-        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():
+        for (texture_key, blendfunc), (nb_vertices, vertices, uvs, colors) in background.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)
@@ -179,7 +177,7 @@ def main(path, stage_num):
         glDisable(GL_FOG)
         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():
+        for (texture_key, blendfunc), (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])
--- a/pytouhou/formats/std.py
+++ b/pytouhou/formats/std.py
@@ -19,9 +19,11 @@ from pytouhou.utils.helpers import read_
 logger = get_logger(__name__)
 
 
-class Object(object):
+class Model(object):
     def __init__(self):
-        self.header = (b'\x00') * 28 #TODO
+        self.unknown = 0
+        self.bounding_box = (0., 0., 0.,
+                             0., 0., 0.)
         self.quads = []
 
 
@@ -31,7 +33,7 @@ class Stage(object):
         self.num = num
         self.name = ''
         self.bgms = (('', ''), ('', ''), ('', ''))
-        self.objects = []
+        self.models = []
         self.object_instances = []
         self.script = []
 
@@ -40,7 +42,7 @@ class Stage(object):
     def read(cls, file, num):
         stage = Stage(num)
 
-        nb_objects, nb_faces = unpack('<HH', file.read(4))
+        nb_models, nb_faces = unpack('<HH', file.read(4))
         object_instances_offset, script_offset = unpack('<II', file.read(8))
         if file.read(4) != b'\x00\x00\x00\x00':
             raise Exception #TODO
@@ -59,11 +61,13 @@ class Stage(object):
 
         stage.bgms = [(bgm_a, bgm_a_path), (bgm_b, bgm_b_path), (bgm_c, bgm_c_path), (bgm_d, bgm_d_path)] #TODO: handle ' '
 
-        # Read object definitions
-        offsets = unpack('<%s' % ('I' * nb_objects), file.read(4 * nb_objects))
+        # Read model definitions
+        offsets = unpack('<%s' % ('I' * nb_models), file.read(4 * nb_models))
         for offset in offsets:
-            obj = Object()
-            obj.header = file.read(28) #TODO: this has to be reversed!
+            model = Model()
+            id_, unknown, x, y, z, width, height, depth = unpack('<HHffffff', file.read(28))
+            model.unknown = unknown
+            model.bounding_box = x, y, z, width, height, depth #TODO: check
             while True:
                 unknown, size = unpack('<HH', file.read(4))
                 if unknown == 0xffff:
@@ -72,8 +76,8 @@ class Stage(object):
                     raise Exception #TODO
                 script_index, _padding, x, y, z, width, height = unpack('<HHfffff', file.read(24))
                 #TODO: store script_index, x, y, z, width and height
-                obj.quads.append((script_index, x, y, z, width, height))
-            stage.objects.append(obj)
+                model.quads.append((script_index, x, y, z, width, height))
+            stage.models.append(model)
 
 
         # Read object usages
@@ -84,7 +88,7 @@ class Stage(object):
                 break
             if unknown != 256:
                 raise Exception #TODO
-            stage.object_instances.append((stage.objects[obj_id], x, y, z))
+            stage.object_instances.append((obj_id, x, y, z))
 
 
         # Read other funny things (script)
--- a/pytouhou/game/background.py
+++ b/pytouhou/game/background.py
@@ -27,7 +27,7 @@ class Background(object):
     def __init__(self, stage, anm_wrapper):
         self.stage = stage
         self.anm_wrapper = anm_wrapper
-        self.objects = []
+        self.models = []
         self.object_instances = []
         self.objects_by_texture = {}
 
@@ -35,17 +35,16 @@ class Background(object):
         self.fog_interpolator = Interpolator((0, 0, 0, 0, 0))
         self.position2_interpolator = Interpolator((0, 0, 0))
 
-        self.build_objects()
+        self.build_models()
         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, face_colors in self.objects[obj_id]:
+            for face_vertices, face_uvs, face_colors in self.models[obj]:
                 obj_instance.append((tuple((x + ox, y + oy, z + oz)
                                         for x, y, z in face_vertices),
                                     face_uvs,
@@ -67,9 +66,9 @@ class Background(object):
         return vertices, uvs, colors
 
 
-    def build_objects(self):
-        self.objects = []
-        for i, obj in enumerate(self.stage.objects):
+    def build_models(self):
+        self.models = []
+        for i, obj in enumerate(self.stage.models):
             faces = []
             for script_index, ox, oy, oz, width_override, height_override in obj.quads:
                 #TODO: per-texture rendering
@@ -82,11 +81,7 @@ class Background(object):
                 uvs, vertices = sprite._uvs, tuple((x + ox, y + oy, z + oz) for x, y, z in sprite._vertices)
                 colors = sprite._colors
                 faces.append((vertices, uvs, colors))
-            self.objects.append(faces)
-
-
-    def get_objects_by_texture(self, objects_by_texture):
-        objects_by_texture.update(self.objects_by_texture)
+            self.models.append(faces)
 
 
     def update(self, frame):
--- a/pytouhou/game/bullet.py
+++ b/pytouhou/game/bullet.py
@@ -97,12 +97,11 @@ class Bullet(object):
         sprite.update_vertices_uvs_colors()
         key = sprite.anm.first_name, sprite.anm.secondary_name
         key = (key, sprite.blendfunc)
-        if not key in objects_by_texture:
-            objects_by_texture[key] = (0, [], [], [])
+        rec = objects_by_texture.setdefault(key, ([], [], []))
         vertices = ((x + self.x, y + self.y, z) for x, y, z in sprite._vertices)
-        objects_by_texture[key][1].extend(vertices)
-        objects_by_texture[key][2].extend(sprite._uvs)
-        objects_by_texture[key][3].extend(sprite._colors)
+        rec[0].extend(vertices)
+        rec[1].extend(sprite._uvs)
+        rec[2].extend(sprite._colors)
 
 
     def update(self):
@@ -112,9 +111,7 @@ class Bullet(object):
             self._anmrunner = ANMRunner(self._game_state.resources.etama_anm_wrappers[0], #TODO
                                         self.anim_idx, self._sprite, self.sprite_idx_offset)
 
-        if self._anmrunner and not self._anmrunner.run_frame():
-            self._anmrunner = None
-
+        self._anmrunner.run_frame()
         self._sprite.update(angle_base=self.angle)
 
         #TODO: flags
--- a/pytouhou/game/enemymanager.py
+++ b/pytouhou/game/enemymanager.py
@@ -180,16 +180,16 @@ class Enemy(object):
         if not self._sprite:
             return
 
-        self._sprite.update_vertices_uvs_colors()
+        sprite = self._sprite
+        sprite.update_vertices_uvs_colors()
 
-        key = self._sprite.anm.first_name, self._sprite.anm.secondary_name
-        key = (key, self._sprite.blendfunc)
-        if not key in objects_by_texture:
-            objects_by_texture[key] = (0, [], [], [])
-        vertices = tuple((x + self.x, y + self.y, z) for x, y, z in self._sprite._vertices)
-        objects_by_texture[key][1].extend(vertices)
-        objects_by_texture[key][2].extend(self._sprite._uvs)
-        objects_by_texture[key][3].extend(self._sprite._colors)
+        key = sprite.anm.first_name, sprite.anm.secondary_name
+        key = (key, sprite.blendfunc)
+        rec = objects_by_texture.setdefault(key, ([], [], []))
+        vertices = ((x + self.x, y + self.y, z) for x, y, z in sprite._vertices)
+        rec[0].extend(vertices)
+        rec[1].extend(sprite._uvs)
+        rec[2].extend(sprite._colors)
 
 
     def update(self):
@@ -319,7 +319,7 @@ class EnemyManager(object):
         # Update enemies
         for enemy in self.enemies:
             enemy.update()
-            for bullet in enemy.bullets:
+            for bullet in tuple(enemy.bullets):
                 if bullet._launched:
                     enemy.bullets.remove(bullet)
                 self.bullets.append(bullet)
@@ -340,7 +340,7 @@ class EnemyManager(object):
                 self.enemies.remove(enemy)
 
         # Filter out-of-scren bullets
-        for bullet in self.bullets:
+        for bullet in tuple(self.bullets):
             if not bullet.is_visible(384, 448):
                 self.bullets.remove(bullet)
 
--- a/pytouhou/game/sprite.py
+++ b/pytouhou/game/sprite.py
@@ -174,11 +174,17 @@ class Sprite(object):
             self.angle = angle_base
             self.force_rotation = force_rotation
 
-        if self.rotations_speed_3d != (0., 0., 0.) or self.scale_speed != (0., 0.):
+        if self.rotations_speed_3d != (0., 0., 0.):
             ax, ay, az = self.rotations_3d
             sax, say, saz = self.rotations_speed_3d
             self.rotations_3d = ax + sax, ay + say, az + saz
-            self.rescale = self.rescale[0] + self.scale_speed[0], self.rescale[1] + self.scale_speed[1]
             self._changed = True
+
+        if self.scale_speed != (0., 0.):
+            rx, ry = self.rescale
+            rsx, rsy = self.scale_speed
+            self.rescale = rx + rsx, ry + rsy
+            self._changed = True
+
         self.frame += 1