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