From 773be7db265dc283b63133e268d435debf37416d Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 22 Dec 2019 18:53:58 +0100 Subject: [PATCH] - moved around a few bits of code to get rid of winbits.cpp/h. --- source/CMakeLists.txt | 1 - source/blood/src/blood.cpp | 1 - source/build/src/sdlayer.cpp | 155 +++++++++++++++++++++++++- source/duke3d/src/game.cpp | 1 - source/platform/win32/winbits.cpp | 173 ------------------------------ source/platform/win32/winbits.h | 8 -- source/rr/src/game.cpp | 1 - 7 files changed, 153 insertions(+), 187 deletions(-) delete mode 100644 source/platform/win32/winbits.cpp delete mode 100644 source/platform/win32/winbits.h diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 5e73d0efa..6a4615026 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -474,7 +474,6 @@ endif() # Start defining source files for Demolition set( PLAT_WIN32_SOURCES glad/src/glad_wgl.c - platform/win32/winbits.cpp platform/win32/i_specialpaths.cpp platform/win32/startwin.game.cpp diff --git a/source/blood/src/blood.cpp b/source/blood/src/blood.cpp index 66afe5ea4..d4cbbfbf1 100644 --- a/source/blood/src/blood.cpp +++ b/source/blood/src/blood.cpp @@ -75,7 +75,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #ifdef _WIN32 # include # define UPDATEINTERVAL 604800 // 1w -# include "win32/winbits.h" #else # ifndef GEKKO # include diff --git a/source/build/src/sdlayer.cpp b/source/build/src/sdlayer.cpp index 724e81b10..50f5fe57f 100644 --- a/source/build/src/sdlayer.cpp +++ b/source/build/src/sdlayer.cpp @@ -59,10 +59,10 @@ # include "osxbits.h" # include # include -#elif defined _WIN32 -# include "win32/winbits.h" #endif +FString progdir; + CVAR(Int, r_displayindex, 0, CVAR_ARCHIVE | CVAR_VIDEOCONFIG) CVAR(Int, r_borderless, 2, CVAR_ARCHIVE | CVAR_VIDEOCONFIG) CVAR(Int, maxrefreshfreq, 0, CVAR_ARCHIVE | CVAR_VIDEOCONFIG) @@ -156,6 +156,71 @@ uint16_t joydead[9], joysatur[9]; #define MAX_ERRORTEXT 4096 +//========================================================================== +// +// CalculateCPUSpeed +// +// Make a decent guess at how much time elapses between TSC steps. This can +// vary over runtime depending on power management settings, so should not +// be used anywhere that truely accurate timing actually matters. +// +//========================================================================== + +double PerfToSec, PerfToMillisec; +#include "stats.h" + +static void CalculateCPUSpeed() +{ + LARGE_INTEGER freq; + + QueryPerformanceFrequency(&freq); + + if (freq.QuadPart != 0) + { + LARGE_INTEGER count1, count2; + cycle_t ClockCalibration; + DWORD min_diff; + + ClockCalibration.Reset(); + + // Count cycles for at least 55 milliseconds. + // The performance counter may be very low resolution compared to CPU + // speeds today, so the longer we count, the more accurate our estimate. + // On the other hand, we don't want to count too long, because we don't + // want the user to notice us spend time here, since most users will + // probably never use the performance statistics. + min_diff = freq.LowPart * 11 / 200; + + // Minimize the chance of task switching during the testing by going very + // high priority. This is another reason to avoid timing for too long. + SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS); + SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); + + // Make sure we start timing on a counter boundary. + QueryPerformanceCounter(&count1); + do + { + QueryPerformanceCounter(&count2); + } while (count1.QuadPart == count2.QuadPart); + + // Do the timing loop. + ClockCalibration.Clock(); + do + { + QueryPerformanceCounter(&count1); + } while ((count1.QuadPart - count2.QuadPart) < min_diff); + ClockCalibration.Unclock(); + + SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS); + SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL); + + PerfToSec = double(count1.QuadPart - count2.QuadPart) / (double(ClockCalibration.GetRawCounter()) * freq.QuadPart); + PerfToMillisec = PerfToSec * 1000.0; + } +} + + + //========================================================================== // @@ -362,6 +427,91 @@ void wm_setapptitle(const char *name) #endif } +//========================================================================== +// +// win_buildargs +// +// This should be removed once everything can use the FArgs list. +// +//========================================================================== + + +int32_t win_buildargs(char** argvbuf) +{ + int32_t buildargc = 0; + + FString cmdline_utf8 = FString(GetCommandLineW()); + + *argvbuf = Xstrdup(cmdline_utf8.GetChars()); + + if (*argvbuf) + { + char quoted = 0, instring = 0, swallownext = 0; + char* wp; + for (const char* p = wp = *argvbuf; *p; p++) + { + if (*p == ' ') + { + if (instring) + { + if (!quoted) + { + // end of a string + *(wp++) = 0; + instring = 0; + } + else + *(wp++) = *p; + } + } + else if (*p == '"' && !swallownext) + { + if (instring) + { + if (quoted && p[1] == ' ') + { + // end of a string + *(wp++) = 0; + instring = 0; + } + quoted = !quoted; + } + else + { + instring = 1; + quoted = 1; + buildargc++; + } + } + else if (*p == '\\' && p[1] == '"' && !swallownext) + swallownext = 1; + else + { + if (!instring) + buildargc++; + + instring = 1; + *(wp++) = *p; + swallownext = 0; + } + } + *wp = 0; + } + + // Figure out what directory the program resides in. + + wchar_t buffer[256]; + GetModuleFileNameW(0, buffer, 256); + progdir = buffer; + progdir.Substitute("\\", "/"); + auto lastsep = progdir.LastIndexOf('/'); + if (lastsep != -1) + progdir.Truncate(lastsep + 1); + + return buildargc; +} + + // // // --------------------------------------- @@ -449,6 +599,7 @@ int main(int argc, char *argv[]) #ifndef _WIN32 setenv("__GL_THREADED_OPTIMIZATIONS", "1", 0); #endif + CalculateCPUSpeed(); buildkeytranslationtable(); diff --git a/source/duke3d/src/game.cpp b/source/duke3d/src/game.cpp index 219487beb..297b775fc 100644 --- a/source/duke3d/src/game.cpp +++ b/source/duke3d/src/game.cpp @@ -65,7 +65,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #ifdef _WIN32 # include # define UPDATEINTERVAL 604800 // 1w -# include "win32/winbits.h" #else # ifndef GEKKO # include diff --git a/source/platform/win32/winbits.cpp b/source/platform/win32/winbits.cpp deleted file mode 100644 index 6b3ccafe7..000000000 --- a/source/platform/win32/winbits.cpp +++ /dev/null @@ -1,173 +0,0 @@ -// Windows layer-independent code - -#define NOMINMAX -#include -#include "compat.h" -#include "build.h" -#include "baselayer.h" -#include "osd.h" - -#include "zstring.h" -#include "winbits.h" - -FString progdir; -// -// CheckWinVersion() -- check to see what version of Windows we happen to be running under (stripped down to what is actually still supported.) -// - -//========================================================================== -// -// win_buildargs -// -// This should be removed once everything can use the FArgs list. -// -//========================================================================== - - -int32_t win_buildargs(char **argvbuf) -{ - int32_t buildargc = 0; - - FString cmdline_utf8 = FString(GetCommandLineW()); - - *argvbuf = Xstrdup(cmdline_utf8.GetChars()); - - if (*argvbuf) - { - char quoted = 0, instring = 0, swallownext = 0; - char *wp; - for (const char *p = wp = *argvbuf; *p; p++) - { - if (*p == ' ') - { - if (instring) - { - if (!quoted) - { - // end of a string - *(wp++) = 0; - instring = 0; - } - else - *(wp++) = *p; - } - } - else if (*p == '"' && !swallownext) - { - if (instring) - { - if (quoted && p[1] == ' ') - { - // end of a string - *(wp++) = 0; - instring = 0; - } - quoted = !quoted; - } - else - { - instring = 1; - quoted = 1; - buildargc++; - } - } - else if (*p == '\\' && p[1] == '"' && !swallownext) - swallownext = 1; - else - { - if (!instring) - buildargc++; - - instring = 1; - *(wp++) = *p; - swallownext = 0; - } - } - *wp = 0; - } - - // Figure out what directory the program resides in. - - wchar_t buffer[256]; - GetModuleFileNameW(0, buffer, 256); - progdir = buffer; - progdir.Substitute("\\", "/"); - auto lastsep = progdir.LastIndexOf('/'); - if (lastsep != -1) - progdir.Truncate(lastsep + 1); - - return buildargc; -} - - -//========================================================================== -// -// CalculateCPUSpeed -// -// Make a decent guess at how much time elapses between TSC steps. This can -// vary over runtime depending on power management settings, so should not -// be used anywhere that truely accurate timing actually matters. -// -//========================================================================== - -double PerfToSec, PerfToMillisec; -#include "stats.h" - -static void CalculateCPUSpeed() -{ - LARGE_INTEGER freq; - - QueryPerformanceFrequency(&freq); - - if (freq.QuadPart != 0) - { - LARGE_INTEGER count1, count2; - cycle_t ClockCalibration; - DWORD min_diff; - - ClockCalibration.Reset(); - - // Count cycles for at least 55 milliseconds. - // The performance counter may be very low resolution compared to CPU - // speeds today, so the longer we count, the more accurate our estimate. - // On the other hand, we don't want to count too long, because we don't - // want the user to notice us spend time here, since most users will - // probably never use the performance statistics. - min_diff = freq.LowPart * 11 / 200; - - // Minimize the chance of task switching during the testing by going very - // high priority. This is another reason to avoid timing for too long. - SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS); - SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); - - // Make sure we start timing on a counter boundary. - QueryPerformanceCounter(&count1); - do - { - QueryPerformanceCounter(&count2); - } while (count1.QuadPart == count2.QuadPart); - - // Do the timing loop. - ClockCalibration.Clock(); - do - { - QueryPerformanceCounter(&count1); - } while ((count1.QuadPart - count2.QuadPart) < min_diff); - ClockCalibration.Unclock(); - - SetPriorityClass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS); - SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL); - - PerfToSec = double(count1.QuadPart - count2.QuadPart) / (double(ClockCalibration.GetRawCounter()) * freq.QuadPart); - PerfToMillisec = PerfToSec * 1000.0; - } -} - -class Initer -{ -public: - Initer() { CalculateCPUSpeed(); } -}; - -static Initer initer; - diff --git a/source/platform/win32/winbits.h b/source/platform/win32/winbits.h deleted file mode 100644 index 895c142d6..000000000 --- a/source/platform/win32/winbits.h +++ /dev/null @@ -1,8 +0,0 @@ -// Windows layer-independent code - -#include "compat.h" - -# define WindowClass "Demolition" - -extern void win_init(void); -extern int32_t win_buildargs(char **argvbuf); \ No newline at end of file diff --git a/source/rr/src/game.cpp b/source/rr/src/game.cpp index ab84e5699..ae211edb9 100644 --- a/source/rr/src/game.cpp +++ b/source/rr/src/game.cpp @@ -64,7 +64,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #ifdef _WIN32 # include # define UPDATEINTERVAL 604800 // 1w -# include "win32/winbits.h" #else # ifndef GEKKO # include