Mercurial > touhou
annotate pytouhou/ui/sdl/sprite.pyx @ 612:73f134f84c7f
Request a RGB888 context, since SDL2’s default of RGB332 sucks.
On X11/GLX, it will select the first config available, that is the best
one, while on EGL it will iterate over them to select the one closest
to what the application requested.
Of course, anything lower than RGB888 looks bad and we really don’t
want that.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 26 Mar 2015 20:20:37 +0100 |
parents | dacdcca59b66 |
children | 4ce3ef053a25 |
rev | line source |
---|---|
512
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
1 # -*- encoding: utf-8 -*- |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
2 ## |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com> |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
4 ## |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
5 ## This program is free software; you can redistribute it and/or modify |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
6 ## it under the terms of the GNU General Public License as published |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
7 ## by the Free Software Foundation; version 3 only. |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
8 ## |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
9 ## This program is distributed in the hope that it will be useful, |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
12 ## GNU General Public License for more details. |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
13 ## |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
14 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
15 |
532
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
16 from libc.stdlib cimport malloc |
512
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
17 from libc.math cimport M_PI as pi |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
18 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
19 |
532
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
20 cdef RenderingData* get_sprite_rendering_data(Sprite sprite) nogil: |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
21 if sprite.changed: |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
22 render_sprite(sprite) |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
23 return <RenderingData*>sprite._rendering_data |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
24 |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
25 |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
26 cdef void render_sprite(Sprite sprite) nogil: |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
27 if sprite._rendering_data == NULL: |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
28 sprite._rendering_data = malloc(sizeof(RenderingData)) |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
29 |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
30 data = <RenderingData*>sprite._rendering_data |
512
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
31 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
32 x = 0 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
33 y = 0 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
34 |
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
|
35 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
|
36 sx, sy = sprite._rescale[0], sprite._rescale[1] |
512
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
37 width = sprite.width_override or (tw * sx) |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
38 height = sprite.height_override or (th * sy) |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
39 |
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
|
40 rz = sprite._rotations_3d[2] |
512
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
41 if sprite.automatic_orientation: |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
42 rz += pi/2. - sprite.angle |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
43 elif sprite.force_rotation: |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
44 rz += sprite.angle |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
45 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
46 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
|
47 x += sprite._dest_offset[0] |
db28538cd399
Use Sprite C arrays instead of their tuple representation where it makes sense.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
525
diff
changeset
|
48 y += sprite._dest_offset[1] |
512
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
49 if not sprite.corner_relative_placement: # Reposition |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
50 x -= width / 2 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
51 y -= height / 2 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
52 |
532
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
53 data.x = <int>x |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
54 data.y = <int>y |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
55 data.width = <int>width |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
56 data.height = <int>height |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
57 |
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:
514
diff
changeset
|
58 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:
514
diff
changeset
|
59 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
|
60 tox, toy = sprite._texoffsets[0], sprite._texoffsets[1] |
532
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
61 data.left = tx * x_1 + tox |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
62 data.right = (tx + tw) * x_1 + tox |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
63 data.bottom = ty * y_1 + toy |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
64 data.top = (ty + th) * y_1 + toy |
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
|
65 |
532
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
66 data.r, data.g, data.b, data.a = sprite._color[0], sprite._color[1], sprite._color[2], sprite._color[3] |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
67 |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
68 data.blendfunc = sprite.blendfunc |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
69 data.rotation = -rz * 180 / pi |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
70 data.flip = sprite.mirrored |
dacdcca59b66
Don’t put back the rendering data into a tuple, use a specialised struct inside Sprite to pass it to the renderer.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
527
diff
changeset
|
71 |
512
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
72 sprite.changed = False |