Commit | Line | Data |
---|---|---|
6566d7e5 TW |
1 | use sdl2::gfx::primitives::DrawRenderer; |
2 | use sdl2::pixels::Color; | |
3 | use sdl2::rect::{Point, Rect}; | |
4 | use sdl2::render::{BlendMode, Canvas, Texture}; | |
5 | use sdl2::video::{FullscreenType, Window}; | |
6 | ||
7 | pub struct Renderer { | |
8 | canvas: Canvas<Window>, | |
9 | } | |
10 | ||
11 | impl Renderer { | |
12 | pub fn new(mut canvas: Canvas<Window>) -> Self { | |
13 | canvas.set_blend_mode(BlendMode::Add); | |
14 | canvas.set_draw_color((0, 0, 0)); | |
15 | canvas.clear(); | |
16 | canvas.present(); | |
17 | Renderer { canvas } | |
18 | } | |
19 | ||
20 | pub fn canvas(&mut self) -> &mut Canvas<Window> { | |
21 | &mut self.canvas | |
22 | } | |
23 | ||
4a7f4e28 | 24 | #[allow(dead_code)] |
6566d7e5 TW |
25 | pub fn viewport(&self) -> (u16, u16) { |
26 | let vp = self.canvas.viewport(); | |
27 | (vp.width() as u16, vp.height() as u16) | |
28 | } | |
29 | ||
30 | pub fn toggle_fullscreen(&mut self) { | |
31 | match self.canvas.window().fullscreen_state() { | |
32 | FullscreenType::Off => self | |
33 | .canvas | |
34 | .window_mut() | |
35 | .set_fullscreen(FullscreenType::Desktop), | |
36 | _ => self.canvas.window_mut().set_fullscreen(FullscreenType::Off), | |
37 | } | |
38 | .unwrap(); | |
39 | } | |
40 | ||
41 | pub fn clear(&mut self) { | |
42 | self.canvas.set_draw_color((0, 0, 0)); | |
43 | self.canvas.clear(); | |
44 | } | |
45 | ||
46 | pub fn present(&mut self) { | |
47 | self.canvas.present(); | |
48 | } | |
49 | ||
50 | pub fn blit<R1, R2>(&mut self, texture: &Texture, src: R1, dst: R2) | |
51 | where R1: Into<Option<Rect>>, | |
52 | R2: Into<Option<Rect>> | |
53 | { | |
54 | self.canvas.copy(texture, src, dst).unwrap(); | |
55 | } | |
56 | ||
57 | pub fn blit_ex<R1, R2, P>(&mut self, | |
58 | texture: &Texture, | |
59 | src: R1, | |
60 | dst: R2, | |
61 | angle: f64, | |
62 | center: P, | |
63 | flip_horizontal: bool, | |
64 | flip_vertical: bool) | |
65 | where R1: Into<Option<Rect>>, | |
66 | R2: Into<Option<Rect>>, | |
67 | P: Into<Option<Point>> | |
68 | { | |
69 | self.canvas.copy_ex(texture, src, dst, angle, center, flip_horizontal, flip_vertical).unwrap(); | |
70 | } | |
71 | ||
72 | pub fn circle<P, C>(&self, pos: P, rad: i16, col: C) | |
73 | where P: Into<(i16, i16)>, | |
74 | C: Into<Color>, | |
75 | { | |
76 | let pos = pos.into(); | |
77 | self.canvas.aa_circle(pos.0, pos.1, rad, col.into()).unwrap(); | |
78 | } | |
79 | ||
80 | pub fn ellipse<P, R, C>(&self, pos: P, rad: R, col: C) | |
81 | where P: Into<(i16, i16)>, | |
82 | R: Into<(i16, i16)>, | |
83 | C: Into<Color>, | |
84 | { | |
85 | let pos = pos.into(); | |
86 | let rad = rad.into(); | |
87 | self.canvas.aa_ellipse(pos.0, pos.1, rad.0, rad.1, col.into()).unwrap(); | |
88 | } | |
89 | ||
90 | pub fn draw_line<P1, P2, C>(&mut self, start: P1, end: P2, col: C) | |
91 | where P1: Into<Point>, | |
92 | P2: Into<Point>, | |
93 | C: Into<Color>, | |
94 | { | |
95 | self.canvas.set_draw_color(col); | |
96 | self.canvas.draw_line(start, end).unwrap(); | |
97 | } | |
98 | } |