Skip to content

Commit

Permalink
Moved basic application functionality to CApplication
Browse files Browse the repository at this point in the history
  • Loading branch information
Xottab-DUTY committed Dec 16, 2023
1 parent bd0887c commit c76dddd
Show file tree
Hide file tree
Showing 13 changed files with 176 additions and 246 deletions.
File renamed without changes.
2 changes: 0 additions & 2 deletions src/xrEngine/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "xrScriptEngine/ScriptExporter.hpp"
#include "XR_IOConsole.h"
#include "xr_input.h"
#include "splash.h"

#include <thread>

Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/xrEngine/embedded_resources_management.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ inline HANDLE ExtractImage(int idx, UINT type)
type, 0, 0, LR_CREATEDIBSECTION);
}


inline SDL_Surface* CreateSurfaceFromBitmap(HBITMAP bitmapHandle)
{
BITMAP bitmap;
Expand Down
110 changes: 0 additions & 110 deletions src/xrEngine/splash.cpp

This file was deleted.

7 changes: 0 additions & 7 deletions src/xrEngine/splash.h

This file was deleted.

164 changes: 152 additions & 12 deletions src/xrEngine/x_ray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>&){};

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<u64>::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<CApplication*>(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();
}
}
21 changes: 15 additions & 6 deletions src/xrEngine/x_ray.h
Original file line number Diff line number Diff line change
@@ -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<SDL_Surface*> 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__
3 changes: 1 addition & 2 deletions src/xrEngine/xrEngine.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="AccessibilityShortcuts.hpp" />
<ClInclude Include="CameraBase.h" />
<ClInclude Include="CameraDefs.h" />
<ClInclude Include="CameraManager.h" />
Expand Down Expand Up @@ -84,7 +85,6 @@
<ClInclude Include="pure_relcase.h" />
<ClInclude Include="Rain.h" />
<ClInclude Include="Render.h" />
<ClInclude Include="splash.h" />
<ClInclude Include="ShadersExternalData.h" />
<ClInclude Include="StatGraph.h" />
<ClInclude Include="Stats.h" />
Expand Down Expand Up @@ -168,7 +168,6 @@
<ClCompile Include="pure_relcase.cpp" />
<ClCompile Include="Rain.cpp" />
<ClCompile Include="Render.cpp" />
<ClCompile Include="splash.cpp" />
<ClCompile Include="StatGraph.cpp" />
<ClCompile Include="Stats.cpp" />
<ClCompile Include="stdafx.cpp">
Expand Down
Loading

0 comments on commit c76dddd

Please sign in to comment.