From daa593bdcbbba721c7a45a6c832640a107902076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 15 Sep 2025 12:42:11 -0600 Subject: [PATCH] Invert the relationship for loading atlas textures --- Common/UI/Context.cpp | 25 ++++++++++--------------- Common/UI/Context.h | 16 ++++++++++++++++ Common/UI/View.cpp | 6 ++++-- UI/NativeApp.cpp | 36 ++++++++++++++++++++++++++++++++++++ UI/Theme.cpp | 6 +++--- 5 files changed, 69 insertions(+), 20 deletions(-) diff --git a/Common/UI/Context.cpp b/Common/UI/Context.cpp index 8139441d6a..e4d6335b7f 100644 --- a/Common/UI/Context.cpp +++ b/Common/UI/Context.cpp @@ -42,22 +42,17 @@ void UIContext::Init(Draw::DrawContext *thin3d, Draw::Pipeline *uipipe, Draw::Pi void UIContext::BeginFrame() { frameStartTime_ = time_now_d(); if (!uitexture_) { - uitexture_ = CreateTextureFromFile(draw_, "ui_atlas.zim", ImageFileType::ZIM, false); - if (!fontTexture_) { -#if PPSSPP_PLATFORM(WINDOWS) || PPSSPP_PLATFORM(ANDROID) || PPSSPP_PLATFORM(MAC) || PPSSPP_PLATFORM(IOS) - // Don't bother with loading font_atlas.zim -#else - fontTexture_ = CreateTextureFromFile(draw_, "font_atlas.zim", ImageFileType::ZIM, false); -#endif - if (!fontTexture_) { - // Load the smaller ascii font only, like on Android. For debug ui etc. - fontTexture_ = CreateTextureFromFile(draw_, "asciifont_atlas.zim", ImageFileType::ZIM, false); - if (!fontTexture_) { - WARN_LOG(Log::System, "Failed to load font_atlas.zim or asciifont_atlas.zim"); - } - } - } + AtlasData data = atlasProvider_(draw_, AtlasChoice::General); + uitexture_ = data.texture; + ui_draw2d.SetAtlas(data.atlas); } + + if (!fontTexture_) { + AtlasData data = atlasProvider_(draw_, AtlasChoice::Font); + fontTexture_ = data.texture; + ui_draw2d.SetFontAtlas(data.atlas); + } + uidrawbuffer_->SetCurZ(0.0f); ActivateTopScissor(); } diff --git a/Common/UI/Context.h b/Common/UI/Context.h index 9e6f8969de..ec8582d3ca 100644 --- a/Common/UI/Context.h +++ b/Common/UI/Context.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "Common/Math/geom2d.h" #include "Common/Math/lin/vec3.h" @@ -43,6 +44,18 @@ struct UITransform { float alpha; }; +enum class AtlasChoice : int { + General, + Font, +}; + +struct AtlasData { + Atlas *atlas; + Draw::Texture *texture; +}; + +typedef std::function UIAtlasProviderFunc; + class UIContext { public: UIContext(); @@ -112,6 +125,7 @@ public: Bounds TransformBounds(const Bounds &bounds); void SetTheme(const UI::Theme *theme) { this->theme = theme; } + void SetAtlasProvider(UIAtlasProviderFunc func) { atlasProvider_ = func; } private: Draw::DrawContext *draw_ = nullptr; @@ -136,4 +150,6 @@ private: std::vector scissorStack_; std::vector transformStack_; + + UIAtlasProviderFunc atlasProvider_{}; }; diff --git a/Common/UI/View.cpp b/Common/UI/View.cpp index 6f882e2a5d..2a732ed66d 100644 --- a/Common/UI/View.cpp +++ b/Common/UI/View.cpp @@ -1833,8 +1833,10 @@ void DrawIconShine(UIContext &dc, const Bounds &bounds, float shine, bool animat dc.Begin(); } const AtlasImage *img = dc.Draw()->GetAtlas()->getImage(ImageID("I_DROP_SHADOW")); - float scale = bounds.w / img->w; - dc.Draw()->DrawImage(ImageID("I_DROP_SHADOW"), bounds.centerX(), bounds.centerY(), scale * 1.7f, colorAlpha(0xFF3EC5FF, 0.75f * shine), ALIGN_CENTER); + if (img) { + float scale = bounds.w / img->w; + dc.Draw()->DrawImage(ImageID("I_DROP_SHADOW"), bounds.centerX(), bounds.centerY(), scale * 1.7f, colorAlpha(0xFF3EC5FF, 0.75f * shine), ALIGN_CENTER); + } dc.Flush(); } diff --git a/UI/NativeApp.cpp b/UI/NativeApp.cpp index 250cf1425d..f23151fdce 100644 --- a/UI/NativeApp.cpp +++ b/UI/NativeApp.cpp @@ -87,6 +87,7 @@ #include "Common/VR/PPSSPPVR.h" #include "Common/Thread/ThreadManager.h" #include "Common/Audio/AudioBackend.h" +#include "Common/Render/ManagedTexture.h" #include "Core/ControlMapper.h" #include "Core/Config.h" @@ -820,6 +821,40 @@ static void NativeMixWrapper(float *dest, int framesToWrite, int sampleRateHz, v } } +static AtlasData AtlasProvider(Draw::DrawContext *draw, AtlasChoice atlas) { + switch (atlas) { + case AtlasChoice::General: + return { + GetUIAtlas(), + CreateTextureFromFile(draw, "ui_atlas.zim", ImageFileType::ZIM, false) + }; + case AtlasChoice::Font: + { + Draw::Texture *fontTexture = nullptr; + // NOTE: This ugly check must be the same as the one in UpdateTheme. This will be fixed. +#if PPSSPP_PLATFORM(WINDOWS) || PPSSPP_PLATFORM(ANDROID) || PPSSPP_PLATFORM(MAC) || PPSSPP_PLATFORM(IOS) + // Don't bother with loading font_atlas.zim +#else + fontTexture = CreateTextureFromFile(draw_, "font_atlas.zim", ImageFileType::ZIM, false); +#endif + if (!fontTexture) { + // Load the smaller ascii font only, like on Android. For debug ui etc. + // NOTE: We better be sure here that the correct metadata is loaded.. + fontTexture = CreateTextureFromFile(draw, "asciifont_atlas.zim", ImageFileType::ZIM, false); + if (!fontTexture) { + WARN_LOG(Log::System, "Failed to load font_atlas.zim or asciifont_atlas.zim"); + } + } + return { + GetFontAtlas(), + fontTexture, + }; + } + default: + return {}; + }; +} + bool NativeInitGraphics(GraphicsContext *graphicsContext) { INFO_LOG(Log::System, "NativeInitGraphics"); @@ -843,6 +878,7 @@ bool NativeInitGraphics(GraphicsContext *graphicsContext) { uiContext = new UIContext(); uiContext->SetTheme(GetTheme()); + uiContext->SetAtlasProvider(&AtlasProvider); UpdateTheme(); ui_draw2d.Init(g_draw, texColorPipeline); diff --git a/UI/Theme.cpp b/UI/Theme.cpp index e42939a0d0..446bc777c8 100644 --- a/UI/Theme.cpp +++ b/UI/Theme.cpp @@ -254,10 +254,10 @@ void UpdateTheme() { // Load any missing atlas metadata (the images are loaded from UIContext). LoadAtlasMetadata(ui_atlas, "ui_atlas.meta", true); -#if !(PPSSPP_PLATFORM(WINDOWS) || PPSSPP_PLATFORM(ANDROID)) - LoadAtlasMetadata(font_atlas, "font_atlas.meta", ui_atlas.num_fonts == 0); -#else +#if PPSSPP_PLATFORM(WINDOWS) || PPSSPP_PLATFORM(ANDROID) || PPSSPP_PLATFORM(MAC) || PPSSPP_PLATFORM(IOS) LoadAtlasMetadata(font_atlas, "asciifont_atlas.meta", ui_atlas.num_fonts == 0); +#else + LoadAtlasMetadata(font_atlas, "font_atlas.meta", ui_atlas.num_fonts == 0); #endif }