From 6984a4e4c1348ab0b24778269cd12251055ca30d Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sun, 15 Oct 2023 10:27:27 +0300 Subject: [PATCH] Show proper error in `load_font` call --- src/graphics.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/graphics.rs b/src/graphics.rs index 023a325..372a0e0 100644 --- a/src/graphics.rs +++ b/src/graphics.rs @@ -761,8 +761,20 @@ impl Graphics { pub fn load_font(&self, path: &str) -> Result { let c_path = CString::new(path).map_err(Error::msg)?; - let font = pd_func_caller!((*self.0).loadFont, c_path.as_ptr(), ptr::null_mut())?; - Font::new(font) + let mut out_err: *const crankstart_sys::ctypes::c_char = ptr::null_mut(); + let font = pd_func_caller!((*self.0).loadFont, c_path.as_ptr(), &mut out_err)?; + if font == ptr::null_mut() { + if out_err != ptr::null_mut() { + let err_msg = unsafe { CStr::from_ptr(out_err).to_string_lossy().into_owned() }; + Err(anyhow!(err_msg)) + } else { + Err(anyhow!( + "load_font failed without providing an error message" + )) + } + } else { + Font::new(font) + } } pub fn set_font(&self, font: &Font) -> Result<(), Error> {