1use ndless::prelude::*;
2
3pub use crate::video::{Color, Surface};
4
5pub mod ll;
6
7pub type Point = (i16, i16);
8
9pub trait Graphics {
10 fn draw_pixel(&self, point: Point, color: Color);
11
12 fn draw_horiz_line(&self, x1: i16, x2: i16, y: i16, color: Color);
13 fn draw_vert_line(&self, x: i16, y1: i16, y2: i16, color: Color);
14 fn draw_line(&self, point1: Point, point2: Point, color: Color);
15 fn draw_antialiased_line(&self, point1: Point, point2: Point, color: Color);
16 fn draw_thick_line(&self, point1: Point, point2: Point, width: u8, color: Color);
17
18 fn draw_rectangle(&self, point1: Point, point2: Point, color: Color);
19 fn draw_rounded_rectangle(&self, point1: Point, point2: Point, radius: i16, color: Color);
20 fn draw_filled_rectangle(&self, point1: Point, point2: Point, color: Color);
21 fn draw_rounded_filled_rectangle(
22 &self,
23 point1: Point,
24 point2: Point,
25 radius: i16,
26 color: Color,
27 );
28
29 fn draw_circle(&self, center: Point, radius: i16, color: Color);
30 fn draw_filled_circle(&self, center: Point, radius: i16, color: Color);
31 fn draw_antialiased_circle(&self, center: Point, radius: i16, color: Color);
32
33 fn draw_arc(&self, center: Point, radius: i16, start: i16, end: i16, color: Color);
34
35 fn draw_ellipse(&self, center: Point, x_radius: i16, y_radius: i16, color: Color);
36 fn draw_filled_ellipse(&self, center: Point, x_radius: i16, y_radius: i16, color: Color);
37 fn draw_antialiased_ellipse(&self, center: Point, x_radius: i16, y_radius: i16, color: Color);
38
39 fn draw_pie(&self, center: Point, radius: i16, start: i16, end: i16, color: Color);
40 fn draw_filled_pie(&self, center: Point, radius: i16, start: i16, end: i16, color: Color);
41
42 fn draw_triangle(&self, points: [Point; 3], color: Color);
43 fn draw_filled_triangle(&self, points: [Point; 3], color: Color);
44 fn draw_antialiased_triangle(&self, points: [Point; 3], color: Color);
45
46 fn draw_polygon(&self, points: &[Point], color: Color) {
47 let (x, y): (Vec<_>, Vec<_>) = points.iter().cloned().unzip();
48 self.draw_polygon_list(&x[..], &y[..], color)
49 }
50
51 fn draw_filled_polygon(&self, points: &[Point], color: Color) {
52 let (x, y): (Vec<_>, Vec<_>) = points.iter().cloned().unzip();
53 self.draw_filled_polygon_list(&x[..], &y[..], color)
54 }
55
56 fn draw_antialiased_polygon(&self, points: &[Point], color: Color) {
57 let (x, y): (Vec<_>, Vec<_>) = points.iter().cloned().unzip();
58 self.draw_antialiased_polygon_list(&x[..], &y[..], color)
59 }
60
61 fn draw_textured_polygon(&self, points: &[Point], texture: &Surface, texture_offset: Point) {
62 let (x, y): (Vec<_>, Vec<_>) = points.iter().cloned().unzip();
63 self.draw_textured_polygon_list(&x[..], &y[..], texture, texture_offset)
64 }
65
66 fn draw_polygon_list(&self, x_points: &[i16], y_points: &[i16], color: Color);
67 fn draw_filled_polygon_list(&self, x_points: &[i16], y_points: &[i16], color: Color);
68 fn draw_antialiased_polygon_list(&self, x_points: &[i16], y_points: &[i16], color: Color);
69 fn draw_textured_polygon_list(
70 &self,
71 x_points: &[i16],
72 y_points: &[i16],
73 texture: &Surface,
74 texture_offset: Point,
75 );
76
77 fn draw_bezier(&self, points: &[Point], interpolation: i32, color: Color) {
78 let (x, y): (Vec<_>, Vec<_>) = points.iter().cloned().unzip();
79 self.draw_bezier_list(&x[..], &y[..], interpolation, color)
80 }
81 fn draw_bezier_list(
82 &self,
83 x_points: &[i16],
84 y_points: &[i16],
85 interpolation: i32,
86 color: Color,
87 );
88}
89
90impl Graphics for Surface {
91 fn draw_pixel(&self, point: Point, color: Color) {
92 unsafe {
93 ll::pixelColor(
94 self.raw,
95 point.0,
96 point.1,
97 color.to_mapped((*self.raw).format),
98 );
99 }
100 }
101
102 fn draw_horiz_line(&self, x1: i16, x2: i16, y: i16, color: Color) {
103 unsafe {
104 ll::hlineColor(self.raw, x1, x2, y, color.to_mapped((*self.raw).format));
105 }
106 }
107
108 fn draw_vert_line(&self, x: i16, y1: i16, y2: i16, color: Color) {
109 unsafe {
110 ll::vlineColor(self.raw, x, y1, y2, color.to_mapped((*self.raw).format));
111 }
112 }
113
114 fn draw_line(&self, point1: Point, point2: Point, color: Color) {
115 unsafe {
116 ll::lineColor(
117 self.raw,
118 point1.0,
119 point1.1,
120 point2.0,
121 point2.1,
122 color.to_mapped((*self.raw).format),
123 );
124 }
125 }
126
127 fn draw_antialiased_line(&self, point1: Point, point2: Point, color: Color) {
128 unsafe {
129 ll::aalineColor(
130 self.raw,
131 point1.0,
132 point1.1,
133 point2.0,
134 point2.1,
135 color.to_mapped((*self.raw).format),
136 );
137 }
138 }
139
140 fn draw_thick_line(&self, point1: Point, point2: Point, width: u8, color: Color) {
141 unsafe {
142 ll::thickLineColor(
143 self.raw,
144 point1.0,
145 point1.1,
146 point2.0,
147 point2.1,
148 width,
149 color.to_mapped((*self.raw).format),
150 );
151 }
152 }
153
154 fn draw_rectangle(&self, point1: Point, point2: Point, color: Color) {
155 unsafe {
156 ll::rectangleColor(
157 self.raw,
158 point1.0,
159 point1.1,
160 point2.0,
161 point2.1,
162 color.to_mapped((*self.raw).format),
163 );
164 }
165 }
166
167 fn draw_rounded_rectangle(&self, point1: Point, point2: Point, radius: i16, color: Color) {
168 unsafe {
169 ll::roundedRectangleColor(
170 self.raw,
171 point1.0,
172 point1.1,
173 point2.0,
174 point2.1,
175 radius,
176 color.to_mapped((*self.raw).format),
177 );
178 }
179 }
180
181 fn draw_filled_rectangle(&self, point1: Point, point2: Point, color: Color) {
182 unsafe {
183 ll::boxColor(
184 self.raw,
185 point1.0,
186 point1.1,
187 point2.0,
188 point2.1,
189 color.to_mapped((*self.raw).format),
190 );
191 }
192 }
193
194 fn draw_rounded_filled_rectangle(
195 &self,
196 point1: Point,
197 point2: Point,
198 radius: i16,
199 color: Color,
200 ) {
201 unsafe {
202 ll::roundedBoxColor(
203 self.raw,
204 point1.0,
205 point1.1,
206 point2.0,
207 point2.1,
208 radius,
209 color.to_mapped((*self.raw).format),
210 );
211 }
212 }
213
214 fn draw_circle(&self, center: Point, radius: i16, color: Color) {
215 unsafe {
216 ll::circleColor(
217 self.raw,
218 center.0,
219 center.1,
220 radius,
221 color.to_mapped((*self.raw).format),
222 );
223 }
224 }
225
226 fn draw_filled_circle(&self, center: Point, radius: i16, color: Color) {
227 unsafe {
228 ll::filledCircleColor(
229 self.raw,
230 center.0,
231 center.1,
232 radius,
233 color.to_mapped((*self.raw).format),
234 );
235 }
236 }
237
238 fn draw_antialiased_circle(&self, center: Point, radius: i16, color: Color) {
239 unsafe {
240 ll::aacircleColor(
241 self.raw,
242 center.0,
243 center.1,
244 radius,
245 color.to_mapped((*self.raw).format),
246 );
247 }
248 }
249
250 fn draw_arc(&self, center: Point, radius: i16, start: i16, end: i16, color: Color) {
251 unsafe {
252 ll::arcColor(
253 self.raw,
254 center.0,
255 center.1,
256 radius,
257 start,
258 end,
259 color.to_mapped((*self.raw).format),
260 );
261 }
262 }
263
264 fn draw_ellipse(&self, center: Point, x_radius: i16, y_radius: i16, color: Color) {
265 unsafe {
266 ll::ellipseColor(
267 self.raw,
268 center.0,
269 center.1,
270 x_radius,
271 y_radius,
272 color.to_mapped((*self.raw).format),
273 );
274 }
275 }
276
277 fn draw_filled_ellipse(&self, center: Point, x_radius: i16, y_radius: i16, color: Color) {
278 unsafe {
279 ll::filledEllipseColor(
280 self.raw,
281 center.0,
282 center.1,
283 x_radius,
284 y_radius,
285 color.to_mapped((*self.raw).format),
286 );
287 }
288 }
289
290 fn draw_antialiased_ellipse(&self, center: Point, x_radius: i16, y_radius: i16, color: Color) {
291 unsafe {
292 ll::aaellipseColor(
293 self.raw,
294 center.0,
295 center.1,
296 x_radius,
297 y_radius,
298 color.to_mapped((*self.raw).format),
299 );
300 }
301 }
302
303 fn draw_pie(&self, center: Point, radius: i16, start: i16, end: i16, color: Color) {
304 unsafe {
305 ll::pieColor(
306 self.raw,
307 center.0,
308 center.1,
309 radius,
310 start,
311 end,
312 color.to_mapped((*self.raw).format),
313 );
314 }
315 }
316
317 fn draw_filled_pie(&self, center: Point, radius: i16, start: i16, end: i16, color: Color) {
318 unsafe {
319 ll::filledPieColor(
320 self.raw,
321 center.0,
322 center.1,
323 radius,
324 start,
325 end,
326 color.to_mapped((*self.raw).format),
327 );
328 }
329 }
330
331 fn draw_triangle(&self, points: [Point; 3], color: Color) {
332 unsafe {
333 ll::trigonColor(
334 self.raw,
335 points[0].0,
336 points[0].1,
337 points[1].0,
338 points[1].1,
339 points[2].0,
340 points[2].1,
341 color.to_mapped((*self.raw).format),
342 );
343 }
344 }
345
346 fn draw_filled_triangle(&self, points: [Point; 3], color: Color) {
347 unsafe {
348 ll::filledTrigonColor(
349 self.raw,
350 points[0].0,
351 points[0].1,
352 points[1].0,
353 points[1].1,
354 points[2].0,
355 points[2].1,
356 color.to_mapped((*self.raw).format),
357 );
358 }
359 }
360
361 fn draw_antialiased_triangle(&self, points: [Point; 3], color: Color) {
362 unsafe {
363 ll::aatrigonColor(
364 self.raw,
365 points[0].0,
366 points[0].1,
367 points[1].0,
368 points[1].1,
369 points[2].0,
370 points[2].1,
371 color.to_mapped((*self.raw).format),
372 );
373 }
374 }
375
376 fn draw_polygon_list(&self, x_points: &[i16], y_points: &[i16], color: Color) {
377 let x_len = x_points.len();
378 let y_len = y_points.len();
379 unsafe {
380 ll::polygonColor(
381 self.raw,
382 x_points.as_ptr(),
383 y_points.as_ptr(),
384 x_len.min(y_len) as i32,
385 color.to_mapped((*self.raw).format),
386 );
387 }
388 }
389
390 fn draw_filled_polygon_list(&self, x_points: &[i16], y_points: &[i16], color: Color) {
391 let x_len = x_points.len();
392 let y_len = y_points.len();
393 unsafe {
394 ll::filledPolygonColor(
395 self.raw,
396 x_points.as_ptr(),
397 y_points.as_ptr(),
398 x_len.min(y_len) as i32,
399 color.to_mapped((*self.raw).format),
400 );
401 }
402 }
403
404 fn draw_antialiased_polygon_list(&self, x_points: &[i16], y_points: &[i16], color: Color) {
405 let x_len = x_points.len();
406 let y_len = y_points.len();
407 unsafe {
408 ll::aapolygonColor(
409 self.raw,
410 x_points.as_ptr(),
411 y_points.as_ptr(),
412 x_len.min(y_len) as i32,
413 color.to_mapped((*self.raw).format),
414 );
415 }
416 }
417
418 fn draw_textured_polygon_list(
419 &self,
420 x_points: &[i16],
421 y_points: &[i16],
422 texture: &Surface,
423 texture_offset: Point,
424 ) {
425 let x_len = x_points.len();
426 let y_len = y_points.len();
427 unsafe {
428 ll::texturedPolygon(
429 self.raw,
430 x_points.as_ptr(),
431 y_points.as_ptr(),
432 x_len.min(y_len) as i32,
433 texture.raw,
434 i32::from(texture_offset.0),
435 i32::from(texture_offset.1),
436 );
437 }
438 }
439
440 fn draw_bezier_list(
441 &self,
442 x_points: &[i16],
443 y_points: &[i16],
444 interpolation: i32,
445 color: Color,
446 ) {
447 let x_len = x_points.len();
448 let y_len = y_points.len();
449 unsafe {
450 ll::bezierColor(
451 self.raw,
452 x_points.as_ptr(),
453 y_points.as_ptr(),
454 x_len.min(y_len) as i32,
455 interpolation,
456 color.to_mapped((*self.raw).format),
457 );
458 }
459 }
460}