diff options
author | Matthew Michelotti <michelotti.matthew@gmail.com> | 2018-06-13 23:21:29 -0500 |
---|---|---|
committer | Matthew Michelotti <michelotti.matthew@gmail.com> | 2018-06-13 23:39:09 -0500 |
commit | f7e382cb8747998c4807380f55cb8d853d0b1fbd (patch) | |
tree | 8d0509dd00d5822c9491051f5f6037579024ea5e | |
parent | 0e5180badae662f68712114a5bbdb48f7e6f3094 (diff) |
Closes #3; Fix to FFI WASM issue
When compiling to wasm32-unknown-unknown, the FFI exports
were no longer being properly recognized since Rust 1.26.
The only way to make these FFI exports recognized consistently
was to place them in the main.rs file. I have created a macro
to make this easier on the user.
-rw-r--r-- | example/src/main.rs | 3 | ||||
-rw-r--r-- | gate/src/core/sdl/mod.rs | 5 | ||||
-rw-r--r-- | gate/src/core/wasm/wasm_exports.rs | 77 |
3 files changed, 63 insertions, 22 deletions
diff --git a/example/src/main.rs b/example/src/main.rs index 78b4a29..d73618c 100644 --- a/example/src/main.rs +++ b/example/src/main.rs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#[macro_use] extern crate gate; use gate::{App, AppContext, AppInfo, KeyCode}; @@ -20,6 +21,8 @@ use gate::renderer::{Renderer, Affine}; mod asset_id { include!(concat!(env!("OUT_DIR"), "/asset_id.rs")); } use asset_id::{AssetId, SpriteId, MusicId, SoundId}; +gate_header!(); + // Note: the assets that we placed in the src_assets directory can be referenced using the // SpriteId, MusicId, and SoundId enums diff --git a/gate/src/core/sdl/mod.rs b/gate/src/core/sdl/mod.rs index f85d39c..0fbc97b 100644 --- a/gate/src/core/sdl/mod.rs +++ b/gate/src/core/sdl/mod.rs @@ -44,6 +44,11 @@ use self::app_clock::AppClock; use self::event_handler::EventHandler; use super::mark_app_created_flag; +#[macro_export] +macro_rules! gate_header { + () => {}; +} + pub fn run<AS: AppAssetId, AP: App<AS>>(info: AppInfo, mut app: AP) { mark_app_created_flag(); diff --git a/gate/src/core/wasm/wasm_exports.rs b/gate/src/core/wasm/wasm_exports.rs index 7681099..9b4d9d9 100644 --- a/gate/src/core/wasm/wasm_exports.rs +++ b/gate/src/core/wasm/wasm_exports.rs @@ -15,39 +15,36 @@ //! This module contains methods exported to the gate javascript code for WebAssembly. //! DO NOT USE DIRECTLY! +#![allow(non_snake_case)] + use std::os::raw::{c_int, c_char}; use ::input::KeyCode; use ::renderer::shaders; use super::{app_runner_is_defined, app_runner_borrow, app_runner_borrow_mut }; -#[no_mangle] -pub unsafe extern "C" fn gateWasmInit() { +pub fn gateWasmInit() { app_runner_borrow_mut().init(); } -#[no_mangle] -pub unsafe extern "C" fn gateWasmOnResize(w: c_int, h: c_int) { +pub fn gateWasmOnResize(w: c_int, h: c_int) { app_runner_borrow_mut().resize((w as u32, h as u32)); } -#[no_mangle] -pub unsafe extern "C" fn gateWasmUpdateAndDraw(time_millis: f64, cursor_x: c_int, cursor_y: c_int) -> c_int { +pub fn gateWasmUpdateAndDraw(time_millis: f64, cursor_x: c_int, cursor_y: c_int) -> c_int { app_runner_borrow_mut().update_cursor(cursor_x as i32, cursor_y as i32); let continuing = app_runner_borrow_mut().update_and_draw(time_millis / 1000.0); if continuing { 1 } else { 0 } } -#[no_mangle] -pub unsafe extern "C" fn gateWasmKeyEvent(code: c_int, down: bool) -> c_int { +pub fn gateWasmKeyEvent(code: c_int, down: bool) -> c_int { assert!(code >= 0 && code <= 255); let code = KeyCode::from_u8(code as u8).unwrap(); let continuing = app_runner_borrow_mut().input(code, down); if continuing { 1 } else { 0 } } -#[no_mangle] -pub unsafe extern "C" fn gateWasmMouseEvent(cursor_x: c_int, cursor_y: c_int, button: c_int, down: bool) -> c_int { +pub fn gateWasmMouseEvent(cursor_x: c_int, cursor_y: c_int, button: c_int, down: bool) -> c_int { app_runner_borrow_mut().update_cursor(cursor_x as i32, cursor_y as i32); let code = match button { 0 => Some(KeyCode::MouseLeft), @@ -63,32 +60,68 @@ pub unsafe extern "C" fn gateWasmMouseEvent(cursor_x: c_int, cursor_y: c_int, bu if continuing { 1 } else { 0 } } -#[no_mangle] -pub unsafe extern "C" fn gateWasmIsAppDefined() -> c_int { +pub fn gateWasmIsAppDefined() -> c_int { if app_runner_is_defined() { 1 } else { 0 } } -#[no_mangle] -pub unsafe extern "C" fn gateWasmMusicCount() -> c_int { +pub fn gateWasmMusicCount() -> c_int { app_runner_borrow().music_count() as c_int } -#[no_mangle] -pub unsafe extern "C" fn gateWasmSoundCount() -> c_int { +pub fn gateWasmSoundCount() -> c_int { app_runner_borrow().sound_count() as c_int } -#[no_mangle] -pub unsafe extern "C" fn gateWasmSpriteVertSrc() -> *const c_char { +pub fn gateWasmSpriteVertSrc() -> *const c_char { shaders::VS_SPRITE_SRC } -#[no_mangle] -pub unsafe extern "C" fn gateWasmSpriteFragSrc() -> *const c_char { +pub fn gateWasmSpriteFragSrc() -> *const c_char { shaders::FS_SPRITE_SRC } -#[no_mangle] -pub unsafe extern "C" fn gateWasmOnRestart() { +pub fn gateWasmOnRestart() { app_runner_borrow_mut().on_restart(); } + +#[macro_export] +macro_rules! gate_header { + () => { + pub mod gate_wasm_exports { + use std::os::raw::{c_int, c_char}; + #[no_mangle] pub unsafe extern "C" fn gateWasmInit() { + ::gate::wasm_exports::gateWasmInit() + } + #[no_mangle] pub unsafe extern "C" fn gateWasmOnResize(w: c_int, h: c_int) { + ::gate::wasm_exports::gateWasmOnResize(w, h) + } + #[no_mangle] pub unsafe extern "C" fn gateWasmUpdateAndDraw(time_millis: f64, cursor_x: c_int, cursor_y: c_int) -> c_int { + ::gate::wasm_exports::gateWasmUpdateAndDraw(time_millis, cursor_x, cursor_y) + } + #[no_mangle] pub unsafe extern "C" fn gateWasmKeyEvent(code: c_int, down: bool) -> c_int { + ::gate::wasm_exports::gateWasmKeyEvent(code, down) + } + #[no_mangle] pub unsafe extern "C" fn gateWasmMouseEvent(cursor_x: c_int, cursor_y: c_int, button: c_int, down: bool) -> c_int { + ::gate::wasm_exports::gateWasmMouseEvent(cursor_x, cursor_y, button, down) + } + #[no_mangle] pub unsafe extern "C" fn gateWasmIsAppDefined() -> c_int { + ::gate::wasm_exports::gateWasmIsAppDefined() + } + #[no_mangle] pub unsafe extern "C" fn gateWasmMusicCount() -> c_int { + ::gate::wasm_exports::gateWasmMusicCount() + } + #[no_mangle] pub unsafe extern "C" fn gateWasmSoundCount() -> c_int { + ::gate::wasm_exports::gateWasmSoundCount() + } + #[no_mangle] pub unsafe extern "C" fn gateWasmSpriteVertSrc() -> *const c_char { + ::gate::wasm_exports::gateWasmSpriteVertSrc() + } + #[no_mangle] pub unsafe extern "C" fn gateWasmSpriteFragSrc() -> *const c_char { + ::gate::wasm_exports::gateWasmSpriteFragSrc() + } + #[no_mangle] pub unsafe extern "C" fn gateWasmOnRestart() { + ::gate::wasm_exports::gateWasmOnRestart() + } + } + }; +} |