Skip to content

Commit a8e0a86

Browse files
FIXME: experimental imgui fixes
1 parent c74c3ed commit a8e0a86

5 files changed

Lines changed: 66 additions & 0 deletions

File tree

34.5 KB
Binary file not shown.

code/osapi/osapi.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,9 @@ namespace os
282282
* @note Implementation may ignore invocations of this function
283283
*/
284284
virtual void restore() = 0;
285+
286+
virtual float getCoordScale() { return 1.0f; }
287+
virtual float getScaled(float val) { return val; }
285288
};
286289

287290
/**

code/source_groups.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ add_file_folder("Default files\\\\data\\\\effects"
280280
def_files/data/effects/volumetric-f.sdr
281281
)
282282

283+
add_file_folder("Default files\\\\data\\\\fonts"
284+
def_files/data/fonts/BankGothic-BT-Light.ttf
285+
)
286+
283287
add_file_folder("Default files\\\\data\\\\maps"
284288
def_files/data/maps/app_icon.png
285289
def_files/data/maps/app_icon_d.png
@@ -315,6 +319,7 @@ add_file_folder("Default files\\\\builtin"
315319
set(default_files_files
316320
${files_Default_files_data}
317321
${files_Default_files_data_effects}
322+
${files_Default_files_data_fonts}
318323
${files_Default_files_data_maps}
319324
${files_Default_files_data_scripts}
320325
${files_Default_files_data_tables}

freespace2/SDLGraphicsOperations.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,24 @@ class SDLWindowViewPort: public os::Viewport {
8585
public:
8686
SDLWindowViewPort(SDL_Window* window, const os::ViewPortProperties& props) : _window(window), _props(props) {
8787
Assertion(window != nullptr, "Invalid window specified");
88+
89+
// scaling for high dpi stuff
90+
{
91+
int window_w, window_h;
92+
int framebuffer_w, framebuffer_h;
93+
94+
SDL_GetWindowSize(window, &window_w, &window_h);
95+
SDL_GetWindowSizeInPixels(window, &framebuffer_w, &framebuffer_h);
96+
97+
float sx = framebuffer_w / static_cast<float>(window_w);
98+
float sy = framebuffer_h / static_cast<float>(window_h);
99+
100+
m_coord_scale = std::max(sx, sy);
101+
m_content_scale = SDL_GetWindowDisplayScale(window);
102+
m_scale_factor = m_content_scale / m_coord_scale;
103+
104+
ImGui::GetStyle().ScaleAllSizes(m_scale_factor);
105+
}
88106
}
89107
~SDLWindowViewPort() override {
90108
SDL_DestroyWindow(_window);
@@ -136,6 +154,13 @@ class SDLWindowViewPort: public os::Viewport {
136154
void restore() override {
137155
SDL_RestoreWindow(_window);
138156
}
157+
158+
float m_coord_scale;
159+
float m_content_scale;
160+
float m_scale_factor;
161+
162+
float getCoordScale() override { return m_coord_scale; }
163+
float getScaled(float val) override { return val * m_scale_factor; }
139164
};
140165

141166
SDLGraphicsOperations::SDLGraphicsOperations() {

freespace2/freespace.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,6 +1820,8 @@ void game_init()
18201820
Random::seed(static_cast<unsigned int>(time(nullptr)));
18211821

18221822
ImGui::CreateContext();
1823+
ImGui::GetIO().IniFilename = nullptr;
1824+
ImGui::LoadIniSettingsFromDisk(os_get_config_path("imgui.ini").c_str());
18231825

18241826
Framerate_delay = 0;
18251827

@@ -1968,6 +1970,31 @@ void game_init()
19681970
return;
19691971
}
19701972

1973+
if ( !Is_standalone ) {
1974+
ImGuiIO &io = ImGui::GetIO(); (void)io;
1975+
1976+
io.Fonts->Clear();
1977+
ImFontConfig fontConfig;
1978+
1979+
fontConfig.RasterizerDensity = os::getMainViewport()->getCoordScale();;
1980+
1981+
auto file = cfopen("BankGothic-BT-Light.ttf", "rb", CF_TYPE_FONT);
1982+
1983+
if (file) {
1984+
const auto size = cfilelength(file);
1985+
1986+
// NOTE: ImGui will free this memory on shutdown
1987+
uint8_t *font = reinterpret_cast<uint8_t*>(IM_ALLOC(size * (sizeof(uint8_t))));
1988+
1989+
cfread(font, 1, size, file);
1990+
1991+
cfclose(file);
1992+
file = nullptr;
1993+
1994+
io.Fonts->AddFontFromMemoryTTF(font, size, os::getMainViewport()->getScaled(12.f), &fontConfig);
1995+
}
1996+
}
1997+
19711998
if (LoggingEnabled && Cmdline_debug_window) {
19721999
outwnd_debug_window_init();
19732000
}
@@ -7000,6 +7027,11 @@ int game_main(int argc, char *argv[])
70007027
break;
70017028
}
70027029

7030+
if (ImGui::GetIO().WantSaveIniSettings) {
7031+
ImGui::SaveIniSettingsToDisk(os_get_config_path("imgui.ini").c_str());
7032+
ImGui::GetIO().WantSaveIniSettings = false;
7033+
}
7034+
70037035
// Since tracing is always active this needs to happen in the main loop
70047036
tracing::process_events();
70057037
}
@@ -7110,6 +7142,7 @@ void game_shutdown(void)
71107142
std_deinit_standalone();
71117143
}
71127144

7145+
ImGui::SaveIniSettingsToDisk(os_get_config_path("imgui.ini").c_str());
71137146
ImGui::DestroyContext();
71147147

71157148
os_cleanup();

0 commit comments

Comments
 (0)