Skip to content

Commit

Permalink
Merge branch 'main' into fix-clippy-lints
Browse files Browse the repository at this point in the history
  • Loading branch information
boozook authored Oct 16, 2023
2 parents 1ada141 + dfe1a0e commit 4929989
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,8 +754,20 @@ impl Graphics {

pub fn load_font(&self, path: &str) -> Result<Font, Error> {
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> {
Expand Down
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod display;
pub mod file;
pub mod geometry;
pub mod graphics;
pub mod lua;
pub mod sound;
pub mod sprite;
pub mod system;
Expand All @@ -18,6 +19,7 @@ use {
display::Display,
file::FileSystem,
graphics::{Graphics, PDRect},
lua::Lua,
sound::Sound,
sprite::{
Sprite, SpriteCollideFunction, SpriteDrawFunction, SpriteManager, SpriteUpdateFunction,
Expand Down Expand Up @@ -49,6 +51,8 @@ impl Playdate {
FileSystem::new(file);
let graphics = playdate_api.graphics;
Graphics::new(graphics);
let lua = playdate_api.lua;
Lua::new(lua);
let sound = playdate_api.sound;
Sound::new(sound)?;
let display = playdate_api.display;
Expand Down Expand Up @@ -201,6 +205,9 @@ impl<T: 'static + Game> GameRunner<T> {
#[macro_export]
macro_rules! crankstart_game {
($game_struct:tt) => {
crankstart_game!($game_struct, PDSystemEvent::kEventInit)
};
($game_struct:tt, $pd_system_event:expr) => {
pub mod game_setup {
extern crate alloc;
use super::*;
Expand Down Expand Up @@ -241,7 +248,7 @@ macro_rules! crankstart_game {
event: PDSystemEvent,
_arg: u32,
) -> crankstart_sys::ctypes::c_int {
if event == PDSystemEvent::kEventInit {
if event == $pd_system_event {
// This would only fail if PlaydateAPI has null pointers, which shouldn't happen.
let mut playdate = match Playdate::new(playdate, sprite_update, sprite_draw) {
Ok(playdate) => playdate,
Expand Down
45 changes: 45 additions & 0 deletions src/lua.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use {
crate::pd_func_caller,
alloc::string::String,
anyhow::{anyhow, Error},
core::ptr,
crankstart_sys::{ctypes, lua_CFunction},
cstr_core::{CStr, CString},
};

static mut LUA: Lua = Lua(ptr::null_mut());

#[derive(Clone, Debug)]
pub struct Lua(*const crankstart_sys::playdate_lua);

impl Lua {
pub(crate) fn new(file: *const crankstart_sys::playdate_lua) {
unsafe {
LUA = Lua(file);
}
}

pub fn get() -> Self {
unsafe { LUA.clone() }
}

pub fn add_function(&self, f: lua_CFunction, name: &str) -> Result<(), Error> {
let c_name = CString::new(name).map_err(Error::msg)?;
let mut out_err: *const crankstart_sys::ctypes::c_char = ptr::null_mut();
pd_func_caller!((*self.0).addFunction, f, c_name.as_ptr(), &mut out_err)?;
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 {
Ok(())
}
}

pub fn get_arg_string(&self, pos: i32) -> Result<String, Error> {
let c_arg_string = pd_func_caller!((*self.0).getArgString, pos as ctypes::c_int)?;
unsafe {
let arg_string = CStr::from_ptr(c_arg_string).to_string_lossy().into_owned();
Ok(arg_string)
}
}
}

0 comments on commit 4929989

Please sign in to comment.