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

use std::cell::RefCell;
use std::error::Error;

use raylib::prelude::*;

use crate::assets::{Assets, AudioData};
use crate::render::{FrameInfo, Renderer};

mod assets;
mod render;

/// The `KeyCode` type represents different keys being pressed on the users keyboard
pub use raylib::consts::KeyboardKey as KeyCode;

/// The `Window` type represents the game window
#[derive(Debug)]
pub struct Window {
	// core raylib handles
	handle: RefCell<RaylibHandle>,
	thread: RaylibThread,
	// static assets
	assets: Assets,
}

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").unwrap();
	/// ```
	pub fn new(width: i32, height: i32, title: &str) -> Result<Self, Box<dyn Error>> {
		let (mut handle, thread) = raylib::init()
			.size(width, height)
			.title(title)
			.resizable()
			.log_level(TraceLogLevel::LOG_WARNING)
			.vsync()
			.build();

		// we will now initalize all assets
		let assets = Assets::load(&mut handle, &thread)?;

		Ok(Self {
			handle: RefCell::new(handle),
			thread,
			assets,
		})
	}

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

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

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

	/// Returns if the provided `KeyCode` has been pressed once
	pub fn is_key_pressed(&self, key: KeyCode) -> bool {
		self.handle.borrow().is_key_pressed(key)
	}

	/// Returns if the provided `KeyCode` is NOT currently pressed
	pub fn is_key_up(&self, key: KeyCode) -> bool {
		self.handle.borrow().is_key_up(key)
	}

	/// Returns if the provided `KeyCode` is currently pressed
	pub fn is_key_down(&self, key: KeyCode) -> bool {
		self.handle.borrow().is_key_down(key)
	}

	/// Get the last key pressed
	pub fn get_key_pressed(&self) -> Option<KeyCode> {
		self.handle.borrow_mut().get_key_pressed()
	}

	/// Get audio data for the window
	pub fn audio(&self) -> &AudioData {
		&self.assets.audio
	}
}