Skip to content

Commit

Permalink
Review OpenGlOptions: separate OpenGlOptions & OpenGlOptionsFilled_
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Jul 6, 2024
1 parent 473ca5a commit 8bf5074
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 134 deletions.
41 changes: 27 additions & 14 deletions src/hello_imgui/doc_params.md
Original file line number Diff line number Diff line change
Expand Up @@ -873,8 +873,9 @@ Source: [dpi_aware.h](https://github.com/pthom/hello_imgui/blob/master/src/hello

//
// Hello ImGui will try its best to automatically handle DPI scaling for you.
// It does this by setting two values:
//
// Parameters to change the scaling behavior:
// ------------------------------------------
// - `dpiWindowSizeFactor`:
// factor by which window size should be multiplied
//
Expand All @@ -885,22 +886,22 @@ Source: [dpi_aware.h](https://github.com/pthom/hello_imgui/blob/master/src/hello
// By default, Hello ImGui will compute them automatically,
// when dpiWindowSizeFactor and fontRenderingScale are set to 0.
//
// Parameters to improve font rendering quality:
// ---------------------------------------------
// - `fontOversampleH` and `fontOversampleV` : Font oversampling parameters
// Rasterize at higher quality for sub-pixel positioning. Probably unused if freeType is used.
// If not zero, these values will be used to set the oversampling factor when loading fonts.
//
//
// How to set those values manually:
// ---------------------------------
// If it fails (i.e. your window and/or fonts are too big or too small),
// you may set them manually:
// (1) Either by setting them programmatically in your application
// (set their values in `runnerParams.dpiAwareParams`)
// (2) Either by setting them in a `hello_imgui.ini` file in the current folder, or any of its parent folders.
// (this is useful when you want to set them for a specific app or set of apps, without modifying the app code)
// (2) Either by setting them in a `hello_imgui.ini` file. See hello_imgui/hello_imgui_example.ini for more info
// Note: if several methods are used, the order of priority is (1) > (2)
//
// Example content of a ini file:
// ------------------------------
// [DpiAwareParams]
// dpiWindowSizeFactor=2
// fontRenderingScale=0.5
//
// For more information, see the documentation on DPI handling, here: https://pthom.github.io/hello_imgui/book/doc_api.html#handling-screens-with-high-dpi
//
struct DpiAwareParams
Expand Down Expand Up @@ -934,10 +935,22 @@ struct DpiAwareParams
// (This parameter will be used to set ImGui::GetIO().FontGlobalScale at startup)
float fontRenderingScale = 0.0f;

// `onlyUseFontDpiResponsive`
// If true, guarantees that only HelloImGui::LoadDpiResponsiveFont will be used to load fonts.
// (also for the default font)
bool onlyUseFontDpiResponsive = false;
// `onlyUseFontDpiResponsive`
// If true, guarantees that only HelloImGui::LoadDpiResponsiveFont will be used to load fonts.
// (also for the default font)
bool onlyUseFontDpiResponsive = false;

// `fontOversampleH` and `fontOversampleV` : Font oversampling parameters
// Rasterize at higher quality for sub-pixel positioning. Probably unused if freeType is used.
// If not zero, these values will be used to set the oversampling factor when loading fonts.
// (i.e. they will be set in ImFontConfig::OversampleH and ImFontConfig::OversampleV)
// OversampleH: The difference between 2 and 3 for OversampleH is minimal.
// You can reduce this to 1 for large glyphs save memory.
// OversampleV: This is not really useful as we don't use sub-pixel positions on the Y axis.
// Read https://github.com/nothings/stb/blob/master/tests/oversample/README.md for details.
int fontOversampleH = 0; // Default is 2 in ImFontConfig
int fontOversampleV = 0; // Default is 1 in ImFontConfig


// `dpiFontLoadingFactor`
// factor by which font size should be multiplied at loading time to get a similar
Expand Down Expand Up @@ -1368,7 +1381,7 @@ struct RendererBackendOptions

// `openGlOptions`:
// Advanced options for OpenGL. Use at your own risk.
std::optional<OpenGlOptions> openGlOptions = std::nullopt;
OpenGlOptions openGlOptions;
};


Expand Down
61 changes: 0 additions & 61 deletions src/hello_imgui/internal/backend_impls/abstract_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,63 +234,6 @@ bool AbstractRunner::ShallSizeWindowRelativeTo96Ppi()
return shallSizeRelativeTo96Ppi;
}

