Skip to content

Commit

Permalink
- quick and dirty dpi awareness
Browse files Browse the repository at this point in the history
- scale imgui with contentScale
  • Loading branch information
kaffeewolf committed Mar 2, 2024
1 parent 61402b6 commit 2de7a68
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
11 changes: 7 additions & 4 deletions code/addons/dynui/imguicontext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ ImguiContext::Create()
Ptr<DisplayDevice> display = DisplayDevice::Instance();
DisplayMode mode = CoreGraphics::WindowGetDisplayMode(display->GetCurrentWindow());

float scaleFactor = mode.GetContentScale();
// setup Imgui
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO();
Expand Down Expand Up @@ -437,6 +438,8 @@ ImguiContext::Create()
style.WindowBorderSize = 1.0f;
style.PopupBorderSize = 0.0f;
style.ChildBorderSize = 0.0f;

style.ScaleAllSizes(scaleFactor);

ImVec4 nebulaOrange(1.0f, 0.30f, 0.0f, 1.0f);
ImVec4 nebulaOrangeActive(0.9f, 0.20f, 0.05f, 1.0f);
Expand Down Expand Up @@ -524,11 +527,11 @@ ImguiContext::Create()
config.OversampleH = 3;
config.OversampleV = 1;
#if __WIN32__
state.normalFont = io.Fonts->AddFontFromFileTTF("c:/windows/fonts/calibri.ttf", 14, &config);
state.smallFont = io.Fonts->AddFontFromFileTTF("c:/windows/fonts/calibri.ttf", 12, &config);
state.normalFont = io.Fonts->AddFontFromFileTTF("c:/windows/fonts/calibri.ttf", scaleFactor * 14, &config);
state.smallFont = io.Fonts->AddFontFromFileTTF("c:/windows/fonts/calibri.ttf", scaleFactor * 12, &config);
#else
state.normalFont = io.Fonts->AddFontFromFileTTF("/usr/share/fonts/truetype/freefont/FreeSans.ttf", 14, &config);
state.smallFont = io.Fonts->AddFontFromFileTTF("/usr/share/fonts/truetype/freefont/FreeSans.ttf", 12, &config);
state.normalFont = io.Fonts->AddFontFromFileTTF("/usr/share/fonts/truetype/freefont/FreeSans.ttf", scaleFactor * 14, &config);
state.smallFont = io.Fonts->AddFontFromFileTTF("/usr/share/fonts/truetype/freefont/FreeSans.ttf", scaleFactor * 12, &config);
#endif

unsigned char* buffer;
Expand Down
2 changes: 1 addition & 1 deletion code/application/appgame/gameapplication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ GameApplication::StepFrame()
void
GameApplication::SetupGameFeatures()
{

// create any features in derived class
}

//------------------------------------------------------------------------------
Expand Down
30 changes: 29 additions & 1 deletion code/render/coregraphics/displaymode.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class DisplayMode
void SetRefreshRate( uint refreshRate );
/// gives you the refresh rate
uint GetRefreshRate() const;
/// set content scale factor
void SetContentScale(float s);
/// get content scale factor
float GetContentScale() const;


private:
Expand All @@ -69,6 +73,7 @@ class DisplayMode
SizeT height;
uint refreshRate;
float aspectRatio;
float contentScale;
PixelFormat::Code pixelFormat;

};
Expand All @@ -84,6 +89,7 @@ DisplayMode::DisplayMode() :
height(768),
refreshRate(60),
aspectRatio(4.0f / 3.0f),
contentScale(1.0f),
pixelFormat(PixelFormat::SRGBA8)
{
// empty
Expand All @@ -101,6 +107,7 @@ DisplayMode::DisplayMode(uint x, uint y, SizeT w, SizeT h) :
height(h),
refreshRate(60),
aspectRatio(float(w) / float(h)),
contentScale(1.0f),
pixelFormat(PixelFormat::SRGBA8)
{
// empty
Expand All @@ -118,6 +125,7 @@ DisplayMode::DisplayMode(SizeT w, SizeT h, PixelFormat::Code p) :
height(h),
refreshRate(60),
aspectRatio(float(w) / float(h)),
contentScale(1.0f),
pixelFormat(p)
{
// empty
Expand All @@ -134,6 +142,7 @@ DisplayMode::DisplayMode(uint x, uint y, SizeT w, SizeT h, PixelFormat::Code p)
height(h),
refreshRate(60),
aspectRatio(float(w) / float(h)),
contentScale(1.0f),
pixelFormat(p)
{
// empty
Expand All @@ -150,7 +159,8 @@ DisplayMode::operator==(const DisplayMode& rhs) const
(this->width == rhs.width) &&
(this->height == rhs.height) &&
(this->refreshRate == rhs.refreshRate) &&
(this->aspectRatio == rhs.aspectRatio) &&
(this->aspectRatio == rhs.aspectRatio) &&
(this->contentScale == rhs.contentScale) &&
(this->pixelFormat == rhs.pixelFormat));
}

Expand Down Expand Up @@ -271,6 +281,24 @@ DisplayMode::GetAspectRatio() const
return this->aspectRatio;
}

//------------------------------------------------------------------------------
/**
*/
inline void
DisplayMode::SetContentScale(float a)
{
this->contentScale = a;
}

//------------------------------------------------------------------------------
/**
*/
inline float
DisplayMode::GetContentScale() const
{
return this->contentScale;
}

//------------------------------------------------------------------------------
/**
*/
Expand Down
9 changes: 8 additions & 1 deletion code/render/coregraphics/glfw/glfwwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,14 @@ InternalSetupFunction(const WindowCreateInfo& info, const Util::Blob& windowData
// if Vulkan, context is created and managed by render device
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
#endif
// scale window according to platform dpi
glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_TRUE);
float xScale, yScale;
glfwGetMonitorContentScale(monitor, &xScale, &yScale);
float contentScale = Math::max(xScale, yScale);
mode.SetWidth(info.mode.GetWidth() * contentScale);
mode.SetHeight(info.mode.GetHeight() * contentScale);
mode.SetContentScale(contentScale);

// set user pointer to this window
WindowId* ptr = new WindowId;
Expand Down Expand Up @@ -409,7 +417,6 @@ InternalSetupFunction(const WindowCreateInfo& info, const Util::Blob& windowData
#endif

glfwWindowAllocator.Get<GLFW_Window>(windowId) = wnd;
glfwWindowAllocator.Get<GLFW_DisplayMode>(windowId) = info.mode;

// notify window is opened
GLFW::GLFWDisplayDevice::Instance()->NotifyEventHandlers(DisplayEvent(DisplayEvent::WindowOpen, id));
Expand Down
2 changes: 2 additions & 0 deletions code/render/coregraphics/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,7 @@ const Util::StringAtom& WindowGetTitle(const WindowId id);
const Util::StringAtom& WindowGetIcon(const WindowId id);
/// get render texture associated with window
const CoreGraphics::TextureId& WindowGetTexture(const WindowId id);
/// retrieve window content scaling (ratio between current DPI and platform default DPI)
Math::vec2 WindowGetContentScale(const WindowId id);

} // CoreGraphics

0 comments on commit 2de7a68

Please sign in to comment.