Skip to content

Commit

Permalink
Implement generic CPU time limiter.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdoeffinger committed Feb 2, 2019
1 parent eef606e commit b03c7d5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
16 changes: 16 additions & 0 deletions impl11/ddraw/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ static int isspace_wrapper(char c)
return std::isspace(static_cast<unsigned char>(c));
}

static void patchTimeGetTime(unsigned *addr)
{
DWORD old, dummy;
VirtualProtect(addr, 4, PAGE_READWRITE, &old);
*addr = reinterpret_cast<unsigned>(emulGetTime);
VirtualProtect(addr, 4, old, &dummy);
}

Config g_config;

Config::Config()
Expand Down Expand Up @@ -219,6 +227,14 @@ Config::Config()
*(unsigned *)0x4f3e54 = 0;
}

if (AutoPatch >= 2 && RefreshLimit == 1)
{
if (isBoP) patchTimeGetTime((unsigned *)0xbb96b8);
if (isXvT) patchTimeGetTime((unsigned *)0x86070c);
if (isXWing) patchTimeGetTime((unsigned *)0x4c31ec);
if (isTIE) patchTimeGetTime((unsigned *)0x4dc264);
}

if (this->JoystickEmul != 0 && isXWing)
{
// TODO: How to check if this is a supported binary?
Expand Down
26 changes: 26 additions & 0 deletions impl11/ddraw/joystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,32 @@
#undef max
#include <algorithm>

// timeGetTime emulation.
// if it is called in a tight loop it will call Sleep()
// prevents high CPU usage due to busy loop
DWORD emulGetTime()
{
static DWORD oldtime;
static DWORD count;
DWORD time = timeGetTime();
if (time != oldtime)
{
oldtime = time;
count = 0;
}
// Trigger value and sleep value derived by trial and error.
// Trigger values down to 10 with sleep value 8 do not seem to affect performance
// even at 60 FPS, and trigger values up to 1000 with sleep value 1 still seem effective
// to reduce CPU load on fast modern computers.
if (++count >= 20)
{
Sleep(2);
time = timeGetTime();
count = 0;
}
return time;
}

static int needsJoyEmul()
{
JOYCAPS caps = {};
Expand Down
1 change: 1 addition & 0 deletions impl11/ddraw/joystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "common.h"

extern "C" DWORD emulGetTime(void);
extern "C" UINT WINAPI emulJoyGetNumDevs(void);
extern "C" UINT WINAPI emulJoyGetDevCaps(UINT_PTR, struct tagJOYCAPSA *, UINT);
extern "C" UINT WINAPI emulJoyGetPosEx(UINT, struct joyinfoex_tag *);

0 comments on commit b03c7d5

Please sign in to comment.