comparison pytouhou/ui/sdl/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 4ce3ef053a25
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
16 from libc.math cimport M_PI as pi 17 from libc.math cimport M_PI as pi
17 18
18 19
19 cpdef tuple get_sprite_rendering_data(Sprite sprite): 20 cdef RenderingData* get_sprite_rendering_data(Sprite sprite) nogil:
20 if not sprite.changed: 21 if sprite.changed:
21 return sprite._rendering_data 22 render_sprite(sprite)
23 return <RenderingData*>sprite._rendering_data
24
25
26 cdef void render_sprite(Sprite sprite) nogil:
27 if sprite._rendering_data == NULL:
28 sprite._rendering_data = malloc(sizeof(RenderingData))
29
30 data = <RenderingData*>sprite._rendering_data
22 31
23 x = 0 32 x = 0
24 y = 0 33 y = 0
25 34
26 tx, ty, tw, th = sprite._texcoords[0], sprite._texcoords[1], sprite._texcoords[2], sprite._texcoords[3] 35 tx, ty, tw, th = sprite._texcoords[0], sprite._texcoords[1], sprite._texcoords[2], sprite._texcoords[3]
39 y += sprite._dest_offset[1] 48 y += sprite._dest_offset[1]
40 if not sprite.corner_relative_placement: # Reposition 49 if not sprite.corner_relative_placement: # Reposition
41 x -= width / 2 50 x -= width / 2
42 y -= height / 2 51 y -= height / 2
43 52
53 data.x = <int>x
54 data.y = <int>y
55 data.width = <int>width
56 data.height = <int>height
57
44 x_1 = sprite.anm.size_inv[0] 58 x_1 = sprite.anm.size_inv[0]
45 y_1 = sprite.anm.size_inv[1] 59 y_1 = sprite.anm.size_inv[1]
46 tox, toy = sprite._texoffsets[0], sprite._texoffsets[1] 60 tox, toy = sprite._texoffsets[0], sprite._texoffsets[1]
47 uvs = (tx * x_1 + tox, 61 data.left = tx * x_1 + tox
48 (tx + tw) * x_1 + tox, 62 data.right = (tx + tw) * x_1 + tox
49 ty * y_1 + toy, 63 data.bottom = ty * y_1 + toy
50 (ty + th) * y_1 + toy) 64 data.top = (ty + th) * y_1 + toy
51 65
52 r, g, b, a = sprite._color[0], sprite._color[1], sprite._color[2], sprite._color[3] 66 data.r, data.g, data.b, data.a = sprite._color[0], sprite._color[1], sprite._color[2], sprite._color[3]
53 67
54 key = sprite.blendfunc 68 data.blendfunc = sprite.blendfunc
55 values = (x, y, width, height), uvs, (r, g, b, a), -rz * 180 / pi, sprite.mirrored 69 data.rotation = -rz * 180 / pi
56 sprite._rendering_data = key, values 70 data.flip = sprite.mirrored
71
57 sprite.changed = False 72 sprite.changed = False
58
59 return sprite._rendering_data