mirror of
https://github.com/rwengine/openrw.git
synced 2024-09-15 15:02:34 +02:00
Improved game loop to support game speed
This commit is contained in:
parent
5f85de5bd3
commit
214095ba02
@ -11,7 +11,7 @@ BasicState::BasicState()
|
||||
, gameMinute{0}
|
||||
, padMode{0}
|
||||
, timeMS{0}
|
||||
, timeScale{0}
|
||||
, timeScale{1.f}
|
||||
, timeStep{0}
|
||||
, timeStep_unclipped{0}
|
||||
, frameCounter{0}
|
||||
|
@ -32,9 +32,8 @@ std::map<GameRenderer::SpecialModel, std::string> kSpecialModels = {
|
||||
{GameRenderer::Arrow, "arrow.dff"}};
|
||||
|
||||
namespace {
|
||||
constexpr float kPhysicsTimeStep = 1.0f/30.0f;
|
||||
constexpr float kMaxPhysicsSubSteps = 4;
|
||||
}
|
||||
constexpr float kMaxPhysicsSubSteps = 2;
|
||||
} // namespace
|
||||
|
||||
#define MOUSE_SENSITIVITY_SCALE 2.5f
|
||||
|
||||
@ -75,7 +74,8 @@ RWGame::RWGame(Logger& log, int argc, char* argv[])
|
||||
btIDebugDraw::DBG_DrawConstraintLimits);
|
||||
debug.setShaderProgram(renderer.worldProg.get());
|
||||
|
||||
data.loadDynamicObjects((config.getGameDataPath() / "data/object.dat").string()); // FIXME: use path
|
||||
data.loadDynamicObjects((config.getGameDataPath() / "data/object.dat")
|
||||
.string()); // FIXME: use path
|
||||
|
||||
data.loadGXT("text/" + config.getGameLanguage() + ".gxt");
|
||||
|
||||
@ -364,7 +364,9 @@ void RWGame::handleCheatInput(char symbol) {
|
||||
|
||||
int RWGame::run() {
|
||||
namespace chrono = std::chrono;
|
||||
|
||||
auto lastFrame = chrono::steady_clock::now();
|
||||
float deltaTime = GAME_TIMESTEP;
|
||||
float accumulatedTime = 0.0f;
|
||||
|
||||
// Loop until we run out of states.
|
||||
@ -412,37 +414,48 @@ int RWGame::run() {
|
||||
}
|
||||
RW_PROFILE_END();
|
||||
|
||||
auto now = chrono::steady_clock::now();
|
||||
auto deltaTime = chrono::duration<float>(now - lastFrame).count();
|
||||
lastFrame = now;
|
||||
auto currentFrame = chrono::steady_clock::now();
|
||||
auto frameTime =
|
||||
chrono::duration<float>(currentFrame - lastFrame).count();
|
||||
lastFrame = currentFrame;
|
||||
|
||||
if(!world->isPaused()) {
|
||||
accumulatedTime += deltaTime;
|
||||
world->dynamicsWorld->stepSimulation(deltaTime * timescale, kMaxPhysicsSubSteps, kPhysicsTimeStep);
|
||||
// Clamp frameTime, so we won't freeze completely
|
||||
if (frameTime > 0.1f) {
|
||||
frameTime = 0.1f;
|
||||
}
|
||||
|
||||
accumulatedTime += frameTime;
|
||||
|
||||
auto deltaTimeWithTimeScale = deltaTime * world->state->basic.timeScale;
|
||||
|
||||
RW_PROFILE_BEGIN("Update");
|
||||
while (accumulatedTime >= GAME_TIMESTEP && !world->isPaused()) {
|
||||
while (accumulatedTime >= deltaTime && !world->isPaused()) {
|
||||
if (!StateManager::currentState()) {
|
||||
break;
|
||||
}
|
||||
accumulatedTime -= GAME_TIMESTEP;
|
||||
|
||||
RW_PROFILE_BEGIN("physics");
|
||||
world->dynamicsWorld->stepSimulation(
|
||||
deltaTimeWithTimeScale, kMaxPhysicsSubSteps, deltaTime);
|
||||
RW_PROFILE_END();
|
||||
|
||||
RW_PROFILE_BEGIN("state");
|
||||
StateManager::get().tick(GAME_TIMESTEP * timescale);
|
||||
StateManager::get().tick(deltaTimeWithTimeScale);
|
||||
RW_PROFILE_END();
|
||||
|
||||
RW_PROFILE_BEGIN("engine");
|
||||
tick(GAME_TIMESTEP * timescale);
|
||||
tick(deltaTimeWithTimeScale);
|
||||
RW_PROFILE_END();
|
||||
|
||||
getState()->swapInputState();
|
||||
|
||||
accumulatedTime -= deltaTime;
|
||||
}
|
||||
RW_PROFILE_END();
|
||||
|
||||
RW_PROFILE_BEGIN("Render");
|
||||
RW_PROFILE_BEGIN("engine");
|
||||
render(1, deltaTime);
|
||||
render(1, frameTime);
|
||||
RW_PROFILE_END();
|
||||
|
||||
RW_PROFILE_BEGIN("state");
|
||||
@ -514,7 +527,6 @@ void RWGame::tick(float dt) {
|
||||
|
||||
state.text.tick(dt);
|
||||
|
||||
|
||||
if (vm) {
|
||||
try {
|
||||
vm->execute(dt);
|
||||
@ -808,19 +820,21 @@ void RWGame::renderProfile() {
|
||||
ti.font = 2;
|
||||
ti.size = lineHeight - 2.f;
|
||||
ti.baseColour = glm::u8vec3(255);
|
||||
std::function<void(const perf::ProfileEntry&, int)> renderEntry = [&](
|
||||
const perf::ProfileEntry& entry, int depth) {
|
||||
std::function<void(const perf::ProfileEntry&, int)> renderEntry =
|
||||
[&](const perf::ProfileEntry& entry, int depth) {
|
||||
int g = 0;
|
||||
for (auto& event : entry.childProfiles) {
|
||||
auto duration = event.end - event.start;
|
||||
float y = 60.f + (depth * (lineHeight + 5.f));
|
||||
renderer.drawColour(
|
||||
perf_colours[(std::hash<std::string>()(entry.label) * (g++)) %
|
||||
perf_colours[(std::hash<std::string>()(entry.label) *
|
||||
(g++)) %
|
||||
perf_colours.size()],
|
||||
{xscale * event.start, y, xscale * duration, lineHeight});
|
||||
ti.screenPosition.x = xscale * (event.start);
|
||||
ti.screenPosition.y = y + 2.f;
|
||||
ti.text = GameStringUtil::fromString(event.label + " " + std::to_string(duration) + " us ");
|
||||
ti.text = GameStringUtil::fromString(
|
||||
event.label + " " + std::to_string(duration) + " us ");
|
||||
renderer.text.renderText(ti);
|
||||
renderEntry(event, depth + 1);
|
||||
}
|
||||
@ -845,10 +859,10 @@ void RWGame::globalKeyEvent(const SDL_Event& event) {
|
||||
world->offsetGameTime(30);
|
||||
break;
|
||||
case SDLK_9:
|
||||
timescale *= 0.5f;
|
||||
world->state->basic.timeScale *= 0.5f;
|
||||
break;
|
||||
case SDLK_0:
|
||||
timescale *= 2.0f;
|
||||
world->state->basic.timeScale *= 2.0f;
|
||||
break;
|
||||
case SDLK_F1:
|
||||
toggle_debug(DebugViewMode::General);
|
||||
|
@ -47,8 +47,6 @@ class RWGame : public GameBase {
|
||||
|
||||
std::string cheatInputWindow = std::string(32, ' ');
|
||||
|
||||
float timescale = 1.f;
|
||||
|
||||
public:
|
||||
RWGame(Logger& log, int argc, char* argv[]);
|
||||
~RWGame() override;
|
||||
|
Loading…
Reference in New Issue
Block a user