| 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 | |
| 24 | #[allow(dead_code)] |
| 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 | #[allow(dead_code)] |
| 58 | pub fn blit_ex<R1, R2, P>(&mut self, |
| 59 | texture: &Texture, |
| 60 | src: R1, |
| 61 | dst: R2, |
| 62 | angle: f64, |
| 63 | center: P, |
| 64 | flip_horizontal: bool, |
| 65 | flip_vertical: bool) |
| 66 | where R1: Into<Option<Rect>>, |
| 67 | R2: Into<Option<Rect>>, |
| 68 | P: Into<Option<Point>> |
| 69 | { |
| 70 | self.canvas.copy_ex(texture, src, dst, angle, center, flip_horizontal, flip_vertical).unwrap(); |
| 71 | } |
| 72 | |
| 73 | pub fn circle<P, C>(&self, pos: P, rad: i16, col: C) |
| 74 | where P: Into<(i16, i16)>, |
| 75 | C: Into<Color>, |
| 76 | { |
| 77 | let pos = pos.into(); |
| 78 | self.canvas.aa_circle(pos.0, pos.1, rad, col.into()).unwrap(); |
| 79 | } |
| 80 | |
| 81 | #[allow(dead_code)] |
| 82 | pub fn ellipse<P, R, C>(&self, pos: P, rad: R, col: C) |
| 83 | where P: Into<(i16, i16)>, |
| 84 | R: Into<(i16, i16)>, |
| 85 | C: Into<Color>, |
| 86 | { |
| 87 | let pos = pos.into(); |
| 88 | let rad = rad.into(); |
| 89 | self.canvas.aa_ellipse(pos.0, pos.1, rad.0, rad.1, col.into()).unwrap(); |
| 90 | } |
| 91 | |
| 92 | pub fn draw_line<P1, P2, C>(&mut self, start: P1, end: P2, col: C) |
| 93 | where P1: Into<Point>, |
| 94 | P2: Into<Point>, |
| 95 | C: Into<Color>, |
| 96 | { |
| 97 | self.canvas.set_draw_color(col); |
| 98 | self.canvas.draw_line(start, end).unwrap(); |
| 99 | } |
| 100 | } |