summaryrefslogtreecommitdiff
path: root/graphics/src/render.rs
blob: aaa9983773f855753e8f6f5c4ab58ffe434b3862 (plain)
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
use std::{io::Write, ops::Div};

use dungeon::{
	Direction, Dungeon, Entity, EntityKind, Floor, Item, MAP_SIZE, PLAYER_INVENTORY_SIZE,
	Player, Pos, Tile,
};
use raylib::{
	RaylibHandle, RaylibThread,
	camera::Camera2D,
	color::Color,
	math::{Rectangle, Vector2},
	prelude::{RaylibDraw, RaylibMode2DExt, RaylibTextureModeExt},
	texture::{RaylibTexture2D, RenderTexture2D, Texture2D},
};

macro_rules! downcast {
	($usize:expr, $type:ty) => {
		<$type>::try_from($usize).unwrap_or(<$type>::MAX)
	};
}

macro_rules! rect {
    {$x:expr, $y:expr, $w:expr, $h:expr $(,)?} => {
        ::raylib::math::Rectangle {
            x: ($x) as f32,
            y: ($y) as f32,
            width: ($w) as f32,
            height: ($h) as f32,
        }
    };
}

macro_rules! vec2 {
    {$x:expr, $y:expr $(,)?} => {
       ::raylib::math::Vector2 {
           x: ($x) as f32,
           y: ($y) as f32,
       }
    };
}

macro_rules! draw_text {
    ($self:ident, $r:expr, $x:expr, $y:expr, $buffer:expr, $offset:expr, $($arg:tt)*) => {{
        let mut buffer = *$buffer;
        let _ = write!(&mut buffer[$offset..], $($arg)*);
        $self.draw_text($r, &buffer, $x, $y);
    }};
}

/// The baseline size of all ingame sprites and tile textures
const BASE_TEXTURE_SIZE: u16 = 16;

/// The height of the wall (offset between tile layers)
const WALL_HEIGHT: u16 = 7;

/// The (prefered) view distance of the game
const VIEW_DISTANCE: u16 = 5;

/// The minimum width/height we will render to
const MIN_RENDER_SIZE: u16 = BASE_TEXTURE_SIZE * (VIEW_DISTANCE + 2 + 2);

// Tile atlas.bmp textures
const ATLAS_WALL_SIDE: (u16, u16) = (0, 0);
const ATLAS_FLOOR_FULL: (u16, u16) = (1, 0);
const ATLAS_FLOOR_EMPTY: (u16, u16) = (2, 0);
const ATLAS_WALL_TOP: (u16, u16) = (3, 0);
const ATLAS_WALL_EDGE: (u16, u16) = (0, 1);

// UI atlas.bmp textures
const ATLAS_INV_CONTAINER: (u16, u16) = (1, 1);
const ATLAS_HEART_ICON: (u16, u16) = (2, 1);
const ATLAS_DAMAGE_ICON: (u16, u16) = (3, 1);

// Misc atlas.bmp textures
const ATLAS_ERROR: (u16, u16) = (3, 3);

/// Full source rec for any texture
const FULL_SOURCE_REC: Rectangle = rect! {
	0.0,
	0.0,
	BASE_TEXTURE_SIZE,
	BASE_TEXTURE_SIZE,
};

#[derive(Debug)]
struct Textures {
	// Tilemap
	atlas: Texture2D,
	// Entity
	player: Texture2D,
	// Misc
	error: Texture2D,
	// Fonts
	font: Texture2D,
}
impl Textures {
	fn new(handle: &mut RaylibHandle, thread: &RaylibThread) -> crate::Result<Self> {
		let atlas = handle.load_texture(thread, "assets/atlas.bmp")?;
		let player = handle.load_texture(thread, "assets/player.bmp")?;
		let error = handle.load_texture(thread, "assets/error.bmp")?;
		let font = handle.load_texture(thread, "assets/font.bmp")?;

		Ok(Self {
			atlas,
			player,
			error,
			font,
		})
	}

	fn item_texture(&self, _item: &Item) -> &Texture2D {
		// TODO: make item textures
		&self.error
	}
}

#[derive(Debug)]
pub struct Renderer {
	/* Persistant Render Data */
	/// All loaded image resources/textures
	textures: Textures,
	/// Top layer of the tilemap (walls)
	tilemap_fg: RenderTexture2D,
	/// Bottom layer of the tilemap (floor and half-height walls)
	tilemap_bg: RenderTexture2D,
	/// Tilemap texture used for the minimap
	tilemap_mm: RenderTexture2D,
	/// Last computed hash of the tilemap (floor)
	tiles_hash: Option<u64>,
	/* Per Frame Caculated Data */
	/// Current tile size we are rendering
	tile_size: u16,
	/// Last known FPS
	fps: u32,
	/// Render width of the current frame
	width: u16,
	/// Render height of the current frame
	height: u16,
	/// The current frame
	frame: u64,
	/// Show debug UI
	debug: bool,
}
impl Renderer {
	pub fn new(handle: &mut RaylibHandle, thread: &RaylibThread) -> crate::Result<Self> {
		// Load resources
		let textures = Textures::new(handle, thread)?;
		// Load render textures
		let tex_size = (MAP_SIZE * BASE_TEXTURE_SIZE) as u32;
		let tilemap_fg = handle.load_render_texture(thread, tex_size, tex_size)?;
		let tilemap_bg = handle.load_render_texture(thread, tex_size, tex_size)?;
		let tilemap_mm =
			handle.load_render_texture(thread, MAP_SIZE as u32, MAP_SIZE as u32)?;

		Ok(Self {
			textures,
			tilemap_fg,
			tilemap_bg,
			tilemap_mm,
			tiles_hash: None,
			tile_size: 0,
			fps: 0,
			width: 0,
			height: 0,
			frame: 0,
			debug: false,
		})
	}

	pub fn toggle_debug(&mut self) {
		self.debug = !self.debug;
	}

	pub fn draw_frame(
		&mut self,
		handle: &mut RaylibHandle,
		t: &RaylibThread,
		dungeon: &Dungeon,
	) {
		self.update_per_frame_data(handle);
		let mut r = handle.begin_drawing(t);
		r.clear_background(Color::BLACK);
		self.draw_dungeon(&mut r, t, dungeon);
		self.draw_ui(&mut r, dungeon);
	}

	/// Update frame metadata while we still have access to the
	/// `RaylibHandle`. These fields are inaccessable once it's
	/// turned into a `RaylibDrawHandle`.
	fn update_per_frame_data(&mut self, handle: &RaylibHandle) {
		// Get last known fps
		self.fps = handle.get_fps();
		// Get size of framebuffer
		self.width = downcast!(handle.get_render_width(), u16).max(MIN_RENDER_SIZE);
		self.height = downcast!(handle.get_render_height(), u16).max(MIN_RENDER_SIZE);
		// Get size (in pixels) to draw each tile
		self.tile_size = {
			let size = self.width.min(self.height);
			let dist = VIEW_DISTANCE * 2 + 1;
			let pixels = size.div(dist).max(BASE_TEXTURE_SIZE);
			1 << (u16::BITS - pixels.leading_zeros())
		};
		// Update frame counter
		self.frame += 1;
	}

	/// Returns the Raylib Camera setup with needed 2D position/transforms
	fn render_camera(&self, dungeon: &Dungeon) -> Camera2D {
		let cpos = dungeon.camera();
		let width = self.width;
		let height = self.height;
		let ui_height = self.get_ui_height();
		Camera2D {
			target: Vector2::from(cpos.xy())
				.scale_by(self.tile_size.into())
				.max(vec2! {width/2, height/2})
				.min(vec2! {
					(MAP_SIZE * self.tile_size) - (width/2),
					(MAP_SIZE * self.tile_size) - (height/2),
				}),
			offset: vec2! {width/2, height/2 + ui_height/2},
			rotation: 0.0,
			zoom: 1.0,
		}
	}

	/// Draws the game dungeon
	fn draw_dungeon<R>(&mut self, r: &mut R, t: &RaylibThread, dungeon: &Dungeon)
	where
		R: RaylibDraw + RaylibMode2DExt + RaylibTextureModeExt,
	{
		self.update_tilemaps(r, t, &dungeon.floor);
		let camera = self.render_camera(dungeon);
		let mut rc = r.begin_mode2D(camera);
		self.draw_bg_tilemap(&mut rc);
		self.draw_entities(&mut rc, dungeon);
		self.draw_fg_tilemap(&mut rc);
	}

