- fixed compilation in debug mode.

- moved timer definitions into their own header/source files. d_main is not the right place for this.
- removed some leftover cruft from the old timer code.
This commit is contained in:
Christoph Oelckers 2017-11-12 09:06:40 +01:00
parent 7d63c3324d
commit 6bd76d6b5a
24 changed files with 220 additions and 172 deletions

View file

@ -877,6 +877,7 @@ set (PCH_SOURCES
hu_scores.cpp
i_module.cpp
i_net.cpp
i_time.cpp
info.cpp
keysections.cpp
lumpconfigfile.cpp

View file

@ -47,7 +47,7 @@
#include "templates.h"
#include "dobject.h"
#include "vm.h"
#include "d_main.h"
#include "i_time.h"
#include <math.h>
#include <stdlib.h>

View file

@ -69,7 +69,7 @@
#include "c_consolebuffer.h"
#include "g_levellocals.h"
#include "vm.h"
#include "d_main.h"
#include "i_time.h"
FString FStringFormat(VM_ARGS); // extern from thingdef_data.cpp

View file

@ -51,6 +51,7 @@
#include "doomerrors.h"
#include "i_time.h"
#include "d_gui.h"
#include "m_random.h"
#include "doomdef.h"
@ -117,8 +118,6 @@
#include "vm.h"
#include "types.h"
#include "r_data/r_vanillatrans.h"
#include <chrono>
#include <thread>
EXTERN_CVAR(Bool, hud_althud)
void DrawHUD();
@ -1082,127 +1081,6 @@ 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

View file

@ -162,25 +162,4 @@ public:
};
// 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

View file

