changeset 120:4300a832f033

Small refactoring and massive performance improvement
author Thibaut Girka <thib@sitedethib.com>
date Thu, 08 Sep 2011 12:46:05 +0200
parents fad7b44cebf2
children 1bc0ad774ed4
files data/ST/make_stage.py pytouhou/game/background.py pytouhou/game/bullet.py pytouhou/game/enemy.py pytouhou/game/sprite.py pytouhou/opengl/gamerenderer.py pytouhou/opengl/sprite.py pytouhou/vm/anmrunner.py
diffstat 8 files changed, 55 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/data/ST/make_stage.py
+++ b/data/ST/make_stage.py
@@ -65,10 +65,9 @@ anm_wrapper = AnmWrapper([Animations.rea
 for model in models:
     vertices = []
     for script_index, ox2, oy2, oz2, width_override, height_override in model.quads:
-        sprite = Sprite()
+        sprite = Sprite(width_override, height_override)
         anmrunner = ANMRunner(anm_wrapper, script_index, sprite)
         anmrunner.run_frame()
-        sprite.update()
         key, (vertices2, uvs2, colors2) = get_sprite_rendering_data(sprite)
         vertices.extend((x + ox2, y + oy2, z + oz2) for x, y, z in vertices2)
     xmin, ymin, zmin = min(x for x, y, z in vertices), min(y for x, y, z in vertices), min(z for x, y, z in vertices)
--- a/pytouhou/game/background.py
+++ b/pytouhou/game/background.py
@@ -51,10 +51,9 @@ class Background(object):
         for obj in self.stage.models:
             quads = []
             for script_index, ox, oy, oz, width_override, height_override in obj.quads:
-                sprite = Sprite()
+                sprite = Sprite(width_override, height_override)
                 anm_runner = ANMRunner(self.anm_wrapper, script_index, sprite)
                 anm_runner.run_frame()
-                sprite.update(width_override, height_override)
                 quads.append((ox, oy, oz, width_override, height_override, sprite))
                 self.anm_runners.append(anm_runner)
             self.models.append(quads)
@@ -83,10 +82,6 @@ class Background(object):
             if not anm_runner.run_frame():
                 self.anm_runners.remove(anm_runner)
 
-        for model in self.models:
-            for ox, oy, oz, width_override, height_override, sprite in model:
-                sprite.update(width_override, height_override)
-
         self.position2_interpolator.update(frame)
         self.fog_interpolator.update(frame)
         self.position_interpolator.update(frame)
--- a/pytouhou/game/bullet.py
+++ b/pytouhou/game/bullet.py
@@ -116,8 +116,13 @@ class Bullet(object):
             self._launched = True
             self.set_anim()
 
-        self._anmrunner.run_frame()
-        self._sprite.update(angle_base=self.angle)
+        sprite = self._sprite
+
+        if self._anmrunner is not None and not self._anmrunner.run_frame():
+            self._anmrunner = None
+        if sprite.automatic_orientation and sprite.angle != self.angle:
+            sprite.angle = self.angle
+            sprite._changed = True
 
         #TODO: flags
         x, y = self.x, self.y
--- a/pytouhou/game/enemy.py
+++ b/pytouhou/game/enemy.py
@@ -240,8 +240,8 @@ class Enemy(object):
             if self._sprite._removed:
                 self._sprite = None
             else:
-                self._sprite.update(angle_base=self.angle,
-                                    force_rotation=self.automatic_orientation)
+                self._sprite.update_orientation(self.angle,
+                                                self.automatic_orientation)
 
 
         if self.bullet_launch_interval != 0:
--- a/pytouhou/game/sprite.py
+++ b/pytouhou/game/sprite.py
@@ -17,13 +17,20 @@ from pytouhou.utils.interpolator import 
 
 
 class Sprite(object):
-    def __init__(self):
+    __slots__ = ('anm', '_removed', '_changed', 'width_override', 'height_override',
+                 'angle', 'force_rotation', 'scale_interpolator', 'fade_interpolator',
+                 'offset_interpolator', 'automatic_orientation', 'blendfunc',
+                 'texcoords', 'dest_offset', 'allow_dest_offset', 'texoffsets',
+                 'mirrored', 'rescale', 'scale_speed', 'rotations_3d',
+                 'rotations_speed_3d', 'corner_relative_placement', 'frame',
+                 'color', 'alpha', '_rendering_data')
+    def __init__(self, width_override=0, height_override=0):
         self.anm = None
         self._removed = False
         self._changed = False
 
-        self.width_override = 0
-        self.height_override = 0
+        self.width_override = width_override
+        self.height_override = height_override
         self.angle = 0
         self.force_rotation = False
 
@@ -73,32 +80,9 @@ class Sprite(object):
             self.offset_interpolator.set_interpolation_end(self.frame + duration - 1, (x, y, z))
 
 
-    def update(self, override_width=0, override_height=0, angle_base=0., force_rotation=False):
-        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._changed = True
-            self.width_override = override_width
-            self.height_override = override_height
+    def update_orientation(self, angle_base=0., force_rotation=False):
+        if (self.angle != angle_base or self.force_rotation != force_rotation):
             self.angle = angle_base
             self.force_rotation = force_rotation
-
-        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._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
-
--- a/pytouhou/opengl/gamerenderer.py
+++ b/pytouhou/opengl/gamerenderer.py
@@ -63,7 +63,6 @@ class GameRenderer(pyglet.window.Window)
         glViewport(0, 0, width, height)
 
 
-
     def update(self, dt):
         if self.background:
             self.background.update(self.game.game_state.frame)
--- a/pytouhou/opengl/sprite.py
+++ b/pytouhou/opengl/sprite.py
@@ -22,18 +22,6 @@ def get_sprite_rendering_data(sprite):
     if not sprite._changed:
         return sprite._rendering_data
 
-    if sprite.fade_interpolator:
-        sprite.fade_interpolator.update(sprite.frame)
-        sprite.alpha = int(sprite.fade_interpolator.values[0])
-
-    if sprite.scale_interpolator:
-        sprite.scale_interpolator.update(sprite.frame)
-        sprite.rescale = sprite.scale_interpolator.values
-
-    if sprite.offset_interpolator:
-        sprite.offset_interpolator.update(sprite.frame)
-        sprite.dest_offset = sprite.offset_interpolator.values
-
     vertmat = Matrix([[-.5,     .5,     .5,    -.5],
                       [-.5,    -.5,     .5,     .5],
                       [ .0,     .0,     .0,     .0],
--- a/pytouhou/vm/anmrunner.py
+++ b/pytouhou/vm/anmrunner.py
@@ -64,6 +64,38 @@ class ANMRunner(object):
                     callback(self, *args)
                     self._sprite._changed = True
         self.frame += 1
+
+        # Update sprite
+        sprite = self._sprite
+        sprite.frame += 1
+
+        if sprite.rotations_speed_3d != (0., 0., 0.):
+            ax, ay, az = sprite.rotations_3d
+            sax, say, saz = sprite.rotations_speed_3d
+            sprite.rotations_3d = ax + sax, ay + say, az + saz
+            sprite._changed = True
+
+        if sprite.scale_speed != (0., 0.):
+            rx, ry = sprite.rescale
+            rsx, rsy = sprite.scale_speed
+            sprite.rescale = rx + rsx, ry + rsy
+            sprite._changed = True
+
+        if sprite.fade_interpolator:
+            sprite.fade_interpolator.update(sprite.frame)
+            sprite.alpha = int(sprite.fade_interpolator.values[0])
+            sprite._changed = True
+
+        if sprite.scale_interpolator:
+            sprite.scale_interpolator.update(sprite.frame)
+            sprite.rescale = sprite.scale_interpolator.values
+            sprite._changed = True
+
+        if sprite.offset_interpolator:
+            sprite.offset_interpolator.update(sprite.frame)
+            sprite.dest_offset = sprite.offset_interpolator.values
+            sprite._changed = True
+
         return self._running