	/// Updates all tilemaps
	fn update_tilemaps<R>(&mut self, r: &mut R, t: &RaylibThread, floor: &Floor)
	where
		R: RaylibDraw + RaylibTextureModeExt,
	{
		let current_hash = floor.hash();
		if let Some(old_hash) = self.tiles_hash
			&& old_hash == current_hash
		{
			// Textures are up to date
			return;
		};
		self.tiles_hash = Some(current_hash);

		self.update_fg_tilemap(r, t, floor);
		self.update_bg_tilemap(r, t, floor);
		self.update_mm_tilemap(r, t, floor);
	}

	/// Draws the foregound tile map
	fn update_fg_tilemap<R>(&mut self, r: &mut R, t: &RaylibThread, floor: &Floor)
	where
		R: RaylibDraw + RaylibTextureModeExt,
	{
		let size = BASE_TEXTURE_SIZE;
		let mut rt = r.begin_texture_mode(t, &mut self.tilemap_fg);
		rt.clear_background(Color::BLANK);

		for pos in Pos::values() {
			let (xs, ys) = (pos.x() * size, pos.y() * size);

			// fg layer only draws a top walls
			if floor.get(pos) != Tile::Wall {
				continue;
			};

			// draw base wall top texture
			rt.draw_atlas(&self.textures.atlas, ATLAS_WALL_TOP, xs, ys, size, 0.0);

			// draw top wall borders
			let is_wall =
				|dir| pos.step(dir).map_or(Tile::Wall, |p| floor.get(p)).is_wall();
			if !is_wall(Direction::North) {
				rt.draw_atlas(&self.textures.atlas, ATLAS_WALL_EDGE, xs, ys, size, 0.0);
			}
			if !is_wall(Direction::East) {
				rt.draw_atlas(&self.textures.atlas, ATLAS_WALL_EDGE, xs, ys, size, 90.0);
			}
			if !is_wall(Direction::South) {
				rt.draw_atlas(&self.textures.atlas, ATLAS_WALL_EDGE, xs, ys, size, 180.0);
			}
			if !is_wall(Direction::West) {
				rt.draw_atlas(&self.textures.atlas, ATLAS_WALL_EDGE, xs, ys, size, 270.0);
			}
		}
	}

	/// Draws the foregound tile map
	fn update_bg_tilemap<R>(&mut self, r: &mut R, t: &RaylibThread, floor: &Floor)
	where
		R: RaylibDraw + RaylibTextureModeExt,
	{
		let size = BASE_TEXTURE_SIZE;
		let mut rt = r.begin_texture_mode(t, &mut self.tilemap_bg);
		rt.clear_background(Color::BLACK);

		for pos in Pos::values() {
			let (x, y) = pos.xy();
			let tile = floor.get(pos);
			let idx = match tile {
				Tile::Wall => ATLAS_WALL_SIDE,
				Tile::Air if (x + y) % 2 == 0 => ATLAS_FLOOR_FULL,
				Tile::Air if (x + y) % 2 == 1 => ATLAS_FLOOR_EMPTY,
				_ => ATLAS_ERROR,
			};
			rt.draw_atlas(&self.textures.atlas, idx, x * size, y * size, size, 0.0);
		}
	}

	/// Draws the foregound tile map
	fn update_mm_tilemap<R>(&mut self, r: &mut R, t: &RaylibThread, floor: &Floor)
	where
		R: RaylibDraw + RaylibTextureModeExt,
	{
		let mut rt = r.begin_texture_mode(t, &mut self.tilemap_mm);
		rt.clear_background(Color::DARKGRAY);

		for pos in Pos::values() {
			let (x, y) = pos.xy();
			let tile = floor.get(pos);
			let color = match tile {
				Tile::Wall => Color::DARKGRAY,
				Tile::Air => Color::GRAY,
				Tile::Stairs => Color::WHITE,
			};
			rt.draw_pixel(x.into(), y.into(), color);
		}
	}

	/// Draw dungeon tiles (foreground layer)
	fn draw_fg_tilemap<R>(&mut self, r: &mut R)
	where
		R: RaylibDraw,
	{
		r.draw_tilemap(
			&self.tilemap_fg,
			0,
			0,
			self.tile_size / BASE_TEXTURE_SIZE,
			WALL_HEIGHT,
		);
	}

	/// Draw dungeon tiles (background layer)
	fn draw_bg_tilemap<R>(&mut self, r: &mut R)
	where
		R: RaylibDraw,
	{
		r.draw_tilemap(
			&self.tilemap_bg,
			0,
			0,
			self.tile_size / BASE_TEXTURE_SIZE,
			0,
		);
	}

	/// Draws the entities on the map
	fn draw_entities<R>(&mut self, r: &mut R, dungeon: &Dungeon)
	where
		R: RaylibDraw,
	{
		self.draw_entity(r, &dungeon.player.entity);
		for enemy in &dungeon.enemies {
			self.draw_entity(r, enemy);
		}
	}

	/// Draws an entity
	fn draw_entity<R>(&self, r: &mut R, entity: &Entity)
	where
		R: RaylibDraw,
	{
		let size = self.tile_size as f32;
		let (fx, fy) = entity.fpos.xy();
		let texture = match entity.kind {
			EntityKind::Player => &self.textures.player,
			_ => &self.textures.error,
		};
		let (x, y) = match entity.dir {
			Direction::North => (0, 0),
			Direction::South => (0, 1),
			Direction::East => (1, 0),
			Direction::West => (1, 1),
		};
		let source_rec = rect! {
			x * BASE_TEXTURE_SIZE,
			y * BASE_TEXTURE_SIZE,
			BASE_TEXTURE_SIZE,
			BASE_TEXTURE_SIZE,
		};
		let dest_rec = rect! {
			fx * size - size/2.0,
			fy * size - size/2.0,
			size,
			size,
		};
		r.draw_texture_pro(
			texture,
			source_rec,
			dest_rec,
			Vector2::zero(),
			0.0,
			Color::WHITE,
		);

		if self.debug {
			r.draw_rectangle_lines_ex(dest_rec, 1.0, Color::YELLOW);
		}
	}

	/// Returns the known UI height for this frame
	fn get_ui_height(&self) -> u16 {
		self.tile_size
	}

	/// Returns the padding used between ui elements
	fn get_ui_padding(&self) -> u16 {
		self.get_ui_height() / 10
	}

	/// Returns the font size for the UI
	fn get_ui_font_size(&self) -> u16 {
		self.get_ui_height() / 4
	}

	/// Draws player HP, inventory, and floor number
	fn draw_ui<R>(&self, r: &mut R, dungeon: &Dungeon)
	where
		R: RaylibDraw,
	{
		// Draw UI base rect
		r.draw_rectangle_ext(0, 0, self.width, self.get_ui_height(), Color::BLACK);

		if self.debug {
			self.draw_debug_ui(r, dungeon);
			return;
		}

		// Draw core ui components
		self.draw_minimap(r, dungeon);
		self.draw_inventory(r, &dungeon.player);
		self.draw_stats(r, &dungeon.player);
	}

	/// Draw in game minimap
	fn draw_minimap<R>(&self, r: &mut R, dungeon: &Dungeon)
	where
		R: RaylibDraw,
	{
		let padding = self.get_ui_padding();
		let y = padding;

		// Draw MAP vert text
		let text_x = padding;
		self.draw_text_vertical(r, b"MAP", text_x, y);

		// Draw minimap in top left of UI bar
		let minimap_x = text_x + self.get_ui_font_size() + padding;
		let minimap_y = padding;
		let size = self.get_ui_height() - padding * 2;
		r.draw_tilemap(&self.tilemap_mm, minimap_x, minimap_y, size / MAP_SIZE, 0);

		// Draw minimap entity's
		let dot_size = (size / MAP_SIZE).max(1);
		let mut draw_dot = |pos: Pos, color| {
			let (x, y) = pos.xy();
			let offset_x = x * (size / MAP_SIZE);
			let offset_y = y * (size / MAP_SIZE);
			r.draw_rectangle_ext(
				minimap_x + offset_x,
				minimap_y + offset_y,
				dot_size,
				dot_size,
				color,
			);
		};

		// Draw enemy dots
		for enemy in &dungeon.enemies {
			draw_dot(enemy.pos, Color::RED);
		}

		// Draw player dot
		draw_dot(dungeon.player.entity.pos, Color::LIME);
	}

