Skip to content

Commit

Permalink
- Switch to C++11 steady clock
Browse files Browse the repository at this point in the history
- Move the C++11 implementation to d_main
- Remove the platform specific timer implementations
  • Loading branch information
dpjudas committed Nov 12, 2017
1 parent 090943e commit 307d893
Show file tree
Hide file tree
Showing 22 changed files with 174 additions and 560 deletions.
1 change: 1 addition & 0 deletions src/c_bind.cpp
Expand Up @@ -47,6 +47,7 @@
#include "templates.h"
#include "dobject.h"
#include "vm.h"
#include "d_main.h"

#include <math.h>
#include <stdlib.h>
Expand Down
3 changes: 2 additions & 1 deletion src/c_console.cpp
Expand Up @@ -69,6 +69,7 @@
#include "c_consolebuffer.h"
#include "g_levellocals.h"
#include "vm.h"
#include "d_main.h"

FString FStringFormat(VM_ARGS); // extern from thingdef_data.cpp

Expand Down Expand Up @@ -528,7 +529,7 @@ static void maybedrawnow (bool tick, bool force)
|| gamestate == GS_STARTUP))
{
static size_t lastprinttime = 0;
size_t nowtime = I_GetTime(false);
size_t nowtime = I_GetTime();

if (nowtime - lastprinttime > 1 || force)
{
Expand Down
125 changes: 124 additions & 1 deletion src/d_main.cpp
Expand Up @@ -117,6 +117,8 @@
#include "vm.h"
#include "types.h"
#include "r_data/r_vanillatrans.h"
#include <chrono>
#include <thread>

EXTERN_CVAR(Bool, hud_althud)
void DrawHUD();
Expand Down Expand Up @@ -280,7 +282,7 @@ void D_ProcessEvents (void)
{
M_SetDefaultMode ();
}
else if (testingmode <= I_GetTime(false))
else if (testingmode <= I_GetTime())
{
M_RestoreMode ();
}
Expand Down Expand Up @@ -1080,6 +1082,127 @@ void D_DoomLoop ()
}
}

//==========================================================================
//
// Tick time functions
//
//==========================================================================

static unsigned int FirstFrameStartTime;
static unsigned int CurrentFrameStartTime;
static unsigned int FreezeTime;

static uint32_t performanceGetTime()
{
using namespace std::chrono;
return (uint32_t)duration_cast<milliseconds>(steady_clock::now().time_since_epoch()).count();
}

void I_SetFrameTime()
{
// Must only be called once per frame/swapbuffers.
//
// Caches all timing information for the current rendered frame so that any
// calls to I_FPSTime, I_MSTime, I_GetTime or I_GetTimeFrac will return
// the same time.

if (FreezeTime == 0)
{
CurrentFrameStartTime = performanceGetTime();
if (FirstFrameStartTime == 0)
FirstFrameStartTime = CurrentFrameStartTime;
}
}

void I_WaitVBL(int count)
{
// 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.

std::this_thread::sleep_for(std::chrono::milliseconds(1000 * count / 70));
I_SetFrameTime();
}

int I_WaitForTic(int prevtic)
{
// Waits until the current tic is greater than prevtic. Time must not be frozen.

int time;
assert(TicFrozen == 0);
while ((time = I_GetTime()) <= prevtic)
{
// The minimum amount of time a thread can sleep is controlled by timeBeginPeriod.
// We set this to 1 ms in DoMain.
int sleepTime = prevtic - time;
if (sleepTime > 2)
std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime - 2));

I_SetFrameTime();
}

return time;
}

unsigned int I_FPSTime()
{
if (FreezeTime == 0)
return CurrentFrameStartTime;
else
return performanceGetTime();
}

unsigned int I_MSTime()
{
if (FreezeTime == 0)
{
return CurrentFrameStartTime - FirstFrameStartTime;
}
else
{
if (FirstFrameStartTime == 0)
{
FirstFrameStartTime = performanceGetTime();
return 0;
}
else
{
return performanceGetTime() - FirstFrameStartTime;
}
}
}

