Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some support for the Lua API #319

Merged
merged 14 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ctrl = { version = "0.3", path = "api/ctrl", package = "playdate-controls", defa
display = { version = "0.3", path = "api/display", package = "playdate-display", default-features = false }
fs = { version = "0.2", path = "api/fs", package = "playdate-fs", default-features = false }
gfx = { version = "0.3", path = "api/gfx", package = "playdate-graphics", default-features = false }
lua = { version = "0.1", path = "api/lua", package = "playdate-lua", default-features = false }
menu = { version = "0.2", path = "api/menu", package = "playdate-menu", default-features = false }
scoreboards = { version = "0.1", path = "api/scoreboards", package = "playdate-scoreboards", default-features = false }
sound = { version = "0.3", path = "api/sound", package = "playdate-sound", default-features = false }
Expand Down
67 changes: 67 additions & 0 deletions api/lua/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
[package]
name = "playdate-lua"
version = "0.1.0"
readme = "README.md"
description = "High-level Lua API built on-top of Playdate API"
keywords = ["playdate", "sdk", "api", "gamedev"]
categories = ["game-development", "api-bindings", "no-std"]
edition.workspace = true
license.workspace = true
authors = ["Paul Young <[email protected]>", "Alex Koz <[email protected]>"]
homepage.workspace = true
repository.workspace = true


[features]
default = ["sys/default"]

# playdate-sys features, should be shared because it's build configuration:

bindgen-runtime = ["sys/bindgen-runtime"]
bindgen-static = ["sys/bindgen-static"]
bindings-derive-debug = ["sys/bindings-derive-debug"]


[dependencies]
sys = { workspace = true, default-features = false }

[dev-dependencies]
system = { workspace = true, default-features = false, features = [ "try-trait-v2" ] }
paulyoung marked this conversation as resolved.
Show resolved Hide resolved


[[example]]
name = "add-function-get-arg-string"
crate-type = ["dylib", "staticlib"]
path = "examples/add-function-get-arg-string.rs"
required-features = ["sys/entry-point", "sys/lang-items"]


[package.metadata.playdate]
bundle-id = "rs.playdate.lua"

[package.metadata.playdate.dev-assets]
# The Lua runtime expects us to provide a main.pdz file at the root.
#
# Compiled with `pdc --skip-unknown --strip sources/main.lua Example.pdx`
"main.pdz" = "examples/Example.pdx/main.pdz"


[package.metadata.docs.rs]
all-features = false
features = [
"sys/bindings-derive-default",
"sys/bindings-derive-eq",
"sys/bindings-derive-copy",
"bindings-derive-debug",
"sys/bindings-derive-hash",
"sys/bindings-derive-ord",
"sys/bindings-derive-partialeq",
"sys/bindings-derive-partialord",
]
rustdoc-args = ["--cfg", "docsrs", "--show-type-layout"]
default-target = "thumbv7em-none-eabihf"
cargo-args = [
"-Zunstable-options",
"-Zrustdoc-scrape-examples",
"-Zbuild-std=core,alloc",
]
17 changes: 17 additions & 0 deletions api/lua/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Lua API for PlayDate

High-level Lua API built on-top of [playdate-sys][].


## Usage

See [examples][].

[examples]: ./examples
[playdate-sys]: https://crates.io/crates/playdate-sys



- - -

This software is not sponsored or supported by Panic.
Binary file added api/lua/examples/Example.pdx/main.pdz
Binary file not shown.
2 changes: 2 additions & 0 deletions api/lua/examples/Example.pdx/pdxinfo
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pdxversion=20400
buildtime=767932885
16 changes: 16 additions & 0 deletions api/lua/examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Examples

These examples additionally use other crates with parts of Playdate API to minimize the amount of code.


# How to run

```bash
cargo playdate run -p=playdate-lua --example=add-function-get-arg-string --features=sys/lang-items,sys/entry-point
```

More information how to use [cargo-playdate][] in help: `cargo playdate --help`.



[cargo-playdate]: https://crates.io/crates/cargo-playdate
68 changes: 68 additions & 0 deletions api/lua/examples/add-function-get-arg-string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#![no_std]
extern crate alloc;

#[macro_use]
extern crate sys;
extern crate playdate_lua as lua;

use core::ffi::c_int;
use core::ptr::NonNull;

use lua::Lua;
use sys::EventLoopCtrl;
use sys::ffi::*;
use system::System;
use system::event::SystemEventExt as _;
use system::update::UpdateCtrl;


/// Entry point, event handler
#[no_mangle]
fn event_handler(_api: NonNull<PlaydateAPI>, event: PDSystemEvent, _: u32) -> EventLoopCtrl {
// We need to set our update callback in the InitLua handler instead of Init.
// https://devforum.play.date/t/lua-c-minimal-example/4354/5
//
// Just for this example, ignore all other events.
if event != PDSystemEvent::InitLua {
return EventLoopCtrl::Continue;
}

// Set update callback
System::Default().set_update_callback_static(Some(on_update), ());

// Add a function that we depend on and call in main.lua
Lua::Default().add_function(Some(log_to_console_from_main_dot_lua), "example.logToConsole")
boozook marked this conversation as resolved.
Show resolved Hide resolved
.expect("add_function 'log_to_console_from_main_dot_lua' should succeed");

// Continue event loop
EventLoopCtrl::Continue
}


/// Update handler
fn on_update(_: &mut ()) -> UpdateCtrl {
// Continue updates
UpdateCtrl::Continue
}


// The function we add to the Lua runtime and call from main.lua
pub unsafe extern "C" fn log_to_console_from_main_dot_lua(_lua_state: *mut lua_State) -> c_int {
// We know that our function takes a single argument which is a string.
let arg_string = Lua::Default().get_arg_string(1)
.expect("get_arg_string should succeed");

// Avoid going from CString to str and back with playdate::sys::log::println
let f = (*(*sys::API).system).logToConsole
.expect("get logToConsole to succeed");

f(arg_string.as_ptr());

// A `lua_CFunction` should return the number of return values it has pushed
// onto the stack.
0
}


// Needed for debug build
ll_symbols!();
1 change: 1 addition & 0 deletions api/lua/examples/sources/main.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example.logToConsole("hello from main.lua");
38 changes: 38 additions & 0 deletions api/lua/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use alloc::borrow::ToOwned;
use core::fmt;
use sys::ffi::CStr;
use sys::ffi::CString;


pub type ApiError = sys::error::Error<self::Error>;


#[derive(Debug)]
pub enum Error {
AddFunction(CString),
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self {
Error::AddFunction(cs) => {
match cs.to_str() {
Ok(err) => err.fmt(f),
Err(_) => f.write_fmt(format_args!("Add function error: {cs:?}")),
}
},
}
}
}


impl From<Error> for ApiError {
fn from(err: Error) -> Self { ApiError::Api(err) }
}

impl From<&'_ CStr> for Error {
fn from(cs: &CStr) -> Self { Self::AddFunction(cs.to_owned()) }
}


impl core::error::Error for Error {}
Loading