Mercurial > touhou
annotate pytouhou/ui/sdl/sprite.pyx @ 640:37d151fe000b
Implement better defaults for Sprite, and a beginning of
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 04 Jul 2019 21:47:53 +0200 |
parents | 4ce3ef053a25 |
children |
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 |
616
4ce3ef053a25
Remove every case where an exception could be silently eaten by a cdef function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
532
diff
changeset
|
15 cimport cython |
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 |
616
4ce3ef053a25
Remove every case where an exception could be silently eaten by a cdef function.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
532
diff
changeset
|
26 @cython.cdivision(True) |
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
|
27 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
|
28 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
|
29 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
|
30 |
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
|
31 data = <RenderingData*>sprite._rendering_data |
512
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
32 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
33 x = 0 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
34 y = 0 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
35 |
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
|
36 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
|
37 sx, sy = sprite._rescale[0], sprite._rescale[1] |
512
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
38 width = sprite.width_override or (tw * sx) |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
39 height = sprite.height_override or (th * sy) |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
40 |
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
|
41 rz = sprite._rotations_3d[2] |
512
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
42 if sprite.automatic_orientation: |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
43 rz += pi/2. - sprite.angle |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
44 elif sprite.force_rotation: |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
45 rz += sprite.angle |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
46 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
47 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
|
48 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
|
49 y += sprite._dest_offset[1] |
512
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
50 if not sprite.corner_relative_placement: # Reposition |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
51 x -= width / 2 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
52 y -= height / 2 |
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
53 |
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
|
54 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
|
55 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
|
56 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
|
57 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
|
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:
514
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:
514
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] |
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
|
62 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
|
63 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
|
64 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
|
65 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
|
66 |
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
|
67 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
|
68 |
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.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
|
70 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
|
71 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
|
72 |
512
b39ad30c6620
Add a pure SDL backend.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
73 sprite.changed = False |