summaryrefslogtreecommitdiff
path: root/glfw
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-10-14 15:49:12 +0200
committerHampusM <hampus@hampusmat.com>2023-10-14 15:49:12 +0200
commit46f27f31e425f5eba494f499bd7a6ac8f8713c2a (patch)
treef8e0d31181607d7144c40e8ce50ad86ce744b1d4 /glfw
parent1a678d877c5a0f5faf95741d27390c923c8dd2b4 (diff)
feat(glfw): add setting window creation hints
Diffstat (limited to 'glfw')
-rw-r--r--glfw/src/window.rs54
1 files changed, 50 insertions, 4 deletions
diff --git a/glfw/src/window.rs b/glfw/src/window.rs
index 5261eec..879d17e 100644
--- a/glfw/src/window.rs
+++ b/glfw/src/window.rs
@@ -129,14 +129,32 @@ impl Window
/// [`Window`] builder.
#[derive(Debug, Clone, Default)]
-pub struct Builder {}
+pub struct Builder
+{
+ hints: Vec<(Hint, i32)>,
+}
impl Builder
{
#[must_use]
pub fn new() -> Self
{
- Self {}
+ Self { hints: Vec::new() }
+ }
+
+ /// Adds a window creation hint to set.
+ #[must_use]
+ pub fn hint(mut self, hint: Hint, value: i32) -> Self
+ {
+ self.hints.push((hint, value));
+
+ self
+ }
+
+ /// Sets the window hints to set.
+ pub fn hints(mut self, hints: impl IntoIterator<Item = (Hint, i32)>)
+ {
+ self.hints = hints.into_iter().collect();
}
/// Creates a new window.
@@ -147,11 +165,22 @@ impl Builder
/// - 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)?;
+ let init = initialize()?;
+
+ for (hint, value) in &self.hints {
+ // SAFETY: The initialize function makes sure the current thread is the main
+ // thread
+ //
+ // Error is not checked for after since the two possible errors
+ // (GLFW_NOT_INITIALIZED and GLFW_INVALID_ENUM) cannot occur.
+ unsafe {
+ crate::ffi::glfwWindowHint(hint.to_glfw(), *value);
+ }
+ }
+
// SAFETY: The initialize function makes sure the current thread is the main
// thread
let handle = unsafe {
@@ -174,6 +203,23 @@ impl Builder
}
}
+/// Window creation hint
+#[derive(Debug, Clone, Copy)]
+pub enum Hint
+{
+ OpenGLDebugContext,
+}
+
+impl Hint
+{
+ fn to_glfw(self) -> c_int
+ {
+ match self {
+ Self::OpenGLDebugContext => crate::ffi::GLFW_OPENGL_DEBUG_CONTEXT,
+ }
+ }
+}
+
/// Window size.
pub struct Size
{