annotate pytouhou/ui/glide/sprite.pyx @ 772:7492d384d122 default tip

Rust: Add a Glide renderer (2D only for now) This is an experiment for a Rust renderer, iterating over the Python data using pyo3. It requires --feature=glide to be passed to cargo build, doesn’t support NPOT textures, text rendering, the background, or even msg faces, some of that may come in a future changeset.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Mon, 05 Sep 2022 17:53:36 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
772
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 # -*- encoding: utf-8 -*-
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 ##
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 ##
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 ## This program is free software; you can redistribute it and/or modify
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 ## it under the terms of the GNU General Public License as published
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 ## by the Free Software Foundation; version 3 only.
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8 ##
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 ## This program is distributed in the hope that it will be useful,
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 ## GNU General Public License for more details.
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 ##
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 from libc.stdlib cimport malloc
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 from libc.string cimport memcpy
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 from libc.math cimport M_PI as pi
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20 from pytouhou.utils.matrix cimport Matrix, scale2d, flip, rotate_x, rotate_y, rotate_z, translate, translate2d
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 cdef Matrix default
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 default = Matrix(-.5, .5, .5, -.5,
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 -.5, -.5, .5, .5,
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 0, 0, 0, 0,
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 1, 1, 1, 1)
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 def get_sprite_rendering_data(Sprite sprite):
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 if sprite.changed:
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 render_sprite(sprite)
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33 data = <RenderingData*>sprite._rendering_data
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 color = <unsigned int>data.color[0] << 24 | <unsigned int>data.color[1] << 16 | <unsigned int>data.color[2] << 8 | <unsigned int>data.color[3]
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 return (data.pos, [data.left, data.right, data.bottom, data.top], color)
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38 cdef void render_sprite(Sprite sprite) nogil:
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 cdef Matrix vertmat
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 if sprite._rendering_data == NULL:
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 sprite._rendering_data = malloc(sizeof(RenderingData))
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 data = <RenderingData*>sprite._rendering_data
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 memcpy(&vertmat, &default, sizeof(Matrix))
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 tx, ty, tw, th = sprite._texcoords[0], sprite._texcoords[1], sprite._texcoords[2], sprite._texcoords[3]
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48 sx, sy = sprite._rescale[0], sprite._rescale[1]
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 width = sprite.width_override or (tw * sx)
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 height = sprite.height_override or (th * sy)
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 scale2d(&vertmat, width, height)
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 if sprite.mirrored:
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 flip(&vertmat)
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 rx, ry, rz = sprite._rotations_3d[0], sprite._rotations_3d[1], sprite._rotations_3d[2]
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 if sprite.automatic_orientation:
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58 rz += pi/2. - sprite.angle
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 elif sprite.force_rotation:
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 rz += sprite.angle
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 if rx:
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 rotate_x(&vertmat, -rx)
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 if ry:
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 rotate_y(&vertmat, ry)
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 if rz:
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 rotate_z(&vertmat, -rz) #TODO: minus, really?
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 if sprite.allow_dest_offset:
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 translate(&vertmat, sprite._dest_offset)
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 if sprite.corner_relative_placement: # Reposition
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 translate2d(&vertmat, width / 2, height / 2)
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 memcpy(data.pos, &vertmat, 12 * sizeof(float))
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 x_1 = sprite.anm.size_inv[0]
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 y_1 = sprite.anm.size_inv[1]
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 tox, toy = sprite._texoffsets[0], sprite._texoffsets[1]
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 data.left = tx * x_1 + tox
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 data.right = (tx + tw) * x_1 + tox
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 data.bottom = ty * y_1 + toy
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 data.top = (ty + th) * y_1 + toy
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 for i in range(4):
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 data.color[i] = sprite._color[i]
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85
7492d384d122 Rust: Add a Glide renderer (2D only for now)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86 sprite.changed = False