annotate pytouhou/ui/opengl/sprite.pyx @ 528:7c3c90468996

Inherit music players from a base class.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 18 Dec 2013 22:36:23 +0100
parents db28538cd399
children dacdcca59b66
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
439
723a3e67a223 Make pytouhou.game.sprite an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
16 from libc.math cimport M_PI as pi
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
17
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 515
diff changeset
18 from pytouhou.utils.matrix cimport Matrix, scale2d, flip, rotate_x, rotate_y, rotate_z, translate, translate2d
513
5e3e0b09a531 Move the OpenGL backend to its own package.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 512
diff changeset
19 from .renderer cimport Texture #XXX
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
20
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
21
514
3d4410de78e1 Remove some useless optimisations now that cython does them for us.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 513
diff changeset
22 cpdef tuple get_sprite_rendering_data(Sprite sprite):
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 515
diff changeset
23 cdef Matrix vertmat
131
fab7ad2f0d8b Use Cython, improve performances!
Thibaut Girka <thib@sitedethib.com>
parents: 125
diff changeset
24
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 279
diff changeset
25 if not sprite.changed:
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
26 return sprite._rendering_data
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
27
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 515
diff changeset
28 vertmat = Matrix(-.5, .5, .5, -.5,
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 515
diff changeset
29 -.5, -.5, .5, .5,
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 515
diff changeset
30 0, 0, 0, 0,
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 515
diff changeset
31 1, 1, 1, 1)
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
32
527
db28538cd399 Use Sprite C arrays instead of their tuple representation where it makes sense.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 525
diff changeset
33 tx, ty, tw, th = sprite._texcoords[0], sprite._texcoords[1], sprite._texcoords[2], sprite._texcoords[3]
db28538cd399 Use Sprite C arrays instead of their tuple representation where it makes sense.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 525
diff changeset
34 sx, sy = sprite._rescale[0], sprite._rescale[1]
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
35 width = sprite.width_override or (tw * sx)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
36 height = sprite.height_override or (th * sy)
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
37
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 515
diff changeset
38 scale2d(&vertmat, width, height)
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
39 if sprite.mirrored:
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 515
diff changeset
40 flip(&vertmat)
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
41
527
db28538cd399 Use Sprite C arrays instead of their tuple representation where it makes sense.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 525
diff changeset
42 rx, ry, rz = sprite._rotations_3d[0], sprite._rotations_3d[1], sprite._rotations_3d[2]
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
43 if sprite.automatic_orientation:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
44 rz += pi/2. - sprite.angle
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
45 elif sprite.force_rotation:
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
46 rz += sprite.angle
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
47
439
723a3e67a223 Make pytouhou.game.sprite an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
48 if rx:
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 515
diff changeset
49 rotate_x(&vertmat, -rx)
439
723a3e67a223 Make pytouhou.game.sprite an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
50 if ry:
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 515
diff changeset
51 rotate_y(&vertmat, ry)
439
723a3e67a223 Make pytouhou.game.sprite an extension type.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 435
diff changeset
52 if rz:
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 515
diff changeset
53 rotate_z(&vertmat, -rz) #TODO: minus, really?
279
3539520fff93 Fix sprite rotation/translation.
Thibaut Girka <thib@sitedethib.com>
parents: 274
diff changeset
54 if sprite.allow_dest_offset:
527
db28538cd399 Use Sprite C arrays instead of their tuple representation where it makes sense.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 525
diff changeset
55 translate(&vertmat, sprite._dest_offset)
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
56 if sprite.corner_relative_placement: # Reposition
523
6e3b3d5d4691 Make matrix a struct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 515
diff changeset
57 translate2d(&vertmat, width / 2, height / 2)
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
58
525
43ecf0f98f4d Precalculate the inverse of the texture size at ANM load, to not recalculate at every sprite change.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 523
diff changeset
59 x_1 = sprite.anm.size_inv[0]
43ecf0f98f4d Precalculate the inverse of the texture size at ANM load, to not recalculate at every sprite change.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 523
diff changeset
60 y_1 = sprite.anm.size_inv[1]
527
db28538cd399 Use Sprite C arrays instead of their tuple representation where it makes sense.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 525
diff changeset
61 tox, toy = sprite._texoffsets[0], sprite._texoffsets[1]
397
c5ba11ede097 Don’t duplicate values in sprite rendering data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 393
diff changeset
62 uvs = (tx * x_1 + tox,
c5ba11ede097 Don’t duplicate values in sprite rendering data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 393
diff changeset
63 (tx + tw) * x_1 + tox,
420
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
64 ty * y_1 + toy,
3a7b36324611 Replace Pyglet’s image loader with our SDL2_image-based one.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 397
diff changeset
65 (ty + th) * y_1 + toy)
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
66
527
db28538cd399 Use Sprite C arrays instead of their tuple representation where it makes sense.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 525
diff changeset
67 r, g, b, a = sprite._color[0], sprite._color[1], sprite._color[2], sprite._color[3]
db28538cd399 Use Sprite C arrays instead of their tuple representation where it makes sense.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 525
diff changeset
68
515
b3193b43a86c Add an indirection layer for textures, to cope with drivers assigning them random names.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 514
diff changeset
69 key = ((<Texture>sprite.anm.texture).key << 1) | sprite.blendfunc
527
db28538cd399 Use Sprite C arrays instead of their tuple representation where it makes sense.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 525
diff changeset
70 values = tuple([x for x in (<float*>&vertmat)[:12]]), uvs, (r, g, b, a)
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
71 sprite._rendering_data = key, values
304
f3099ebf4f61 Update attribute names to reflect the actual interface.
Thibaut Girka <thib@sitedethib.com>
parents: 279
diff changeset
72 sprite.changed = False
108
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
73
2a03940deea3 Move everything graphical to pytouhou.opengl!
Thibaut Girka <thib@sitedethib.com>
parents:
diff changeset
74 return sprite._rendering_data