int I_GetTime()
{
return (CurrentFrameStartTime - FirstFrameStartTime) * TICRATE / 1000 + 1;
}

double I_GetTimeFrac(uint32_t *ms)
{
unsigned int currentTic = (CurrentFrameStartTime - FirstFrameStartTime) * TICRATE / 1000;
unsigned int ticStartTime = FirstFrameStartTime + currentTic * 1000 / TICRATE;
unsigned int ticNextTime = FirstFrameStartTime + (currentTic + 1) * 1000 / TICRATE;

if (ms)
*ms = currentTic + 1;

return (CurrentFrameStartTime - ticStartTime) / (double)(ticNextTime - ticStartTime);
}

void I_FreezeTime(bool frozen)
{
if (frozen)
{
FreezeTime = performanceGetTime();
}
else
{
FirstFrameStartTime += performanceGetTime() - FreezeTime;
FreezeTime = 0;
I_SetFrameTime();
}
}

//==========================================================================
//
// D_PageTicker
Expand Down
20 changes: 20 additions & 0 deletions src/d_main.h
Expand Up @@ -162,5 +162,25 @@ class FIWadManager

};

// Called by D_DoomLoop, sets the time for the current frame
void I_SetFrameTime();

// Called by D_DoomLoop, returns current time in tics.
int I_GetTime();

double I_GetTimeFrac(uint32_t *ms);

// like I_GetTime, except it waits for a new tic before returning
int I_WaitForTic(int);

// Freezes tic counting temporarily. While frozen, calls to I_GetTime()
// will always return the same value. This does not affect I_MSTime().
// You must also not call I_WaitForTic() while freezing time, since the
// tic will never arrive (unless it's the current one).
void I_FreezeTime(bool frozen);

// [RH] Returns millisecond-accurate time
unsigned int I_MSTime();
unsigned int I_FPSTime();

#endif
13 changes: 7 additions & 6 deletions src/d_net.cpp
Expand Up @@ -68,6 +68,7 @@
#include "intermission/intermission.h"
#include "g_levellocals.h"
#include "events.h"
#include "d_main.h"

EXTERN_CVAR (Int, disableautosave)
EXTERN_CVAR (Int, autosavecount)
Expand Down Expand Up @@ -956,7 +957,7 @@ void NetUpdate (void)
}

// check time
nowtime = I_GetTime (false);
nowtime = I_GetTime ();
newtics = nowtime - gametime;
gametime = nowtime;

Expand Down Expand Up @@ -1830,7 +1831,7 @@ void TryRunTics (void)
}
else
{
entertic = I_GetTime (false);
entertic = I_GetTime ();
}
realtics = entertic - oldentertics;
oldentertics = entertic;
Expand Down Expand Up @@ -1913,7 +1914,7 @@ void TryRunTics (void)
Net_CheckLastReceived (counts);

