- Cleaned up win32/i_system.cpp.

- Put back the previous event-driven ticker, except now the timer isn't
  started until the first time it is needed.


SVN r1975 (trunk)
This commit is contained in:
Randy Heit 2009-11-13 03:55:23 +00:00
parent f09420fce7
commit a0e7cdb73d
2 changed files with 552 additions and 199 deletions

View file

@ -1,3 +1,8 @@
November 12, 2009
- Cleaned up win32/i_system.cpp.
- Put back the previous event-driven ticker, except now the timer isn't
started until the first time it is needed.
November 11, 2009
- Modified the event-driven ticks to use the same code for calculating the
time as the polled timer so that the timer does not start running until the

View file

@ -1,25 +1,38 @@
// Emacs style mode select -*- C++ -*-
//-----------------------------------------------------------------------------
//
// $Id:$
//
// Copyright (C) 1993-1996 by id Software, Inc.
//
// This source is available for distribution and/or modification
// only under the terms of the DOOM Source Code License as
// published by id Software. All rights reserved.
//
// The source is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
// for more details.
//
// $Log:$
//
// DESCRIPTION:
//
//-----------------------------------------------------------------------------
/*
** i_system.cpp
** Timers, pre-console output, IWAD selection, and misc system routines.
**
**---------------------------------------------------------------------------
** Copyright 1998-2009 Randy Heit
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
*/
// HEADER FILES ------------------------------------------------------------
#include <stdlib.h>
#include <stdio.h>
@ -39,10 +52,6 @@
#include <richedit.h>
#include <wincrypt.h>
#ifdef _MSC_VER
#pragma warning(disable:4244)
#endif
#define USE_WINDOWS_DWORD
#include "hardware.h"
#include "doomerrors.h"
@ -75,23 +84,51 @@
#include "v_palette.h"
#include "stats.h"
EXTERN_CVAR (String, language)
// MACROS ------------------------------------------------------------------
// TYPES -------------------------------------------------------------------
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
extern void CheckCPUID(CPUInfo *cpu);
extern void LayoutMainWindow(HWND hWnd, HWND pane);
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
static void CalculateCPUSpeed();
static void I_SelectTimer();
static int I_GetTimePolled(bool saveMS);
static int I_WaitForTicPolled(int prevtic);
static void I_FreezeTimePolled(bool frozen);
static int I_GetTimeEventDriven(bool saveMS);
static int I_WaitForTicEvent(int prevtic);
static void I_FreezeTimeEventDriven(bool frozen);
static void CALLBACK TimerTicked(UINT id, UINT msg, DWORD_PTR user, DWORD_PTR dw1, DWORD_PTR dw2);
// EXTERNAL DATA DECLARATIONS ----------------------------------------------
EXTERN_CVAR(String, language);
EXTERN_CVAR (Bool, queryiwad);
extern HWND Window, ConWindow, GameTitleWindow;
extern HANDLE StdOut;
extern bool FancyStdOut;
extern HINSTANCE g_hInst;
extern FILE *Logfile;
// PUBLIC DATA DEFINITIONS -------------------------------------------------
CVAR (String, queryiwad_key, "shift", CVAR_GLOBALCONFIG|CVAR_ARCHIVE);
double PerfToSec, PerfToMillisec;
UINT TimerPeriod;
UINT TimerEventID;
UINT MillisecondsPerTic;
HANDLE NewTicArrived;
uint32 LanguageIDs[4];
void CalculateCPUSpeed ();
const IWADInfo *DoomStartupInfo;
@ -100,6 +137,37 @@ int (*I_WaitForTic) (int);
void (*I_FreezeTime) (bool frozen);
os_t OSPlatform;
bool gameisdead;
// PRIVATE DATA DEFINITIONS ------------------------------------------------
static ticcmd_t emptycmd;
static bool HasExited;
static DWORD basetime = 0;
// These are for the polled timer.
static DWORD TicStart;
static DWORD TicNext;
static int TicFrozen;
// These are for the event-driven timer.
static int tics;
static DWORD ted_start, ted_next;
static WadStuff *WadList;
static int NumWads;
static int DefaultWad;
// CODE --------------------------------------------------------------------
//==========================================================================
//
// I_Tactile
//
// Doom calls it when you take damage, so presumably it could be converted
// to something compatible with force feedback.
//
//==========================================================================
void I_Tactile(int on, int off, int total)
{
@ -107,35 +175,121 @@ void I_Tactile (int on, int off, int total)
on = off = total = 0;
}
ticcmd_t emptycmd;
ticcmd_t *I_BaseTiccmd(void)
//==========================================================================
//
// I_BaseTiccmd
//
// Returns an empty ticcmd. I have no idea why this should be system-
// specific.
//
//==========================================================================
ticcmd_t *I_BaseTiccmd()
{
return &emptycmd;
}
static DWORD basetime = 0;
// Stubs that select the timer to use and then call into it ----------------
// [RH] Returns time in milliseconds
unsigned int I_MSTime (void)
//==========================================================================
//
// I_GetTimeSelect
//
//==========================================================================
static int I_GetTimeSelect(bool saveMS)
{
DWORD tm;
tm = timeGetTime();
if (!basetime)
basetime = tm;
return tm - basetime;
I_SelectTimer();
return I_GetTime(saveMS);
}
static DWORD TicStart;
static DWORD TicNext;
static int TicFrozen;
//==========================================================================
//
// I_WaitForTicSelect
//
//==========================================================================
static int I_WaitForTicSelect(int prevtic)
{
I_SelectTimer();
return I_WaitForTic(prevtic);
}
//==========================================================================
//
// I_GetTime
// returns time in 1/35th second tics
// I_SelectTimer
//
int I_GetTimePolled (bool saveMS)
// Tries to create a timer event for efficent CPU use when the FPS is
// capped. Failing that, it sets things up for a polling timer instead.
//
//==========================================================================
static void I_SelectTimer()
{
assert(basetime == 0);
// Use a timer event if possible.
NewTicArrived = CreateEvent(NULL, FALSE, FALSE, NULL);
if (NewTicArrived)
{
UINT delay;
char *cmdDelay;
cmdDelay = Args->CheckValue("-timerdelay");
delay = 0;
if (cmdDelay != 0)
{
delay = atoi(cmdDelay);
}
if (delay == 0)
{
delay = 1000/TICRATE;
}
MillisecondsPerTic = delay;
TimerEventID = timeSetEvent(delay, 0, TimerTicked, 0, TIME_PERIODIC);
}
// Get the current time as the basetime.
basetime = timeGetTime();
// Set timer functions.
if (TimerEventID != 0)
{
I_GetTime = I_GetTimeEventDriven;
I_WaitForTic = I_WaitForTicEvent;
I_FreezeTime = I_FreezeTimeEventDriven;
}
else
{
I_GetTime = I_GetTimePolled;
I_WaitForTic = I_WaitForTicPolled;
I_FreezeTime = I_FreezeTimePolled;
}
}
//==========================================================================
//
// I_MSTime
//
// Returns the current time in milliseconds, where 0 is the first call
// to I_GetTime or I_WaitForTic.
//
//==========================================================================
unsigned int I_MSTime()
{
assert(basetime != 0);
return timeGetTime() - basetime;
}
//==========================================================================
//
// I_GetTimePolled
//
// Returns the current time in tics. If saveMS is true, then calls to
// I_GetTimeFrac() will use this tic as 0 and the next tic as 1.
//
//==========================================================================
static int I_GetTimePolled(bool saveMS)
{
DWORD tm;
@ -146,8 +300,9 @@ int I_GetTimePolled (bool saveMS)
tm = timeGetTime();
if (basetime == 0)
{
basetime = tm;
}
if (saveMS)
{
TicStart = tm;
@ -157,18 +312,35 @@ int I_GetTimePolled (bool saveMS)
return ((tm-basetime)*TICRATE)/1000;
}
int I_WaitForTicPolled (int prevtic)
//==========================================================================
//
// I_WaitForTicPolled
//
// Busy waits until the current tic is greater than prevtic. Time must not
// be frozen.
//
//==========================================================================
static int I_WaitForTicPolled(int prevtic)
{
int time;
assert(TicFrozen == 0);
while ((time = I_GetTimePolled(false)) <= prevtic)
;
{ }
return time;
}
void I_FreezeTimePolled (bool frozen)
//==========================================================================
//
// I_FreezeTimePolled
//
// Freeze/unfreeze the timer.
//
//==========================================================================
static void I_FreezeTimePolled(bool frozen)
{
if (frozen)
{
@ -185,26 +357,92 @@ void I_FreezeTimePolled (bool frozen)
}
}
//==========================================================================
//
// I_GetTimeEventDriven
//
// Returns the current tick counter. This is incremented asynchronously as
// the timer event fires.
//
//==========================================================================
int I_WaitForTicEvent (int prevtic)
static int I_GetTimeEventDriven(bool saveMS)
{
assert(!TicFrozen);
int tics = I_GetTimePolled(false);
while (prevtic >= tics)
if (saveMS)
{
WaitForSingleObject(NewTicArrived, 1000/TICRATE);
tics = I_GetTimePolled(false);
TicStart = ted_start;
TicNext = ted_next;
}
return tics;
}
// Returns the fractional amount of a tic passed since the most recent tic
//==========================================================================
//
// I_WaitForTicEvent
//
// Waits on the timer event as long as the current tic is not later than
// prevtic.
//
//==========================================================================
static int I_WaitForTicEvent(int prevtic)
{
assert(!TicFrozen);
while (prevtic >= tics)
{
WaitForSingleObject(NewTicArrived, 1000/TICRATE);
}
return tics;
}
//==========================================================================
//
// I_FreezeTimeEventDriven
//
// Freeze/unfreeze the ticker.
//
//==========================================================================
static void I_FreezeTimeEventDriven(bool frozen)
{
TicFrozen = frozen;
}
//==========================================================================
//
// TimerTicked
//
// Advance the tick count and signal the NewTicArrived event.
//
//==========================================================================
static void CALLBACK TimerTicked(UINT id, UINT msg, DWORD_PTR user, DWORD_PTR dw1, DWORD_PTR dw2)
{
if (!TicFrozen)
{
tics++;
}
ted_start = timeGetTime ();
ted_next = ted_start + MillisecondsPerTic;
SetEvent(NewTicArrived);
}
//==========================================================================
//
// I_GetTimeFrac
//
// Returns the fractional amount of a tic passed since the most recently
// saved tic.
//
//==========================================================================
fixed_t I_GetTimeFrac(uint32 *ms)
{
DWORD now = timeGetTime();
if (ms) *ms = TicNext;
if (ms != NULL)
{
*ms = TicNext;
}
DWORD step = TicNext - TicStart;
if (step == 0)
{
@ -217,14 +455,29 @@ fixed_t I_GetTimeFrac (uint32 *ms)
}
}
//==========================================================================
//
// I_WaitVBL
//
// I_WaitVBL is never used to actually synchronize to the vertical blank.
// Instead, it's used for delay purposes. Doom used a 70 Hz display mode,
// so that's what we use to determine how long to wait for.
//
//==========================================================================
void I_WaitVBL(int count)
{
// I_WaitVBL is never used to actually synchronize to the
// vertical blank. Instead, it's used for delay purposes.
Sleep(1000 * count / 70);
}
// [RH] Detect the OS the game is running under
//==========================================================================
//
// I_DetectOS
//
// Determine which version of Windows the game is running on.
//
//==========================================================================
void I_DetectOS(void)
{
OSVERSIONINFO info;
@ -303,9 +556,14 @@ void I_DetectOS (void)
}
}
//==========================================================================
//
// SubsetLanguageIDs
//
// Helper function for SetLanguageIDs.
//
//==========================================================================
static void SubsetLanguageIDs(LCID id, LCTYPE type, int idx)
{
char buf[8];
@ -325,9 +583,12 @@ static void SubsetLanguageIDs (LCID id, LCTYPE type, int idx)
idp[3] = 0;
}
//==========================================================================
//
// SetLanguageIDs
//
//==========================================================================
void SetLanguageIDs()
{
size_t langlen = strlen(language);
@ -354,6 +615,16 @@ void SetLanguageIDs ()
}
}
//==========================================================================
//
// 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.
//
//==========================================================================
void CalculateCPUSpeed()
{
LARGE_INTEGER freq;
@ -400,79 +671,58 @@ void CalculateCPUSpeed()
Printf ("CPU Speed: %.0f MHz\n", 0.001 / PerfToMillisec);
}
//==========================================================================
//
// I_Init
//
//==========================================================================
void I_Init (void)
void I_Init()
{
CheckCPUID(&CPU);
CalculateCPUSpeed();
DumpCPUInfo(&CPU);
// Use a timer event if possible
NewTicArrived = CreateEvent (NULL, FALSE, FALSE, NULL);
if (NewTicArrived)
{
UINT delay;
char *cmdDelay;
cmdDelay = Args->CheckValue ("-timerdelay");
delay = 0;
if (cmdDelay != 0)
{
delay = atoi (cmdDelay);
}
if (delay == 0)
{
delay = 1000/TICRATE;
}
TimerEventID = timeSetEvent(delay, 0, (LPTIMECALLBACK)NewTicArrived, 0, TIME_PERIODIC | TIME_CALLBACK_EVENT_SET);
MillisecondsPerTic = delay;
}
if (TimerEventID != 0)
{
I_GetTime = I_GetTimePolled;
I_WaitForTic = I_WaitForTicEvent;
I_FreezeTime = I_FreezeTimePolled;
}
else
{ // If no timer event, busy-loop with timeGetTime
I_GetTime = I_GetTimePolled;
I_WaitForTic = I_WaitForTicPolled;
I_FreezeTime = I_FreezeTimePolled;
}
I_GetTime = I_GetTimeSelect;
I_WaitForTic = I_WaitForTicSelect;
atterm (I_ShutdownSound);
I_InitSound ();
}
//==========================================================================
//
// I_Quit
//
static int has_exited;
//==========================================================================
void I_Quit (void)
void I_Quit()
{
has_exited = 1; /* Prevent infinitely recursive exits -- killough */
HasExited = true; /* Prevent infinitely recursive exits -- killough */
if (TimerEventID)
if (TimerEventID != 0)
{
timeKillEvent(TimerEventID);
if (NewTicArrived)
}
if (NewTicArrived != NULL)
{
CloseHandle(NewTicArrived);
}
timeEndPeriod(TimerPeriod);
if (demorecording)
{
G_CheckDemoStatus();
}
}
//==========================================================================
//
// I_Error
// I_FatalError
//
extern FILE *Logfile;
bool gameisdead;
// Throw an error that will end the game.
//
//==========================================================================
void STACK_ARGS I_FatalError(const char *error, ...)
{
@ -498,13 +748,22 @@ void STACK_ARGS I_FatalError (const char *error, ...)
throw CFatalError(errortext);
}
if (!has_exited) // If it hasn't exited yet, exit now -- killough
if (!HasExited) // If it hasn't exited yet, exit now -- killough
{
has_exited = 1; // Prevent infinitely recursive exits -- killough
HasExited = 1; // Prevent infinitely recursive exits -- killough
exit(-1);
}
}
//==========================================================================
//
// I_Error
//
// Throw an error that will send us to the console if we are far enough
// along in the startup process.
//
//==========================================================================
void STACK_ARGS I_Error(const char *error, ...)
{
va_list argptr;
@ -517,7 +776,11 @@ void STACK_ARGS I_Error (const char *error, ...)
throw CRecoverableError(errortext);
}
extern void LayoutMainWindow (HWND hWnd, HWND pane);
//==========================================================================
//
// I_SetIWADInfo
//
//==========================================================================
void I_SetIWADInfo(const IWADInfo *info)
{
@ -527,6 +790,15 @@ void I_SetIWADInfo (const IWADInfo *info)
LayoutMainWindow(Window, NULL);
}
//==========================================================================
//
// I_PrintStr
//
// Send output to the list box shown during startup (and hidden during
// gameplay).
//
//==========================================================================
void I_PrintStr(const char *cp)
{
if (ConWindow == NULL && StdOut == NULL)
@ -552,7 +824,7 @@ void I_PrintStr (const char *cp)
SendMessage(edit, EM_EXGETSEL, 0, (LPARAM)&endselection);
// Remember how many lines there were before we added text.
lines_before = SendMessage (edit, EM_GETLINECOUNT, 0, 0);
lines_before = (LONG)SendMessage(edit, EM_GETLINECOUNT, 0, 0);
}
while (*cp != 0)
@ -594,7 +866,7 @@ void I_PrintStr (const char *cp)
float h, s, v, r, g, b;
WORD attrib = 0;
RGBtoHSV(color.r / 255.0, color.g / 255.0, color.b / 255.0, &h, &s, &v);
RGBtoHSV(color.r / 255.f, color.g / 255.f, color.b / 255.f, &h, &s, &v);
if (s != 0)
{ // color
HSVtoRGB(&r, &g, &b, h, 1, 1);
@ -646,7 +918,7 @@ void I_PrintStr (const char *cp)
if (selection.cpMin == endselection.cpMin && selection.cpMax == endselection.cpMax)
{
selection.cpMax = selection.cpMin = GetWindowTextLength (edit);
lines_after = SendMessage (edit, EM_GETLINECOUNT, 0, 0);
lines_after = (LONG)SendMessage(edit, EM_GETLINECOUNT, 0, 0);
if (lines_after > lines_before)
{
SendMessage(edit, EM_LINESCROLL, 0, lines_after - lines_before);
@ -663,16 +935,19 @@ void I_PrintStr (const char *cp)
}
}
EXTERN_CVAR (Bool, queryiwad);
CVAR (String, queryiwad_key, "shift", CVAR_GLOBALCONFIG|CVAR_ARCHIVE);
static WadStuff *WadList;
static int NumWads;
static int DefaultWad;
//==========================================================================
//
// SetQueryIWAD
//
// The user had the "Don't ask again" box checked when they closed the
// IWAD selection dialog.
//
//==========================================================================
static void SetQueryIWad(HWND dialog)
{
HWND checkbox = GetDlgItem(dialog, IDC_DONTASKIWAD);
int state = SendMessage (checkbox, BM_GETCHECK, 0, 0);
int state = (int)SendMessage(checkbox, BM_GETCHECK, 0, 0);
bool query = (state != BST_CHECKED);
if (!query && queryiwad)
@ -687,6 +962,14 @@ static void SetQueryIWad (HWND dialog)
queryiwad = query;
}
//==========================================================================
//
// IWADBoxCallback
//
// Dialog proc for the IWAD selector.
//
//==========================================================================
BOOL CALLBACK IWADBoxCallback(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND ctrl;
@ -745,6 +1028,14 @@ BOOL CALLBACK IWADBoxCallback (HWND hDlg, UINT message, WPARAM wParam, LPARAM lP
return FALSE;
}
//==========================================================================
//
// I_PickIWad
//
// Open a dialog to pick the IWAD, if there is more than one found.
//
//==========================================================================
int I_PickIWad(WadStuff *wads, int numwads, bool showwin, int defaultiwad)
{
int vkey;
@ -767,12 +1058,20 @@ int I_PickIWad (WadStuff *wads, int numwads, bool showwin, int defaultiwad)
NumWads = numwads;
DefaultWad = defaultiwad;
return DialogBox (g_hInst, MAKEINTRESOURCE(IDD_IWADDIALOG),
return (int)DialogBox(g_hInst, MAKEINTRESOURCE(IDD_IWADDIALOG),
(HWND)Window, (DLGPROC)IWADBoxCallback);
}
return defaultiwad;
}
//==========================================================================
//
// I_WriteIniFailed
//
// Display a message when the config failed to save.
//
//==========================================================================
bool I_WriteIniFailed()
{
char *lpMsgBuf;
@ -793,20 +1092,53 @@ bool I_WriteIniFailed ()
return MessageBox(Window, errortext.GetChars(), GAMENAME " configuration not saved", MB_ICONEXCLAMATION | MB_RETRYCANCEL) == IDRETRY;
}
//==========================================================================
//
// I_FindFirst
//
// Start a pattern matching sequence.
//
//==========================================================================
void *I_FindFirst(const char *filespec, findstate_t *fileinfo)
{
return FindFirstFileA(filespec, (LPWIN32_FIND_DATAA)fileinfo);
}
//==========================================================================
//
// I_FindNext
//
// Return the next file in a pattern matching sequence.
//
//==========================================================================
int I_FindNext(void *handle, findstate_t *fileinfo)
{
return !FindNextFileA((HANDLE)handle, (LPWIN32_FIND_DATAA)fileinfo);
}
//==========================================================================
//
// I_FindClose
//
// Finish a pattern matching sequence.
//
//==========================================================================
int I_FindClose(void *handle)
{
return FindClose((HANDLE)handle);
}
//==========================================================================
//
// QueryPathKey
//
// Returns the value of a registry key into the output variable value.
//
//==========================================================================
static bool QueryPathKey(HKEY key, const char *keypath, const char *valname, FString &value)
{
HKEY steamkey;
@ -833,6 +1165,15 @@ static bool QueryPathKey(HKEY key, const char *keypath, const char *valname, FSt
return value.IsNotEmpty();
}
//==========================================================================
//
// I_GetSteamPath
//
// Check the registry for the path to Steam, so that we can search for
// IWADs that were bought with Steam.
//
//==========================================================================
FString I_GetSteamPath()
{
FString path;
@ -849,7 +1190,14 @@ FString I_GetSteamPath()
return path;
}
// Return a random seed, preferably one with lots of entropy.
//==========================================================================
//
// I_MakeRNGSeed
//
// Returns a 32-bit random seed, preferably one with lots of entropy.
//
//==========================================================================
unsigned int I_MakeRNGSeed()
{
unsigned int seed;