diff options
Diffstat (limited to 'glfw/src/lib.rs')
-rw-r--r-- | glfw/src/lib.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/glfw/src/lib.rs b/glfw/src/lib.rs new file mode 100644 index 0000000..889015b --- /dev/null +++ b/glfw/src/lib.rs @@ -0,0 +1,50 @@ +#![deny(clippy::all, clippy::pedantic)] + +use std::ffi::{c_char, CStr}; +use std::ptr::null; + +mod ffi; +mod init; +mod util; + +pub mod window; + +pub use window::{Size as WindowSize, Window}; + +#[derive(Debug, thiserror::Error)] +pub enum Error +{ + /// The current thread is not the main thread. + #[error("The current thread is not the main thread")] + NotInMainThread, + + /// An internal nul byte was found in a window title. + #[error("An internal nul byte was found in the window title")] + InternalNulByteInWindowTitle, + + /// GLFW error. + #[error("GLFW error {0} occured. {1}")] + GlfwError(i32, String), +} + +fn get_glfw_error() -> Result<(), Error> +{ + let mut description_ptr: *const c_char = null(); + + let err = unsafe { crate::ffi::glfwGetError(&mut description_ptr) }; + + if err == crate::ffi::GLFW_NO_ERROR { + return Ok(()); + } + + // SAFETY: The description is guaranteed by GLFW to be valid UTF-8 + let desc_str = unsafe { + std::str::from_utf8_unchecked(CStr::from_ptr(description_ptr).to_bytes()) + }; + + // The description has to be copied because it is guaranteed to be valid only + // until the next GLFW error occurs or the GLFW library is terminated. + let description = desc_str.to_string(); + + Err(Error::GlfwError(err, description)) +} |