comparison pytouhou/ui/opengl/sprite.pyx @ 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 db28538cd399
children e15672733c93
comparison
equal deleted inserted replaced
531:a7dc55ad9380 532:dacdcca59b66
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details. 12 ## GNU General Public License for more details.
13 ## 13 ##
14 14
15 15
16 from libc.stdlib cimport malloc
17 from libc.string cimport memcpy
16 from libc.math cimport M_PI as pi 18 from libc.math cimport M_PI as pi
17 19
18 from pytouhou.utils.matrix cimport Matrix, scale2d, flip, rotate_x, rotate_y, rotate_z, translate, translate2d 20 from pytouhou.utils.matrix cimport Matrix, scale2d, flip, rotate_x, rotate_y, rotate_z, translate, translate2d
19 from .renderer cimport Texture #XXX 21 from .renderer cimport Texture #XXX
20 22
21 23
22 cpdef tuple get_sprite_rendering_data(Sprite sprite): 24 cdef Matrix default
25 default = Matrix(-.5, .5, .5, -.5,
26 -.5, -.5, .5, .5,
27 0, 0, 0, 0,
28 1, 1, 1, 1)
29
30
31 cdef RenderingData* get_sprite_rendering_data(Sprite sprite) nogil:
32 if sprite.changed:
33 render_sprite(sprite)
34 return <RenderingData*>sprite._rendering_data
35
36
37 cdef void render_sprite(Sprite sprite) nogil:
23 cdef Matrix vertmat 38 cdef Matrix vertmat
24 39
25 if not sprite.changed: 40 if sprite._rendering_data == NULL:
26 return sprite._rendering_data 41 sprite._rendering_data = malloc(sizeof(RenderingData))
27 42
28 vertmat = Matrix(-.5, .5, .5, -.5, 43 data = <RenderingData*>sprite._rendering_data
29 -.5, -.5, .5, .5, 44 memcpy(&vertmat, &default, sizeof(Matrix))
30 0, 0, 0, 0,
31 1, 1, 1, 1)
32 45
33 tx, ty, tw, th = sprite._texcoords[0], sprite._texcoords[1], sprite._texcoords[2], sprite._texcoords[3] 46 tx, ty, tw, th = sprite._texcoords[0], sprite._texcoords[1], sprite._texcoords[2], sprite._texcoords[3]
34 sx, sy = sprite._rescale[0], sprite._rescale[1] 47 sx, sy = sprite._rescale[0], sprite._rescale[1]
35 width = sprite.width_override or (tw * sx) 48 width = sprite.width_override or (tw * sx)
36 height = sprite.height_override or (th * sy) 49 height = sprite.height_override or (th * sy)
54 if sprite.allow_dest_offset: 67 if sprite.allow_dest_offset:
55 translate(&vertmat, sprite._dest_offset) 68 translate(&vertmat, sprite._dest_offset)
56 if sprite.corner_relative_placement: # Reposition 69 if sprite.corner_relative_placement: # Reposition
57 translate2d(&vertmat, width / 2, height / 2) 70 translate2d(&vertmat, width / 2, height / 2)
58 71
72 memcpy(data.pos, &vertmat, 12 * sizeof(float))
73
59 x_1 = sprite.anm.size_inv[0] 74 x_1 = sprite.anm.size_inv[0]
60 y_1 = sprite.anm.size_inv[1] 75 y_1 = sprite.anm.size_inv[1]
61 tox, toy = sprite._texoffsets[0], sprite._texoffsets[1] 76 tox, toy = sprite._texoffsets[0], sprite._texoffsets[1]
62 uvs = (tx * x_1 + tox, 77 data.left = tx * x_1 + tox
63 (tx + tw) * x_1 + tox, 78 data.right = (tx + tw) * x_1 + tox
64 ty * y_1 + toy, 79 data.bottom = ty * y_1 + toy
65 (ty + th) * y_1 + toy) 80 data.top = (ty + th) * y_1 + toy
66 81
67 r, g, b, a = sprite._color[0], sprite._color[1], sprite._color[2], sprite._color[3] 82 for i in xrange(4):
83 data.color[i] = sprite._color[i]
68 84
69 key = ((<Texture>sprite.anm.texture).key << 1) | sprite.blendfunc 85 data.key = ((<Texture>sprite.anm.texture).key << 1) | sprite.blendfunc
70 values = tuple([x for x in (<float*>&vertmat)[:12]]), uvs, (r, g, b, a)
71 sprite._rendering_data = key, values
72 sprite.changed = False 86 sprite.changed = False
73
74 return sprite._rendering_data