use sdl2::gfx::primitives::DrawRenderer; use sdl2::pixels::Color; use sdl2::rect::{Point, Rect}; use sdl2::render::{BlendMode, Canvas, Texture}; use sdl2::video::{FullscreenType, Window}; pub struct Renderer { canvas: Canvas, } impl Renderer { pub fn new(mut canvas: Canvas) -> Self { canvas.set_blend_mode(BlendMode::Add); canvas.set_draw_color((0, 0, 0)); canvas.clear(); canvas.present(); Renderer { canvas } } pub fn canvas(&mut self) -> &mut Canvas { &mut self.canvas } #[allow(dead_code)] pub fn viewport(&self) -> (u16, u16) { let vp = self.canvas.viewport(); (vp.width() as u16, vp.height() as u16) } pub fn toggle_fullscreen(&mut self) { match self.canvas.window().fullscreen_state() { FullscreenType::Off => self .canvas .window_mut() .set_fullscreen(FullscreenType::Desktop), _ => self.canvas.window_mut().set_fullscreen(FullscreenType::Off), } .unwrap(); } pub fn clear(&mut self) { self.canvas.set_draw_color((0, 0, 0)); self.canvas.clear(); } pub fn present(&mut self) { self.canvas.present(); } pub fn blit(&mut self, texture: &Texture, src: R1, dst: R2) where R1: Into>, R2: Into> { self.canvas.copy(texture, src, dst).unwrap(); } pub fn blit_ex(&mut self, texture: &Texture, src: R1, dst: R2, angle: f64, center: P, flip_horizontal: bool, flip_vertical: bool) where R1: Into>, R2: Into>, P: Into> { self.canvas.copy_ex(texture, src, dst, angle, center, flip_horizontal, flip_vertical).unwrap(); } pub fn circle(&self, pos: P, rad: i16, col: C) where P: Into<(i16, i16)>, C: Into, { let pos = pos.into(); self.canvas.aa_circle(pos.0, pos.1, rad, col.into()).unwrap(); } pub fn ellipse(&self, pos: P, rad: R, col: C) where P: Into<(i16, i16)>, R: Into<(i16, i16)>, C: Into, { let pos = pos.into(); let rad = rad.into(); self.canvas.aa_ellipse(pos.0, pos.1, rad.0, rad.1, col.into()).unwrap(); } pub fn draw_line(&mut self, start: P1, end: P2, col: C) where P1: Into, P2: Into, C: Into, { self.canvas.set_draw_color(col); self.canvas.draw_line(start, end).unwrap(); } }