From 2596f7e6192d53458da2305a03e2b31546eb198c Mon Sep 17 00:00:00 2001 From: HampusM Date: Sat, 14 Oct 2023 14:55:42 +0200 Subject: refactor(glfw): move window creation to builder --- glfw/src/lib.rs | 2 +- glfw/src/window.rs | 81 +++++++++++++++++++++++++++++++----------------------- 2 files changed, 48 insertions(+), 35 deletions(-) (limited to 'glfw/src') diff --git a/glfw/src/lib.rs b/glfw/src/lib.rs index 889015b..7d2f25c 100644 --- a/glfw/src/lib.rs +++ b/glfw/src/lib.rs @@ -9,7 +9,7 @@ mod util; pub mod window; -pub use window::{Size as WindowSize, Window}; +pub use window::{Builder as WindowBuilder, Size as WindowSize, Window}; #[derive(Debug, thiserror::Error)] pub enum Error diff --git a/glfw/src/window.rs b/glfw/src/window.rs index fa5dcd7..5261eec 100644 --- a/glfw/src/window.rs +++ b/glfw/src/window.rs @@ -15,40 +15,6 @@ pub struct Window impl Window { - /// Creates a new window. - /// - /// # Errors - /// Will return `Err` if - /// - The title contains an internal nul byte - /// - A GLFW error occurs - pub fn create(size: &Size, title: &str) -> Result - { - let init = initialize()?; - - let c_title = - CString::new(title).map_err(|_| Error::InternalNulByteInWindowTitle)?; - - // SAFETY: The initialize function makes sure the current thread is the main - // thread - let handle = unsafe { - #[allow(clippy::cast_possible_wrap)] - crate::ffi::glfwCreateWindow( - size.width as i32, - size.height as i32, - c_title.as_ptr(), - null_mut(), - null_mut(), - ) - }; - - get_glfw_error()?; - - Ok(Self { - _init: init, - handle, - }) - } - /// Makes the context of the window current for the calling thread. /// /// # Errors @@ -161,6 +127,53 @@ impl Window } } +/// [`Window`] builder. +#[derive(Debug, Clone, Default)] +pub struct Builder {} + +impl Builder +{ + #[must_use] + pub fn new() -> Self + { + Self {} + } + + /// Creates a new window. + /// + /// # Errors + /// Will return `Err` if + /// - The title contains an internal nul byte + /// - A GLFW error occurs + pub fn create(&self, size: &Size, title: &str) -> Result + { + let init = initialize()?; + + let c_title = + CString::new(title).map_err(|_| Error::InternalNulByteInWindowTitle)?; + + // SAFETY: The initialize function makes sure the current thread is the main + // thread + let handle = unsafe { + #[allow(clippy::cast_possible_wrap)] + crate::ffi::glfwCreateWindow( + size.width as i32, + size.height as i32, + c_title.as_ptr(), + null_mut(), + null_mut(), + ) + }; + + get_glfw_error()?; + + Ok(Window { + _init: init, + handle, + }) + } +} + /// Window size. pub struct Size { -- cgit v1.2.3-18-g5258