changeset 532:dacdcca59b66

Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Thu, 19 Dec 2013 21:55:26 +0100
parents a7dc55ad9380
children de778a80820a
files pytouhou/game/sprite.pxd pytouhou/game/sprite.pyx pytouhou/ui/opengl/background.pyx pytouhou/ui/opengl/renderer.pyx pytouhou/ui/opengl/sprite.pxd pytouhou/ui/opengl/sprite.pyx pytouhou/ui/sdl/gamerenderer.pxd pytouhou/ui/sdl/gamerenderer.py pytouhou/ui/sdl/sprite.pxd pytouhou/ui/sdl/sprite.pyx
diffstat 10 files changed, 117 insertions(+), 74 deletions(-) [+]
line wrap: on
line diff
--- a/pytouhou/game/sprite.pxd
+++ b/pytouhou/game/sprite.pxd
@@ -11,7 +11,8 @@ cdef class Sprite:
     cdef public Interpolator offset_interpolator, rotation_interpolator
     cdef public Interpolator color_interpolator
     cdef public ANM anm
-    cdef public object _rendering_data
+
+    cdef void *_rendering_data
 
     cdef float _dest_offset[3]
     cdef double _texcoords[4]
--- a/pytouhou/game/sprite.pyx
+++ b/pytouhou/game/sprite.pyx
@@ -12,7 +12,16 @@
 ## GNU General Public License for more details.
 ##
 
+from libc.stdlib cimport free
+from libc.string cimport memcpy
+
+
 cdef class Sprite:
+    def __dealloc__(self):
+        if self._rendering_data != NULL:
+            free(self._rendering_data)
+
+
     def __init__(self, width_override=0, height_override=0):
         self.anm = None
         self.removed = False
@@ -51,8 +60,6 @@ cdef class Sprite:
         for i in xrange(4):
             self._color[i] = 255
 
-        self._rendering_data = None
-
 
     property scale_speed:
         def __get__(self):
@@ -181,7 +188,6 @@ cdef class Sprite:
 
         sprite.alpha = self.alpha
         sprite.anm = self.anm
-        sprite._rendering_data = self._rendering_data
 
         return sprite
 
--- a/pytouhou/ui/opengl/background.pyx
+++ b/pytouhou/ui/opengl/background.pyx
@@ -82,16 +82,17 @@ cdef class BackgroundRenderer:
         for ox, oy, oz, model_id, model in background.object_instances:
             for ox2, oy2, oz2, width_override, height_override, sprite in model:
                 #TODO: view frustum culling
-                key, (vertices, uvs, colors) = get_sprite_rendering_data(sprite)
-                x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4 = vertices
-                left, right, bottom, top = uvs
-                r, g, b, a = colors
+                data = get_sprite_rendering_data(sprite)
+                key = data.key
+
+                x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4 = data.pos[0], data.pos[1], data.pos[2], data.pos[3], data.pos[4], data.pos[5], data.pos[6], data.pos[7], data.pos[8], data.pos[9], data.pos[10], data.pos[11]
+                r, g, b, a = data.color[0], data.color[1], data.color[2], data.color[3]
 
                 # Pack data
-                vertex_buffer[nb_vertices] = Vertex(x1 + ox + ox2, y1 + oy + oy2, z1 + oz + oz2, left, bottom, r, g, b, a)
-                vertex_buffer[nb_vertices+1] = Vertex(x2 + ox + ox2, y2 + oy + oy2, z2 + oz + oz2, right, bottom, r, g, b, a)
-                vertex_buffer[nb_vertices+2] = Vertex(x3 + ox + ox2, y3 + oy + oy2, z3 + oz + oz2, right, top, r, g, b, a)
-                vertex_buffer[nb_vertices+3] = Vertex(x4 + ox + ox2, y4 + oy + oy2, z4 + oz + oz2, left, top, r, g, b, a)
+                vertex_buffer[nb_vertices] = Vertex(x1 + ox + ox2, y1 + oy + oy2, z1 + oz + oz2, data.left, data.bottom, r, g, b, a)
+                vertex_buffer[nb_vertices+1] = Vertex(x2 + ox + ox2, y2 + oy + oy2, z2 + oz + oz2, data.right, data.bottom, r, g, b, a)
+                vertex_buffer[nb_vertices+2] = Vertex(x3 + ox + ox2, y3 + oy + oy2, z3 + oz + oz2, data.right, data.top, r, g, b, a)
+                vertex_buffer[nb_vertices+3] = Vertex(x4 + ox + ox2, y4 + oy + oy2, z4 + oz + oz2, data.left, data.top, r, g, b, a)
 
                 # Add indices
                 indices[nb_indices] = nb_vertices
