Skip to content

Commit

Permalink
ImGuiIntegration: test text rendering.
Browse files Browse the repository at this point in the history
Mainly to test that font atlas upload and sampling works, especially
when using single-channel format + swizzling on OpenGL and GLES3
  • Loading branch information
pezcode committed Nov 22, 2024
1 parent a0c029e commit 978dda8
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Magnum/ImGuiIntegration/Test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# 2020, 2021, 2022, 2023, 2024
# Vladimír Vondruš <[email protected]>
# Copyright © 2018 Jonathan Hale <[email protected]>
# Copyright © 2022 Pablo Escobar <[email protected]>
# Copyright © 2022, 2024 Pablo Escobar <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
Expand Down Expand Up @@ -63,6 +63,7 @@ if(MAGNUM_BUILD_GL_TESTS)
ContextTestFiles/draw.png
ContextTestFiles/draw-scissor.png
ContextTestFiles/draw-texture.png
ContextTestFiles/draw-text.png
ContextTestFiles/texture.png)
target_include_directories(ImGuiContextGLTest PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
if(MAGNUM_IMGUIINTEGRATION_BUILD_STATIC)
Expand Down
47 changes: 47 additions & 0 deletions src/Magnum/ImGuiIntegration/Test/ContextGLTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ struct ContextGLTest: GL::OpenGLTester {
void draw();
void drawCallback();
void drawTexture();
void drawText();
void drawScissor();
void drawVertexOffset();
void drawIndexOffset();
Expand Down Expand Up @@ -313,6 +314,7 @@ ContextGLTest::ContextGLTest() {
addTests({&ContextGLTest::draw,
&ContextGLTest::drawCallback,
&ContextGLTest::drawTexture,
&ContextGLTest::drawText,
&ContextGLTest::drawScissor,
&ContextGLTest::drawVertexOffset,
&ContextGLTest::drawIndexOffset},
Expand Down Expand Up @@ -1397,6 +1399,51 @@ void ContextGLTest::drawTexture() {
(DebugTools::CompareImageToFile{_manager, 1.0f, 0.5f}));
}

void ContextGLTest::drawText() {
Context c{{200, 200}};

/* Scale up default font so large text output is not a complete blurry mess */
ImGui::GetIO().Fonts->Clear();
auto* font = ImGui::GetIO().Fonts->AddFontDefault();
font->Scale = 8.0f;

c.relayout({200, 200}, {70, 70}, _framebuffer.viewport().size());

/* ImGui doesn't draw anything the first frame */
c.newFrame();
c.drawFrame();

MAGNUM_VERIFY_NO_GL_ERROR();

Utility::System::sleep(1);

c.newFrame();

/* Last drawlist that gets rendered, covers the entire display */
ImDrawList* drawList = ImGui::GetForegroundDrawList();
const ImVec2& size = ImGui::GetIO().DisplaySize;

drawList->AddRectFilled({size.x*0.1f, size.y*0.2f}, {size.x*0.9f, size.y*0.8f},
IM_COL32(255, 128, 128, 255));
drawList->AddText(nullptr, font->FontSize*font->Scale,
{size.x*0.3f, size.y*0.3f}, IM_COL32(255, 255, 0, 200), "ABC");

c.drawFrame();

MAGNUM_VERIFY_NO_GL_ERROR();

/* Catch also ABI and interface mismatch errors */
if(!(_manager.load("AnyImageImporter") & PluginManager::LoadState::Loaded) ||
!(_manager.load("PngImporter") & PluginManager::LoadState::Loaded))
CORRADE_SKIP("AnyImageImporter / PngImporter plugin can't be loaded.");

CORRADE_COMPARE_WITH(
/* Dropping the alpha channel, as it's always 1.0 */
Containers::arrayCast<Color3ub>(_framebuffer.read(_framebuffer.viewport(), {PixelFormat::RGBA8Unorm}).pixels<Color4ub>()),
Utility::Path::join(IMGUIINTEGRATION_TEST_DIR, "ContextTestFiles/draw-text.png"),
(DebugTools::CompareImageToFile{_manager, 1.0f, 0.5f}));
}

void ContextGLTest::drawScissor() {
Context c{{200, 200}, {70, 70}, _framebuffer.viewport().size()};

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 978dda8

Please sign in to comment.