diff options
author | HampusM <hampus@hampusmat.com> | 2023-10-06 20:55:07 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-10-06 20:55:07 +0200 |
commit | f255db0f9252f4041b120dcaa00470889c4cb9f4 (patch) | |
tree | 94a56d2acc3cacdaebdae1dd9b37d533b9e7368f /glfw/src/init.rs | |
parent | e40fd63dd35430f234d65806ca7e0d6bea364bfc (diff) |
feat: add GLFW wrapper library
Diffstat (limited to 'glfw/src/init.rs')
-rw-r--r-- | glfw/src/init.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/glfw/src/init.rs b/glfw/src/init.rs new file mode 100644 index 0000000..d102db6 --- /dev/null +++ b/glfw/src/init.rs @@ -0,0 +1,44 @@ +use crate::util::is_main_thread; +use crate::{get_glfw_error, Error}; + +/// Initializes GLFW and returns a initialization token. +/// +/// # Errors +/// Will return `Err` if +/// - The current thread is not the main thread +/// - A GLFW error occurs +pub fn initialize() -> Result<Glfw, Error> +{ + if !is_main_thread() { + return Err(Error::NotInMainThread); + } + + // SAFETY: The current thread is the main thread + let success = unsafe { crate::ffi::glfwInit() }; + + if success == crate::ffi::GLFW_FALSE { + get_glfw_error()?; + } + + Ok(Glfw { _priv: &() }) +} + +/// GLFW initialization token. +pub struct Glfw +{ + /// This field has two purposes + /// - To make the struct not constructable without calling [`initialize`]. + /// - To make the struct `!Send` and `!Sync`. + _priv: *const (), +} + +impl Drop for Glfw +{ + fn drop(&mut self) + { + // SAFETY: The current thread cannot be any other thread than the main thread + // since the initialize function checks it and the GLFW initialization token is + // neither Send or Sync + unsafe { crate::ffi::glfwTerminate() }; + } +} |