我在 Web Worker 中创建 SDL 画布时遇到问题。
该程序作为库启动,并且在 Emscripten 构建中没有名为 main() 的函数:
// Run as Library (e.g. Android and WebAssembly)
#[no_mangle]
#[allow(non_snake_case)]
pub extern fn SDL_main(_argc: libc::c_int, _argv: *const *const libc::c_char) -> libc::c_int {
game::start();
return 0;
}
触发问题的代码行(在 Rust 中从主线程启动的渲染线程中):
// ...
let window: Window = video_subsystem.window("Alexis' Game Engine", 800, 600)
.position_centered()
.resizable()
.build()
.expect("Could Not Make a Window");
debug!("Window Created...");
// The below line of code is what triggers the issue
let mut canvas: Canvas<Window> = window.into_canvas()
.accelerated()
.present_vsync()
.build()
.expect("Could Not Make a Canvas");
debug!("Canvas Created...");
// ...
下面的代码是从主线程启动渲染线程的代码
// ...
#[cfg(feature="client")]
let render_thread: JoinHandle<()> = thread::Builder::new().name("render".to_string())
.spawn(|| render::start(rrtx, srrx)).unwrap(); // Client
// ...
根据链接文档,OFFSCREEN_FRAMEBUFFER和OFFSCREENCANVAS_SUPPORT都应该有助于解决此问题
// These are the flags that are used to build the webassembly files. This includes main.js, main.wasm, main.worker.js, and main.ww.js. main.ww.js is unused.
println!("cargo:rustc-env=EMCC_CFLAGS=-O3 -pthread -s STRICT_JS=1 \
-s WASM=1 \
-s WASM_BIGINT=1 \
-s SUPPORT_BIG_ENDIAN=1 \
-s ALLOW_MEMORY_GROWTH=1 \
-s SHARED_MEMORY=1 \
-s ABORT_ON_WASM_EXCEPTIONS=0 \
-s WASM_WORKERS=1 \
-s WASMFS=1 \
-s MINIMAL_RUNTIME_STREAMING_WASM_INSTANTIATION=1 \
-s TRUSTED_TYPES=1 \
-s ASSERTIONS=1 \
-s PTHREADS_DEBUG=0 \
-s RUNTIME_DEBUG=0 \
-s ALLOW_BLOCKING_ON_MAIN_THREAD=0 \
-s PTHREAD_POOL_SIZE=3 \
-s OFFSCREEN_FRAMEBUFFER=1 \
-s OFFSCREENCANVAS_SUPPORT=1 \
-lSDL2 \
-lSDL2_image \
-lSDL2_ttf \
-s EXPORTED_FUNCTIONS=\"['_SDL_main', '_malloc']\"
");
下面的 Firefox 屏幕截图显示了错误
TypeError: GLctx is undefined
下面的 Chrome 屏幕截图显示了错误
(index):49 TypeError: Cannot read properties of undefined (reading 'createShader')
你的问题解决了吗?我有一模一样的 附:我没有资格写评论;|