# HG changeset patch # User Emmanuel Gil Peyrot # Date 1387386945 -3600 # Node ID 43ecf0f98f4d9f101306ba1337ef8068da169ae7 # Parent 7f016dfbdfb1381151bbe86febb57663ea369742 Precalculate the inverse of the texture size at ANM load, to not recalculate at every sprite change. diff --git a/pytouhou/formats/anm0.pxd b/pytouhou/formats/anm0.pxd 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] diff --git a/pytouhou/formats/anm0.py b/pytouhou/formats/anm0.pyx 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. / width, 1. / 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('size[0] - y_1 = 1 / 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, diff --git a/pytouhou/ui/opengl/texture.pyx b/pytouhou/ui/opengl/texture.pyx --- 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 diff --git a/pytouhou/ui/sdl/sprite.pyx b/pytouhou/ui/sdl/sprite.pyx --- 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 / size[0] - y_1 = 1 / 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, diff --git a/pytouhou/ui/sdl/texture.pyx b/pytouhou/ui/sdl/texture.pyx --- 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 diff --git a/setup.py b/setup.py --- 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,