comparison pytouhou/ui/sprite.pyx @ 439:723a3e67a223

Make pytouhou.game.sprite an extension type.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sat, 10 Aug 2013 20:48:17 +0200
parents 878273a984c4
children d56536ef28e8
comparison
equal deleted inserted replaced
438:43a8fed9a8d8 439:723a3e67a223
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 math import pi 16 from libc.math cimport M_PI as pi
17 17
18 from pytouhou.utils.matrix cimport Matrix 18 from pytouhou.utils.matrix cimport Matrix
19 19
20 20
21 cpdef object get_sprite_rendering_data(object sprite): 21 cpdef object get_sprite_rendering_data(Sprite sprite):
22 cdef Matrix vertmat 22 cdef Matrix vertmat
23 cdef double tx, ty, tw, th, sx, sy, rx, ry, rz, tox, toy
24 cdef object tmp1, tmp2
23 25
24 if not sprite.changed: 26 if not sprite.changed:
25 return sprite._rendering_data 27 return sprite._rendering_data
26 28
27 vertmat = Matrix([-.5, .5, .5, -.5, 29 tmp1 = .5
28 -.5, -.5, .5, .5, 30 tmp2 = -.5
29 .0, .0, .0, .0, 31 vertmat = Matrix([tmp2, tmp1, tmp1, tmp2,
30 1., 1., 1., 1.]) 32 tmp2, tmp2, tmp1, tmp1,
33 0, 0, 0, 0,
34 1, 1, 1, 1])
31 35
32 tx, ty, tw, th = sprite.texcoords 36 tx, ty, tw, th = sprite.texcoords
33 sx, sy = sprite.rescale 37 sx, sy = sprite.rescale
34 width = sprite.width_override or (tw * sx) 38 width = sprite.width_override or (tw * sx)
35 height = sprite.height_override or (th * sy) 39 height = sprite.height_override or (th * sy)
42 if sprite.automatic_orientation: 46 if sprite.automatic_orientation:
43 rz += pi/2. - sprite.angle 47 rz += pi/2. - sprite.angle
44 elif sprite.force_rotation: 48 elif sprite.force_rotation:
45 rz += sprite.angle 49 rz += sprite.angle
46 50
47 if (rx, ry, rz) != (0., 0., 0.): 51 if rx:
48 if rx: 52 vertmat.rotate_x(-rx)
49 vertmat.rotate_x(-rx) 53 if ry:
50 if ry: 54 vertmat.rotate_y(ry)
51 vertmat.rotate_y(ry) 55 if rz:
52 if rz: 56 vertmat.rotate_z(-rz) #TODO: minus, really?
53 vertmat.rotate_z(-rz) #TODO: minus, really?
54 if sprite.allow_dest_offset: 57 if sprite.allow_dest_offset:
55 vertmat.translate(sprite.dest_offset[0], sprite.dest_offset[1], sprite.dest_offset[2]) 58 vertmat.translate(sprite.dest_offset[0], sprite.dest_offset[1], sprite.dest_offset[2])
56 if sprite.corner_relative_placement: # Reposition 59 if sprite.corner_relative_placement: # Reposition
57 vertmat.translate(width / 2., height / 2., 0.) 60 vertmat.translate(width / 2, height / 2, 0)
58 61
59 x_1 = 1. / sprite.anm.size[0] 62 x_1 = 1 / <double>sprite.anm.size[0]
60 y_1 = 1. / sprite.anm.size[1] 63 y_1 = 1 / <double>sprite.anm.size[1]
61 tox, toy = sprite.texoffsets 64 tox, toy = sprite.texoffsets
62 uvs = (tx * x_1 + tox, 65 uvs = (tx * x_1 + tox,
63 (tx + tw) * x_1 + tox, 66 (tx + tw) * x_1 + tox,
64 ty * y_1 + toy, 67 ty * y_1 + toy,
65 (ty + th) * y_1 + toy) 68 (ty + th) * y_1 + toy)
69 values = tuple([x for x in vertmat.data[:12]]), uvs, (r, g, b, sprite.alpha) 72 values = tuple([x for x in vertmat.data[:12]]), uvs, (r, g, b, sprite.alpha)
70 sprite._rendering_data = key, values 73 sprite._rendering_data = key, values
71 sprite.changed = False 74 sprite.changed = False
72 75
73 return sprite._rendering_data 76 return sprite._rendering_data
74