summaryrefslogtreecommitdiff
path: root/graphics/src/lib.rs
blob: 4f55388ced98a66dbb0b8f8a80b76e21062796e1 (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
//! The `graphics` crate contains the core functionality for
//! rendering using the `raylib` library.

use raylib::prelude::*;

use crate::render::{FrameInfo, Renderer};

pub mod render;

/// The `Window` type represents the game window
#[derive(Debug)]
pub struct Window {
	handle: RaylibHandle,
	thread: RaylibThread,
}

impl Window {
	/// Instantiates a new window provided with the default
	/// window `width`, `height`, and `title`.
	/// 
	/// # Examples
	/// ```no_run
	/// use graphics::Window;
	///
	/// let window = Window::new(800, 600, "Dungeon Crawl");
	/// ```
	pub fn new(width: i32, height: i32, title: &str) -> Self {
		let (handle, thread) = raylib::init()
			.size(width, height)
			.title(title)
			.resizable()
			.log_level(TraceLogLevel::LOG_WARNING)
			.vsync()
			.build();
		Self { handle, thread }
	}

	/// Returns if the window should be closed.
	/// This usually means the 'x' button has been pressed.
	pub fn is_open(&self) -> bool {
		!self.handle.window_should_close()
	}

	/// Returns the renderer for the game
	/// 
	/// # Examples
	/// ```no_run
	/// use graphics::Window;
	/// 
	/// let mut window = Window::new(800, 600, "Dungeon Crawl");
	/// let mut renderer = window.renderer();
	/// ```
	pub fn renderer(&mut self) -> Renderer<'_> {
		let info = FrameInfo::new(&self.handle);
		let handle = self.handle.begin_drawing(&self.thread);
		Renderer::new(handle, info)
	}

	/// Returns the per frame delta time
	pub fn delta_time(&self) -> f32 {
		self.handle.get_frame_time()
	}
}