From c76ddddcf1c22d446c79b16a327e61341ee3274f Mon Sep 17 00:00:00 2001 From: Xottab-DUTY Date: Sat, 16 Dec 2023 11:43:18 +0500 Subject: [PATCH] Moved basic application functionality to CApplication --- .../AccessibilityShortcuts.hpp | 0 src/xrEngine/device.cpp | 2 - src/xrEngine/embedded_resources_management.h | 1 - src/xrEngine/splash.cpp | 110 ------------ src/xrEngine/splash.h | 7 - src/xrEngine/x_ray.cpp | 164 ++++++++++++++++-- src/xrEngine/x_ray.h | 21 ++- src/xrEngine/xrEngine.vcxproj | 3 +- src/xrEngine/xrEngine.vcxproj.filters | 9 +- src/xr_3da/entry_point.cpp | 80 +-------- src/xr_3da/packages.config | 5 - src/xr_3da/xr_3da.vcxproj | 16 +- src/xr_3da/xr_3da.vcxproj.filters | 4 - 13 files changed, 176 insertions(+), 246 deletions(-) rename src/{xr_3da => xrEngine}/AccessibilityShortcuts.hpp (100%) delete mode 100644 src/xrEngine/splash.cpp delete mode 100644 src/xrEngine/splash.h delete mode 100644 src/xr_3da/packages.config diff --git a/src/xr_3da/AccessibilityShortcuts.hpp b/src/xrEngine/AccessibilityShortcuts.hpp similarity index 100% rename from src/xr_3da/AccessibilityShortcuts.hpp rename to src/xrEngine/AccessibilityShortcuts.hpp diff --git a/src/xrEngine/device.cpp b/src/xrEngine/device.cpp index e6bcdb58be2..1f7622df24e 100644 --- a/src/xrEngine/device.cpp +++ b/src/xrEngine/device.cpp @@ -12,7 +12,6 @@ #include "xrScriptEngine/ScriptExporter.hpp" #include "XR_IOConsole.h" #include "xr_input.h" -#include "splash.h" #include @@ -430,7 +429,6 @@ void CRenderDevice::Run() // Pre start seqAppStart.Process(); - splash::hide(); SDL_HideWindow(m_sdlWnd); // workaround for SDL bug UpdateWindowProps(); SDL_ShowWindow(m_sdlWnd); diff --git a/src/xrEngine/embedded_resources_management.h b/src/xrEngine/embedded_resources_management.h index 8c62709cd66..0e4cb1076ad 100644 --- a/src/xrEngine/embedded_resources_management.h +++ b/src/xrEngine/embedded_resources_management.h @@ -37,7 +37,6 @@ inline HANDLE ExtractImage(int idx, UINT type) type, 0, 0, LR_CREATEDIBSECTION); } - inline SDL_Surface* CreateSurfaceFromBitmap(HBITMAP bitmapHandle) { BITMAP bitmap; diff --git a/src/xrEngine/splash.cpp b/src/xrEngine/splash.cpp deleted file mode 100644 index 25d39cff79a..00000000000 --- a/src/xrEngine/splash.cpp +++ /dev/null @@ -1,110 +0,0 @@ -#include "stdafx.h" -#include "splash.h" -#include "embedded_resources_management.h" - -#include - -constexpr u32 SPLASH_FRAMERATE = 30; - -class splash_screen -{ - SDL_Window* m_window; - Event m_should_exit; - bool m_thread_operational; - - size_t m_current_surface_idx; - xr_vector m_surfaces; - -public: - splash_screen() : m_window(nullptr), m_current_surface_idx(0) {} - - static void splash_proc(void* self_ptr) - { - auto& self = *static_cast(self_ptr); - self.m_thread_operational = true; - - while (true) - { - if (self.m_should_exit.Wait(SPLASH_FRAMERATE)) - break; - - if (self.m_surfaces.size() > 1) - { - if (self.m_current_surface_idx >= self.m_surfaces.size()) - self.m_current_surface_idx = 0; - - const auto current = SDL_GetWindowSurface(self.m_window); - const auto next = self.m_surfaces[self.m_current_surface_idx++]; // It's important to have postfix increment! - SDL_BlitSurface(next, nullptr, current, nullptr); - SDL_UpdateWindowSurface(self.m_window); - } - } - - for (SDL_Surface* surface : self.m_surfaces) - SDL_FreeSurface(surface); - self.m_surfaces.clear(); - - SDL_DestroyWindow(self.m_window); - self.m_window = nullptr; - - self.m_thread_operational = false; - } - - void show(bool topmost) - { - if (m_window) - return; - - m_surfaces = std::move(ExtractSplashScreen()); - - if (m_surfaces.empty()) - { - Log("! Couldn't create surface from image:", SDL_GetError()); - return; - } - - Uint32 flags = SDL_WINDOW_BORDERLESS | SDL_WINDOW_HIDDEN; - -#if SDL_VERSION_ATLEAST(2,0,5) - if (topmost) - flags |= SDL_WINDOW_ALWAYS_ON_TOP; -#endif - - SDL_Surface* surface = m_surfaces.front(); - m_window = SDL_CreateWindow("OpenXRay", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, surface->w, surface->h, flags); - - const auto current = SDL_GetWindowSurface(m_window); - SDL_BlitSurface(surface, nullptr, current, nullptr); - SDL_ShowWindow(m_window); - SDL_UpdateWindowSurface(m_window); - - Threading::SpawnThread(splash_proc, "X-Ray Splash Thread", 0, this); - - while (!m_thread_operational) - SDL_PumpEvents(); - SDL_PumpEvents(); - } - - void hide() - { - m_should_exit.Set(); - while (m_thread_operational) - { - SDL_PumpEvents(); - std::this_thread::yield(); - } - } -} g_splash_screen; - -namespace splash -{ -void show(const bool topmost) -{ - g_splash_screen.show(topmost); -} - -void hide() -{ - g_splash_screen.hide(); -} -} // namespace splash diff --git a/src/xrEngine/splash.h b/src/xrEngine/splash.h deleted file mode 100644 index 024108dea21..00000000000 --- a/src/xrEngine/splash.h +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -namespace splash -{ -void ENGINE_API show(const bool topmost); -void ENGINE_API hide(); -} diff --git a/src/xrEngine/x_ray.cpp b/src/xrEngine/x_ray.cpp index 06a6f0a2f2f..616b1ac5d86 100644 --- a/src/xrEngine/x_ray.cpp +++ b/src/xrEngine/x_ray.cpp @@ -6,29 +6,169 @@ // AlexMX - Alexander Maksimchuk //----------------------------------------------------------------------------- #include "stdafx.h" -#include "IGame_Level.h" -#include "IGame_Persistent.h" -#include "XR_IOConsole.h" #include "x_ray.h" -#include "std_classes.h" -#include "GameFont.h" -#include "xrCDB/ISpatial.h" -#include "xrSASH.h" -#include "xr_input.h" -//--------------------------------------------------------------------- +#include "main.h" +#include "AccessibilityShortcuts.hpp" +#include "embedded_resources_management.h" -ENGINE_API CApplication Application; +//#define PROFILE_TASK_SYSTEM -////////////////////////////////////////////////////////////////////////// +#ifdef PROFILE_TASK_SYSTEM +#include "xrCore/Threading/ParallelForEach.hpp" +#endif -CApplication::CApplication() +constexpr u32 SPLASH_FRAMERATE = 30; + +CApplication::CApplication(pcstr commandLine) { + xrDebug::Initialize(commandLine); + R_ASSERT3(SDL_Init(SDL_INIT_VIDEO) == 0, "Unable to initialize SDL", SDL_GetError()); + +#ifdef XR_PLATFORM_WINDOWS + AccessibilityShortcuts shortcuts; + if (!GEnv.isDedicatedServer) + shortcuts.Disable(); +#endif + + if (!strstr(commandLine, "-nosplash")) + { + const bool topmost = !strstr(commandLine, "-splashnotop"); +#ifndef PROFILE_TASK_SYSTEM + ShowSplash(topmost); +#endif + } + + pcstr fsltx = "-fsltx "; + string_path fsgame = ""; + if (strstr(commandLine, fsltx)) + { + const size_t sz = xr_strlen(fsltx); + sscanf(strstr(commandLine, fsltx) + sz, "%[^ ] ", fsgame); + } + + Core.Initialize("OpenXRay", commandLine, nullptr, true, *fsgame ? fsgame : nullptr); +#ifdef PROFILE_TASK_SYSTEM + const auto task = [](const TaskRange&){}; + + constexpr int task_count = 1048576; + constexpr int iterations = 250; + u64 results[iterations]; + + CTimer timer; + for (int i = 0; i < iterations; ++i) + { + timer.Start(); + xr_parallel_for(TaskRange(0, task_count, 1), task); + results[i] = timer.GetElapsed_ns(); + } + + u64 min = std::numeric_limits::max(); + u64 average{}; + for (int i = 0; i < iterations; ++i) + { + min = std::min(min, results[i]); + average += results[i] / 1000; + Log("Time:", results[i]); + } + Msg("Time min: %f microseconds", float(min) / 1000.f); + Msg("Time average: %f microseconds", float(average) / float(iterations)); +#endif } CApplication::~CApplication() { + Core._destroy(); + SDL_Quit(); +} + +int CApplication::Run() +{ +#ifdef PROFILE_TASK_SYSTEM + return 0; +#endif + HideSplash(); + return RunApplication(); +} + +void CApplication::ShowSplash(bool topmost) +{ + if (m_window) + return; + + m_surfaces = std::move(ExtractSplashScreen()); + + if (m_surfaces.empty()) + { + Log("! Couldn't create surface from image:", SDL_GetError()); + return; + } + + Uint32 flags = SDL_WINDOW_BORDERLESS | SDL_WINDOW_HIDDEN; + +#if SDL_VERSION_ATLEAST(2,0,5) + if (topmost) + flags |= SDL_WINDOW_ALWAYS_ON_TOP; +#endif + + SDL_Surface* surface = m_surfaces.front(); + m_window = SDL_CreateWindow("OpenXRay", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, surface->w, surface->h, flags); + + const auto current = SDL_GetWindowSurface(m_window); + SDL_BlitSurface(surface, nullptr, current, nullptr); + SDL_ShowWindow(m_window); + SDL_UpdateWindowSurface(m_window); + + Threading::SpawnThread(SplashProc, "X-Ray Splash Thread", 0, this); + + while (!m_thread_operational) + SDL_PumpEvents(); + SDL_PumpEvents(); +} + +void CApplication::SplashProc(void* self_ptr) +{ + auto& self = *static_cast(self_ptr); + self.m_thread_operational = true; + + while (true) + { + if (self.m_should_exit.Wait(SPLASH_FRAMERATE)) + break; + + if (self.m_surfaces.size() > 1) + { + if (self.m_current_surface_idx >= self.m_surfaces.size()) + self.m_current_surface_idx = 0; + + const auto current = SDL_GetWindowSurface(self.m_window); + const auto next = self.m_surfaces[self.m_current_surface_idx++]; // It's important to have postfix increment! + SDL_BlitSurface(next, nullptr, current, nullptr); + SDL_UpdateWindowSurface(self.m_window); + } + } + + for (SDL_Surface* surface : self.m_surfaces) + SDL_FreeSurface(surface); + self.m_surfaces.clear(); + + SDL_DestroyWindow(self.m_window); + self.m_window = nullptr; + + self.m_thread_operational = false; +} + +void CApplication::HideSplash() +{ + if (!m_window) + return; + m_should_exit.Set(); + while (m_thread_operational) + { + SDL_PumpEvents(); + SwitchToThread(); + } } diff --git a/src/xrEngine/x_ray.h b/src/xrEngine/x_ray.h index 45a6efde65f..9612d162a19 100644 --- a/src/xrEngine/x_ray.h +++ b/src/xrEngine/x_ray.h @@ -1,20 +1,29 @@ #ifndef __X_RAY_H__ #define __X_RAY_H__ -// refs -class ENGINE_API CGameFont; -class ILoadingScreen; - // definition class ENGINE_API CApplication final { + SDL_Window* m_window{}; + Event m_should_exit; + bool m_thread_operational{}; + + size_t m_current_surface_idx{}; + xr_vector m_surfaces; + +private: + static void SplashProc(void* self_ptr); + + void ShowSplash(bool topmost); + void HideSplash(); public: // Other - CApplication(); + CApplication(pcstr commandLine); ~CApplication(); + + int Run(); }; -extern ENGINE_API CApplication Application; #endif //__XR_BASE_H__ diff --git a/src/xrEngine/xrEngine.vcxproj b/src/xrEngine/xrEngine.vcxproj index 4d971be9f86..8d9047eed80 100644 --- a/src/xrEngine/xrEngine.vcxproj +++ b/src/xrEngine/xrEngine.vcxproj @@ -29,6 +29,7 @@ + @@ -84,7 +85,6 @@ - @@ -168,7 +168,6 @@ - diff --git a/src/xrEngine/xrEngine.vcxproj.filters b/src/xrEngine/xrEngine.vcxproj.filters index 5823069915d..03c7eab12f7 100644 --- a/src/xrEngine/xrEngine.vcxproj.filters +++ b/src/xrEngine/xrEngine.vcxproj.filters @@ -354,9 +354,6 @@ General\Profiler - - General - Render\Visibility @@ -399,6 +396,9 @@ editor + + General + @@ -590,9 +590,6 @@ General\Profiler - - General - Render\Device diff --git a/src/xr_3da/entry_point.cpp b/src/xr_3da/entry_point.cpp index cc66a82a744..0d5f7e0240a 100644 --- a/src/xr_3da/entry_point.cpp +++ b/src/xr_3da/entry_point.cpp @@ -1,26 +1,14 @@ #include "stdafx.h" -#include "resource.h" -#include "xrEngine/main.h" -#include "xrEngine/splash.h" +#include "xrEngine/x_ray.h" -#if defined(XR_PLATFORM_WINDOWS) -#include "AccessibilityShortcuts.hpp" -#elif defined(XR_PLATFORM_LINUX) || defined(XR_PLATFORM_BSD) || defined(XR_PLATFORM_APPLE) +#if defined(XR_PLATFORM_LINUX) || defined(XR_PLATFORM_BSD) || defined(XR_PLATFORM_APPLE) #include #include #include #include #endif -#include - -//#define PROFILE_TASK_SYSTEM - -#ifdef PROFILE_TASK_SYSTEM -#include "xrCore/Threading/ParallelForEach.hpp" -#endif - // Always request high performance GPU extern "C" { @@ -33,72 +21,12 @@ XR_EXPORT u32 AmdPowerXpressRequestHighPerformance = 0x00000001; // PowerXpress int entry_point(pcstr commandLine) { - xrDebug::Initialize(commandLine); - R_ASSERT3(SDL_Init(SDL_INIT_VIDEO) == 0, "Unable to initialize SDL", SDL_GetError()); - - if (!strstr(commandLine, "-nosplash")) - { - const bool topmost = !strstr(commandLine, "-splashnotop"); -#ifndef PROFILE_TASK_SYSTEM - splash::show(topmost); -#endif - } - if (strstr(commandLine, "-dedicated")) GEnv.isDedicatedServer = true; -#ifdef XR_PLATFORM_WINDOWS - AccessibilityShortcuts shortcuts; - if (!GEnv.isDedicatedServer) - shortcuts.Disable(); -#endif - - pcstr fsltx = "-fsltx "; - string_path fsgame = ""; - if (strstr(commandLine, fsltx)) - { - const size_t sz = xr_strlen(fsltx); - sscanf(strstr(commandLine, fsltx) + sz, "%[^ ] ", fsgame); - } -#ifdef PROFILE_TASK_SYSTEM - Core.Initialize("OpenXRay", commandLine, nullptr, false, *fsgame ? fsgame : nullptr); - - const auto task = [](const TaskRange&){}; - - constexpr int task_count = 1048576; - constexpr int iterations = 250; - u64 results[iterations]; - - CTimer timer; - for (int i = 0; i < iterations; ++i) - { - timer.Start(); - xr_parallel_for(TaskRange(0, task_count, 1), task); - results[i] = timer.GetElapsed_ns(); - } - - u64 min = std::numeric_limits::max(); - u64 average{}; - for (int i = 0; i < iterations; ++i) - { - min = std::min(min, results[i]); - average += results[i] / 1000; - Log("Time:", results[i]); - } - Msg("Time min: %f microseconds", float(min) / 1000.f); - Msg("Time average: %f microseconds", float(average) / float(iterations)); - - const auto result = 0; -#else - Core.Initialize("OpenXRay", commandLine, nullptr, true, *fsgame ? fsgame : nullptr); + CApplication app{ commandLine }; - const auto result = RunApplication(); -#endif // PROFILE_TASK_SYSTEM - - Core._destroy(); - - SDL_Quit(); - return result; + return app.Run(); } #if defined(XR_PLATFORM_WINDOWS) diff --git a/src/xr_3da/packages.config b/src/xr_3da/packages.config deleted file mode 100644 index 7539664cbfc..00000000000 --- a/src/xr_3da/packages.config +++ /dev/null @@ -1,5 +0,0 @@ - - - - - \ No newline at end of file diff --git a/src/xr_3da/xr_3da.vcxproj b/src/xr_3da/xr_3da.vcxproj index 13779fcc7a4..fccddc4fccb 100644 --- a/src/xr_3da/xr_3da.vcxproj +++ b/src/xr_3da/xr_3da.vcxproj @@ -63,7 +63,6 @@ - @@ -87,19 +86,6 @@ - - - - - - - - - - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - - + \ No newline at end of file diff --git a/src/xr_3da/xr_3da.vcxproj.filters b/src/xr_3da/xr_3da.vcxproj.filters index d27d4292d33..eb136a002cf 100644 --- a/src/xr_3da/xr_3da.vcxproj.filters +++ b/src/xr_3da/xr_3da.vcxproj.filters @@ -9,7 +9,6 @@ resources - @@ -55,7 +54,4 @@ Dependecies - - - \ No newline at end of file