void AbstractRunner::ReadOpenGlOptions()
{
// cf renderer_backend_options.h:
//
// OpenGlOptions contains advanced options used at the startup of OpenGL.
// These parameters are reserved for advanced users.
// By default, Hello ImGui will select reasonable default values, and these parameters are not used.
// Use at your own risk, as they make break the multi-platform compatibility of your application!
// All these parameters are platform dependent.
// For real multiplatform examples, see
// hello_imgui/src/hello_imgui/internal/backend_impls/opengl_setup_helper/opengl_setup_glfw.cpp
// and
// hello_imgui/src/hello_imgui/internal/backend_impls/opengl_setup_helper/opengl_setup_sdl.cpp
//
// How to set those values manually:
// ---------------------------------
// you may set them manually:
// (1) Either by setting them programmatically in your application
// (set their values in `runnerParams.rendererBackendOptions.openGlOptions`)
// (2) Either by setting them in a `hello_imgui.ini` file. See hello_imgui/hello_imgui_example.ini for more info
// Note: if several methods are used, the order of priority is (1) > (2)

// If options specified by the program, do not read them.
if (params.rendererBackendOptions.openGlOptions.has_value())
return;

std::string openGlSection = "OpenGlOptions";

auto majorVersion = HelloImGuiIniAnyParentFolder::readIntValue(openGlSection, "MajorVersion");
auto minorVersion = HelloImGuiIniAnyParentFolder::readIntValue(openGlSection, "MinorVersion");
auto useCoreProfile = HelloImGuiIniAnyParentFolder::readBoolValue(openGlSection, "UseCoreProfile");
auto useForwardCompat = HelloImGuiIniAnyParentFolder::readBoolValue(openGlSection, "UseForwardCompat");
auto glslVersion = HelloImGuiIniAnyParentFolder::readStringValue(openGlSection, "GlslVersion");
auto antiAliasingSamples = HelloImGuiIniAnyParentFolder::readIntValue(openGlSection, "AntiAliasingSamples");

bool isAnySettingsSpecified = majorVersion.has_value() || minorVersion.has_value() || useCoreProfile.has_value() || useForwardCompat.has_value() || glslVersion.has_value() || antiAliasingSamples.has_value();

if (isAnySettingsSpecified)
{
params.rendererBackendOptions.openGlOptions = OpenGlOptions();
auto& openGlOptions = params.rendererBackendOptions.openGlOptions.value();
if (majorVersion.has_value())
openGlOptions.MajorVersion = majorVersion.value();
if (minorVersion.has_value())
openGlOptions.MinorVersion = minorVersion.value();
if (useCoreProfile.has_value())
openGlOptions.UseCoreProfile = useCoreProfile.value();
if (useForwardCompat.has_value())
openGlOptions.UseForwardCompat = useForwardCompat.value();
if (glslVersion.has_value())
openGlOptions.GlslVersion = glslVersion.value();
if (antiAliasingSamples.has_value())
openGlOptions.AntiAliasingSamples = antiAliasingSamples.value();
}

}

