diff options
author | HampusM <hampus@hampusmat.com> | 2023-10-14 14:55:42 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-10-14 14:55:42 +0200 |
commit | 2596f7e6192d53458da2305a03e2b31546eb198c (patch) | |
tree | e039f683b47e90e5cf96cdf986dc85105e7021d6 /glfw | |
parent | 12f48046b2606fc77a1312a6d5e5fc7ff9feff88 (diff) |
refactor(glfw): move window creation to builder
Diffstat (limited to 'glfw')
-rw-r--r-- | glfw/src/lib.rs | 2 | ||||
-rw-r--r-- | glfw/src/window.rs | 81 |
2 files changed, 48 insertions, 35 deletions
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<Self, Error> - { - 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<Window, Error> + { + 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 { |