index : snake-rs
An implementation of the classic Snake game in Rust using SDL2
about summary refs log tree commit diff
snake-rs / src / renderer.rs
-rw-r--r-- blob c1df5dc4ccfb4aafce23fb7080d71a35ac032cc6 3431 bytes (plain)
1
use sdl2::{pixels::Color, rect::Rect, ttf};
2
3
use crate::{
4
    game_context::{GameContext, GameState, Point},
5
    ui::Ui,
6
};
7
8
pub struct Renderer {
9
    pub canvas: sdl2::render::Canvas<sdl2::video::Window>,
10
    pub font: sdl2::ttf::Font<'static, 'static>,
11
    pub ui: Ui,
12
}
13
14
impl Renderer {
15
    pub const GRID_X_SIZE: u32 = 40;
16
    pub const GRID_Y_SIZE: u32 = 30;
17
    pub const DOT_SIZE_IN_PXS: u32 = 20;
18
19
    pub fn new(sdl: &mut sdl2::Sdl) -> Result<Self, String> {
20
        let video_subsystem: sdl2::VideoSubsystem = sdl.video()?;
21
        let window: sdl2::video::Window = video_subsystem
22
            .window(
23
                "rust-sdl2 demo",
24
                Self::GRID_X_SIZE * Self::DOT_SIZE_IN_PXS,
25
                Self::GRID_Y_SIZE * Self::DOT_SIZE_IN_PXS,
26
            )
27
            .position_centered()
28
            .opengl()
29
            .build()
30
            .unwrap();
31
32
        let canvas: sdl2::render::Canvas<sdl2::video::Window> =
33
            window.clone().into_canvas().build().unwrap();
34
35
        let ttf = ttf::init().map_err(|e| e.to_string())?;
36
        let ttf_leaked: &'static sdl2::ttf::Sdl2TtfContext = Box::leak(Box::new(ttf));
37
38
        let font_data = include_bytes!("../assets/fonts/JetBrainsMonoNerdFont.ttf");
39
40
        let font = ttf_leaked
41
            .load_font_from_rwops(sdl2::rwops::RWops::from_bytes(font_data)?, 24)
42
            .map_err(|e| e.to_string())?;
43
44
        let ui: Ui = Ui::new();
45
46
        Ok(Self { canvas, font, ui })
47
    }
48
49
    pub fn get_center(&mut self) -> (i32, i32) {
50
        (
51
            ((Renderer::GRID_X_SIZE * Renderer::DOT_SIZE_IN_PXS) / 2) as i32,
52
            ((Renderer::GRID_Y_SIZE * Renderer::DOT_SIZE_IN_PXS) / 2) as i32,
53
        )
54
    }
55
56
    pub fn draw(&mut self, game_context: &GameContext) -> Result<(), String> {
57
        self.draw_background(game_context)?;
58
        self.draw_player(game_context)?;
59
        self.draw_food(game_context)?;
60
61
        for button in self.ui.buttons.iter_mut() {
62
            button.draw(&mut self.canvas, &self.font)?;
63
        }
64
65
        for text in self.ui.texts.iter_mut() {
66
            text.draw(&mut self.canvas, &self.font)?;
67
        }
68
69
        self.canvas.present();
70
        Ok(())
71
    }
72
73
    pub fn draw_background(&mut self, game_context: &GameContext) -> Result<(), String> {
74
        let bg_color = match game_context.state {
75
            GameState::Playing => Color::RGB(0, 0, 0),
76
            GameState::Paused => Color::RGB(30, 30, 30),
77
            GameState::GameOver => Color::RGB(30, 30, 30),
78
        };
79
80
        self.canvas.set_draw_color(bg_color);
81
        self.canvas.clear();
82
        Ok(())
83
    }
84
85
    pub fn draw_dot(&mut self, point: &Point) -> Result<(), String> {
86
        let Point(x, y) = point;
87
        self.canvas.fill_rect(Rect::new(
88
            x * Self::DOT_SIZE_IN_PXS as i32,
89
            y * Self::DOT_SIZE_IN_PXS as i32,
90
            Self::DOT_SIZE_IN_PXS,
91
            Self::DOT_SIZE_IN_PXS,
92
        ))
93
    }
94
95
    pub fn draw_player(&mut self, game_context: &GameContext) -> Result<(), String> {
96
        self.canvas.set_draw_color(Color::GREEN);
97
        for point in &game_context.player_position {
98
            self.draw_dot(point)?;
99
        }
100
        Ok(())
101
    }
102
103
    pub fn draw_food(&mut self, game_context: &GameContext) -> Result<(), String> {
104
        self.canvas.set_draw_color(Color::RED);
105
        for point in &game_context.food {
106
            self.draw_dot(point)?;
107
        }
108
        Ok(())
109
    }
110
}