1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
use dungeon::{Dungeon, Entity};
use raylib::{
color::Color,
prelude::{RaylibDraw, RaylibDrawHandle, RaylibHandle},
};
/// Information used each frame
pub struct FrameInfo {
/// Time in seconds since last frame drawn
pub delta: f32,
/// FPS for last frame drawn
pub fps: u32,
}
impl FrameInfo {
pub fn new(handle: &RaylibHandle) -> Self {
Self {
delta: handle.get_frame_time(),
fps: handle.get_fps(),
}
}
}
/// A `Renderer` is a renderer for a single
/// frame of the game. It is created per frame.
pub struct Renderer<'a> {
handle: RaylibDrawHandle<'a>,
info: FrameInfo,
}
impl<'a> Renderer<'a> {
/// Creates the renderer for the current frame
pub(crate) fn new(handle: RaylibDrawHandle<'a>, info: FrameInfo) -> Self {
Self { handle, info }
}
/// Returns the info struct for the current frame
pub fn info(&self) -> &FrameInfo {
&self.info
}
/// Returns the per frame delta time
pub fn delta_time(&self) -> f32 {
self.info.delta
}
/// Returns the current render width
pub fn render_width(&mut self) -> i32 {
self.handle.get_render_width()
}
/// Returns the current render height
pub fn render_height(&mut self) -> i32 {
self.handle.get_render_height()
}
/// Clear the screen
pub fn clear(&mut self) {
self.handle.clear_background(Color::BLACK);
}
/// Draws an entire frame
pub fn draw_frame(&mut self, dungeon: &Dungeon) {
// Clear the background to black
self.clear();
// Draw the dungeon
self.draw_tiles(dungeon);
self.draw_player(dungeon);
#[cfg(feature = "debug")]
// Draw fps (debug only)
self.draw_fps(self.info.fps);
}
/// Draw game over screen
pub fn draw_game_over(&mut self) {
// TODO:
}
/// Draw the player sprite
pub fn draw_player(&mut self, dungeon: &Dungeon) {
self.draw_entity(dungeon.player());
}
/// Draws an entity
pub fn draw_entity(&mut self, _entity: &Entity) {
// TODO:
}
/// Draw dungeon tiles
pub fn draw_tiles(&mut self, _dungeon: &Dungeon) {
// TODO:
}
/// Draw FPS counter
pub fn draw_fps(&mut self, fps: u32) {
let fps_str = format!("{fps}");
self.handle.draw_text(&fps_str, 10, 10, 30, Color::YELLOW);
}
}
|