firefly_rust/graphics/
funcs.rs

1use super::{bindings as b, *};
2use crate::*;
3
4/// Fill the whole frame with the given color.
5pub fn clear_screen(c: Color) {
6    unsafe {
7        b::clear_screen(c.into());
8    }
9}
10
11/// Set a color value in the palette.
12pub fn set_color(c: Color, v: RGB) {
13    unsafe {
14        b::set_color(c.into(), v.r.into(), v.g.into(), v.b.into());
15    }
16}
17
18/// Set a single point (1 pixel is scaling is 1) on the frame.
19pub fn draw_point(p: Point, c: Color) {
20    unsafe {
21        b::draw_point(p.x, p.y, c.into());
22    }
23}
24
25/// Draw a straight line from point a to point b.
26pub fn draw_line(a: Point, b: Point, s: LineStyle) {
27    unsafe {
28        b::draw_line(a.x, a.y, b.x, b.y, s.color.into(), s.width);
29    }
30}
31
32/// Draw a rectangle filling the given bounding box.
33pub fn draw_rect(p: Point, b: Size, s: Style) {
34    unsafe {
35        b::draw_rect(
36            p.x,
37            p.y,
38            b.width,
39            b.height,
40            s.fill_color.into(),
41            s.stroke_color.into(),
42            s.stroke_width,
43        );
44    }
45}
46
47/// Draw a rectangle with rounded corners.
48pub fn draw_rounded_rect(p: Point, b: Size, corner: Size, s: Style) {
49    unsafe {
50        b::draw_rounded_rect(
51            p.x,
52            p.y,
53            b.width,
54            b.height,
55            corner.width,
56            corner.height,
57            s.fill_color.into(),
58            s.stroke_color.into(),
59            s.stroke_width,
60        );
61    }
62}
63
64/// Draw a circle with the given diameter.
65pub fn draw_circle(p: Point, d: i32, s: Style) {
66    unsafe {
67        b::draw_circle(
68            p.x,
69            p.y,
70            d,
71            s.fill_color.into(),
72            s.stroke_color.into(),
73            s.stroke_width,
74        );
75    }
76}
77
78/// Draw an ellipse (oval).
79pub fn draw_ellipse(p: Point, b: Size, s: Style) {
80    unsafe {
81        b::draw_ellipse(
82            p.x,
83            p.y,
84            b.width,
85            b.height,
86            s.fill_color.into(),
87            s.stroke_color.into(),
88            s.stroke_width,
89        );
90    }
91}
92
93/// Draw a triangle.
94///
95/// The order of points doesn't matter.
96pub fn draw_triangle(a: Point, b: Point, c: Point, s: Style) {
97    unsafe {
98        b::draw_triangle(
99            a.x,
100            a.y,
101            b.x,
102            b.y,
103            c.x,
104            c.y,
105            s.fill_color.into(),
106            s.stroke_color.into(),
107            s.stroke_width,
108        );
109    }
110}
111
112/// Draw an arc.
113pub fn draw_arc(p: Point, d: i32, start: Angle, sweep: Angle, s: Style) {
114    unsafe {
115        b::draw_arc(
116            p.x,
117            p.y,
118            d,
119            start.0,
120            sweep.0,
121            s.fill_color.into(),
122            s.stroke_color.into(),
123            s.stroke_width,
124        );
125    }
126}
127
128/// Draw a sector.
129pub fn draw_sector(p: Point, d: i32, start: Angle, sweep: Angle, s: Style) {
130    unsafe {
131        b::draw_sector(
132            p.x,
133            p.y,
134            d,
135            start.0,
136            sweep.0,
137            s.fill_color.into(),
138            s.stroke_color.into(),
139            s.stroke_width,
140        );
141    }
142}
143
144/// Render text using the given font.
145///
146/// Unlike in the other drawing functions, here [Point] points not to the top-left corner
147/// but to the baseline start position.
148pub fn draw_text(t: &str, f: &Font, p: Point, c: Color) {
149    let text_ptr = t.as_ptr();
150    let text_len = t.len();
151    let font_ptr = f.raw.as_ptr();
152    let font_len = f.raw.len();
153    unsafe {
154        b::draw_text(
155            text_ptr as u32,
156            text_len as u32,
157            font_ptr as u32,
158            font_len as u32,
159            p.x,
160            p.y,
161            c.into(),
162        );
163    }
164}
165
166/// Render a QR code for the given text.
167pub fn draw_qr(t: &str, p: Point, black: Color, white: Color) {
168    let ptr = t.as_ptr();
169    let len = t.len();
170    unsafe {
171        b::draw_qr(ptr as u32, len as u32, p.x, p.y, black.into(), white.into());
172    }
173}
174
175/// Render an image using the given colors.
176pub fn draw_image(i: &Image, p: Point) {
177    let ptr = i.raw.as_ptr();
178    let len = i.raw.len();
179    unsafe {
180        b::draw_image(ptr as u32, len as u32, p.x, p.y);
181    }
182}
183
184/// Draw a subregion of an image.
185///
186/// Most often used to draw a sprite from a sprite atlas.
187pub fn draw_sub_image(i: &SubImage, p: Point) {
188    let ptr = i.raw.as_ptr();
189    let len = i.raw.len();
190    unsafe {
191        b::draw_sub_image(
192            ptr as u32,
193            len as u32,
194            p.x,
195            p.y,
196            i.point.x,
197            i.point.y,
198            i.size.width,
199            i.size.height,
200        );
201    }
202}
203
204/// Set canvas to be used for all subsequent drawing operations.
205pub fn set_canvas(c: &Canvas) {
206    let ptr = c.raw.as_ptr();
207    let len = c.raw.len();
208    unsafe {
209        b::set_canvas(ptr as u32, len as u32);
210    }
211}
212
213/// Unset canvas set by [`set_canvas`]. All subsequent drawing operations will target frame buffer.
214pub fn unset_canvas() {
215    unsafe {
216        b::unset_canvas();
217    }
218}