changeset 525:43ecf0f98f4d

Precalculate the inverse of the texture size at ANM load, to not recalculate at every sprite change.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 18 Dec 2013 18:15:45 +0100
parents 7f016dfbdfb1
children 0b2a92a25245
files pytouhou/formats/anm0.pxd pytouhou/formats/anm0.py pytouhou/formats/anm0.pyx pytouhou/game/sprite.pxd pytouhou/ui/opengl/sprite.pyx pytouhou/ui/opengl/texture.pyx pytouhou/ui/sdl/sprite.pyx pytouhou/ui/sdl/texture.pyx setup.py
diffstat 8 files changed, 36 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/pytouhou/formats/anm0.pxd
@@ -0,0 +1,7 @@
+cdef class ANM:
+    cdef public long version
+    cdef public unicode first_name, secondary_name
+    cdef public dict sprites, scripts
+    cdef public object texture
+
+    cdef double size_inv[2]
rename from pytouhou/formats/anm0.py
rename to pytouhou/formats/anm0.pyx
--- a/pytouhou/formats/anm0.py
+++ b/pytouhou/formats/anm0.pyx
@@ -39,6 +39,22 @@ class Script(list):
 
 
 
+cdef class ANM:
+    def __init__(self):
+        self.version = 0
+        self.size_inv[:] = [0, 0]
+        self.first_name = None
+        self.secondary_name = None
+        self.sprites = {}
+        self.scripts = {}
+
+    property size:
+        def __set__(self, tuple value):
+            width, height = value
+            self.size_inv[:] = [1. / <double>width, 1. / <double>height]
+
+
+
 class ANM0(object):
     _instructions = {0: {0: ('', 'delete'),
                          1: ('I', 'set_sprite'),
@@ -120,15 +136,6 @@ class ANM0(object):
                          80: ('I', None)}}
 
 
-    def __init__(self):
-        self.version = 0
-        self.size = (0, 0)
-        self.first_name = None
-        self.secondary_name = None
-        self.sprites = {}
-        self.scripts = {}
-
-
     @classmethod
     def read(cls, file):
         anm_list = []
@@ -156,7 +163,7 @@ class ANM0(object):
             sprite_offsets = [unpack('<I', file.read(4))[0] for i in range(nb_sprites)]
             script_offsets = [unpack('<II', file.read(8)) for i in range(nb_scripts)]
 
-            self = cls()
+            self = ANM()
 
             self.size = (width, height)
             self.version = version
--- a/pytouhou/game/sprite.pxd
+++ b/pytouhou/game/sprite.pxd
@@ -1,4 +1,5 @@
 from pytouhou.utils.interpolator cimport Interpolator
+from pytouhou.formats.anm0 cimport ANM
 
 cdef class Sprite:
     cdef public long blendfunc, frame
@@ -12,7 +13,8 @@ cdef class Sprite:
     cdef public tuple texcoords, dest_offset, texoffsets, rescale, scale_speed
     cdef public tuple rotations_3d, rotations_speed_3d, color
     cdef public unsigned char alpha
-    cdef public object anm, _rendering_data
+    cdef public ANM anm
+    cdef public object _rendering_data
 
     cpdef fade(self, unsigned long duration, alpha, formula=*)
     cpdef scale_in(self, unsigned long duration, sx, sy, formula=*)
--- a/pytouhou/ui/opengl/sprite.pyx
+++ b/pytouhou/ui/opengl/sprite.pyx
@@ -57,9 +57,8 @@ cpdef tuple get_sprite_rendering_data(Sp
     if sprite.corner_relative_placement: # Reposition
         translate2d(&vertmat, width / 2, height / 2)
 
-    size = sprite.anm.size
-    x_1 = 1 / <double>size[0]
-    y_1 = 1 / <double>size[1]
+    x_1 = sprite.anm.size_inv[0]
+    y_1 = sprite.anm.size_inv[1]
     tox, toy = sprite.texoffsets
     uvs = (tx * x_1 + tox,
            (tx + tw) * x_1 + tox,
--- a/pytouhou/ui/opengl/texture.pyx
+++ b/pytouhou/ui/opengl/texture.pyx
@@ -39,7 +39,7 @@ cdef class TextureManager:
     cdef void load(self, dict anms):
         for anm in sorted(anms.values(), key=is_ascii):
             for entry in anm:
-                if not hasattr(entry, 'texture'):
+                if entry.texture is None:
                     texture = decode_png(self.loader, entry.first_name, entry.secondary_name)
                 elif not isinstance(entry.texture, self.texture_class):
                     texture = entry.texture
--- a/pytouhou/ui/sdl/sprite.pyx
+++ b/pytouhou/ui/sdl/sprite.pyx
@@ -43,9 +43,8 @@ cpdef tuple get_sprite_rendering_data(Sp
         x -= width / 2
         y -= height / 2
 
-    size = sprite.anm.size
-    x_1 = 1 / <double>size[0]
-    y_1 = 1 / <double>size[1]
+    x_1 = sprite.anm.size_inv[0]
+    y_1 = sprite.anm.size_inv[1]
     tox, toy = sprite.texoffsets
     uvs = (tx * x_1 + tox,
            (tx + tw) * x_1 + tox,
--- a/pytouhou/ui/sdl/texture.pyx
+++ b/pytouhou/ui/sdl/texture.pyx
@@ -26,7 +26,7 @@ cdef class TextureManager:
     cdef void load(self, dict anms):
         for anm in sorted(anms.values(), key=is_ascii):
             for entry in anm:
-                if not hasattr(entry, 'texture'):
+                if entry.texture is None:
                     texture = decode_png(self.loader, entry.first_name, entry.secondary_name)
                 #elif not isinstance(entry.texture, self.texture_class):
                 #    texture = entry.texture
--- a/setup.py
+++ b/setup.py
@@ -72,7 +72,7 @@ except OSError:
 for directory, _, files in os.walk('pytouhou'):
     package = directory.replace(os.path.sep, '.')
     packages.append(package)
-    if package not in ('pytouhou.game', 'pytouhou.lib', 'pytouhou.utils') and not package.startswith('pytouhou.ui'):
+    if package not in ('pytouhou.formats', 'pytouhou.game', 'pytouhou.lib', 'pytouhou.utils') and not package.startswith('pytouhou.ui'):
         continue
     if package == 'pytouhou.ui' or package == 'pytouhou.ui.sdl':
         package_args = sdl_args
@@ -91,6 +91,8 @@ for directory, _, files in os.walk('pyto
                 compile_args = opengl_args
             elif extension_name == 'pytouhou.ui.anmrenderer' and not anmviewer:
                 continue
+            elif package == 'pytouhou.formats' and extension_name != 'pytouhou.formats.anm0':
+                continue
             else:
                 compile_args = package_args
             extensions.append(Extension(extension_name,