Skip to content

Commit

Permalink
[gui] Add option to save nuklear font texture
Browse files Browse the repository at this point in the history
  • Loading branch information
itszn committed Jun 19, 2021
1 parent 63fe54a commit 164309d
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Main/include/GameConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ DefineEnum(GameConfigKeys,
SettingsLastTab,
TransferScoresOnChartUpdate,

KeepFontTexture,

CurrentProfileName,

// Gameplay options
Expand Down
6 changes: 4 additions & 2 deletions Main/src/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1013,8 +1013,10 @@ bool Application::m_Init()
g_transition = TransitionScreen::Create();
}

BasicNuklearGui::StartFontInit();
m_fontBakeThread = Thread(BasicNuklearGui::BakeFontWithLock);
if (g_gameConfig.GetBool(GameConfigKeys::KeepFontTexture)) {
BasicNuklearGui::StartFontInit();
m_fontBakeThread = Thread(BasicNuklearGui::BakeFontWithLock);
}

///TODO: check if directory exists already?
Path::CreateDir(Path::Absolute("screenshots"));
Expand Down
6 changes: 6 additions & 0 deletions Main/src/GameConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ void GameConfig::InitDefaults()

Set(GameConfigKeys::CurrentProfileName, "Main");
Set(GameConfigKeys::UpdateChannel, "master");

#ifndef EMBEDDED
Set(GameConfigKeys::KeepFontTexture, true);
#else
Set(GameConfigKeys::KeepFontTexture, false);
#endif
}

void GameConfig::UpdateVersion()
Expand Down
21 changes: 19 additions & 2 deletions Main/src/GuiUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ void BasicNuklearGui::ShutdownNuklear()
g_gameWindow->OnAnyEvent.RemoveAll(this);
nk_sdl_shutdown_keep_font();

if (!g_gameConfig.GetBool(GameConfigKeys::KeepFontTexture)) {
glDeleteTextures(1, &s_fontTexture);
s_fontTexture = 0;
s_hasFontTexture = false;
}

m_nuklearRunning = false;
}

Expand Down Expand Up @@ -113,7 +119,18 @@ void BasicNuklearGui::BakeFont()

void BasicNuklearGui::DestroyFont()
{
glDeleteTextures(1, &s_fontTexture);
if (s_hasFontTexture)
{
glDeleteTextures(1, &s_fontTexture);
s_hasFontTexture = false;
}
if (s_atlas)
{
nk_font_atlas_clear(s_atlas);
delete s_atlas;
s_atlas = nullptr;
s_font = nullptr;
}
}

void BasicNuklearGui::InitNuklearFontAtlas()
Expand All @@ -128,7 +145,7 @@ void BasicNuklearGui::InitNuklearFontAtlas()
if (!s_hasFontTexture && s_atlas->pixel == nullptr)
{
// Our thread didn't work
Log("Failed to bake font in thread, trying again on main thread", Logger::Severity::Warning);
Log("Baking nuklear font on main thread", Logger::Severity::Warning);
BakeFont();
}

Expand Down
1 change: 1 addition & 0 deletions Main/src/SettingsScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ class SettingsPage_System : public SettingsPage
SelectionSetting(GameConfigKeys::AntiAliasing, m_aaModes, "Anti-aliasing (requires restart):");
SetApply(ToggleSetting(GameConfigKeys::VSync, "VSync"));
SetApply(ToggleSetting(GameConfigKeys::ShowFps, "Show FPS"));
SetApply(ToggleSetting(GameConfigKeys::KeepFontTexture, "Save font texture (settings load faster but uses more memory)"));

SectionHeader("Update");

Expand Down
10 changes: 0 additions & 10 deletions Main/stdafx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,16 +371,6 @@ void usc_nk_sdl_use_atlas(nk_font_atlas* atlas, GLuint texture)
nk_style_set_font(&sdl.ctx, &sdl.atlas.default_font->handle);
}

NK_API void
nk_sdl_device_upload_pregenerated_atlas(GLuint texture, const void* image, int width, int height)
{
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLsizei)width, (GLsizei)height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, image);
}

NK_API void
nk_sdl_device_destroy_keep_font(void)
{
Expand Down
1 change: 0 additions & 1 deletion Main/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ void usc_nk_sdl_font_stash_end();
const void* usc_nk_bake_atlas(nk_font_atlas * atlas, int& w, int& h);
GLuint usc_nk_sdl_generate_texture(nk_font_atlas * atlas, const void* image, int w, int h);
void usc_nk_sdl_use_atlas(nk_font_atlas * atlas, GLuint texture);
void nk_sdl_device_upload_pregenerated_atlas(GLuint texture, const void* image, int width, int height);
void nk_sdl_device_destroy_keep_font(void);
void nk_sdl_shutdown_keep_font(void);
void nk_atlas_font_stash_begin(struct nk_font_atlas* atlas);

0 comments on commit 164309d

Please sign in to comment.