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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
//! The `render` module contains the structures for displaying
//! the game, with each frame represented by a `Renderer` and
//! frame specific information in `FrameInfo`.
/// The (prefered) view distance of the game
const VIEW_DISTANCE: i32 = 10;
/// The baseline size of all ingame sprites and tile textures
const BASE_TILE_SIZE: i32 = 16;
use std::ops::Div;
use dungeon::{Dungeon, Entity, FPos, Floor, MAP_SIZE, Pos, Tile};
use raylib::{
RaylibThread,
camera::Camera2D,
color::Color,
math::Vector2,
prelude::{
RaylibDraw, RaylibDrawHandle, RaylibHandle, RaylibMode2D, RaylibMode2DExt,
RaylibTextureModeExt,
},
texture::{RaylibTexture2D, RenderTexture2D},
};
use crate::assets::ImageData;
/// The `FrameInfo` struct stores persistant information used thought a frame not
/// accessable from `RaylibDraw`
#[derive(Clone, Copy, Debug)]
struct FrameInfo {
/// The last calculated fps
fps: u32,
/// The render width of the framebuffer in pixels
width: i32,
/// The render height of the framebuffer in pixels
height: i32,
/// The tile size in pixels
tile_size: i32,
}
impl FrameInfo {
fn new(handle: &RaylibHandle) -> Self {
let fps = handle.get_fps();
let width = handle.get_render_width();
let height = handle.get_render_height();
let tile_size = {
let size = width.min(height);
let dist = VIEW_DISTANCE * 2 + 1;
// TODO: force by 16 scaling levels
size.div(dist).max(BASE_TILE_SIZE)
};
Self {
fps,
width,
height,
tile_size,
}
}
}
/// The `Renderer` struct is the persistant renderer
/// for the duration for the application.
#[derive(Debug)]
pub struct Renderer {
/// Set of sprites to be drawn
#[expect(dead_code)]
image: ImageData,
/// Pre-rendered map texture that updates if the map changes
/// Stores the hash of the map tiles to check this
tiles_tex: Option<RenderTexture2D>,
/// Hash of tiles used to draw on `tiles_tex`
tiles_hash: u64,
}
impl Renderer {
pub(crate) fn new(image: ImageData) -> Self {
Self {
image,
tiles_tex: None,
tiles_hash: 0,
}
}
/// Invokes the renderer for the current frame
pub(crate) fn invoke<'a>(
&'a mut self,
handle: &'a mut RaylibHandle,
thread: &'a RaylibThread,
) -> crate::Result<FrameRendererImpl<'a>> {
let info = FrameInfo::new(handle);
// allocate tile texture
let size = info.tile_size;
let pixels = (MAP_SIZE as i32) * size;
match &self.tiles_tex {
Some(tex) if tex.width() == pixels => (),
_ => {
// texture is not yet allocated, or screen sized changed enough to change the tile
// size
let tex =
handle.load_render_texture(thread, pixels as u32, pixels as u32)?;
self.tiles_tex = Some(tex);
self.tiles_hash = 0;
}
};
Ok(FrameRenderer {
handle: handle.begin_drawing(thread),
thread,
info,
renderer: self,
})
}
}
pub type FrameRendererImpl<'a> = FrameRenderer<'a, RaylibDrawHandle<'a>>;
pub struct FrameRenderer<'a, T>
where
T: RaylibDraw,
{
/// The current draw handle for raylib
handle: T,
/// The raylib thread
thread: &'a RaylibThread,
/// Non drawing information for this current frame
info: FrameInfo,
/// Mutable reference to the main renderer (stores persistant data)
renderer: &'a mut Renderer,
}
impl<'a, T> FrameRenderer<'a, T>
where
T: RaylibDraw + RaylibMode2DExt + RaylibTextureModeExt,
{
/// Draws an entire frame
pub fn draw_frame(&mut self, dungeon: &Dungeon) {
self.clear();
self.draw_dungeon(dungeon);
self.draw_ui(dungeon);
}
/// Draws the dungeon, (tiles and entities)
pub fn draw_dungeon(&mut self, dungeon: &Dungeon) {
let camera = dungeon.camera();
self.update_tilemap(&dungeon.floor);
let mut renderer = self.camera_renderer(camera);
renderer.draw_tiles();
renderer.draw_entities(dungeon);
}
}
impl<'a, T> FrameRenderer<'a, T>
where
T: RaylibDraw + RaylibMode2DExt,
{
/// Returns a raylib camera for the given position
#[must_use]
fn camera_renderer<'b>(
&'b mut self,
cpos: FPos,
) -> FrameRenderer<'b, RaylibMode2D<'b, T>> {
let width = self.info.width;
let height = self.info.height;
let camera = Camera2D {
target: Vector2::from(cpos.xy())
.scale_by(self.info.tile_size as f32)
.max(Vector2::new(width as f32 / 2.0, height as f32 / 2.0))
.min(Vector2::new(
(MAP_SIZE as i32 * self.info.tile_size) as f32 - (width as f32 / 2.0),
(MAP_SIZE as i32 * self.info.tile_size) as f32 - (height as f32 / 2.0),
)),
offset: Vector2::new(width as f32 / 2.0, height as f32 / 2.0),
rotation: 0.0,
zoom: 1.0,
};
let handle = self.handle.begin_mode2D(camera);
FrameRenderer {
handle,
thread: self.thread,
info: self.info,
renderer: self.renderer,
}
}
}
impl<'a, T> FrameRenderer<'a, T>
where
T: RaylibDraw + RaylibTextureModeExt,
{
/// Draw tiles on a provided texture
fn update_tilemap(&mut self, floor: &Floor) {
let hash = floor.hash();
if self.renderer.tiles_hash == hash {
// Texture is up to date
return;
}
let Some(tex) = self.renderer.tiles_tex.as_mut() else {
// BUG: error here?
return;
};
let mut handle = self.handle.begin_texture_mode(self.thread, tex);
let size = self.info.tile_size;
handle.clear_background(Color::BLACK);
for pos in Pos::values() {
let (x, y) = pos.xy();
let color = tile_color(floor.get(pos));
handle.draw_rectangle(x as i32 * size, y as i32 * size, size, size, color);
}
self.renderer.tiles_hash = hash;
}
}
impl<'a, T> FrameRenderer<'a, T>
where
T: RaylibDraw,
{
/// Clear the screen
pub fn clear(&mut self) {
self.handle.clear_background(Color::BLACK);
}
/// Draws player HP, inventory, and floor number
pub fn draw_ui(&mut self, _dungeon: &Dungeon) {
#[cfg(feature = "debug")]
// Draw fps (debug only)
self.draw_fps();
}
/// Draws the entities on the map
fn draw_entities(&mut self, dungeon: &Dungeon) {
self.draw_entity(&dungeon.player.entity);
}
/// Draws an entity
#[expect(clippy::cast_possible_truncation)]
fn draw_entity(&mut self, entity: &Entity) {
let size = self.info.tile_size;
let x = (entity.fpos.x() * size as f32) as i32;
let y = (entity.fpos.y() * size as f32) as i32;
// TODO: per entity color
self.handle.draw_rectangle(x, y, size, size, Color::GREEN);
}
/// Draw dungeon tiles
fn draw_tiles(&mut self) {
let Some(tex) = &self.renderer.tiles_tex else {
// BUG: error here?
return;
};
self.handle.draw_texture(tex, 0, 0, Color::WHITE);
}
/// Draw FPS counter
fn draw_fps(&mut self) {
let fps_str = format!("{}", self.info.fps);
self.handle.draw_text(&fps_str, 10, 10, 30, Color::YELLOW);
}
}
fn tile_color(tile: Tile) -> Color {
// TODO: use textures instead of colors :)
match tile {
Tile::Wall => Color::BLUE,
Tile::Air => Color::RED,
Tile::Stairs => Color::GRAY,
}
}
trait Vector2Ext {
fn min(self, other: Self) -> Self;
fn max(self, other: Self) -> Self;
}
impl Vector2Ext for Vector2 {
fn min(self, other: Self) -> Self {
Self::new(self.x.min(other.x), self.y.min(other.y))
}
fn max(self, other: Self) -> Self {
Self::new(self.x.max(other.x), self.y.max(other.y))
}
}
|