Skip to content

Commit

Permalink
Hackily add 2D UI support for emscripten hellotriangle demo
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkewley committed Jul 4, 2024
1 parent 4fd7702 commit ee4b274
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 36 deletions.
32 changes: 10 additions & 22 deletions apps/hellotriangle/hellotriangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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()
Expand Down
33 changes: 19 additions & 14 deletions src/oscar/UI/ui_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ namespace rgs = std::ranges;

namespace
{
constexpr auto c_icon_ranges = std::to_array<ImWchar>({ 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)
Expand All @@ -40,6 +38,7 @@ namespace
return ptr;
}

#ifndef EMSCRIPTEN
void add_resource_as_font(
const ImFontConfig& config,
ImFontAtlas& atlas,
Expand All @@ -55,6 +54,7 @@ namespace
glyph_ranges
);
}
#endif
}

void osc::ui::context::init()
Expand All @@ -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{};
Expand All @@ -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;
Expand All @@ -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<ImWchar>({ 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(
Expand Down

0 comments on commit ee4b274

Please sign in to comment.