summaryrefslogtreecommitdiff
path: root/glfw/src/window.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-10-14 14:55:42 +0200
committerHampusM <hampus@hampusmat.com>2023-10-14 14:55:42 +0200
commit2596f7e6192d53458da2305a03e2b31546eb198c (patch)
treee039f683b47e90e5cf96cdf986dc85105e7021d6 /glfw/src/window.rs
parent12f48046b2606fc77a1312a6d5e5fc7ff9feff88 (diff)
refactor(glfw): move window creation to builder
Diffstat (limited to 'glfw/src/window.rs')
-rw-r--r--glfw/src/window.rs81
1 files changed, 47 insertions, 34 deletions
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
{