18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-15 01:25:11 +01:00
Files
maszyna/utilities/Timer.cpp
jerrrrycho 02e29f6971 feat(simulation): test mode, --test runs a scenery unattended and exits with the result (eu07/maszyna#541)
eu07 --test -s <scenery> [--test-timeout <s>] runs the scenery in ghostview with
the null renderer (models loaded, no textures), a hidden window, no sound,
network or python, and in fixed steps of 0.03 s of simulation time per frame
(three 10 ms physics steps) as fast as it can. Without -seed it uses a fixed
seed, so two runs give byte-identical physicslogs.

The scenery reports its result to the __test.result memory cell, which the
simulator always creates: value 1 passes once every vehicle has stood still for
1 s, value 0 fails at once. The run ends with exit code
  0 pass, 1 fail,
  2 a train passed a signal at stop (SetVelocity 0 0, as seen in its driver's
    speed table; SetVelocity 0 x on automatic block doesn't count),
  3 timeout (test.timeout, default 3600 s of simulation time),
  4 standstill (every vehicle still for test.standstill s, default 300, with no
    result; a pass with test.pass_on_standstill yes),
  5 error (no -s, the scenery didn't load or has no vehicles, an exception).
The test.* keys go in the scenery's config block; test.fail_if_stop_overran no
only logs a signal passed at danger. Outside the test mode a signal passed at
danger is logged.

The null renderer gets a ui backend which draws nothing, hands out the null
texture and material instead of throwing, and ends the ui frame. A GL context
is still made for the hidden window, without asking for a version, as parts
of the ui create textures with gl calls of their own. No crash dialog or
message box in test mode; the window code no longer needs a monitor.
2026-11-07 16:20:42 +01:00

128 lines
2.8 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "stdafx.h"
#include "utilities/Timer.h"
#include "utilities/Globals.h"
#include "winheaders.h"
namespace Timer {
subsystem_stopwatches subsystem;
double DeltaTime = 0.0;
double DeltaRenderTime = 0.0;
double fFPS{ 0.0f };
double fLastTime{ 0.0f };
DWORD dwFrames{ 0 };
double fSimulationTime{ 0.0 };
double fRenderTime{ 0.0 };
double fSoundTimer{ 0.0 };
double fSinceStart{ 0.0 };
double override_delta = -1.0f;
double fixed_step = 0.0;
double GetTime()
{
return fSimulationTime;
}
double GetRenderTime() {
return fRenderTime;
}
double GetDeltaTime()
{ // czas symulacji (stoi gdy pauza)
if (override_delta != -1.0f)
return override_delta;
return DeltaTime;
}
double GetDeltaRenderTime()
{ // czas renderowania (do poruszania się)
return DeltaRenderTime;
}
void set_delta_override(double t)
{
override_delta = t;
}
void set_fixed_step(double t)
{
fixed_step = t;
}
void ResetTimers()
{
UpdateTimers( Global.iPause != 0 );
DeltaTime = 0.0;
DeltaRenderTime = 0.0;
}
uint64_t fr;
uint64_t count;
uint64_t oldCount;
void UpdateTimers(bool pause)
{
#ifdef _WIN32
QueryPerformanceFrequency((LARGE_INTEGER *)&fr);
QueryPerformanceCounter((LARGE_INTEGER *)&count);
#elif __unix__
timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
count = (uint64_t)ts.tv_sec * 1000000000 + (uint64_t)ts.tv_nsec;
fr = 1000000000;
#endif
DeltaRenderTime = double(count - oldCount) / double(fr);
if (fixed_step > 0.0)
{
// the frame lasts exactly as long as said, however long it took to compute; the time speed doesn't apply
DeltaRenderTime = fixed_step;
}
fRenderTime += DeltaRenderTime;
if (!pause)
{
DeltaTime = (fixed_step > 0.0 ? 1.0 : Global.fTimeSpeed) * DeltaRenderTime;
fSoundTimer += DeltaTime;
if (fSoundTimer > 0.1)
fSoundTimer = 0.0;
if (DeltaTime > 1.0)
DeltaTime = 1.0;
fSimulationTime += GetDeltaTime();
}
else
DeltaTime = 0.0; // wszystko stoi, bo czas nie płynie
oldCount = count;
// Keep track of the time lapse and frame count
#if __unix__
auto fTime = (double)(count / 1000000000);
#elif _WIN32_WINNT >= _WIN32_WINNT_VISTA
double fTime = ::GetTickCount64() * 0.001f; // Get current time in seconds
#elif _WIN32
double fTime = ::GetTickCount() * 0.001f; // Get current time in seconds
#endif
++dwFrames; // licznik ramek
// update the frame rate once per second
if (fTime - fLastTime > 1.0f)
{
fFPS = dwFrames / (fTime - fLastTime);
fLastTime = fTime;
dwFrames = 0L;
}
};
}; // namespace timer
//---------------------------------------------------------------------------