From ee4b274fda90a1fbd21d684282705b7e53f88d9e Mon Sep 17 00:00:00 2001 From: Adam Kewley Date: Thu, 4 Jul 2024 12:34:07 +0200 Subject: [PATCH] Hackily add 2D UI support for emscripten hellotriangle demo --- apps/hellotriangle/hellotriangle.cpp | 32 +++++++++------------------ src/oscar/UI/ui_context.cpp | 33 ++++++++++++++++------------ 2 files changed, 29 insertions(+), 36 deletions(-) diff --git a/apps/hellotriangle/hellotriangle.cpp b/apps/hellotriangle/hellotriangle.cpp index cd8a96ade..73d525c9f 100644 --- a/apps/hellotriangle/hellotriangle.cpp +++ b/apps/hellotriangle/hellotriangle.cpp @@ -61,39 +61,22 @@ void main() private: void impl_on_mount() override { - // ui::context::init(); + ui::context::init(); } void impl_on_unmount() override { - // ui::context::shutdown(); + ui::context::shutdown(); } bool impl_on_event(const SDL_Event& e) override { - if (e.type == SDL_KEYDOWN) { - switch (e.key.keysym.sym) { - case SDLK_LEFT: - edited_torus_parameters_.torus_radius += 0.1f; - return true; - case SDLK_RIGHT: - edited_torus_parameters_.torus_radius -= 0.1f; - return true; - case SDLK_UP: - edited_torus_parameters_.tube_radius += 0.1f; - return true; - case SDLK_DOWN: - edited_torus_parameters_.tube_radius -= 0.1f; - return true; - } - } - - return false; // return ui::context::on_event(e); + return ui::context::on_event(e); } void impl_on_draw() override { - // ui::context::on_start_new_frame(); + ui::context::on_start_new_frame(); // ensure target texture matches screen dimensions { @@ -108,7 +91,12 @@ void main() camera_.render_to(target_texture_); graphics::blit_to_screen(target_texture_, Rect{{}, App::get().main_window_dimensions()}, gamma_correcter_); - // ui::context::render(); + ui::begin_panel("window"); + ui::draw_float_slider("torus_radius", &edited_torus_parameters_.torus_radius, 0.0f, 5.0f); + ui::draw_float_slider("tube_radius", &edited_torus_parameters_.tube_radius, 0.0f, 5.0f); + ui::end_panel(); + + ui::context::render(); } void update_torus_if_params_changed() diff --git a/src/oscar/UI/ui_context.cpp b/src/oscar/UI/ui_context.cpp index f91b5fa8f..77fe3eda7 100644 --- a/src/oscar/UI/ui_context.cpp +++ b/src/oscar/UI/ui_context.cpp @@ -25,8 +25,6 @@ namespace rgs = std::ranges; namespace { - constexpr auto c_icon_ranges = std::to_array({ ICON_MIN_FA, ICON_MAX_FA, 0 }); - // this is necessary because ImGui will take ownership, but will free the // font atlas with `free`, rather than `delete`, which memory sanitizers // like libASAN dislike (`malloc`/`free`, or `new`/`delete` - no mixes) @@ -40,6 +38,7 @@ namespace return ptr; } +#ifndef EMSCRIPTEN void add_resource_as_font( const ImFontConfig& config, ImFontAtlas& atlas, @@ -55,6 +54,7 @@ namespace glyph_ranges ); } +#endif } void osc::ui::context::init() @@ -70,18 +70,9 @@ void osc::ui::context::init() // load application-level ImGui settings, then the user one, // so that the user settings takes precedence - { - const std::string base_ini_data = App::slurp("imgui_base_config.ini"); - ImGui::LoadIniSettingsFromMemory(base_ini_data.data(), base_ini_data.size()); - - // CARE: the reason this filepath is `static` is because ImGui requires that - // the string outlives the ImGui context - static const std::string s_user_imgui_ini_file_path = (App::get().user_data_dir() / "imgui.ini").string(); - - ImGui::LoadIniSettingsFromDisk(s_user_imgui_ini_file_path.c_str()); - io.IniFilename = s_user_imgui_ini_file_path.c_str(); - } - +#ifdef EMSCRIPTEN + io.IniFilename = NULL; +#else float dpi_scale_factor = [&]() { float dpi{}; @@ -98,6 +89,18 @@ void osc::ui::context::init() return 1.0f; // else: assume it's an unscaled 96dpi screen }(); + { + const std::string base_ini_data = App::slurp("imgui_base_config.ini"); + ImGui::LoadIniSettingsFromMemory(base_ini_data.data(), base_ini_data.size()); + + // CARE: the reason this filepath is `static` is because ImGui requires that + // the string outlives the ImGui context + static const std::string s_user_imgui_ini_file_path = (App::get().user_data_dir() / "imgui.ini").string(); + + ImGui::LoadIniSettingsFromDisk(s_user_imgui_ini_file_path.c_str()); + io.IniFilename = s_user_imgui_ini_file_path.c_str(); + } + ImFontConfig base_config; base_config.SizePixels = dpi_scale_factor*15.0f; base_config.PixelSnapH = true; @@ -110,8 +113,10 @@ void osc::ui::context::init() config.MergeMode = true; config.GlyphMinAdvanceX = floor(1.5f * config.SizePixels); config.GlyphMaxAdvanceX = floor(1.5f * config.SizePixels); + static constexpr auto c_icon_ranges = std::to_array({ ICON_MIN_FA, ICON_MAX_FA, 0 }); add_resource_as_font(config, *io.Fonts, "oscar/fonts/fa-solid-900.ttf", c_icon_ranges.data()); } +#endif // init ImGui for SDL2 /w OpenGL ImGui_ImplSDL2_InitForOpenGL(