void ReadDpiAwareParams(DpiAwareParams* dpiAwareParams)
{
// cf dpi_aware.h
Expand Down Expand Up @@ -729,10 +672,6 @@ void AbstractRunner::Setup()
InitImGuiContext();
SetImGuiPrefs();

#ifdef HELLOIMGUI_HAS_OPENGL
ReadOpenGlOptions();
#endif

// Init platform backend (SDL, Glfw)
Impl_InitPlatformBackend();

Expand Down
1 change: 0 additions & 1 deletion src/hello_imgui/internal/backend_impls/abstract_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ class AbstractRunner
void SetImGuiPrefs();
void InitRenderBackendCallbacks();

void ReadOpenGlOptions();
void SetupDpiAwareParams();
bool CheckDpiAwareParamsChanges();
void PrepareWindowGeometry();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,94 @@
#include "opengl_setup_api.h"
#include "hello_imgui/hello_imgui_include_opengl.h"
#include "hello_imgui/hello_imgui.h"
#include "hello_imgui/internal/hello_imgui_ini_any_parent_folder.h"

#include <string>


namespace HelloImGui { namespace BackendApi
{
void ModifyOpenGlOptionsFromUserSettingsInParams(OpenGlOptionsFilled_ *openGlOptions)
{
OpenGlOptions& userOptions =HelloImGui::GetRunnerParams()->rendererBackendOptions.openGlOptions;

if (userOptions.MajorVersion.has_value())
openGlOptions->MajorVersion = userOptions.MajorVersion.value();
if (userOptions.MinorVersion.has_value())
openGlOptions->MinorVersion = userOptions.MinorVersion.value();
if (userOptions.UseCoreProfile.has_value())
openGlOptions->UseCoreProfile = userOptions.UseCoreProfile.value();
if (userOptions.UseForwardCompat.has_value())
openGlOptions->UseForwardCompat = userOptions.UseForwardCompat.value();
if (userOptions.AntiAliasingSamples.has_value())
openGlOptions->AntiAliasingSamples = userOptions.AntiAliasingSamples.value();
}


void ModifyOpenGlOptionsFromUserSettingsInHelloImGuiIniFile(OpenGlOptionsFilled_ *openGlOptions)
{
// cf renderer_backend_options.h:
//
// OpenGlOptions contains advanced options used at the startup of OpenGL.
// These parameters are reserved for advanced users.
// By default, Hello ImGui will select reasonable default values, and these parameters are not used.
// Use at your own risk, as they make break the multi-platform compatibility of your application!
// All these parameters are platform dependent.
// For real multiplatform examples, see
// hello_imgui/src/hello_imgui/internal/backend_impls/opengl_setup_helper/opengl_setup_glfw.cpp
// and
// hello_imgui/src/hello_imgui/internal/backend_impls/opengl_setup_helper/opengl_setup_sdl.cpp
//
// How to set those values manually:
// ---------------------------------
// you may set them manually:
// (1) Either by setting them programmatically in your application
// (set their values in `runnerParams.rendererBackendOptions.openGlOptions`)
// (2) Either by setting them in a `hello_imgui.ini` file. See hello_imgui/hello_imgui_example.ini for more info
// Note: if several methods are used, the order of priority is (1) > (2)

std::string openGlSection = "OpenGlOptions";

auto majorVersion = HelloImGuiIniAnyParentFolder::readIntValue(openGlSection, "MajorVersion");
if (majorVersion.has_value())
openGlOptions->MajorVersion = majorVersion.value();

auto minorVersion = HelloImGuiIniAnyParentFolder::readIntValue(openGlSection, "MinorVersion");
if (minorVersion.has_value())
openGlOptions->MinorVersion = minorVersion.value();

auto useCoreProfile = HelloImGuiIniAnyParentFolder::readBoolValue(openGlSection, "UseCoreProfile");
if (useCoreProfile.has_value())
openGlOptions->UseCoreProfile = useCoreProfile.value();

auto useForwardCompat = HelloImGuiIniAnyParentFolder::readBoolValue(openGlSection, "UseForwardCompat");
if (useForwardCompat.has_value())
openGlOptions->UseForwardCompat = useForwardCompat.value();

auto glslVersion = HelloImGuiIniAnyParentFolder::readStringValue(openGlSection, "GlslVersion");
if (glslVersion.has_value())
openGlOptions->GlslVersion = glslVersion.value();

auto antiAliasingSamples = HelloImGuiIniAnyParentFolder::readIntValue(openGlSection, "AntiAliasingSamples");
if (antiAliasingSamples.has_value())
openGlOptions->AntiAliasingSamples = antiAliasingSamples.value();
}

OpenGlOptionsFilled_ IOpenGlSetup::OpenGlOptionsWithUserSettings()
{
// We compute this only once
static OpenGlOptionsFilled_ openGlOptions;
static bool wasFilled = false;
if (! wasFilled)
{
openGlOptions = Impl_MakeDefaultOpenGlOptionsForPlatform();
ModifyOpenGlOptionsFromUserSettingsInHelloImGuiIniFile(&openGlOptions);
ModifyOpenGlOptionsFromUserSettingsInParams(&openGlOptions);
wasFilled = true;
}

OpenGlOptionsFilled_ r = openGlOptions;
return r;
}

}} // namespace HelloImGui { namespace BackendApi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "hello_imgui/screen_bounds.h"
#include "hello_imgui/renderer_backend_options.h"

#include <string>

Expand All @@ -12,8 +13,10 @@ namespace HelloImGui { namespace BackendApi
IOpenGlSetup() {}
virtual ~IOpenGlSetup() {}

virtual void SelectOpenGlVersion() = 0;
virtual void SelectOpenGlVersion(const OpenGlOptionsFilled_& options) = 0;
virtual void InitGlLoader() = 0;
virtual std::string GlslVersion() = 0;
virtual OpenGlOptionsFilled_ Impl_MakeDefaultOpenGlOptionsForPlatform() = 0;

OpenGlOptionsFilled_ OpenGlOptionsWithUserSettings();
};
}} // namespace HelloImGui { namespace BackendApi
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace HelloImGui { namespace BackendApi
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
return glfwCreateWindow(64, 32, "Dummy", NULL, NULL);
return glfwCreateWindow(64, 32, "Dummy", nullptr, nullptr);
}();

