Skip to content

Commit

Permalink
Moved application cycle processing to CApplication
Browse files Browse the repository at this point in the history
  • Loading branch information
Xottab-DUTY committed Dec 16, 2023
1 parent d4707ef commit b13bbda
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 110 deletions.
155 changes: 51 additions & 104 deletions src/xrEngine/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ ENGINE_API bool g_bRendering = false;
int ps_fps_limit = 501;
int ps_fps_limit_in_menu = 60;

constexpr size_t MAX_WINDOW_EVENTS = 32;

bool g_bLoaded = false;
ref_light precache_light = 0;

Expand Down Expand Up @@ -298,128 +296,77 @@ void CRenderDevice::ProcessFrame()
Sleep(1);
}

void CRenderDevice::message_loop()
void CRenderDevice::ProcessEvent(const SDL_Event& event)
{
while (!SDL_QuitRequested()) // SDL_PumpEvents is here
switch (event.type)
{
bool canCallActivate = false;
bool shouldActivate = false;

SDL_Event events[MAX_WINDOW_EVENTS];
const int count = SDL_PeepEvents(events, MAX_WINDOW_EVENTS,
SDL_GETEVENT, SDL_WINDOWEVENT, SDL_WINDOWEVENT);

for (int i = 0; i < count; ++i)
{
const SDL_Event event = events[i];

switch (event.type)
{
#if SDL_VERSION_ATLEAST(2, 0, 9)
case SDL_DISPLAYEVENT:
{
switch (event.display.type)
{
case SDL_DISPLAYEVENT_ORIENTATION:
case SDL_DISPLAYEVENT:
{
switch (event.display.type)
{
case SDL_DISPLAYEVENT_ORIENTATION:
#if SDL_VERSION_ATLEAST(2, 0, 14)
case SDL_DISPLAYEVENT_CONNECTED:
case SDL_DISPLAYEVENT_DISCONNECTED:
case SDL_DISPLAYEVENT_CONNECTED:
case SDL_DISPLAYEVENT_DISCONNECTED:
#endif
CleanupVideoModes();
FillVideoModes();
CleanupVideoModes();
FillVideoModes();
#if SDL_VERSION_ATLEAST(2, 0, 14)
if (event.display.display == psDeviceMode.Monitor && event.display.type != SDL_DISPLAYEVENT_CONNECTED)
if (event.display.display == psDeviceMode.Monitor && event.display.type != SDL_DISPLAYEVENT_CONNECTED)
#else
if (event.display.display == psDeviceMode.Monitor)
#endif
Reset();
else
UpdateWindowProps();
break;
} // switch (event.display.type)
break;
}
Reset();
else
UpdateWindowProps();
break;
} // switch (event.display.type)
break;
}
#endif
case SDL_WINDOWEVENT:
{
switch (event.window.event)
{
case SDL_WINDOWEVENT_MOVED:
{
UpdateWindowRects();
case SDL_WINDOWEVENT:
{
switch (event.window.event)
{
case SDL_WINDOWEVENT_MOVED:
{
UpdateWindowRects();
#if !SDL_VERSION_ATLEAST(2, 0, 18) // without SDL_WINDOWEVENT_DISPLAY_CHANGED, let's detect monitor change ourselves
const int display = SDL_GetWindowDisplayIndex(m_sdlWnd);
if (display != -1)
psDeviceMode.Monitor = display;
#endif
break;
}
break;
}

#if SDL_VERSION_ATLEAST(2, 0, 18)
case SDL_WINDOWEVENT_DISPLAY_CHANGED:
psDeviceMode.Monitor = event.window.data1;
break;
case SDL_WINDOWEVENT_DISPLAY_CHANGED:
psDeviceMode.Monitor = event.window.data1;
break;
#endif

case SDL_WINDOWEVENT_SIZE_CHANGED:
{
if (psDeviceMode.WindowStyle != rsFullscreen)
{
if (static_cast<int>(psDeviceMode.Width) == event.window.data1 &&
static_cast<int>(psDeviceMode.Height) == event.window.data2)
break; // we don't need to reset device if resolution wasn't really changed

psDeviceMode.Width = event.window.data1;
psDeviceMode.Height = event.window.data2;

Reset();
}
else
UpdateWindowRects();

break;
}

case SDL_WINDOWEVENT_SHOWN:
case SDL_WINDOWEVENT_FOCUS_GAINED:
case SDL_WINDOWEVENT_RESTORED:
case SDL_WINDOWEVENT_MAXIMIZED:
canCallActivate = true;
shouldActivate = true;
break;

case SDL_WINDOWEVENT_HIDDEN:
case SDL_WINDOWEVENT_FOCUS_LOST:
case SDL_WINDOWEVENT_MINIMIZED:
canCallActivate = true;
shouldActivate = false;
break;

case SDL_WINDOWEVENT_ENTER:
SDL_ShowCursor(SDL_FALSE);
break;

case SDL_WINDOWEVENT_LEAVE:
SDL_ShowCursor(SDL_TRUE);
break;

case SDL_WINDOWEVENT_CLOSE:
Engine.Event.Defer("KERNEL:disconnect");
Engine.Event.Defer("KERNEL:quit");
break;
} // switch (event.window.event)
case SDL_WINDOWEVENT_SIZE_CHANGED:
{
if (psDeviceMode.WindowStyle != rsFullscreen)
{
if (static_cast<int>(psDeviceMode.Width) == event.window.data1 &&
static_cast<int>(psDeviceMode.Height) == event.window.data2)
break; // we don't need to reset device if resolution wasn't really changed

psDeviceMode.Width = event.window.data1;
psDeviceMode.Height = event.window.data2;

Reset();
}
} // switch (event.type)
} // for (int i = 0; i < count; ++i)
else
UpdateWindowRects();

// Workaround for screen blinking when there's too much timeouts
if (canCallActivate)
{
OnWindowActivate(shouldActivate);
break;
}

ProcessFrame();
} // switch (event.window.event)
}
} // switch (event.type)
}

void CRenderDevice::Run()
Expand Down Expand Up @@ -448,10 +395,10 @@ void CRenderDevice::Run()
SDL_RaiseWindow(m_sdlWnd);
if (GEnv.isDedicatedServer || strstr(Core.Params, "-center_screen"))
SDL_SetWindowPosition(m_sdlWnd, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
}

// Message cycle
message_loop();

void CRenderDevice::Shutdown()
{
// Stop Balance-Thread
mt_bMustExit = true;

Expand Down
12 changes: 7 additions & 5 deletions src/xrEngine/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,16 @@ class ENGINE_API CRenderDevice : public IWindowHandler
public:
// Creation & Destroying
void Create();
void Run();
void Destroy();

void Reset(bool precache = true);

void Run();
void Shutdown();

void ProcessEvent(const SDL_Event& event);
void OnWindowActivate(bool activated);

void UpdateWindowProps();
void UpdateWindowRects();
void SelectResolution(bool windowed);
Expand Down Expand Up @@ -258,10 +264,6 @@ class ENGINE_API CRenderDevice : public IWindowHandler
private:
void CalcFrameStats();

void OnWindowActivate(bool activated);

void message_loop();

public:
[[nodiscard]]
auto& editor() { return m_editor; }
Expand Down
70 changes: 69 additions & 1 deletion src/xrEngine/x_ray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
// global variables
constexpr u32 SPLASH_FRAMERATE = 30;

constexpr size_t MAX_WINDOW_EVENTS = 32;

ENGINE_API CInifile* pGameIni = nullptr;
ENGINE_API bool CallOfPripyatMode = false;
ENGINE_API bool ClearSkyMode = false;
Expand Down Expand Up @@ -357,8 +359,74 @@ int CApplication::Run()
#endif

// Main cycle
HideSplash();
Device.Run();
HideSplash();

while (!SDL_QuitRequested()) // SDL_PumpEvents is here
{
bool canCallActivate = false;
bool shouldActivate = false;

SDL_Event events[MAX_WINDOW_EVENTS];
const int count = SDL_PeepEvents(events, MAX_WINDOW_EVENTS,
SDL_GETEVENT, SDL_WINDOWEVENT, SDL_WINDOWEVENT);

for (int i = 0; i < count; ++i)
{
const SDL_Event event = events[i];

switch (event.type)
{
case SDL_WINDOWEVENT:
{
switch (event.window.event)
{
case SDL_WINDOWEVENT_SHOWN:
case SDL_WINDOWEVENT_FOCUS_GAINED:
case SDL_WINDOWEVENT_RESTORED:
case SDL_WINDOWEVENT_MAXIMIZED:
canCallActivate = true;
shouldActivate = true;
continue;

case SDL_WINDOWEVENT_HIDDEN:
case SDL_WINDOWEVENT_FOCUS_LOST:
case SDL_WINDOWEVENT_MINIMIZED:
canCallActivate = true;
shouldActivate = false;
continue;

case SDL_WINDOWEVENT_ENTER:
SDL_ShowCursor(SDL_FALSE);
continue;

case SDL_WINDOWEVENT_LEAVE:
SDL_ShowCursor(SDL_TRUE);
continue;

case SDL_WINDOWEVENT_CLOSE:
Engine.Event.Defer("KERNEL:disconnect");
Engine.Event.Defer("KERNEL:quit");
continue;
} // switch (event.window.event)
}
} // switch (event.type)

// Only process event in Device
// if it wasn't processed in the switch above
Device.ProcessEvent(event);
} // for (int i = 0; i < count; ++i)

// Workaround for screen blinking when there's too much timeouts
if (canCallActivate)
{
Device.OnWindowActivate(shouldActivate);
}

Device.ProcessFrame();
} // while (!SDL_QuitRequested())

Device.Shutdown();

return 0;
}
Expand Down

0 comments on commit b13bbda

Please sign in to comment.