	/// Draws the player's inventory
	/// NOTE: Nothing is drawn if the inventory is empty
	fn draw_inventory<R>(&self, r: &mut R, player: &Player)
	where
		R: RaylibDraw,
	{
		let slots = downcast!(PLAYER_INVENTORY_SIZE, u16);
		let padding = self.get_ui_padding();
		let font_size = self.get_ui_font_size();
		let ui_height = self.get_ui_height();

		let text_len = font_size + padding;
		let slot_len = ui_height - padding * 2;
		let slots_len = slot_len * slots;
		let full_len = text_len + slots_len;

		let start_x = self.width / 2 - full_len / 2;
		let text_x = start_x;
		let slots_x = text_x + text_len;

		// Draw text
		self.draw_text_vertical(r, b"INV", text_x, padding);

		// Draw slots
		for idx in 0..PLAYER_INVENTORY_SIZE {
			if idx >= PLAYER_INVENTORY_SIZE {
				// This should never happen!
				// Maybe use a different type??
				break;
			}

			let slot_x = slots_x + slot_len * downcast!(idx, u16);
			let size = ui_height - padding * 2;

			// Draw slot container
			r.draw_inv_atlas(
				&self.textures.atlas,
				ATLAS_INV_CONTAINER,
				slot_x,
				padding,
				size,
				0.0,
			);

			if let Some(item) = player.inventory.get(idx) {
				let tex = self.textures.item_texture(item);
				let item_padding = padding * 3;
				let dest_rec = rect! {
					slot_x + item_padding / 2,
					padding+ item_padding / 2,
					size - item_padding,
					size - item_padding,
				};
				r.draw_texture_pro(
					tex,
					FULL_SOURCE_REC,
					dest_rec,
					Vector2::zero(),
					0.0,
					Color::WHITE,
				);
			}
		}
	}

	/// Draw player health & equpped weapon damage
	fn draw_stats<R>(&self, r: &mut R, player: &Player)
	where
		R: RaylibDraw,
	{
		let health = player.entity.health.unwrap_or(0);
		let damage = 0; // TODO: calc damage

		let padding = self.get_ui_padding();
		let font_size = self.get_ui_font_size();

		let text_width = font_size * 3 + padding;
		let icon_width = font_size + padding;
		let stats_x = self.width - text_width - icon_width;
		let icon_x = stats_x;
		let text_x = icon_x + icon_width;

		// draw health
		let heart_y = padding;
		r.draw_inv_atlas(
			&self.textures.atlas,
			ATLAS_HEART_ICON,
			icon_x,
			heart_y,
			icon_width,
			0.0,
		);
		draw_text!(self, r, text_x, heart_y + padding, b"x00", 1, "{health:02}");

		// draw damage
		let damage_y = heart_y + font_size + padding;
		r.draw_inv_atlas(
			&self.textures.atlas,
			ATLAS_DAMAGE_ICON,
			icon_x,
			damage_y,
			icon_width,
			0.0,
		);
		draw_text!(
			self,
			r,
			text_x,
			damage_y + padding,
			b"x00",
			1,
			"{damage:02}"
		);
	}

	/// Draws debug information ontop of other UI elements
	fn draw_debug_ui<R>(&self, r: &mut R, dungeon: &Dungeon)
	where
		R: RaylibDraw,
	{
		let padding = self.get_ui_padding();
		let font_size = self.get_ui_font_size();
		let player = &dungeon.player;

		// X Positions
		let col1 = padding;
		let col2 = padding + font_size * 10;

		// Y Positions
		let row1 = padding;
		let row2 = row1 + font_size;
		let row3 = row2 + font_size;

		// Draw FPS
		draw_text!(self, r, col1, row1, b"FPS      ", 4, "{}", self.fps);

		// Draw Player position
		let (x, y) = &player.entity.pos.xy();
		draw_text!(self, r, col1, row2, b"POS 00,00", 4, "{x:02},{y:02}");

		// Draw Player direction
		let dir = &player.entity.dir;
		draw_text!(self, r, col1, row3, b"DIR      ", 4, "{dir}");

		// Draw Player Seed
		let seed = &dungeon.floor.seed();
		draw_text!(
			self,
			r,
			col2,
			row1,
			b" SEED 0x0000000000000000",
			8,
			"{seed:X}"
		);

		// Draw Dungeon Hash
		let hash = dungeon.floor.hash();
		draw_text!(
			self,
			r,
			col2,
			row2,
			b" HASH 0x0000000000000000",
			8,
			"{hash:X}"
		);

		// Draw current frame number
		let frame = &self.frame;
		draw_text!(
			self,
			r,
			col2,
			row3,
			b"FRAME                   ",
			6,
			"{frame}"
		);
	}