--- a/pytouhou/ui/opengl/renderer.pyx
+++ b/pytouhou/ui/opengl/renderer.pyx
@@ -118,10 +118,7 @@ cdef class Renderer:
 
 
     cdef void render_elements(self, elements):
-        cdef int key
-        cdef short x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4, ox, oy
-        cdef float left, right, bottom, top
-        cdef unsigned char r, g, b, a
+        cdef Element element
 
         nb_elements = find_objects(self, elements)
         if not nb_elements:
@@ -132,9 +129,9 @@ cdef class Renderer:
 
         for element_idx in xrange(nb_elements):
             element = <object>self.elements[element_idx]
-            sprite = element.sprite
-            ox, oy = element.x, element.y
-            key, (vertices, uvs, colors) = get_sprite_rendering_data(sprite)
+            ox, oy = <short>element.x, <short>element.y
+            data = get_sprite_rendering_data(element.sprite)
+            key = data.key
 
             blendfunc = key & 1
             texture = key >> 1
@@ -143,13 +140,12 @@ cdef class Renderer:
             next_indice = self.last_indices[key]
 
             # Pack data in buffer
-            x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4 = vertices
-            left, right, bottom, top = uvs
-            r, g, b, a = colors
-            self.vertex_buffer[nb_vertices] = Vertex(x1 + ox, y1 + oy, z1, 0, left, bottom, r, g, b, a)
-            self.vertex_buffer[nb_vertices+1] = Vertex(x2 + ox, y2 + oy, z2, 0, right, bottom, r, g, b, a)
-            self.vertex_buffer[nb_vertices+2] = Vertex(x3 + ox, y3 + oy, z3, 0, right, top, r, g, b, a)
-            self.vertex_buffer[nb_vertices+3] = Vertex(x4 + ox, y4 + oy, z4, 0, left, top, r, g, b, a)
+            x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4 = <short>data.pos[0], <short>data.pos[1], <short>data.pos[2], <short>data.pos[3], <short>data.pos[4], <short>data.pos[5], <short>data.pos[6], <short>data.pos[7], <short>data.pos[8], <short>data.pos[9], <short>data.pos[10], <short>data.pos[11]
+            r, g, b, a = data.color[0], data.color[1], data.color[2], data.color[3]
+            self.vertex_buffer[nb_vertices] = Vertex(x1 + ox, y1 + oy, z1, 0, data.left, data.bottom, r, g, b, a)
+            self.vertex_buffer[nb_vertices+1] = Vertex(x2 + ox, y2 + oy, z2, 0, data.right, data.bottom, r, g, b, a)
+            self.vertex_buffer[nb_vertices+2] = Vertex(x3 + ox, y3 + oy, z3, 0, data.right, data.top, r, g, b, a)
+            self.vertex_buffer[nb_vertices+3] = Vertex(x4 + ox, y4 + oy, z4, 0, data.left, data.top, r, g, b, a)
 
             # Add indices
             rec[next_indice] = nb_vertices
--- a/pytouhou/ui/opengl/sprite.pxd
+++ b/pytouhou/ui/opengl/sprite.pxd
@@ -1,3 +1,9 @@
 from pytouhou.game.sprite cimport Sprite
 
-cpdef tuple get_sprite_rendering_data(Sprite sprite)
+cdef struct RenderingData:
+    float pos[12], left, right, bottom, top
+    unsigned char color[4]
+    long key
+
+cdef RenderingData* get_sprite_rendering_data(Sprite sprite) nogil
+cdef void render_sprite(Sprite sprite) nogil
--- a/pytouhou/ui/opengl/sprite.pyx
+++ b/pytouhou/ui/opengl/sprite.pyx
@@ -13,22 +13,35 @@
 ##
 
 
