annotate pytouhou/opengl/sprite.pyx @ 152:86807b8a63bd

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