	/// Draws vertical text
	fn draw_text_vertical<R>(&self, r: &mut R, text: &[u8], x: u16, y_start: u16)
	where
		R: RaylibDraw,
	{
		let spacing = self.get_ui_font_size() + self.get_ui_padding() / 2;
		for (idx, char) in text.iter().enumerate() {
			let y = y_start + downcast!(idx, u16) * spacing;
			self.draw_char(r, *char, x, y);
		}
	}

	/// Draw text helper function
	fn draw_text<R>(&self, r: &mut R, text: &[u8], x_start: u16, y: u16)
	where
		R: RaylibDraw,
	{
		for (idx, char) in text.iter().enumerate() {
			let x = x_start + downcast!(idx, u16) * self.get_ui_font_size();
			self.draw_char(r, *char, x, y);
		}
	}

	/// Draws a char to the screen
	fn draw_char<R>(&self, r: &mut R, char: u8, x: u16, y: u16)
	where
		R: RaylibDraw,
	{
		if !(32..=127).contains(&char) {
			return;
		}
		let ax = char % 16;
		let ay = (char - 32) / 16;
		r.draw_inv_atlas(
			&self.textures.font,
			(ax.into(), ay.into()),
			x,
			y,
			self.get_ui_font_size(),
			0.0,
		);
	}
}

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))
	}
}

trait RaylibDrawExt
where
	Self: RaylibDraw,
{
	/// Draw an atlas texture index
	fn draw_atlas(
		&mut self,
		tex: &Texture2D,
		(ax, ay): (u16, u16),
		x: impl Into<f32>,
		y: impl Into<f32>,
		size: impl Into<f32>,
		rotation: impl Into<f32>,
	) {
		let size_into = size.into();
		let source_rec = rect! {
			ax * BASE_TEXTURE_SIZE,
			ay * BASE_TEXTURE_SIZE,
			BASE_TEXTURE_SIZE,
			BASE_TEXTURE_SIZE,
		};
		let dest_rec = rect! {
			x.into(),
			y.into(),
			size_into,
			size_into,
		};
		let origin = vec2! {
			dest_rec.width / 2.0,
			dest_rec.height / 2.0,
		};
		self.draw_texture_pro(
			tex,
			source_rec,
			dest_rec,
			origin,
			rotation.into(),
			Color::WHITE,
		);
	}

	/// Draw in INV element from an atlas
	fn draw_inv_atlas(
		&mut self,
		tex: &Texture2D,
		(ax, ay): (u16, u16),
		x: impl Into<f32>,
		y: impl Into<f32>,
		size: impl Into<f32>,
		rotation: impl Into<f32>,
	) {
		let size_into = size.into();
		self.draw_atlas(
			tex,
			(ax, ay),
			x.into() + size_into / 2.0,
			y.into() + size_into / 2.0,
			size_into,
			rotation,
		);
	}

	/// Draw dungeon tiles helper function
	fn draw_tilemap(
		&mut self,
		tex: &RenderTexture2D,
		x: u16,
		y: u16,
		scale: u16,
		offset: u16,
	) {
		let width = downcast!(tex.width(), u16);
		let height = downcast!(tex.height(), u16);
		let source_rec = rect! {
			0,
			0,
			width,
			-height.cast_signed(),
		};
		let dest_rec = rect! {
			x,
			y.cast_signed() - (offset * scale).cast_signed(),
			width * scale,
			height * scale,
		};
		let origin = Vector2::zero();
		self.draw_texture_pro(tex, source_rec, dest_rec, origin, 0.0, Color::WHITE);
	}

	fn draw_rectangle_ext(
		&mut self,
		x: impl Into<i32>,
		y: impl Into<i32>,
		width: impl Into<i32>,
		height: impl Into<i32>,
		color: Color,
	) {
		self.draw_rectangle(x.into(), y.into(), width.into(), height.into(), color);
	}
}
impl<T> RaylibDrawExt for T where T: RaylibDraw {}