+from libc.stdlib cimport malloc
+from libc.string cimport memcpy
 from libc.math cimport M_PI as pi
 
 from pytouhou.utils.matrix cimport Matrix, scale2d, flip, rotate_x, rotate_y, rotate_z, translate, translate2d
 from .renderer cimport Texture #XXX
 
 
-cpdef tuple get_sprite_rendering_data(Sprite sprite):
+cdef Matrix default
+default = Matrix(-.5,   .5,   .5,  -.5,
+                 -.5,  -.5,   .5,   .5,
+                 0,    0,    0,    0,
+                 1,    1,    1,    1)
+
+
+cdef RenderingData* get_sprite_rendering_data(Sprite sprite) nogil:
+    if sprite.changed:
+        render_sprite(sprite)
+    return <RenderingData*>sprite._rendering_data
+
+
+cdef void render_sprite(Sprite sprite) nogil:
     cdef Matrix vertmat
 
-    if not sprite.changed:
-        return sprite._rendering_data
+    if sprite._rendering_data == NULL:
+        sprite._rendering_data = malloc(sizeof(RenderingData))
 
-    vertmat = Matrix(-.5,   .5,   .5,  -.5,
-                     -.5,  -.5,   .5,   .5,
-                     0,    0,    0,    0,
-                     1,    1,    1,    1)
+    data = <RenderingData*>sprite._rendering_data
+    memcpy(&vertmat, &default, sizeof(Matrix))
 
     tx, ty, tw, th = sprite._texcoords[0], sprite._texcoords[1], sprite._texcoords[2], sprite._texcoords[3]
     sx, sy = sprite._rescale[0], sprite._rescale[1]
