comparison pytouhou/ui/opengl/sprite.pyx @ 513:5e3e0b09a531

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