summaryrefslogtreecommitdiff
path: root/graphics
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2025-11-10 13:31:04 -0500
committerFreya Murphy <freya@freyacat.org>2025-11-10 13:31:04 -0500
commitb50a79e6f9cd09973a406e5ccc50510aee3423f2 (patch)
tree67d7dd951cc5680f6e2526f48bef7b1bde5db1c3 /graphics
parentupdate raylib to 5.6-dev (diff)
downloadDungeonCrawl-b50a79e6f9cd09973a406e5ccc50510aee3423f2.tar.gz
DungeonCrawl-b50a79e6f9cd09973a406e5ccc50510aee3423f2.tar.bz2
DungeonCrawl-b50a79e6f9cd09973a406e5ccc50510aee3423f2.zip
graphics: have some window arguments passed in through cmd args, fix camera pos
Diffstat (limited to 'graphics')
-rw-r--r--graphics/src/lib.rs139
-rw-r--r--graphics/src/render.rs4
2 files changed, 92 insertions, 51 deletions
diff --git a/graphics/src/lib.rs b/graphics/src/lib.rs
index 27e49d3..811494c 100644
--- a/graphics/src/lib.rs
+++ b/graphics/src/lib.rs
@@ -1,6 +1,7 @@
//! The `graphics` crate contains the core functionality for
//! rendering using the `raylib` library.
+use std::borrow::Cow;
use std::cell::RefCell;
use dungeon::Dungeon;
@@ -21,37 +22,75 @@ pub type Error = Box<dyn std::error::Error>;
/// The `Result` type used witin this crate
pub type Result<T> = std::result::Result<T, crate::Error>;
-/// The `Window` type represents the game window
+/// The `WindowBuilder` type allows setting partial options for the window
#[derive(Debug)]
-pub struct Window {
- // persistant renderer
- renderer: Renderer,
- // core raylib handles
- handle: RefCell<RaylibHandle>,
- thread: RaylibThread,
- // audio data/subsystem
- audio: Audio,
+pub struct WindowBuilder<'a> {
+ title: Cow<'a, str>,
+ width: u16,
+ height: u16,
+ vsync: bool,
+ verbose: bool,
}
-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: u16, height: u16, title: &str) -> crate::Result<Self> {
- let (mut handle, thread) = raylib::init()
- .size(width.into(), height.into())
- .title(title)
- .resizable()
- .log_level(TraceLogLevel::LOG_WARNING)
- .build();
+impl<'a> WindowBuilder<'a> {
+ /// Default window builder
+ pub fn new() -> Self {
+ Self::default()
+ }
+
+ /// Set the window title
+ pub fn title(&mut self, title: impl Into<Cow<'a, str>>) -> &mut Self {
+ self.title = title.into();
+ self
+ }
+
+ /// Set the default window width
+ pub fn width(&mut self, width: u16) -> &mut Self {
+ self.width = width;
+ self
+ }
+
+ /// Set the default window height
+ pub fn height(&mut self, height: u16) -> &mut Self {
+ self.height = height;
+ self
+ }
+
+ /// Toggle vsync support
+ pub fn vsync(&mut self, vsync: bool) -> &mut Self {
+ self.vsync = vsync;
+ self
+ }
+
+ /// Toggle verbose logging
+ pub fn verbose(&mut self, verbose: bool) -> &mut Self {
+ self.verbose = verbose;
+ self
+ }
+
+ /// Build the window
+ pub fn build(&self) -> crate::Result<Window> {
+ let mut builder = raylib::init();
+
+ // Set raylib args from builder
+ builder.size(self.width.into(), self.height.into());
+ builder.title(&self.title);
+ builder.resizable();
+ if self.vsync {
+ builder.vsync();
+ }
+ if self.verbose {
+ builder.log_level(TraceLogLevel::LOG_INFO);
+ } else {
+ builder.log_level(TraceLogLevel::LOG_WARNING);
+ }
+
+ let (mut handle, thread) = builder.build();
+ if !handle.is_window_ready() {
+ return Err("Raylib window not ready!".into());
+ }
// update window min size
- handle.set_window_min_size(width.into(), height.into());
+ handle.set_window_min_size(self.width.into(), self.height.into());
// load audio
let audio = Audio::load()?;
@@ -59,26 +98,38 @@ impl Window {
// load renderer
let renderer = Renderer::new(&mut handle, &thread)?;
- Ok(Self {
+ Ok(Window {
handle: RefCell::new(handle),
thread,
renderer,
audio,
})
}
-
- /// Instantiates a new window with a default tile and size
- ///
- /// # Examples
- /// ```no_run
- /// use graphics::Window;
- ///
- /// let window = Window::new_default().unwrap();
- /// ```
- pub fn new_default() -> crate::Result<Self> {
- Self::new(render::RENDER_WIDTH, render::RENDER_HEIGHT, "Dungeon Crawl")
+}
+impl Default for WindowBuilder<'_> {
+ fn default() -> Self {
+ Self {
+ title: Cow::Borrowed("Dungeon Crawl"),
+ width: render::RENDER_WIDTH,
+ height: render::RENDER_HEIGHT,
+ vsync: false,
+ verbose: false,
+ }
}
+}
+/// The `Window` type represents the game window
+#[derive(Debug)]
+pub struct Window {
+ // persistant renderer
+ renderer: Renderer,
+ // core raylib handles
+ handle: RefCell<RaylibHandle>,
+ thread: RaylibThread,
+ // audio data/subsystem
+ audio: Audio,
+}
+impl Window {
/// Returns if the window should be closed.
/// This usually means the 'x' button has been pressed.
pub fn is_open(&self) -> bool {
@@ -86,16 +137,6 @@ impl Window {
}
/// Draws the next ingame frame
- ///
- /// # Examples
- /// ```no_run
- /// use graphics::Window;
- /// use dungeon::Dungeon;
- ///
- /// let dungeon = Dungeon::new();
- /// let mut window = Window::new(800, 600, "Dungeon Crawl").unwrap();
- /// window.draw_frame(&dungeon);
- /// ```
pub fn draw_frame(&mut self, dungeon: &Dungeon) {
self.renderer
.draw_frame(self.handle.get_mut(), &self.thread, dungeon);
diff --git a/graphics/src/render.rs b/graphics/src/render.rs
index 1061af3..945528b 100644
--- a/graphics/src/render.rs
+++ b/graphics/src/render.rs
@@ -736,8 +736,8 @@ impl DungeonExt for Dungeon {
};
/// The maximum position the camera is allowed to go
const CAMERA_MAX: Vector2 = vec2! {
- MAP_SIZE * TILE_SIZE - RENDER_WIDTH/2,
- MAP_SIZE * TILE_SIZE - RENDER_HEIGHT/2 - UI_HEIGHT/2,
+ MAP_SIZE * TILE_SIZE - RENDER_WIDTH/2 - TILE_SIZE/2,
+ MAP_SIZE * TILE_SIZE - RENDER_HEIGHT/2 - TILE_SIZE/2,
};
let pos = self.camera();