if (!dummyWindow)
Expand All @@ -38,7 +38,7 @@ namespace HelloImGui { namespace BackendApi
return maxSamples;
}

static void ApplyAntiAliasingSamples(OpenGlOptions& openGlOptions)
static void ApplyAntiAliasingSamples(const OpenGlOptionsFilled_& openGlOptions)
{
int userQuerySamples = openGlOptions.AntiAliasingSamples;
int maxGpuSamples = QueryMaxAntiAliasingSamples();
Expand Down Expand Up @@ -69,7 +69,7 @@ namespace HelloImGui { namespace BackendApi
}
#endif // #ifdef HELLOIMGUI_HAS_OPENGL3

static void ApplyOpenGlOptions(OpenGlOptions& openGlOptions)
static void ApplyOpenGlOptions(const OpenGlOptionsFilled_& openGlOptions)
{
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, openGlOptions.MajorVersion);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, openGlOptions.MinorVersion);
Expand All @@ -80,18 +80,14 @@ namespace HelloImGui { namespace BackendApi

#ifdef HELLOIMGUI_HAS_OPENGL3
ApplyAntiAliasingSamples(openGlOptions);
#endif // #ifdef HELLOIMGUI_HAS_OPENGL3
#endif
}

static OpenGlOptions MakeOpenGlOptions()
OpenGlOptionsFilled_ OpenGlSetupGlfw::Impl_MakeDefaultOpenGlOptionsForPlatform()
{
auto* runnerParams = HelloImGui::GetRunnerParams();
if (runnerParams->rendererBackendOptions.openGlOptions.has_value())
return runnerParams->rendererBackendOptions.openGlOptions.value();

OpenGlOptions openGlOptions;
OpenGlOptionsFilled_ openGlOptions;

openGlOptions.AntiAliasingSamples = -1; // -1 <=> not set, will use the default value of 8
openGlOptions.AntiAliasingSamples = 8;

//
// Compute MajorVersion, MinorVersion, UseCoreProfile, UseForwardCompat
Expand Down Expand Up @@ -137,10 +133,9 @@ namespace HelloImGui { namespace BackendApi
return openGlOptions;
}

void OpenGlSetupGlfw::SelectOpenGlVersion()
void OpenGlSetupGlfw::SelectOpenGlVersion(const OpenGlOptionsFilled_& options)
{
OpenGlOptions openGlOptions = MakeOpenGlOptions();
ApplyOpenGlOptions(openGlOptions);
ApplyOpenGlOptions(options);
}

void OpenGlSetupGlfw::InitGlLoader()
Expand All @@ -163,12 +158,6 @@ namespace HelloImGui { namespace BackendApi
}
#endif // #ifndef __EMSCRIPTEN__
}

std::string OpenGlSetupGlfw::GlslVersion()
{
OpenGlOptions openGlOptions = MakeOpenGlOptions();
return std::string("#version ") + openGlOptions.GlslVersion;
}
}} // namespace HelloImGui { namespace BackendApi

#endif // #ifdef HELLOIMGUI_USE_GLFW3
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace HelloImGui { namespace BackendApi
public:
virtual ~OpenGlSetupGlfw() {}

void SelectOpenGlVersion() override;
void SelectOpenGlVersion(const OpenGlOptionsFilled_& options) override;
void InitGlLoader() override;
std::string GlslVersion() override;
OpenGlOptionsFilled_ Impl_MakeDefaultOpenGlOptionsForPlatform() override;
};
}} // namespace HelloImGui { namespace BackendApi

Expand Down
Loading

0 comments on commit 8bf5074

Please sign in to comment.