comparison pytouhou/ui/opengl/sprite.pyx @ 523:6e3b3d5d4691

Make matrix a struct.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 18 Dec 2013 17:53:29 +0100
parents b3193b43a86c
children 43ecf0f98f4d
comparison
equal deleted inserted replaced
522:e8496e5ba056 523:6e3b3d5d4691
13 ## 13 ##
14 14
15 15
16 from libc.math cimport M_PI as 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, scale2d, flip, rotate_x, rotate_y, rotate_z, translate, translate2d
19 from .renderer cimport Texture #XXX 19 from .renderer cimport Texture #XXX
20 20
21 21
22 cpdef tuple get_sprite_rendering_data(Sprite sprite): 22 cpdef tuple get_sprite_rendering_data(Sprite sprite):
23 cdef double tx, ty, tw, th, sx, sy, rx, ry, rz, tox, toy 23 cdef double tx, ty, tw, th, sx, sy, rx, ry, rz, tox, toy
24 cdef Matrix vertmat
24 25
25 if not sprite.changed: 26 if not sprite.changed:
26 return sprite._rendering_data 27 return sprite._rendering_data
27 28
28 vertmat = Matrix([-.5, .5, .5, -.5, 29 vertmat = Matrix(-.5, .5, .5, -.5,
29 -.5, -.5, .5, .5, 30 -.5, -.5, .5, .5,
30 0, 0, 0, 0, 31 0, 0, 0, 0,
31 1, 1, 1, 1]) 32 1, 1, 1, 1)
32 33
33 tx, ty, tw, th = sprite.texcoords 34 tx, ty, tw, th = sprite.texcoords
34 sx, sy = sprite.rescale 35 sx, sy = sprite.rescale
35 width = sprite.width_override or (tw * sx) 36 width = sprite.width_override or (tw * sx)
36 height = sprite.height_override or (th * sy) 37 height = sprite.height_override or (th * sy)
37 38
38 vertmat.scale2d(width, height) 39 scale2d(&vertmat, width, height)
39 if sprite.mirrored: 40 if sprite.mirrored:
40 vertmat.flip() 41 flip(&vertmat)
41 42
42 rx, ry, rz = sprite.rotations_3d 43 rx, ry, rz = sprite.rotations_3d
43 if sprite.automatic_orientation: 44 if sprite.automatic_orientation:
44 rz += pi/2. - sprite.angle 45 rz += pi/2. - sprite.angle
45 elif sprite.force_rotation: 46 elif sprite.force_rotation:
46 rz += sprite.angle 47 rz += sprite.angle
47 48
48 if rx: 49 if rx:
49 vertmat.rotate_x(-rx) 50 rotate_x(&vertmat, -rx)
50 if ry: 51 if ry:
51 vertmat.rotate_y(ry) 52 rotate_y(&vertmat, ry)
52 if rz: 53 if rz:
53 vertmat.rotate_z(-rz) #TODO: minus, really? 54 rotate_z(&vertmat, -rz) #TODO: minus, really?
54 if sprite.allow_dest_offset: 55 if sprite.allow_dest_offset:
55 vertmat.translate(sprite.dest_offset[0], sprite.dest_offset[1], sprite.dest_offset[2]) 56 translate(&vertmat, sprite.dest_offset[0], sprite.dest_offset[1], sprite.dest_offset[2])
56 if sprite.corner_relative_placement: # Reposition 57 if sprite.corner_relative_placement: # Reposition
57 vertmat.translate(width / 2, height / 2, 0) 58 translate2d(&vertmat, width / 2, height / 2)
58 59
59 size = sprite.anm.size 60 size = sprite.anm.size
60 x_1 = 1 / <double>size[0] 61 x_1 = 1 / <double>size[0]
61 y_1 = 1 / <double>size[1] 62 y_1 = 1 / <double>size[1]
62 tox, toy = sprite.texoffsets 63 tox, toy = sprite.texoffsets
65 ty * y_1 + toy, 66 ty * y_1 + toy,
66 (ty + th) * y_1 + toy) 67 (ty + th) * y_1 + toy)
67 68
68 key = ((<Texture>sprite.anm.texture).key << 1) | sprite.blendfunc 69 key = ((<Texture>sprite.anm.texture).key << 1) | sprite.blendfunc
69 r, g, b = sprite.color 70 r, g, b = sprite.color
70 values = tuple([x for x in vertmat.data[:12]]), uvs, (r, g, b, sprite.alpha) 71 values = tuple([x for x in (<float*>&vertmat)[:12]]), uvs, (r, g, b, sprite.alpha)
71 sprite._rendering_data = key, values 72 sprite._rendering_data = key, values
72 sprite.changed = False 73 sprite.changed = False
73 74
74 return sprite._rendering_data 75 return sprite._rendering_data