@ -68,7 +68,7 @@
#include "intermission/intermission.h"
#include "g_levellocals.h"
#include "events.h"
#include "d_main.h"
#include "i_time.h"
EXTERN_CVAR (Int, disableautosave)
EXTERN_CVAR (Int, autosavecount)
@ -532,7 +532,7 @@ void HSendPacket (int node, int len)
{
PacketStore store;
store.message = doomcom;
store.timer = I_GetTime(false) + ((net_fakelatency / 2) / (1000 / TICRATE));
store.timer = I_GetTime() + ((net_fakelatency / 2) / (1000 / TICRATE));
OutBuffer.Push(store);
}
else
@ -540,7 +540,7 @@ void HSendPacket (int node, int len)
for (unsigned int i = 0; i < OutBuffer.Size(); i++)
{
if (OutBuffer[i].timer <= I_GetTime(false))
if (OutBuffer[i].timer <= I_GetTime())
{
doomcom = OutBuffer[i].message;
I_NetCmd();
@ -581,7 +581,7 @@ bool HGetPacket (void)
{
PacketStore store;
store.message = doomcom;
store.timer = I_GetTime(false) + ((net_fakelatency / 2) / (1000 / TICRATE));
store.timer = I_GetTime() + ((net_fakelatency / 2) / (1000 / TICRATE));
InBuffer.Push(store);
doomcom.remotenode = -1;
}
@ -591,7 +591,7 @@ bool HGetPacket (void)
bool gotmessage = false;
for (unsigned int i = 0; i < InBuffer.Size(); i++)
{
if (InBuffer[i].timer <= I_GetTime(false))
if (InBuffer[i].timer <= I_GetTime())
{
doomcom = InBuffer[i].message;
InBuffer.Delete(i);

View file

@ -51,7 +51,7 @@
#include "vm.h"
#include "g_levellocals.h"
#include "types.h"
#include "d_main.h"
#include "i_time.h"
//==========================================================================
//

View file

@ -57,14 +57,19 @@ typedef enum
#endif
#endif
// The maximum number of players, multiplayer/networking.
#define MAXPLAYERS 8
// Global constants that were defines.
enum
{
// The maximum number of players, multiplayer/networking.
MAXPLAYERS = 8,
// State updates, number of tics / second.
#define TICRATE 35
// State updates, number of tics / second.
TICRATE = 35,
// Amount of damage done by a telefrag.
TELEFRAG_DAMAGE = 1000000
};
// Amount of damage done by a telefrag.
#define TELEFRAG_DAMAGE 1000000
// The current state of the game: whether we are
// playing, gazing at the intermission screen,
@ -114,6 +119,7 @@ enum ESkillLevels
// DOOM keyboard definition. Everything below 0x100 matches
// a mode 1 keyboard scan code.
//
#define KEY_PAUSE 0xc5 // DIK_PAUSE
#define KEY_RIGHTARROW 0xcd // DIK_RIGHT
#define KEY_LEFTARROW 0xcb // DIK_LEFT

View file

@ -32,6 +32,7 @@
#include <CoreServices/CoreServices.h>
#endif
#include "i_time.h"
#include "templates.h"
#include "version.h"
#include "doomdef.h"

View file

@ -94,7 +94,7 @@
#include "g_levellocals.h"
#include "actorinlines.h"
#include "vm.h"
#include "d_main.h"
#include "i_time.h"
#include <string.h>

View file

@ -39,7 +39,7 @@
#include "d_player.h"
#include "g_levellocals.h"
#include "r_utility.h"
#include "d_main.h"
#include "i_time.h"
//#include "resources/voxels.h"
//#include "gl/gl_intern.h"

View file

@ -26,6 +26,7 @@
*/
#include "gl/system/gl_system.h"
#include "i_time.h"
#include "gi.h"
#include "m_png.h"
#include "m_random.h"
@ -45,7 +46,6 @@
#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"

View file

@ -52,7 +52,7 @@
#include "g_levellocals.h"
#include "gl/utility/gl_clock.h"
#include "gl/utility/gl_convert.h"
#include "d_main.h"
#include "i_time.h"
glcycle_t RenderWall,SetupWall,ClipWall;
glcycle_t RenderFlat,SetupFlat;

161
src/i_time.cpp Normal file
View file

@ -0,0 +1,161 @@
/*
** i_time.cpp
** Implements the timer
**
**---------------------------------------------------------------------------
** Copyright 1998-2916 Randy Heit
** Copyright 2917 Magnus Norddahl
** 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.
**---------------------------------------------------------------------------
**
*/
#include <chrono>
#include <thread>
#include <stdint.h>
#include "i_time.h"
#include "doomdef.h"
//==========================================================================
//
// 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;
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();
}
}

24
src/i_time.h Normal file
View file

@ -0,0 +1,24 @@
#pragma once
#include <stdint.h>
// 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();

View file

@ -55,7 +55,7 @@
#include "hardware.h"
#include "vm.h"
#include "r_videoscale.h"
#include "d_main.h"
#include "i_time.h"
/*=======================================
*

View file

@ -68,7 +68,7 @@
#include "r_utility.h"
#include "cmdlib.h"
#include "g_levellocals.h"
#include "d_main.h"
#include "i_time.h"
void P_GetPolySpots (MapData * lump, TArray<FNodeBuilder::FPolyStart> &spots, TArray<FNodeBuilder::FPolyStart> &anchors);

View file

@ -115,7 +115,7 @@
#endif
#include "events.h"
#include "types.h"
#include "d_main.h"
#include "i_time.h"
#include "fragglescript/t_fs.h"

View file

@ -38,6 +38,7 @@
#include "st_console.h"
#include "v_text.h"
#include "version.h"
#include "i_time.h"
static NSColor* RGB(const uint8_t red, const uint8_t green, const uint8_t blue)

View file

@ -86,10 +86,6 @@ int I_PickIWad_Cocoa (WadStuff *wads, int numwads, bool showwin, int defaultiwad
double PerfToSec, PerfToMillisec;
uint32_t LanguageIDs[4];
int (*I_GetTime) (bool saveMS);
int (*I_WaitForTic) (int);
void (*I_FreezeTime) (bool frozen);
void I_Tactile (int /*on*/, int /*off*/, int /*total*/)
{
}

View file

@ -66,7 +66,7 @@
#include "sbar.h"
#include "math/cmath.h"
#include "vm.h"
#include "d_main.h"
#include "i_time.h"
// EXTERNAL DATA DECLARATIONS ----------------------------------------------

View file

@ -40,9 +40,9 @@
#include "r_data/voxels.h"
#include "r_data/sprites.h"
#include "d_net.h"
#include "d_main.h"
#include "po_man.h"
#include "r_utility.h"
#include "i_time.h"
#include "swrenderer/drawers/r_draw.h"
#include "swrenderer/drawers/r_thread.h"
#include "swrenderer/things/r_visiblesprite.h"

View file

@ -84,7 +84,7 @@
#include "r_data/voxels.h"
#include "vm.h"
#include "r_videoscale.h"
#include "d_main.h"
#include "i_time.h"
EXTERN_CVAR(Bool, r_blendmethod)

View file

@ -56,6 +56,7 @@ enum
// Don't make these definitions available to the main body of the source code.
struct tagRAWINPUT;
class FInputDevice