@@ -56,19 +69,18 @@ cpdef tuple get_sprite_rendering_data(Sp
     if sprite.corner_relative_placement: # Reposition
         translate2d(&vertmat, width / 2, height / 2)
 
+    memcpy(data.pos, &vertmat, 12 * sizeof(float))
+
     x_1 = sprite.anm.size_inv[0]
     y_1 = sprite.anm.size_inv[1]
     tox, toy = sprite._texoffsets[0], sprite._texoffsets[1]
-    uvs = (tx * x_1 + tox,
-           (tx + tw) * x_1 + tox,
-           ty * y_1 + toy,
-           (ty + th) * y_1 + toy)
-
-    r, g, b, a = sprite._color[0], sprite._color[1], sprite._color[2], sprite._color[3]
+    data.left = tx * x_1 + tox
+    data.right = (tx + tw) * x_1 + tox
+    data.bottom = ty * y_1 + toy
+    data.top = (ty + th) * y_1 + toy
 
-    key = ((<Texture>sprite.anm.texture).key << 1) | sprite.blendfunc
-    values = tuple([x for x in (<float*>&vertmat)[:12]]), uvs, (r, g, b, a)
-    sprite._rendering_data = key, values
+    for i in xrange(4):
+        data.color[i] = sprite._color[i]
+
+    data.key = ((<Texture>sprite.anm.texture).key << 1) | sprite.blendfunc
     sprite.changed = False
-
-    return sprite._rendering_data
--- a/pytouhou/ui/sdl/gamerenderer.pxd
+++ b/pytouhou/ui/sdl/gamerenderer.pxd
@@ -1,5 +1,6 @@
 from pytouhou.game.game cimport Game
 from .texture cimport TextureManager
+from .sprite cimport get_sprite_rendering_data
 from pytouhou.ui.window cimport Window
 
 cdef class GameRenderer:
--- a/pytouhou/ui/sdl/gamerenderer.py
+++ b/pytouhou/ui/sdl/gamerenderer.py
@@ -15,7 +15,6 @@
 from itertools import chain
 
 from pytouhou.lib.sdl import Rect
-from .sprite import get_sprite_rendering_data
 
 from pytouhou.utils.helpers import get_logger
 logger = get_logger(__name__)
@@ -105,27 +104,26 @@ class GameRenderer(object):
             sprite = element.sprite
             if sprite and sprite.visible:
                 ox, oy = element.x, element.y
-                blendfunc, (vertices, uvs, colors, rotation, flip) = get_sprite_rendering_data(sprite)
-
-                # Pack data in buffer
-                x, y, width, height = vertices
-                left, right, bottom, top = uvs
-                r, g, b, a = colors #TODO: use it.
+                data = get_sprite_rendering_data(sprite)
 
                 #XXX
                 texture_width = 256
                 texture_height = 256
 
-                source = Rect(left * texture_width, bottom * texture_height, (right - left) * texture_width, (top - bottom) * texture_height)
-                dest = Rect(ox + x, oy + y, width, height)
+                source = Rect(data.left * texture_width,
+                              data.bottom * texture_height,
+                              (data.right - data.left) * texture_width,
+                              (data.top - data.bottom) * texture_height)
+
+                dest = Rect(ox + data.x, oy + data.y, data.width, data.height)
 
                 texture = sprite.anm.texture
-                texture.set_color_mod(r, g, b)
-                texture.set_alpha_mod(a)
-                texture.set_blend_mode(2 if blendfunc else 1)
+                texture.set_color_mod(data.r, data.g, data.b)
+                texture.set_alpha_mod(data.a)
+                texture.set_blend_mode(2 if data.blendfunc else 1)
 
-                if rotation or flip:
-                    self.window.win.render_copy_ex(texture, source, dest, rotation, flip)
+                if data.rotation or data.flip:
+                    self.window.win.render_copy_ex(texture, source, dest, data.rotation, data.flip)
                 else:
                     self.window.win.render_copy(texture, source, dest)
 
--- a/pytouhou/ui/sdl/sprite.pxd
+++ b/pytouhou/ui/sdl/sprite.pxd
@@ -1,3 +1,12 @@
 from pytouhou.game.sprite cimport Sprite
 
-cpdef tuple get_sprite_rendering_data(Sprite sprite)
+cdef struct RenderingData:
+    int x, y, width, height
+    float left, right, bottom, top
+    unsigned char r, g, b, a
+    long blendfunc
+    float rotation
+    bint flip
+
+cdef RenderingData* get_sprite_rendering_data(Sprite sprite) nogil
+cdef void render_sprite(Sprite sprite) nogil
--- a/pytouhou/ui/sdl/sprite.pyx
+++ b/pytouhou/ui/sdl/sprite.pyx
@@ -13,12 +13,21 @@
 ##
 
 
+from libc.stdlib cimport malloc
 from libc.math cimport M_PI as pi
 
 
-cpdef tuple get_sprite_rendering_data(Sprite sprite):
-    if not sprite.changed:
-        return sprite._rendering_data
+cdef RenderingData* get_sprite_rendering_data(Sprite sprite) nogil:
+    if sprite.changed:
+        render_sprite(sprite)
+    return <RenderingData*>sprite._rendering_data
+
+
+cdef void render_sprite(Sprite sprite) nogil:
+    if sprite._rendering_data == NULL:
+        sprite._rendering_data = malloc(sizeof(RenderingData))
+
+    data = <RenderingData*>sprite._rendering_data
 
     x = 0
     y = 0
@@ -41,19 +50,23 @@ cpdef tuple get_sprite_rendering_data(Sp
         x -= width / 2
         y -= height / 2
 
+    data.x = <int>x
+    data.y = <int>y
+    data.width = <int>width
+    data.height = <int>height
+
     x_1 = sprite.anm.size_inv[0]
     y_1 = sprite.anm.size_inv[1]
     tox, toy = sprite._texoffsets[0], sprite._texoffsets[1]
-    uvs = (tx * x_1 + tox,
-           (tx + tw) * x_1 + tox,
-           ty * y_1 + toy,
-           (ty + th) * y_1 + toy)
-
-    r, g, b, a = sprite._color[0], sprite._color[1], sprite._color[2], sprite._color[3]
+    data.left = tx * x_1 + tox
+    data.right = (tx + tw) * x_1 + tox
+    data.bottom = ty * y_1 + toy
+    data.top = (ty + th) * y_1 + toy
 
-    key = sprite.blendfunc
-    values = (x, y, width, height), uvs, (r, g, b, a), -rz * 180 / pi, sprite.mirrored
-    sprite._rendering_data = key, values
+    data.r, data.g, data.b, data.a = sprite._color[0], sprite._color[1], sprite._color[2], sprite._color[3]
+
+    data.blendfunc = sprite.blendfunc
+    data.rotation = -rz * 180 / pi
+    data.flip = sprite.mirrored
+
     sprite.changed = False
-
-    return sprite._rendering_data