comparison 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
comparison
equal deleted inserted replaced
771:79c3f782dd41 772:7492d384d122
1 # -*- encoding: utf-8 -*-
2 ##
3 ## Copyright (C) 2011 Thibaut Girka <thib@sitedethib.com>
4 ##
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published
7 ## by the Free Software Foundation; version 3 only.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
13 ##
14
15
16 from libc.stdlib cimport malloc
17 from libc.string cimport memcpy
18 from libc.math cimport M_PI as pi
19
20 from pytouhou.utils.matrix cimport Matrix, scale2d, flip, rotate_x, rotate_y, rotate_z, translate, translate2d
21
22
23 cdef Matrix default
24 default = Matrix(-.5, .5, .5, -.5,
25 -.5, -.5, .5, .5,
26 0, 0, 0, 0,
27 1, 1, 1, 1)
28
29
30 def get_sprite_rendering_data(Sprite sprite):
31 if sprite.changed:
32 render_sprite(sprite)
33 data = <RenderingData*>sprite._rendering_data
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]
35 return (data.pos, [data.left, data.right, data.bottom, data.top], color)
36
37
38 cdef void render_sprite(Sprite sprite) nogil:
39 cdef Matrix vertmat
40
41 if sprite._rendering_data == NULL:
42 sprite._rendering_data = malloc(sizeof(RenderingData))
43
44 data = <RenderingData*>sprite._rendering_data
45 memcpy(&vertmat, &default, sizeof(Matrix))
46
47 tx, ty, tw, th = sprite._texcoords[0], sprite._texcoords[1], sprite._texcoords[2], sprite._texcoords[3]
48 sx, sy = sprite._rescale[0], sprite._rescale[1]
49 width = sprite.width_override or (tw * sx)
50 height = sprite.height_override or (th * sy)
51
52 scale2d(&vertmat, width, height)
53 if sprite.mirrored:
54 flip(&vertmat)
55
56 rx, ry, rz = sprite._rotations_3d[0], sprite._rotations_3d[1], sprite._rotations_3d[2]
57 if sprite.automatic_orientation:
58 rz += pi/2. - sprite.angle
59 elif sprite.force_rotation:
60 rz += sprite.angle
61
62 if rx:
63 rotate_x(&vertmat, -rx)
64 if ry:
65 rotate_y(&vertmat, ry)
66 if rz:
67 rotate_z(&vertmat, -rz) #TODO: minus, really?
68 if sprite.allow_dest_offset:
69 translate(&vertmat, sprite._dest_offset)
70 if sprite.corner_relative_placement: # Reposition
71 translate2d(&vertmat, width / 2, height / 2)
72
73 memcpy(data.pos, &vertmat, 12 * sizeof(float))
74
75 x_1 = sprite.anm.size_inv[0]
76 y_1 = sprite.anm.size_inv[1]
77 tox, toy = sprite._texoffsets[0], sprite._texoffsets[1]
78 data.left = tx * x_1 + tox
79 data.right = (tx + tw) * x_1 + tox
80 data.bottom = ty * y_1 + toy
81 data.top = (ty + th) * y_1 + toy
82
83 for i in range(4):
84 data.color[i] = sprite._color[i]
85
86 sprite.changed = False