diff --git a/Makefile b/Makefile index 5f88cdd..ed35e24 100644 --- a/Makefile +++ b/Makefile @@ -143,7 +143,7 @@ endif examples/webgpu/webgpu: examples/webgpu/webgpu.c RGFW.h ifeq ($(CC),emcc) # web ASM - emcc main.c -I. --shell-file ./webasm/shell.html -s USE_WEBGPU=1 -o $@$(EXT) + emcc $< -I. --shell-file ./webasm/shell.html -s USE_WEBGPU=1 -o $@$(EXT) else @echo webgpu is not supported on $(detected_OS) endif diff --git a/examples/webgpu/webgpu.c b/examples/webgpu/webgpu.c index 301c197..0f5ede0 100644 --- a/examples/webgpu/webgpu.c +++ b/examples/webgpu/webgpu.c @@ -43,7 +43,6 @@ static state_t state; //-------------------------------------------------- // callbacks -static int resize(int, const EmscriptenUiEvent*, void*); static void draw(); // helper functions @@ -111,8 +110,13 @@ int main(int argc, const char* argv[]) { //----------------- // init //----------------- - resize(0, NULL, NULL); // set size and create swapchain - emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, 0, false, (em_ui_callback_func)resize); + if (state.wgpu.swapchain) { + wgpuSwapChainRelease(state.wgpu.swapchain); + state.wgpu.swapchain = NULL; + } + + state.wgpu.swapchain = create_swapchain(); + } //----------------- // setup pipeline @@ -255,7 +259,16 @@ int main(int argc, const char* argv[]) { //----------------- while (RGFW_window_shouldClose(win) == RGFW_FALSE) { - RGFW_window_checkEvent(win); + while (RGFW_window_checkEvent(win)) { + if (win->event.type == RGFW_windowResized) { + if (state.wgpu.swapchain) { + wgpuSwapChainRelease(state.wgpu.swapchain); + state.wgpu.swapchain = NULL; + } + + state.wgpu.swapchain = create_swapchain(); + } + } draw(); } @@ -323,20 +336,6 @@ void draw() { wgpuTextureViewRelease(back_buffer); } -// resize callback -int resize(int event_type, const EmscriptenUiEvent* ui_event, void* user_data) { - (void)event_type, (void)ui_event, (void)user_data; // unused - - if (state.wgpu.swapchain) { - wgpuSwapChainRelease(state.wgpu.swapchain); - state.wgpu.swapchain = NULL; - } - - state.wgpu.swapchain = create_swapchain(); - - return 1; -} - // helper functions WGPUSwapChain create_swapchain() { WGPUSurface surface = wgpuInstanceCreateSurface(win->src.ctx, &(WGPUSurfaceDescriptor){