ImGui: Add TTF font support

We use the Roboto font that we're already shipping for now, although, we could
also support other fonts or have a setting.
This commit is contained in:
Henrik Rydgård 2024-11-22 13:54:19 +01:00
parent 32d8b12d47
commit 6dfc5ea9ef
5 changed files with 26 additions and 6 deletions

View file

@ -27,7 +27,7 @@ using namespace std::placeholders;
#include "Common/Render/Text/draw_text.h"
#include "Common/File/FileUtil.h"
#include "Common/Battery/Battery.h"
#include "Common/File/VFS/VFS.h"
#include "Common/UI/Root.h"
#include "Common/UI/UI.h"
#include "Common/UI/Context.h"
@ -1646,7 +1646,13 @@ void EmuScreen::renderImDebugger() {
if (!imguiInited_) {
imguiInited_ = true;
imDebugger_ = std::make_unique<ImDebugger>();
ImGui_ImplThin3d_Init(draw);
// Read the TTF font
size_t size = 0;
uint8_t *fontData = g_VFS.ReadFile("Roboto-Condensed.ttf", &size);
// This call works even if fontData is nullptr, in which case the font just won't get loaded.
// This takes ownership of the font array.
ImGui_ImplThin3d_Init(draw, fontData, size);
}
if (PSP_IsInited()) {

View file

@ -399,7 +399,8 @@ void ImDebugger::Frame(MIPSDebugInterface *mipsDebug, GPUDebugInterface *gpuDebu
if (ImGui::MenuItem("Close Debugger")) {
g_Config.bShowImDebugger = false;
}
ImGui::MenuItem("Dear ImGUI Demo", nullptr, &cfg_.demoOpen);
ImGui::MenuItem("Dear ImGui Demo", nullptr, &cfg_.demoOpen);
ImGui::MenuItem("Dear ImGui Style editor", nullptr, &cfg_.styleEditorOpen);
ImGui::EndMenu();
}
ImGui::EndMainMenuBar();
@ -409,6 +410,10 @@ void ImDebugger::Frame(MIPSDebugInterface *mipsDebug, GPUDebugInterface *gpuDebu
ImGui::ShowDemoWindow(&cfg_.demoOpen);
}
if (cfg_.styleEditorOpen) {
ImGui::ShowStyleEditor();
}
if (cfg_.disasmOpen) {
disasm_.Draw(mipsDebug, &cfg_.disasmOpen, coreState);
}

View file

@ -65,6 +65,7 @@ struct ImConfig {
bool atracOpen = true;
bool structViewerOpen = false;
bool framebuffersOpen = false;
bool styleEditorOpen = false;
// HLE explorer settings
// bool filterByUsed = true;

View file

@ -217,8 +217,16 @@ void ImGui_ImplThin3d_DestroyDeviceObjects() {
}
}
bool ImGui_ImplThin3d_Init(Draw::DrawContext *draw) {
bool ImGui_ImplThin3d_Init(Draw::DrawContext *draw, const uint8_t *ttf_font, size_t size) {
ImGuiIO& io = ImGui::GetIO();
if (ttf_font) {
io.Fonts->AddFontFromMemoryTTF((void *)ttf_font, size, 18.0f * g_display.dpi_scale_x, nullptr, io.Fonts->GetGlyphRangesDefault());
} else {
// necessary?
io.Fonts->AddFontDefault();
}
ImGui::GetStyle().ScaleAllSizes(g_display.dpi_scale_x);
IMGUI_CHECKVERSION();
IM_ASSERT(io.BackendRendererUserData == nullptr && "Already initialized a renderer backend!");

View file

@ -33,8 +33,8 @@
#include "Common/GPU/thin3d.h"
#include "Common/Math/lin/matrix4x4.h"
// Called by user code
IMGUI_IMPL_API bool ImGui_ImplThin3d_Init(Draw::DrawContext *draw);
// Called by user code. Takes ownership of the font buffer and later deletes it.
IMGUI_IMPL_API bool ImGui_ImplThin3d_Init(Draw::DrawContext *draw, const uint8_t *ttf_font, size_t size);
IMGUI_IMPL_API void ImGui_ImplThin3d_Shutdown();
IMGUI_IMPL_API void ImGui_ImplThin3d_NewFrame(Draw::DrawContext *draw, Lin::Matrix4x4 drawMatrix);
IMGUI_IMPL_API void ImGui_ImplThin3d_RenderDrawData(ImDrawData* draw_data, Draw::DrawContext *draw);