comparison pytouhou/opengl/sprite.pyx @ 131:fab7ad2f0d8b

Use Cython, improve performances!
author Thibaut Girka <thib@sitedethib.com>
date Sun, 11 Sep 2011 02:02:59 +0200
parents pytouhou/opengl/sprite.py@0313ca2c50e9
children
comparison
equal deleted inserted replaced
130:11ab06f4c4c6 131:fab7ad2f0d8b
1 # -*- encoding: utf-8 -*-
2 ##
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
4 ##
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published
7 ## by the Free Software Foundation; version 3 only.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
13 ##
14
15
16 from math import pi
17
18 from pytouhou.utils.matrix cimport Matrix
19
20
21 cpdef object get_sprite_rendering_data(object sprite):
22 cdef Matrix vertmat
23
24 if not sprite._changed:
25 return sprite._rendering_data
26
27 vertmat = Matrix([[-.5, .5, .5, -.5],
28 [-.5, -.5, .5, .5],
29 [ .0, .0, .0, .0],
30 [ 1., 1., 1., 1.]])
31
32 tx, ty, tw, th = sprite.texcoords
33 sx, sy = sprite.rescale
34 width = sprite.width_override or (tw * sx)
35 height = sprite.height_override or (th * sy)
36
37 vertmat.scale2d(width, height)
38 if sprite.mirrored:
39 vertmat.flip()
40
41 rx, ry, rz = sprite.rotations_3d
42 if sprite.automatic_orientation:
43 rz += pi/2. - sprite.angle
44 elif sprite.force_rotation:
45 rz += sprite.angle
46
47 if (rx, ry, rz) != (0., 0., 0.):
48 if rx:
49 vertmat.rotate_x(-rx)
50 if ry:
51 vertmat.rotate_y(ry)
52 if rz:
53 vertmat.rotate_z(-rz) #TODO: minus, really?
54 if sprite.corner_relative_placement: # Reposition
55 vertmat.translate(width / 2., height / 2., 0.)
56 if sprite.allow_dest_offset:
57 vertmat.translate(sprite.dest_offset[0], sprite.dest_offset[1], sprite.dest_offset[2])
58
59 x_1 = 1. / sprite.anm.size[0]
60 y_1 = 1. / sprite.anm.size[1]
61 tox, toy = sprite.texoffsets
62 uvs = [tx * x_1 + tox, 1. - (ty * y_1) + toy,
63 (tx + tw) * x_1 + tox, 1. - (ty * y_1) + toy,
64 (tx + tw) * x_1 + tox, 1. - ((ty + th) * y_1 + toy),
65 tx * x_1 + tox, 1. - ((ty + th) * y_1 + toy)]
66
67 (x1, x2 , x3, x4), (y1, y2, y3, y4), (z1, z2, z3, z4), _ = vertmat.data
68
69 key = (sprite.anm.first_name, sprite.anm.secondary_name), sprite.blendfunc
70 r, g, b = sprite.color
71 values = ((x1, y1, z1), (x2, y2, z2), (x3, y3, z3), (x4, y4, z4)), uvs, [r, g, b, sprite.alpha] * 4
72 sprite._rendering_data = key, values
73 sprite._changed = False
74
75 return sprite._rendering_data
76