From d2ba28fa1a7bf03ff209f0ce8b936a0583a95b2a Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 29 Apr 2024 02:17:22 -0400 Subject: [PATCH] support emscripten window resizing, F1 is now used over escape on web --- CMakeLists.txt | 2 +- include/blt/gfx/window.h | 2 ++ libraries/imgui | 2 +- libraries/openal-soft | 2 +- src/blt/gfx/renderer/camera.cpp | 8 +++++++- src/blt/gfx/state.cpp | 1 + src/blt/gfx/window.cpp | 29 ++++++++++++++++++++++++++--- 7 files changed, 39 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 53458d3..0208fbc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.25) -set(BLT_GRAPHICS_VERSION 0.11.1) +set(BLT_GRAPHICS_VERSION 0.11.2) set(BLT_GRAPHICS_TEST_VERSION 0.0.1) project(BLT_WITH_GRAPHICS VERSION ${BLT_GRAPHICS_VERSION}) diff --git a/include/blt/gfx/window.h b/include/blt/gfx/window.h index 44bc732..1d73277 100644 --- a/include/blt/gfx/window.h +++ b/include/blt/gfx/window.h @@ -118,6 +118,8 @@ namespace blt::gfx std::int64_t getFrameDelta(); void cleanup(); + + } #endif //BLT_WITH_GRAPHICS_TEMPLATE_WINDOW_H diff --git a/libraries/imgui b/libraries/imgui index 231cbee..a1b0682 160000 --- a/libraries/imgui +++ b/libraries/imgui @@ -1 +1 @@ -Subproject commit 231cbee0fc4f59dbe5b8b853a11b08dc84e57c65 +Subproject commit a1b06823fe2d964a62fda99385499b218cf5cea5 diff --git a/libraries/openal-soft b/libraries/openal-soft index 111397c..6675317 160000 --- a/libraries/openal-soft +++ b/libraries/openal-soft @@ -1 +1 @@ -Subproject commit 111397c71a5f1c2c88e05da9c84edfdba2e472a4 +Subproject commit 6675317107257c2cc16c947b359d557821d85bf2 diff --git a/src/blt/gfx/renderer/camera.cpp b/src/blt/gfx/renderer/camera.cpp index ce1a5e3..11b93b8 100644 --- a/src/blt/gfx/renderer/camera.cpp +++ b/src/blt/gfx/renderer/camera.cpp @@ -25,7 +25,13 @@ void blt::gfx::first_person_camera::update() float speed_multi = 1; - if (isKeyPressed(GLFW_KEY_ESCAPE) && keyPressedLastFrame()) + int locking_key = GLFW_KEY_ESCAPE; + +#ifdef __EMSCRIPTEN__ + locking_key = GLFW_KEY_F1; +#endif + + if (isKeyPressed(locking_key) && keyPressedLastFrame()) { if (isCursorLocked()) unlockCursor(); diff --git a/src/blt/gfx/state.cpp b/src/blt/gfx/state.cpp index 7115b8e..b26291e 100644 --- a/src/blt/gfx/state.cpp +++ b/src/blt/gfx/state.cpp @@ -16,6 +16,7 @@ * along with this program. If not, see . */ #include +#include #include "blt/std/assert.h" void blt::gfx::matrix_state_manager::update() diff --git a/src/blt/gfx/window.cpp b/src/blt/gfx/window.cpp index aed36d4..77b1be2 100644 --- a/src/blt/gfx/window.cpp +++ b/src/blt/gfx/window.cpp @@ -14,6 +14,12 @@ #include "backends/imgui_impl_glfw.h" #include +#ifdef __EMSCRIPTEN__ + + #include + +#endif + void error_callback(int error, const char* description) { BLT_ERROR("GLFW Error (%d): %s", error, description); @@ -124,8 +130,9 @@ namespace blt::gfx io.Fonts->AddFontFromMemoryCompressedBase85TTF(fontAwesomeRegular_compressed_data_base85, 13.0f, &config, icon_ranges); io.Fonts->AddFontFromMemoryCompressedTTF(fontAwesomeSolid_compressed_data, static_cast(fontAwesomeSolid_compressed_size), 13.0, &config, icon_ranges); - io.Fonts->AddFontFromMemoryCompressedTTF(fontAwesomeBrands_compressed_data, static_cast(fontAwesomeBrands_compressed_size), 13.0, &config, - icon_ranges); + io.Fonts + ->AddFontFromMemoryCompressedTTF(fontAwesomeBrands_compressed_data, static_cast(fontAwesomeBrands_compressed_size), 13.0, &config, + icon_ranges); //ImGui::StyleColorsLight(); @@ -174,11 +181,27 @@ namespace blt::gfx window_state.nanoDelta = static_cast(window_state.deltaTime) / 1e9f; window_state.millisDelta = static_cast(window_state.deltaTime) / 1e6f; } + +#ifdef __EMSCRIPTEN__ + + EM_BOOL emscripten_resize_callback(int, const EmscriptenUiEvent* event, void* data) + { + int width = event->windowInnerWidth; + int height = event->windowInnerHeight; + + glfwSetWindowSize(window_state.window, width, height); + + return false; + } + +#endif void init(const window_data& data) { #ifdef __EMSCRIPTEN__ blt::logging::setLogOutputFormat("[${{TIME}}] [${{LOG_LEVEL}}] (${{FILE}}:${{LINE}}) ${{STR}}\n"); + emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, nullptr, false, + emscripten_resize_callback); #endif /* -- Set up Error Callback -- */ glfwSetErrorCallback(error_callback); @@ -223,7 +246,7 @@ namespace blt::gfx * make sure you render at a proper smooth rate that lines up properly with the browser and monitor." * https://emscripten.org/docs/api_reference/emscripten.h.html */ - emscripten_set_main_loop_arg(loop, (void*)&data, 0, true); + emscripten_set_main_loop_arg(loop, (void*) &data, 0, true); #else /* -- General Loop -- */ while (!glfwWindowShouldClose(window_state.window))