Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Just simplify and delete a bunch of shit
Browse files Browse the repository at this point in the history
  • Loading branch information
Causeless committed Dec 25, 2023
1 parent ce3328c commit 3ab51fe
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 69 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
</details>

<details><summary><b>Removed</b></summary>

- Removed RealToSimCap and OneSimUpdatePerFrame. Instead we try to always target 60fps, even if it slows the simulation down a little.

</details>

***
Expand Down
3 changes: 1 addition & 2 deletions Lua/LuaBindingsManagers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -379,13 +379,12 @@ namespace RTE {
return luabind::class_<TimerMan>("TimerManager")

.property("TimeScale", &TimerMan::GetTimeScale, &TimerMan::SetTimeScale)
.property("RealToSimCap", &TimerMan::GetRealToSimCap, &TimerMan::SetRealToSimCap)
.property("RealToSimCap", &TimerMan::GetRealToSimCap)
.property("DeltaTimeTicks", &LuaAdaptersTimerMan::GetDeltaTimeTicks, &TimerMan::SetDeltaTimeTicks)
.property("DeltaTimeSecs", &TimerMan::GetDeltaTimeSecs, &TimerMan::SetDeltaTimeSecs)
.property("DeltaTimeMS", &TimerMan::GetDeltaTimeMS)
.property("AIDeltaTimeSecs", &TimerMan::GetAIDeltaTimeSecs)
.property("AIDeltaTimeMS", &TimerMan::GetAIDeltaTimeMS)
.property("OneSimUpdatePerFrame", &TimerMan::IsOneSimUpdatePerFrame, &TimerMan::SetOneSimUpdatePerFrame)

.property("TicksPerSecond", &LuaAdaptersTimerMan::GetTicksPerSecond)

Expand Down
12 changes: 6 additions & 6 deletions Managers/AudioMan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ namespace RTE {
FMOD_RESULT status = FMOD_OK;

float globalPitch = 1.0F;
if (g_TimerMan.IsOneSimUpdatePerFrame()) {
float simSpeed = g_TimerMan.GetSimSpeed();
// Soften the ratio of the pitch adjustment so it's not such an extreme effect on the audio.
// TODO: This coefficient should probably move to SettingsMan and be loaded from ini. That way this effect can be lessened or even turned off entirely by users. 0.35 is a good default value though.
globalPitch = simSpeed + (1.0F - simSpeed) * 0.35F;
}

float simSpeed = g_TimerMan.GetSimSpeed();
// Soften the ratio of the pitch adjustment so it's not such an extreme effect on the audio.
// TODO: This coefficient should probably move to SettingsMan and be loaded from ini. That way this effect can be lessened or even turned off entirely by users. 0.35 is a good default value though.
globalPitch = simSpeed + (1.0F - simSpeed) * 0.35F;

SetGlobalPitch(globalPitch);

if (!g_ActivityMan.ActivityPaused()) {
Expand Down
22 changes: 8 additions & 14 deletions Managers/PerformanceMan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,43 +133,37 @@ namespace RTE {
std::snprintf(str, sizeof(str), "Time Scale: x%.2f ([1]-, [2]+, [Ctrl+1]Rst) | Sim Speed: x%.2f", g_TimerMan.GetTimeScale(), g_TimerMan.GetSimSpeed());
guiFont->DrawAligned(&drawBitmap, c_StatsOffsetX, c_StatsHeight + 20, str, GUIFont::Left);

std::snprintf(str, sizeof(str), "Real to Sim Cap: %.2f ms ([3]-, [4]+, [Ctrl+3]Rst)", g_TimerMan.GetRealToSimCap() * 1000.0F);
guiFont->DrawAligned(&drawBitmap, c_StatsOffsetX, c_StatsHeight + 30, str, GUIFont::Left);

float deltaTime = g_TimerMan.GetDeltaTimeMS();
std::snprintf(str, sizeof(str), "DeltaTime: %.2f ms ([5]-, [6]+, [Ctrl+5]Rst)", deltaTime);
guiFont->DrawAligned(&drawBitmap, c_StatsOffsetX, c_StatsHeight + 40, str, GUIFont::Left);
guiFont->DrawAligned(&drawBitmap, c_StatsOffsetX, c_StatsHeight + 30, str, GUIFont::Left);

std::snprintf(str, sizeof(str), "Actors: %li", g_MovableMan.GetActorCount());
guiFont->DrawAligned(&drawBitmap, c_StatsOffsetX, c_StatsHeight + 50, str, GUIFont::Left);
guiFont->DrawAligned(&drawBitmap, c_StatsOffsetX, c_StatsHeight + 40, str, GUIFont::Left);

std::snprintf(str, sizeof(str), "Particles: %li", g_MovableMan.GetParticleCount());
guiFont->DrawAligned(&drawBitmap, c_StatsOffsetX, c_StatsHeight + 60, str, GUIFont::Left);
guiFont->DrawAligned(&drawBitmap, c_StatsOffsetX, c_StatsHeight + 50, str, GUIFont::Left);

std::snprintf(str, sizeof(str), "Objects: %i", g_MovableMan.GetKnownObjectsCount());
guiFont->DrawAligned(&drawBitmap, c_StatsOffsetX, c_StatsHeight + 70, str, GUIFont::Left);
guiFont->DrawAligned(&drawBitmap, c_StatsOffsetX, c_StatsHeight + 60, str, GUIFont::Left);

std::snprintf(str, sizeof(str), "MOIDs: %i", g_MovableMan.GetMOIDCount());
guiFont->DrawAligned(&drawBitmap, c_StatsOffsetX, c_StatsHeight + 80, str, GUIFont::Left);

std::snprintf(str, sizeof(str), "Sim Updates Since Last Drawn: %i", g_TimerMan.SimUpdatesSinceDrawn());
guiFont->DrawAligned(&drawBitmap, c_StatsOffsetX, c_StatsHeight + 90, str, GUIFont::Left);
guiFont->DrawAligned(&drawBitmap, c_StatsOffsetX, c_StatsHeight + 70, str, GUIFont::Left);

if (int totalPlayingChannelCount = 0, realPlayingChannelCount = 0; g_AudioMan.GetPlayingChannelCount(&totalPlayingChannelCount, &realPlayingChannelCount)) {
std::snprintf(str, sizeof(str), "Sound Channels: %d / %d Real | %d / %d Virtual", realPlayingChannelCount, g_AudioMan.GetTotalRealChannelCount(), totalPlayingChannelCount - realPlayingChannelCount, g_AudioMan.GetTotalVirtualChannelCount());
}
guiFont->DrawAligned(&drawBitmap, c_StatsOffsetX, c_StatsHeight + 100, str, GUIFont::Left);
guiFont->DrawAligned(&drawBitmap, c_StatsOffsetX, c_StatsHeight + 80, str, GUIFont::Left);

if (!m_SortedScriptTimings.empty()) {
std::snprintf(str, sizeof(str), "Lua scripts taking the most time to call Update() this frame:");
guiFont->DrawAligned(&drawBitmap, c_StatsOffsetX, c_StatsHeight + 120, str, GUIFont::Left);
guiFont->DrawAligned(&drawBitmap, c_StatsOffsetX, c_StatsHeight + 100, str, GUIFont::Left);

for (int i = 0; i < std::min((size_t)3, m_SortedScriptTimings.size()); i++)
{
std::pair<std::string, ScriptTiming> scriptTiming = m_SortedScriptTimings.at(i);

std::snprintf(str, sizeof(str), "%.1fms total with %i calls in %s", scriptTiming.second.m_Time / 1000.0, scriptTiming.second.m_CallCount, scriptTiming.first.c_str());
guiFont->DrawAligned(&drawBitmap, c_StatsOffsetX, c_StatsHeight + 130 + i * 10, str, GUIFont::Left);
guiFont->DrawAligned(&drawBitmap, c_StatsOffsetX, c_StatsHeight + 110 + i * 10, str, GUIFont::Left);
}
}

Expand Down
2 changes: 0 additions & 2 deletions Managers/SettingsMan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ namespace RTE {
MatchProperty("EnableParticleSettling", { reader >> g_MovableMan.m_SettlingEnabled; });
MatchProperty("EnableMOSubtraction", { reader >> g_MovableMan.m_MOSubtractionEnabled; });
MatchProperty("DeltaTime", { g_TimerMan.SetDeltaTimeSecs(std::stof(reader.ReadPropValue())); });
MatchProperty("RealToSimCap", { g_TimerMan.SetRealToSimCap(std::stof(reader.ReadPropValue())); });
MatchProperty("AllowSavingToBase", { reader >> m_AllowSavingToBase; });
MatchProperty("ShowMetaScenes", { reader >> m_ShowMetaScenes; });
MatchProperty("SkipIntro", { reader >> m_SkipIntro; });
Expand Down Expand Up @@ -319,7 +318,6 @@ namespace RTE {
writer.NewPropertyWithValue("EnableParticleSettling", g_MovableMan.m_SettlingEnabled);
writer.NewPropertyWithValue("EnableMOSubtraction", g_MovableMan.m_MOSubtractionEnabled);
writer.NewPropertyWithValue("DeltaTime", g_TimerMan.GetDeltaTimeSecs());
writer.NewPropertyWithValue("RealToSimCap", g_TimerMan.GetRealToSimCap());

// No experimental settings right now :)
//writer.NewLine(false, 2);
Expand Down
24 changes: 14 additions & 10 deletions Managers/TimerMan.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "TimerMan.h"

#include "Constants.h"
#include "PerformanceMan.h"
#include "SettingsMan.h"
Expand All @@ -17,7 +18,6 @@ namespace RTE {
m_StartTime = std::chrono::steady_clock::now();
m_TicksPerSecond = 1000000;
m_RealTimeTicks = 0;
m_RealToSimCap = 0.0F;
m_SimTimeTicks = 0;
m_SimUpdateCount = 0;
m_SimAccumulator = 0;
Expand All @@ -29,7 +29,6 @@ namespace RTE {
m_SimSpeed = 1.0F;
m_TimeScale = 1.0F;
m_SimPaused = false;
m_OneSimUpdatePerFrame = true;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -39,7 +38,6 @@ namespace RTE {

ResetTime();
if (m_DeltaTimeS <= 0) { SetDeltaTimeSecs(c_DefaultDeltaTimeS); }
if (m_RealToSimCap <= 0) { SetRealToSimCap(c_DefaultRealToSimCap); }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -48,6 +46,12 @@ namespace RTE {
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now().time_since_epoch()).count();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

float TimerMan::GetRealToSimCap() const {
return c_RealToSimCap;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

float TimerMan::GetAIDeltaTimeSecs() const {
Expand Down Expand Up @@ -92,23 +96,23 @@ namespace RTE {
long long prevTime = m_RealTimeTicks;
m_RealTimeTicks = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now() - m_StartTime).count();

// Cap timeIncrease if too long (as when the app went out of focus), to m_RealToSimCap.
long long timeIncrease = std::min(m_RealTimeTicks - prevTime, m_RealToSimCap);
// Cap timeIncrease if too long (as when the app went out of focus), to c_RealToSimCap.
long long timeIncrease = std::min(m_RealTimeTicks - prevTime, static_cast<long long>(c_RealToSimCap * m_TicksPerSecond));

RTEAssert(timeIncrease > 0, "It seems your CPU is giving bad timing data to the game, this is known to happen on some multi-core processors. This may be fixed by downloading the latest CPU drivers from AMD or Intel.");

// If not paused, add the new time difference to the sim accumulator, scaling by the TimeScale.
if (!m_SimPaused) {
m_SimAccumulator += static_cast<long long>(static_cast<float>(timeIncrease) * m_TimeScale);
// If not paused, add the new time difference to the sim accumulator
if (!m_SimPaused) {
m_SimAccumulator += static_cast<long long>(static_cast<float>(timeIncrease) * m_TimeScale);
}

// Make sure we don't get runaway behind schedule
m_SimAccumulator = std::min(m_SimAccumulator, m_DeltaTime * 2);
m_SimAccumulator = std::min(m_SimAccumulator, m_DeltaTime + (m_DeltaTime / 2));

RTEAssert(m_SimAccumulator >= 0, "Negative sim time accumulator?!");

// Reset the counter since the last drawn update. Set it negative since we're counting full pure sim updates and this will be incremented to 0 on next SimUpdate.
if (m_DrawnSimUpdate || m_OneSimUpdatePerFrame) {
if (m_DrawnSimUpdate) {
m_SimUpdatesSinceDrawn = -1;
}

Expand Down
22 changes: 1 addition & 21 deletions Managers/TimerMan.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,7 @@ namespace RTE {
/// Gets the cap of the amount of seconds which can be transferred from the real time to the simulated time in one update.
/// </summary>
/// <returns>A float describing the current cap in seconds.</returns>
float GetRealToSimCap() const { return static_cast<float>(m_RealToSimCap) / static_cast<float>(m_TicksPerSecond); }

/// <summary>
/// Sets the cap of the amount of seconds which can be transferred from the real time to the simulated time in one update.
/// </summary>
/// <param name="newCap">A float specifying the new cap in seconds.</param>
void SetRealToSimCap(float newCap) { m_RealToSimCap = static_cast<long long>(newCap * static_cast<float>(m_TicksPerSecond)); }

/// <summary>
/// Shows whether to force this to artificially make time for only one single sim update for the graphics frame. Useful for debugging or profiling.
/// </summary>
/// <returns>Whether the sim should be set to only update once per graphics frame or not.</returns>
bool IsOneSimUpdatePerFrame() const { return m_OneSimUpdatePerFrame; }

/// <summary>
/// Sets whether to force this to artificially make time for only one single sim update for the graphics frame. Useful for debugging or profiling.
/// </summary>
/// <param name="oneUpdate">Whether the sim should be set to only update once per graphics frame or not.</param>
void SetOneSimUpdatePerFrame(bool oneUpdate = true) { m_OneSimUpdatePerFrame = oneUpdate; }
float GetRealToSimCap() const;

/// <summary>
/// Gets the number of ticks per second (the resolution of the timer).
Expand Down Expand Up @@ -212,7 +194,6 @@ namespace RTE {
std::chrono::steady_clock::time_point m_StartTime; //!< The point in real time when the simulation (re)started.
long long m_TicksPerSecond; //!< The frequency of ticks each second, ie the resolution of the timer.
long long m_RealTimeTicks; //!< The number of actual microseconds counted so far.
long long m_RealToSimCap; //!< The cap of number of ticks that the real time can add to the accumulator each update.
long long m_SimTimeTicks; //!< The number of simulation time ticks counted so far.
long long m_SimUpdateCount; //!< The number of whole simulation updates have been made since reset.
long long m_SimAccumulator; //!< Simulation time accumulator keeps track of how much actual time has passed and is chunked into whole DeltaTime:s upon UpdateSim.
Expand All @@ -228,7 +209,6 @@ namespace RTE {
float m_TimeScale; //!< The relationship between the real world actual time and the simulation time. A value of 2.0 means simulation runs twice as fast as normal, as perceived by a player.

bool m_SimPaused; //!< Simulation paused; no real time ticks will go to the sim accumulator.
bool m_OneSimUpdatePerFrame; //!< Whether to force this to artificially make time for only one single sim update for the graphics frame. Useful for debugging or profiling.

private:

Expand Down
13 changes: 0 additions & 13 deletions Managers/UInputMan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,18 +942,13 @@ namespace RTE {
// Ctrl+P to toggle performance stats
} else if (KeyPressed(SDLK_p)) {
g_PerformanceMan.ShowPerformanceStats(!g_PerformanceMan.IsShowingPerformanceStats());
// Ctrl+O to toggle one sim update per frame
} else if (KeyPressed(SDLK_o)) {
g_TimerMan.SetOneSimUpdatePerFrame(!g_TimerMan.IsOneSimUpdatePerFrame());
} else if (KeyPressed(SDLK_F2)) {
g_PresetMan.QuickReloadEntityPreset();
} else if (KeyPressed(SDLK_F9)) {
g_ActivityMan.LoadAndLaunchGame("AutoSave");
} else if (g_PerformanceMan.IsShowingPerformanceStats()) {
if (KeyHeld(SDLK_1)) {
g_TimerMan.SetTimeScale(1.0F);
} else if (KeyHeld(SDLK_3)) {
g_TimerMan.SetRealToSimCap(c_DefaultRealToSimCap);
} else if (KeyHeld(SDLK_5)) {
g_TimerMan.SetDeltaTimeSecs(c_DefaultDeltaTimeS);
}
Expand Down Expand Up @@ -1005,14 +1000,6 @@ namespace RTE {
g_TimerMan.SetTimeScale(g_TimerMan.GetTimeScale() - 0.01F);
}

// Manipulate real to sim cap
if (KeyHeld(SDLK_4)) {
g_TimerMan.SetRealToSimCap(g_TimerMan.GetRealToSimCap() + 0.001F);
}
if (KeyHeld(SDLK_3) && g_TimerMan.GetRealToSimCap() > 0) {
g_TimerMan.SetRealToSimCap(g_TimerMan.GetRealToSimCap() - 0.001F);
}

// Manipulate DeltaTime
if (KeyHeld(SDLK_6)) {
g_TimerMan.SetDeltaTimeSecs(g_TimerMan.GetDeltaTimeSecs() + 0.001F);
Expand Down
2 changes: 1 addition & 1 deletion System/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace RTE {

#pragma region Time Constants
static constexpr float c_DefaultDeltaTimeS = 0.0166666F; //!< The default simulation update step size, in seconds.
static constexpr float c_DefaultRealToSimCap = 0.0333333F; //!< The default cap of number of ticks that the real time can add to the tick accumulator each update.
static constexpr float c_RealToSimCap = 0.0166666F; //!< The default cap of number of ticks that the real time can add to the tick accumulator each update.
#pragma endregion

#pragma region AI Constants
Expand Down

0 comments on commit 3ab51fe

Please sign in to comment.