Work on wasm build

This commit is contained in:
Stephen Seo 2023-02-22 12:20:14 +09:00
parent f1bd86d60e
commit 09159a1f53
11 changed files with 108 additions and 3 deletions

View 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"

View file

@ -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");

View file

@ -577,7 +577,9 @@ impl RaylibGame {
Self::native_setup();
unsafe {
ffi::InitAudioDevice();
while !ffi::IsAudioDeviceReady() {
ffi::InitAudioDevice();
}
}
let mut self_unboxed = Self {

38
src/lib.rs Normal file
View file

@ -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();
}
}

5
wasm/.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
/ld45.html
/ld45.js
/ld45.data
/ld45.wasm
/index.html

24
wasm/Makefile Normal file
View file

@ -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

1
wasm/emsdk_version Normal file
View file

@ -0,0 +1 @@
3.1.28

8
wasm/include/ld45_lib.h Normal file
View file

@ -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

1
wasm/include/raylib.h Symbolic link
View file

@ -0,0 +1 @@
../raylib/raylib.h

BIN
wasm/lib/libraylib.a Normal file

Binary file not shown.

16
wasm/src/main.c Normal file
View file

@ -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;
}