-
-
Notifications
You must be signed in to change notification settings - Fork 13
Add some support for the Lua API #319
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
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
889fca3
Add some support for the Lua API
paulyoung 66746b6
Merge branch 'main' into paulyoung/lua
paulyoung 416fc6b
Add example
paulyoung 3ca9496
[WIP] Debug incorrect main.pdz asset location
paulyoung afd669b
Remove accidentally committed files
paulyoung 3d41e57
Add another example of incorrect main.pdz location
paulyoung 1c37e2d
Merge branch 'main' of github.com:boozook/playdate into paulyoung/lua
paulyoung eeec894
Update asset path for main.pdz
paulyoung 9f0c181
Update README
paulyoung 3863ac1
Update authors
paulyoung 1b1dca5
Fix typo in README
paulyoung 4f3e324
minor bump `playdate` crate, remove unused feature and email, followi…
boozook 695445d
CI: add package test for example of `playdate-lua` crate
boozook d5c52c8
Merge pull request #2 from boozook/fix/319-improvements
paulyoung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", "Alex Koz <a@koz.email>"] | ||
| 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 } | ||
|
|
||
|
|
||
| [[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", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| pdxversion=20400 | ||
| buildtime=767932885 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") | ||
| .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!(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| example.logToConsole("hello from main.lua"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 {} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.