Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce CPU Load #650

Merged
merged 4 commits into from
Dec 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions source_files/edge/e_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
#include "edge_profiling.h"

extern cvar_c r_doubleframes;
extern cvar_c n_busywait;

extern cvar_c v_gamma;

Expand Down Expand Up @@ -1909,6 +1910,7 @@ void E_EngineShutdown(void)

S_Shutdown();
R_Shutdown();
N_Shutdown();
}

// Local Prototypes
Expand Down Expand Up @@ -2155,6 +2157,10 @@ void E_Main(int argc, const char **argv)

if (app_state & APP_STATE_ACTIVE)
E_Tick();
else if (!n_busywait.d)
{
I_Sleep(5);
}
}
#else
return;
Expand Down
20 changes: 20 additions & 0 deletions source_files/edge/i_system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
extern FILE *debugfile;
extern FILE *logfile;

#if !defined(__MINGW32__) && (defined(WIN32) || defined(_WIN32) || defined(_WIN64))
extern HANDLE windows_timer;
#endif

// output string buffer
#define MSGBUFSIZE 4096
static char msgbuf[MSGBUFSIZE];
Expand Down Expand Up @@ -136,6 +140,22 @@ u32_t I_GetMicros(void)

void I_Sleep(int millisecs)
{

#if !defined(__MINGW32__) && (defined(WIN32) || defined(_WIN32) || defined(_WIN64))
// On Windows use high resolution timer if available, the Sleep Win32 call defaults to 15.6ms resolution and
// timeBeginPeriod is problematic
if (windows_timer != NULL)
{
LARGE_INTEGER due_time;
due_time.QuadPart = -((LONGLONG)(millisecs * 1000000) / 100);
if (SetWaitableTimer(windows_timer, &due_time, 0, NULL, NULL, FALSE))
{
WaitForSingleObject(windows_timer, INFINITE);
}
return;
}
#endif

SDL_Delay(millisecs);
}

Expand Down
40 changes: 36 additions & 4 deletions source_files/edge/n_network.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,13 @@ extern void VM_SetFloat(coal::vm_c *vm, const char *mod_name, const char
// only true if packets are exchanged with a server
bool netgame = false;


// 70Hz
DEF_CVAR(r_doubleframes, "1", CVAR_ARCHIVE)
DEF_CVAR(n_busywait, "1", CVAR_ROM)

#if !defined(__MINGW32__) && (defined(WIN32) || defined(_WIN32) || defined(_WIN64))
HANDLE windows_timer = NULL;
#endif

// gametic is the tic about to (or currently being) run.
// maketic is the tic that hasn't had control made for it yet.
Expand All @@ -77,6 +81,25 @@ void N_InitNetwork(void)
srand(I_PureRandom());

N_ResetTics();

#if !defined(__MINGW32__) && (defined(WIN32) || defined(_WIN32) || defined(_WIN64))
windows_timer = CreateWaitableTimerExW(NULL, NULL, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS);
if (windows_timer != NULL)
{
n_busywait = 0;
}
#endif
}

void N_Shutdown(void)
{
#if !defined(__MINGW32__) && (defined(WIN32) || defined(_WIN32) || defined(_WIN64))
if (windows_timer)
{
CloseHandle(windows_timer);
windows_timer = NULL;
}
#endif
}

static void PreInput()
Expand Down Expand Up @@ -149,10 +172,9 @@ void N_GrabTiccmds(void)
memcpy(&p->cmd, p->in_cmds + buf, sizeof(ticcmd_t));
}
if (LUA_UseLuaHud())
LUA_SetFloat(LUA_GetGlobalVM(), "sys", "gametic", gametic / (r_doubleframes.d ? 2 : 1));
LUA_SetFloat(LUA_GetGlobalVM(), "sys", "gametic", gametic / (r_doubleframes.d ? 2 : 1));
else
VM_SetFloat(ui_vm, "sys", "gametic", gametic / (r_doubleframes.d ? 2 : 1));


gametic++;
}
Expand Down Expand Up @@ -198,7 +220,7 @@ int N_NetUpdate()
int N_TryRunTics()
{
EDGE_ZoneScoped;

if (singletics)
{
PreInput();
Expand All @@ -223,6 +245,11 @@ int N_TryRunTics()
nowtime = N_NetUpdate();
realtics = nowtime - last_tryrun_tic;
last_tryrun_tic = nowtime;

if (!n_busywait.d && realtics <= 0)
{
I_Sleep(5);
}
}

// this limit is rather arbitrary
Expand Down Expand Up @@ -253,6 +280,11 @@ int N_TryRunTics()
while (maketic < gametic + tics)
{
N_NetUpdate();

if (!n_busywait.d && (maketic < gametic + tics))
{
I_Sleep(5);
}
}

return tics;
Expand Down
1 change: 1 addition & 0 deletions source_files/edge/n_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
extern bool netgame;

void N_InitNetwork(void);
void N_Shutdown(void);

// Create any new ticcmds and broadcast to other players.
// returns value of I_GetTime().
Expand Down