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 corner
146/// but to the baseline start position.
147pub fn draw_text(t: &str, f: &Font, p: Point, c: Color) {
148    let text_ptr = t.as_ptr();
149    let text_len = t.len();
150    let font_ptr = f.raw.as_ptr();
151    let font_len = f.raw.len();
152    unsafe {
153        b::draw_text(
154            text_ptr as u32,
155            text_len as u32,
156            font_ptr as u32,
157            font_len as u32,
158            p.x,
159            p.y,
160            c.into(),
161        );
162    }
163}
164
165/// Render a QR code for the given text.
166pub fn draw_qr(t: &str, p: Point, black: Color, white: Color) {
167    let ptr = t.as_ptr();
168    let len = t.len();
169    unsafe {
170        b::draw_qr(ptr as u32, len as u32, p.x, p.y, black.into(), white.into());
171    }
172}
173
174/// Render an image using the given colors.
175pub fn draw_image(i: &Image, p: Point) {
176    let ptr = i.raw.as_ptr();
177    let len = i.raw.len();
178    unsafe {
179        b::draw_image(ptr as u32, len as u32, p.x, p.y);
180    }
181}
182
183/// Draw a subregion of an image.
184///
185/// Most often used to draw a sprite from a sprite atlas.
186pub fn draw_sub_image(i: &SubImage, p: Point) {
187    let ptr = i.raw.as_ptr();
188    let len = i.raw.len();
189    unsafe {
190        b::draw_sub_image(
191            ptr as u32,
192            len as u32,
193            p.x,
194            p.y,
195            i.point.x,
196            i.point.y,
197            i.size.width,
198            i.size.height,
199        );
200    }
201}
202
203/// Set canvas to be used for all subsequent drawing operations.
204pub fn set_canvas(c: &Canvas) {
205    let ptr = c.raw.as_ptr();
206    let len = c.raw.len();
207    unsafe {
208        b::set_canvas(ptr as u32, len as u32);
209    }
210}
211
212/// Unset canvas set by [`set_canvas`]. All subsequent drawing operations will target frame buffer.
213pub fn unset_canvas() {
214    unsafe {
215        b::unset_canvas();
216    }
217}