CommonHostInterface: Move fonts/logo to resources directory

This commit is contained in:
Connor McLaughlin 2021-08-09 23:56:17 +10:00
parent c4f0dafb1f
commit 4f190aa902
11 changed files with 108 additions and 10098 deletions

Binary file not shown.

View file

@ -556,10 +556,40 @@ bool CommonHostInterface::CreateHostDisplayResources()
if (!m_display->CreateImGuiContext()) if (!m_display->CreateImGuiContext())
{ {
Log_ErrorPrintf("Failed to create ImGui device context"); ReportError("Failed to create ImGui device context");
return false; return false;
} }
// load text font
{
std::unique_ptr<ByteStream> stream = OpenPackageFile("resources" FS_OSPATH_SEPARATOR_STR "roboto-regular.ttf",
BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
std::vector<u8> font_data;
if (!stream || (font_data = FileSystem::ReadBinaryStream(stream.get()), font_data.empty()))
{
ReportError("Failed to load text font");
m_display->DestroyImGuiContext();
return false;
}
ImGuiFullscreen::SetFontData(std::move(font_data));
}
// load icon font
{
std::unique_ptr<ByteStream> stream = OpenPackageFile("resources" FS_OSPATH_SEPARATOR_STR "fa-solid-900.ttf",
BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
std::vector<u8> font_data;
if (!stream || (font_data = FileSystem::ReadBinaryStream(stream.get()), font_data.empty()))
{
ReportError("Failed to load icon font");
m_display->DestroyImGuiContext();
return false;
}
ImGuiFullscreen::SetIconFontData(std::move(font_data));
}
if (m_fullscreen_ui_enabled) if (m_fullscreen_ui_enabled)
{ {
if (!FullscreenUI::Initialize(this)) if (!FullscreenUI::Initialize(this))
@ -572,9 +602,7 @@ bool CommonHostInterface::CreateHostDisplayResources()
if (!m_fullscreen_ui_enabled) if (!m_fullscreen_ui_enabled)
ImGuiFullscreen::ResetFonts(); ImGuiFullscreen::ResetFonts();
m_logo_texture = m_display->CreateTexture(APP_ICON_WIDTH, APP_ICON_HEIGHT, 1, 1, 1, HostDisplayPixelFormat::RGBA8, if (!m_display->UpdateImGuiFontTexture())
APP_ICON_DATA, sizeof(u32) * APP_ICON_WIDTH, false);
if (!m_logo_texture || !m_display->UpdateImGuiFontTexture())
{ {
Log_ErrorPrintf("Failed to create ImGui font text"); Log_ErrorPrintf("Failed to create ImGui font text");
if (m_fullscreen_ui_enabled) if (m_fullscreen_ui_enabled)
@ -585,6 +613,10 @@ bool CommonHostInterface::CreateHostDisplayResources()
return false; return false;
} }
m_logo_texture = FullscreenUI::LoadTextureResource("logo.png", false);
if (!m_logo_texture)
m_logo_texture = FullscreenUI::LoadTextureResource("duck.png", true);
return true; return true;
} }
@ -3314,8 +3346,8 @@ void CommonHostInterface::DisplayLoadingScreen(const char* message, int progress
// eat the last imgui frame, it might've been partially rendered by the caller. // eat the last imgui frame, it might've been partially rendered by the caller.
ImGui::NewFrame(); ImGui::NewFrame();
const float logo_width = static_cast<float>(APP_ICON_WIDTH) * scale; const float logo_width = 260.0f * scale;
const float logo_height = static_cast<float>(APP_ICON_HEIGHT) * scale; const float logo_height = 260.0f * scale;
ImGui::SetNextWindowSize(ImVec2(logo_width, logo_height), ImGuiCond_Always); ImGui::SetNextWindowSize(ImVec2(logo_width, logo_height), ImGuiCond_Always);
ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x * 0.5f, (io.DisplaySize.y * 0.5f) - (50.0f * scale)), ImGui::SetNextWindowPos(ImVec2(io.DisplaySize.x * 0.5f, (io.DisplaySize.y * 0.5f) - (50.0f * scale)),

File diff suppressed because it is too large Load diff

View file

@ -123,7 +123,6 @@ static std::optional<u32> s_open_leaderboard_id;
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// Resources // Resources
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
static std::unique_ptr<HostDisplayTexture> LoadTextureResource(const char* name, bool allow_fallback = true);
static bool LoadResources(); static bool LoadResources();
static void DestroyResources(); static void DestroyResources();
@ -462,19 +461,6 @@ bool LoadResources()
return false; return false;
} }
{
std::unique_ptr<ByteStream> stream = s_host_interface->OpenPackageFile(
"resources" FS_OSPATH_SEPARATOR_STR "fa-solid-900.ttf", BYTESTREAM_OPEN_READ | BYTESTREAM_OPEN_STREAMED);
if (!stream)
return false;
std::vector<u8> font_data = FileSystem::ReadBinaryStream(stream.get());
if (font_data.empty())
return false;
ImGuiFullscreen::SetIconFontData(std::move(font_data));
}
return true; return true;
} }
@ -497,7 +483,7 @@ static std::unique_ptr<HostDisplayTexture> LoadTexture(const char* path, bool fr
{ {
std::unique_ptr<ByteStream> stream; std::unique_ptr<ByteStream> stream;
if (from_package) if (from_package)
stream = s_host_interface->OpenPackageFile(path, BYTESTREAM_OPEN_READ); stream = g_host_interface->OpenPackageFile(path, BYTESTREAM_OPEN_READ);
else else
stream = FileSystem::OpenFile(path, BYTESTREAM_OPEN_READ); stream = FileSystem::OpenFile(path, BYTESTREAM_OPEN_READ);
if (!stream) if (!stream)
@ -513,7 +499,7 @@ static std::unique_ptr<HostDisplayTexture> LoadTexture(const char* path, bool fr
return {}; return {};
} }
std::unique_ptr<HostDisplayTexture> texture = s_host_interface->GetDisplay()->CreateTexture( std::unique_ptr<HostDisplayTexture> texture = g_host_interface->GetDisplay()->CreateTexture(
image.GetWidth(), image.GetHeight(), 1, 1, 1, HostDisplayPixelFormat::RGBA8, image.GetPixels(), image.GetWidth(), image.GetHeight(), 1, 1, 1, HostDisplayPixelFormat::RGBA8, image.GetPixels(),
image.GetByteStride()); image.GetByteStride());
if (!texture) if (!texture)
@ -538,7 +524,7 @@ std::unique_ptr<HostDisplayTexture> LoadTextureResource(const char* name, bool a
Log_ErrorPrintf("Missing resource '%s', using fallback", name); Log_ErrorPrintf("Missing resource '%s', using fallback", name);
texture = s_host_interface->GetDisplay()->CreateTexture(PLACEHOLDER_ICON_WIDTH, PLACEHOLDER_ICON_HEIGHT, 1, 1, 1, texture = g_host_interface->GetDisplay()->CreateTexture(PLACEHOLDER_ICON_WIDTH, PLACEHOLDER_ICON_HEIGHT, 1, 1, 1,
HostDisplayPixelFormat::RGBA8, PLACEHOLDER_ICON_DATA, HostDisplayPixelFormat::RGBA8, PLACEHOLDER_ICON_DATA,
sizeof(u32) * PLACEHOLDER_ICON_WIDTH, false); sizeof(u32) * PLACEHOLDER_ICON_WIDTH, false);
if (!texture) if (!texture)

View file

@ -1,7 +1,9 @@
#pragma once #pragma once
#include "common/types.h" #include "common/types.h"
#include <string> #include <string>
#include <memory>
class HostDisplayTexture;
class CommonHostInterface; class CommonHostInterface;
class SettingsInterface; class SettingsInterface;
struct Settings; struct Settings;
@ -60,6 +62,7 @@ void Render();
bool IsBindingInput(); bool IsBindingInput();
bool HandleKeyboardBinding(const char* keyName, bool pressed); bool HandleKeyboardBinding(const char* keyName, bool pressed);
std::unique_ptr<HostDisplayTexture> LoadTextureResource(const char* name, bool allow_fallback = true);
bool InvalidateCachedTexture(const std::string& path); bool InvalidateCachedTexture(const std::string& path);
// Returns true if the message has been dismissed. // Returns true if the message has been dismissed.

File diff suppressed because it is too large Load diff

View file

@ -4,10 +4,6 @@ constexpr int WINDOW_ICON_WIDTH = 64;
constexpr int WINDOW_ICON_HEIGHT = 64; constexpr int WINDOW_ICON_HEIGHT = 64;
extern unsigned int WINDOW_ICON_DATA[WINDOW_ICON_WIDTH * WINDOW_ICON_HEIGHT]; extern unsigned int WINDOW_ICON_DATA[WINDOW_ICON_WIDTH * WINDOW_ICON_HEIGHT];
constexpr int APP_ICON_WIDTH = 260;
constexpr int APP_ICON_HEIGHT = 260;
extern unsigned int APP_ICON_DATA[APP_ICON_WIDTH * APP_ICON_HEIGHT];
constexpr int PLACEHOLDER_ICON_WIDTH = 128; constexpr int PLACEHOLDER_ICON_WIDTH = 128;
constexpr int PLACEHOLDER_ICON_HEIGHT = 96; constexpr int PLACEHOLDER_ICON_HEIGHT = 96;
extern unsigned int PLACEHOLDER_ICON_DATA[PLACEHOLDER_ICON_WIDTH * PLACEHOLDER_ICON_HEIGHT]; extern unsigned int PLACEHOLDER_ICON_DATA[PLACEHOLDER_ICON_WIDTH * PLACEHOLDER_ICON_HEIGHT];

View file

@ -33,6 +33,7 @@ float g_menu_bar_size = 0.0f;
static std::string s_font_filename; static std::string s_font_filename;
static std::string s_icon_font_filename; static std::string s_icon_font_filename;
static std::vector<u8> s_text_font_data;
static std::vector<u8> s_icon_font_data; static std::vector<u8> s_icon_font_data;
static float s_font_size = 15.0f; static float s_font_size = 15.0f;
static const ImWchar* s_font_glyph_range = nullptr; static const ImWchar* s_font_glyph_range = nullptr;
@ -56,6 +57,11 @@ void SetFontFilename(std::string filename)
std::string().swap(s_font_filename); std::string().swap(s_font_filename);
} }
void SetFontData(std::vector<u8> data)
{
s_text_font_data = std::move(data);
}
void SetIconFontFilename(std::string icon_font_filename) void SetIconFontFilename(std::string icon_font_filename)
{ {
if (!icon_font_filename.empty()) if (!icon_font_filename.empty())
@ -92,6 +98,49 @@ void SetResolveTextureFunction(ResolveTextureHandleCallback callback)
s_resolve_texture_handle = callback; s_resolve_texture_handle = callback;
} }
static ImFont* AddTextFont(float size /*= 15.0f*/)
{
static const ImWchar default_ranges[] = {
// Basic Latin + Latin Supplement + Central European diacritics
0x0020,
0x017F,
// Cyrillic + Cyrillic Supplement
0x0400,
0x052F,
// Cyrillic Extended-A
0x2DE0,
0x2DFF,
// Cyrillic Extended-B
0xA640,
0xA69F,
0,
};
ImFontConfig cfg;
if (!s_font_filename.empty())
{
return ImGui::GetIO().Fonts->AddFontFromFileTTF(s_font_filename.c_str(), size, &cfg,
s_font_glyph_range ? s_font_glyph_range : default_ranges);
}
else if (!s_text_font_data.empty())
{
cfg.FontDataOwnedByAtlas = false;
return ImGui::GetIO().Fonts->AddFontFromMemoryTTF(s_text_font_data.data(),
static_cast<int>(s_text_font_data.size()), size, &cfg,
s_font_glyph_range ? s_font_glyph_range : default_ranges);
}
else
{
Panic("No text font provided");
return nullptr;
}
}
static void AddIconFonts(float size) static void AddIconFonts(float size)
{ {
static const ImWchar range_fa[] = {ICON_MIN_FA, ICON_MAX_FA, 0}; static const ImWchar range_fa[] = {ICON_MIN_FA, ICON_MAX_FA, 0};
@ -102,7 +151,11 @@ static void AddIconFonts(float size)
cfg.GlyphMinAdvanceX = size * 0.75f; cfg.GlyphMinAdvanceX = size * 0.75f;
cfg.GlyphMaxAdvanceX = size * 0.75f; cfg.GlyphMaxAdvanceX = size * 0.75f;
if (!s_icon_font_data.empty()) if (!s_icon_font_filename.empty())
{
ImGui::GetIO().Fonts->AddFontFromFileTTF(s_icon_font_filename.c_str(), size * 0.75f, &cfg, range_fa);
}
else if (!s_icon_font_data.empty())
{ {
cfg.FontDataOwnedByAtlas = false; cfg.FontDataOwnedByAtlas = false;
ImGui::GetIO().Fonts->AddFontFromMemoryTTF(s_icon_font_data.data(), static_cast<int>(s_icon_font_data.size()), ImGui::GetIO().Fonts->AddFontFromMemoryTTF(s_icon_font_data.data(), static_cast<int>(s_icon_font_data.size()),
@ -110,7 +163,7 @@ static void AddIconFonts(float size)
} }
else else
{ {
ImGui::GetIO().Fonts->AddFontFromFileTTF(s_icon_font_filename.c_str(), size * 0.75f, &cfg, range_fa); Panic("No icon font provided");
} }
} }
@ -129,26 +182,12 @@ bool UpdateFonts()
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
io.Fonts->Clear(); io.Fonts->Clear();
if (s_font_filename.empty()) g_standard_font = AddTextFont(standard_font_size);
{ AddIconFonts(standard_font_size);
g_standard_font = ImGui::AddRobotoRegularFont(standard_font_size); g_medium_font = AddTextFont(medium_font_size);
AddIconFonts(standard_font_size); AddIconFonts(medium_font_size);
g_medium_font = ImGui::AddRobotoRegularFont(medium_font_size); g_large_font = AddTextFont(large_font_size);
AddIconFonts(medium_font_size); AddIconFonts(large_font_size);
g_large_font = ImGui::AddRobotoRegularFont(large_font_size);
AddIconFonts(large_font_size);
}
else
{
g_standard_font =
io.Fonts->AddFontFromFileTTF(s_font_filename.c_str(), standard_font_size, nullptr, s_font_glyph_range);
AddIconFonts(standard_font_size);
g_medium_font =
io.Fonts->AddFontFromFileTTF(s_font_filename.c_str(), medium_font_size, nullptr, s_font_glyph_range);
AddIconFonts(medium_font_size);
g_large_font = io.Fonts->AddFontFromFileTTF(s_font_filename.c_str(), large_font_size, nullptr, s_font_glyph_range);
AddIconFonts(large_font_size);
}
if (!io.Fonts->Build()) if (!io.Fonts->Build())
Panic("Failed to rebuild font atlas"); Panic("Failed to rebuild font atlas");
@ -163,15 +202,8 @@ void ResetFonts()
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
io.Fonts->Clear(); io.Fonts->Clear();
if (s_font_filename.empty()) g_standard_font = AddTextFont(standard_font_size);
{ AddIconFonts(standard_font_size);
g_standard_font = ImGui::AddRobotoRegularFont(standard_font_size);
}
else
{
g_standard_font =
io.Fonts->AddFontFromFileTTF(s_font_filename.c_str(), standard_font_size, nullptr, s_font_glyph_range);
}
g_medium_font = nullptr; g_medium_font = nullptr;
g_large_font = nullptr; g_large_font = nullptr;

View file

@ -133,6 +133,7 @@ static ALWAYS_INLINE ImVec4 UISecondaryTextColor()
} }
void SetFontFilename(std::string filename); void SetFontFilename(std::string filename);
void SetFontData(std::vector<u8> data);
void SetIconFontFilename(std::string icon_font_filename); void SetIconFontFilename(std::string icon_font_filename);
void SetIconFontData(std::vector<u8> data); void SetIconFontData(std::vector<u8> data);
void SetFontSize(float size_pixels); void SetFontSize(float size_pixels);

View file

@ -54,31 +54,3 @@ void ImGui::StyleColorsDarker(ImGuiStyle* dst)
colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f); colors[ImGuiCol_NavWindowingDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.20f);
colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f); colors[ImGuiCol_ModalWindowDimBg] = ImVec4(0.80f, 0.80f, 0.80f, 0.35f);
} }
#include "font_roboto_regular.inl"
ImFont* ImGui::AddRobotoRegularFont(float size /*= 15.0f*/)
{
static const ImWchar ranges[] = {
// Basic Latin + Latin Supplement + Central European diacritics
0x0020,
0x017F,
// Cyrillic + Cyrillic Supplement
0x0400,
0x052F,
// Cyrillic Extended-A
0x2DE0,
0x2DFF,
// Cyrillic Extended-B
0xA640,
0xA69F,
0,
};
return ImGui::GetIO().Fonts->AddFontFromMemoryCompressedTTF(
s_font_roboto_regular_compressed_data, s_font_roboto_regular_compressed_size, size, nullptr, ranges);
}

View file

@ -3,5 +3,4 @@
namespace ImGui { namespace ImGui {
void StyleColorsDarker(ImGuiStyle* dst = nullptr); void StyleColorsDarker(ImGuiStyle* dst = nullptr);
ImFont* AddRobotoRegularFont(float size = 15.0f);
} // namespace ImGui } // namespace ImGui