Skip to main content

firefly_rust/graphics/
funcs.rs

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