// don't stay in here forever -- give the menu a chance to work
if (I_GetTime (false) - entertic >= 1)
if (I_GetTime () - entertic >= 1)
{
C_Ticker ();
M_Ticker ();
Expand All @@ -1928,7 +1929,7 @@ void TryRunTics (void)
hadlate = false;
for (i = 0; i < MAXPLAYERS; i++)
players[i].waiting = false;
lastglobalrecvtime = I_GetTime (false); //Update the last time the game tic'd over
lastglobalrecvtime = I_GetTime (); //Update the last time the game tic'd over

// run the count tics
if (counts > 0)
Expand Down Expand Up @@ -1961,9 +1962,9 @@ void Net_CheckLastReceived (int counts)
{
// [Ed850] Check to see the last time a packet was received.
// If it's longer then 3 seconds, a node has likely stalled.
if (I_GetTime(false) - lastglobalrecvtime >= TICRATE * 3)
if (I_GetTime() - lastglobalrecvtime >= TICRATE * 3)
{
lastglobalrecvtime = I_GetTime(false); //Bump the count
lastglobalrecvtime = I_GetTime(); //Bump the count

if (NetMode == NET_PeerToPeer || consoleplayer == Net_Arbitrator)
{
Expand Down
1 change: 1 addition & 0 deletions src/dobject.cpp
Expand Up @@ -51,6 +51,7 @@
#include "vm.h"
#include "g_levellocals.h"
#include "types.h"
#include "d_main.h"

//==========================================================================
//
Expand Down
3 changes: 2 additions & 1 deletion src/g_game.cpp
Expand Up @@ -92,6 +92,7 @@
#include "g_hub.h"
#include "g_levellocals.h"
#include "events.h"
#include "d_main.h"


static FRandom pr_dmspawn ("DMSpawn");
Expand Down Expand Up @@ -2887,7 +2888,7 @@ bool G_CheckDemoStatus (void)
int endtime = 0;

if (timingdemo)
endtime = I_GetTime (false) - starttime;
endtime = I_GetTime () - starttime;

C_RestoreCVars (); // [RH] Restore cvars demo might have changed
M_Free (demobuffer);
Expand Down
3 changes: 2 additions & 1 deletion src/g_level.cpp
Expand Up @@ -94,6 +94,7 @@
#include "g_levellocals.h"
#include "actorinlines.h"
#include "vm.h"
#include "d_main.h"

#include <string.h>

Expand Down Expand Up @@ -1020,7 +1021,7 @@ void G_DoLoadLevel (int position, bool autosave)

if (firstTime)
{
starttime = I_GetTime (false);
starttime = I_GetTime ();
firstTime = false;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/gl/models/gl_models.cpp
Expand Up @@ -39,6 +39,7 @@
#include "d_player.h"
#include "g_levellocals.h"
#include "r_utility.h"
#include "d_main.h"
//#include "resources/voxels.h"
//#include "gl/gl_intern.h"

Expand Down
1 change: 1 addition & 0 deletions src/gl/scene/gl_scene.cpp
Expand Up @@ -45,6 +45,7 @@
#include "serializer.h"
#include "g_levellocals.h"
#include "events.h"
#include "d_main.h"

#include "gl/dynlights/gl_lightbuffer.h"
#include "gl/system/gl_interface.h"
Expand Down
2 changes: 1 addition & 1 deletion src/gl/utility/gl_clock.cpp
Expand Up @@ -52,7 +52,7 @@
#include "g_levellocals.h"
#include "gl/utility/gl_clock.h"
#include "gl/utility/gl_convert.h"

#include "d_main.h"

glcycle_t RenderWall,SetupWall,ClipWall;
glcycle_t RenderFlat,SetupFlat;
Expand Down
3 changes: 2 additions & 1 deletion src/menu/videomenu.cpp
Expand Up @@ -55,6 +55,7 @@
#include "hardware.h"
#include "vm.h"
#include "r_videoscale.h"
#include "d_main.h"

/*=======================================
*
Expand Down Expand Up @@ -312,7 +313,7 @@ DEFINE_ACTION_FUNCTION(DVideoModeMenu, SetSelectedSize)
OldBits = DisplayBits;
NewBits = BitTranslate[DummyDepthCvar];
setmodeneeded = true;
testingmode = I_GetTime(false) + 5 * TICRATE;
testingmode = I_GetTime() + 5 * TICRATE;
SetModesMenu (NewWidth, NewHeight, NewBits);
ACTION_RETURN_BOOL(true);
}
Expand Down
1 change: 1 addition & 0 deletions src/p_glnodes.cpp
Expand Up @@ -68,6 +68,7 @@
#include "r_utility.h"
#include "cmdlib.h"
#include "g_levellocals.h"
#include "d_main.h"

void P_GetPolySpots (MapData * lump, TArray<FNodeBuilder::FPolyStart> &spots, TArray<FNodeBuilder::FPolyStart> &anchors);

Expand Down
1 change: 1 addition & 0 deletions src/p_setup.cpp
Expand Up @@ -115,6 +115,7 @@
#endif
#include "events.h"
#include "types.h"
#include "d_main.h"

#include "fragglescript/t_fs.h"

Expand Down

0 comments on commit 307d893

Please sign in to comment.