]> git.seodisparate.com - LudumDare45_StartWithNothing/commitdiff
Work on wasm build
authorStephen Seo <seo.disparate@gmail.com>
Wed, 22 Feb 2023 03:20:14 +0000 (12:20 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Wed, 22 Feb 2023 03:20:14 +0000 (12:20 +0900)
Cargo.toml
build.rs
src/agnostic_interface/raylib_impl.rs
src/lib.rs [new file with mode: 0644]
wasm/.gitignore [new file with mode: 0644]
wasm/Makefile [new file with mode: 0644]
wasm/emsdk_version [new file with mode: 0644]
wasm/include/ld45_lib.h [new file with mode: 0644]
wasm/include/raylib.h [new symlink]
wasm/lib/libraylib.a [new file with mode: 0644]
wasm/src/main.c [new file with mode: 0644]

index a503789e798d986a6502ea92b71a7a1e7a33f18a..920779b1126cd8a6a92588a2820d085a7e3ea9cf 100644 (file)
@@ -2,7 +2,7 @@
 name = "ld45_one_and_all"
 version = "0.1.1"
 authors = ["Stephen Seo <seo.disparate@gmail.com>"]
-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"
index 1ed36cf91d85070f53ef7e69ee240a5098a631b3..e16c200922e8296dac199a62a5661093f3e7358c 100644 (file)
--- 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");
 
index 1e345ba37bf5413068a6a56d92089676e811bd28..e0eb388dbb8808ed59d77baa882bcd0e1a8e0f1b 100644 (file)
@@ -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 (file)
index 0000000..c8d8453
--- /dev/null
@@ -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 (file)
index 0000000..b9cd2ac
--- /dev/null
@@ -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 (file)
index 0000000..9821613
--- /dev/null
@@ -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 (file)
index 0000000..ab6c88e
--- /dev/null
@@ -0,0 +1 @@
+3.1.28
diff --git a/wasm/include/ld45_lib.h b/wasm/include/ld45_lib.h
new file mode 100644 (file)
index 0000000..cb87442
--- /dev/null
@@ -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 (symlink)
index 0000000..0ace726
--- /dev/null
@@ -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 (file)
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 (file)
index 0000000..56be6a8
--- /dev/null
@@ -0,0 +1,16 @@
+#include <emscripten.h>
+#include <emscripten/html5.h>
+
+#include <ld45_lib.h>
+
+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;
+}