From 1e6da339f93cb24efff09bdd4235f4ae86ee8ddc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tur=C3=A1nszki=20J=C3=A1nos?= Date: Tue, 21 May 2024 07:35:58 +0200 Subject: [PATCH] added loading screen background image scaling modes --- WickedEngine/wiLoadingScreen.cpp | 50 ++++++++++++++++++------ WickedEngine/wiLoadingScreen.h | 7 ++++ WickedEngine/wiLoadingScreen_BindLua.cpp | 38 ++++++++++++++++++ WickedEngine/wiLoadingScreen_BindLua.h | 2 + WickedEngine/wiVersion.cpp | 2 +- 5 files changed, 86 insertions(+), 13 deletions(-) diff --git a/WickedEngine/wiLoadingScreen.cpp b/WickedEngine/wiLoadingScreen.cpp index 95ca3febf..a07e333e2 100644 --- a/WickedEngine/wiLoadingScreen.cpp +++ b/WickedEngine/wiLoadingScreen.cpp @@ -84,26 +84,52 @@ namespace wi const float canvas_aspect = GetLogicalWidth() / GetLogicalHeight(); const float image_aspect = float(desc.width) / float(desc.height); - if (canvas_aspect > image_aspect) + switch (background_mode) { - // display aspect is wider than image: - fx.siz.x = GetLogicalWidth() / canvas_aspect * image_aspect; - fx.siz.y = GetLogicalHeight(); - } - else - { - // image aspect is wider or equal to display - fx.siz.x = GetLogicalWidth(); - fx.siz.y = GetLogicalHeight() * canvas_aspect / image_aspect; + default: + case wi::LoadingScreen::BackgroundMode::Fill: + if (canvas_aspect > image_aspect) + { + // display aspect is wider than image: + fx.siz.x = GetLogicalWidth(); + fx.siz.y = GetLogicalHeight() / image_aspect * canvas_aspect; + } + else + { + // image aspect is wider or equal to display + fx.siz.x = GetLogicalWidth() / canvas_aspect * image_aspect; + fx.siz.y = GetLogicalHeight(); + } + fx.pos = XMFLOAT3(GetLogicalWidth() * 0.5f, GetLogicalHeight() * 0.5f, 0); + fx.pivot = XMFLOAT2(0.5f, 0.5f); + break; + case wi::LoadingScreen::BackgroundMode::Fit: + if (canvas_aspect > image_aspect) + { + // display aspect is wider than image: + fx.siz.x = GetLogicalWidth() / canvas_aspect * image_aspect; + fx.siz.y = GetLogicalHeight(); + } + else + { + // image aspect is wider or equal to display + fx.siz.x = GetLogicalWidth(); + fx.siz.y = GetLogicalHeight() * canvas_aspect / image_aspect; + } + fx.pos = XMFLOAT3(GetLogicalWidth() * 0.5f, GetLogicalHeight() * 0.5f, 0); + fx.pivot = XMFLOAT2(0.5f, 0.5f); + break; + case wi::LoadingScreen::BackgroundMode::Stretch: + fx.enableFullScreen(); + break; } - fx.pos = XMFLOAT3(GetLogicalWidth() * 0.5f, GetLogicalHeight() * 0.5f, 0); - fx.pivot = XMFLOAT2(0.5f, 0.5f); fx.blendFlag = wi::enums::BLENDMODE_ALPHA; if (colorspace != ColorSpace::SRGB) { fx.enableLinearOutputMapping(hdr_scaling); } + wi::image::Draw(&tex, fx, cmd); } diff --git a/WickedEngine/wiLoadingScreen.h b/WickedEngine/wiLoadingScreen.h index 775259000..f28bf40b6 100644 --- a/WickedEngine/wiLoadingScreen.h +++ b/WickedEngine/wiLoadingScreen.h @@ -22,6 +22,13 @@ namespace wi public: wi::Resource backgroundTexture; + enum class BackgroundMode + { + Fill, // fill the whole screen, will cut off parts of the image if aspects don't match + Fit, // fit the image completely inside the screen, will result in black bars on screen if aspects don't match + Stretch // fill the whole screen, and stretch the image if needed + } background_mode = BackgroundMode::Fill; + //Add a loading task which should be executed void addLoadingFunction(std::function loadingFunction); //Helper for loading a whole renderable component diff --git a/WickedEngine/wiLoadingScreen_BindLua.cpp b/WickedEngine/wiLoadingScreen_BindLua.cpp index 479167c7d..2c36a9de7 100644 --- a/WickedEngine/wiLoadingScreen_BindLua.cpp +++ b/WickedEngine/wiLoadingScreen_BindLua.cpp @@ -37,6 +37,8 @@ namespace wi::lua lunamethod(LoadingScreen_BindLua, GetProgress), lunamethod(LoadingScreen_BindLua, SetBackgroundTexture), lunamethod(LoadingScreen_BindLua, GetBackgroundTexture), + lunamethod(LoadingScreen_BindLua, SetBackgroundMode), + lunamethod(LoadingScreen_BindLua, GetBackgroundMode), { NULL, NULL } }; Luna::PropertyType LoadingScreen_BindLua::properties[] = { @@ -271,6 +273,34 @@ namespace wi::lua Luna::push(L, loading->backgroundTexture); return 1; } + int LoadingScreen_BindLua::SetBackgroundMode(lua_State* L) + { + LoadingScreen* loading = dynamic_cast(component); + if (loading == nullptr) + { + wi::lua::SError(L, "SetBackgroundMode(int mode): loading screen is not valid!"); + return 0; + } + int argc = wi::lua::SGetArgCount(L); + if (argc < 1) + { + wi::lua::SError(L, "SetBackgroundMode(int mode): not enough arguments!"); + return 0; + } + loading->background_mode = (LoadingScreen::BackgroundMode)wi::lua::SGetInt(L, 1); + return 0; + } + int LoadingScreen_BindLua::GetBackgroundMode(lua_State* L) + { + LoadingScreen* loading = dynamic_cast(component); + if (loading == nullptr) + { + wi::lua::SError(L, "GetBackgroundMode(): loading screen is not valid!"); + return 0; + } + wi::lua::SSetInt(L, (int)loading->background_mode); + return 1; + } void LoadingScreen_BindLua::Bind() { @@ -279,6 +309,14 @@ namespace wi::lua { initialized = true; Luna::Register(wi::lua::GetLuaState()); + + wi::lua::RunText(R"( +BackgroundMode = { + Fill = 0, + Fit = 1, + Stretch = 2 +} +)"); } } diff --git a/WickedEngine/wiLoadingScreen_BindLua.h b/WickedEngine/wiLoadingScreen_BindLua.h index 947cd9dee..a810fc4b5 100644 --- a/WickedEngine/wiLoadingScreen_BindLua.h +++ b/WickedEngine/wiLoadingScreen_BindLua.h @@ -31,6 +31,8 @@ namespace wi::lua int GetProgress(lua_State* L); int SetBackgroundTexture(lua_State* L); int GetBackgroundTexture(lua_State* L); + int SetBackgroundMode(lua_State* L); + int GetBackgroundMode(lua_State* L); static void Bind(); }; diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index ed2f317bf..4e5bfb771 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wi::version // minor features, major updates, breaking compatibility changes const int minor = 71; // minor bug fixes, alterations, refactors, updates - const int revision = 462; + const int revision = 463; const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision);