+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<Window>,
+}
+
+impl Renderer {
+ pub fn new(mut canvas: Canvas<Window>) -> 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<Window> {
+ &mut self.canvas
+ }
+
+ 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<R1, R2>(&mut self, texture: &Texture, src: R1, dst: R2)
+ where R1: Into<Option<Rect>>,
+ R2: Into<Option<Rect>>
+ {
+ self.canvas.copy(texture, src, dst).unwrap();
+ }
+
+ pub fn blit_ex<R1, R2, P>(&mut self,
+ texture: &Texture,
+ src: R1,
+ dst: R2,
+ angle: f64,
+ center: P,
+ flip_horizontal: bool,
+ flip_vertical: bool)
+ where R1: Into<Option<Rect>>,
+ R2: Into<Option<Rect>>,
+ P: Into<Option<Point>>
+ {
+ self.canvas.copy_ex(texture, src, dst, angle, center, flip_horizontal, flip_vertical).unwrap();
+ }
+
+ pub fn circle<P, C>(&self, pos: P, rad: i16, col: C)
+ where P: Into<(i16, i16)>,
+ C: Into<Color>,
+ {
+ let pos = pos.into();
+ self.canvas.aa_circle(pos.0, pos.1, rad, col.into()).unwrap();
+ }
+
+ pub fn ellipse<P, R, C>(&self, pos: P, rad: R, col: C)
+ where P: Into<(i16, i16)>,
+ R: Into<(i16, i16)>,
+ C: Into<Color>,
+ {
+ 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<P1, P2, C>(&mut self, start: P1, end: P2, col: C)
+ where P1: Into<Point>,
+ P2: Into<Point>,
+ C: Into<Color>,
+ {
+ self.canvas.set_draw_color(col);
+ self.canvas.draw_line(start, end).unwrap();
+ }
+}