diff --git a/Cargo.toml b/Cargo.toml index a503789..920779b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ name = "ld45_one_and_all" version = "0.1.1" authors = ["Stephen Seo "] -edition = "2018" +edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -16,4 +16,13 @@ bindgen = "0.64" [features] default = [] -no_link_libs = [] +no_link_libs = [] + +[lib] +name = "ld45_lib" +crate-type = ["staticlib"] +required-features = ["no_link_libs"] + +[[bin]] +name = "ld45_bin" +path = "src/main.rs" diff --git a/build.rs b/build.rs index 1ed36cf..e16c200 100644 --- a/build.rs +++ b/build.rs @@ -19,6 +19,7 @@ fn main() { let bindings = bindgen::Builder::default() .header("raylib/raylib.h") .parse_callbacks(Box::new(bindgen::CargoCallbacks)) + .clang_arg("-fvisibility=default") .generate() .expect("Unable to generate bindings"); diff --git a/src/agnostic_interface/raylib_impl.rs b/src/agnostic_interface/raylib_impl.rs index 1e345ba..e0eb388 100644 --- a/src/agnostic_interface/raylib_impl.rs +++ b/src/agnostic_interface/raylib_impl.rs @@ -577,7 +577,9 @@ impl RaylibGame { Self::native_setup(); unsafe { - ffi::InitAudioDevice(); + while !ffi::IsAudioDeviceReady() { + ffi::InitAudioDevice(); + } } let mut self_unboxed = Self { diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..c8d8453 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,38 @@ +use agnostic_interface::raylib_impl::RaylibGame; +use faux_quicksilver::Window; +use original_impl::GameState; + +mod agnostic_interface; +mod faux_quicksilver; +mod original_impl; +mod shaders; + +struct WasmState { + pub window: Window, + pub game_state: GameState, +} + +#[no_mangle] +pub extern "C" fn ld45_initialize() -> *mut ::std::os::raw::c_void { + let game_interface = RaylibGame::new_boxed(800, 600); + let mut window = Window::new(game_interface); + let game_state = GameState::new(&mut window).unwrap(); + + Box::into_raw(Box::new(WasmState { window, game_state })) as *mut ::std::os::raw::c_void +} + +#[no_mangle] +pub extern "C" fn ld45_iterate(context: *mut ::std::os::raw::c_void) { + let state_ptr = context as *mut WasmState; + unsafe { + (*state_ptr).window.update_music().unwrap(); + (*state_ptr) + .game_state + .update(&mut (*state_ptr).window) + .unwrap(); + (*state_ptr) + .game_state + .draw(&mut (*state_ptr).window) + .unwrap(); + } +} diff --git a/wasm/.gitignore b/wasm/.gitignore new file mode 100644 index 0000000..b9cd2ac --- /dev/null +++ b/wasm/.gitignore @@ -0,0 +1,5 @@ +/ld45.html +/ld45.js +/ld45.data +/ld45.wasm +/index.html diff --git a/wasm/Makefile b/wasm/Makefile new file mode 100644 index 0000000..9821613 --- /dev/null +++ b/wasm/Makefile @@ -0,0 +1,24 @@ +CC = source "${HOME}/git/emsdk/emsdk_env.sh" && emcc + +all: ld45.html + +ld45.html: src/main.c ../target/wasm32-unknown-emscripten/release/libld45_lib.a + ${CC} -o ld45.html -s USE_GLFW=3 -Iinclude \ + -Llib -lraylib \ + -L../target/wasm32-unknown-emscripten/debug -lld45_lib \ + -sSAFE_HEAP=1 \ + -sEXPORTED_RUNTIME_METHODS=ccall,cwrap \ + --preload-file ../static src/main.c + ln -sf ld45.html index.html + +../target/wasm32-unknown-emscripten/debug/libld45_lib.a: ../src/lib.rs + cd ..; source "${HOME}/git/emsdk/emsdk_env.sh"; cargo build --lib --target wasm32-unknown-emscripten + +.PHONY: clean + +clean: + rm -f ld45.html + rm -f ld45.js + rm -f ld45.wasm + rm -f ld45.data + rm -f index.html diff --git a/wasm/emsdk_version b/wasm/emsdk_version new file mode 100644 index 0000000..ab6c88e --- /dev/null +++ b/wasm/emsdk_version @@ -0,0 +1 @@ +3.1.28 diff --git a/wasm/include/ld45_lib.h b/wasm/include/ld45_lib.h new file mode 100644 index 0000000..cb87442 --- /dev/null +++ b/wasm/include/ld45_lib.h @@ -0,0 +1,8 @@ +#ifndef LD45_ONE_AND_ALL_LIB_H_ +#define LD45_ONE_AND_ALL_LIB_H_ + +extern void *ld45_initialize(); + +extern void ld45_iterate(void *context); + +#endif diff --git a/wasm/include/raylib.h b/wasm/include/raylib.h new file mode 120000 index 0000000..0ace726 --- /dev/null +++ b/wasm/include/raylib.h @@ -0,0 +1 @@ +../raylib/raylib.h \ No newline at end of file diff --git a/wasm/lib/libraylib.a b/wasm/lib/libraylib.a new file mode 100644 index 0000000..bcc4351 Binary files /dev/null and b/wasm/lib/libraylib.a differ diff --git a/wasm/src/main.c b/wasm/src/main.c new file mode 100644 index 0000000..56be6a8 --- /dev/null +++ b/wasm/src/main.c @@ -0,0 +1,16 @@ +#include +#include + +#include + +void main_loop(void *ud) { + ld45_iterate(ud); +} + +int main(void) { + void *ld45_context = ld45_initialize(); + + emscripten_set_main_loop_arg(main_loop, ld45_context, 0, 1); + + return 0; +}