From 6a0621a4da35a236b1c3a4a578cdd16fa7bc1af1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Gallet?= Date: Fri, 6 May 2022 17:29:58 +0200 Subject: [PATCH] Users can set the initial monotonic time (#480) --- CHANGELOG.md | 1 + src/program/GameLoop.cpp | 26 +++++++++++++++++--------- src/program/movie/MovieFileHeader.cpp | 4 ++++ src/program/ui/MainWindow.cpp | 17 ++++++++++++----- src/shared/SharedConfig.h | 10 ++++++++-- 5 files changed, 42 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 495dd5a5..eff1e6a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ * Split deterministic timer into monotonic/realtime (#474) * Users can change realtime (#474) +* Users can set the initial monotonic time (#480) ### Changed ### Fixed diff --git a/src/program/GameLoop.cpp b/src/program/GameLoop.cpp index 3fe28cfd..a74fedc1 100644 --- a/src/program/GameLoop.cpp +++ b/src/program/GameLoop.cpp @@ -249,8 +249,8 @@ void GameLoop::init() /* Set the current realtime to the initial time, except when restarting */ if (context->status != Context::RESTARTING) { - context->current_time_sec = 0; - context->current_time_nsec = 0; + context->current_time_sec = context->config.sc.initial_monotonic_time_sec; + context->current_time_nsec = context->config.sc.initial_monotonic_time_nsec; context->current_realtime_sec = context->config.sc.initial_time_sec; context->current_realtime_nsec = context->config.sc.initial_time_nsec; context->new_realtime_sec = context->current_realtime_sec; @@ -419,8 +419,12 @@ bool GameLoop::startFrameMessages() if (!notTruncInputs || (context->framecount > context->config.sc.movie_framecount)) { context->config.sc.movie_framecount = context->framecount; - context->movie_time_sec = context->current_time_sec; - context->movie_time_nsec = context->current_time_nsec; + context->movie_time_sec = context->current_time_sec - context->config.sc.initial_monotonic_time_sec; + context->movie_time_nsec = context->current_time_nsec - context->config.sc.initial_monotonic_time_nsec; + if (context->movie_time_nsec < 0) { + context->movie_time_nsec += 1000000000; + context->movie_time_sec--; + } } } break; @@ -682,14 +686,18 @@ void GameLoop::processInputs(AllInputs &ai) /* First frame after movie end */ if (ret == -2) { /* Check for the moviefile length */ + uint64_t cur_sec, cur_nsec; + cur_sec = context->current_time_sec - context->config.sc.initial_monotonic_time_sec; + cur_nsec = context->current_time_nsec - context->config.sc.initial_monotonic_time_nsec; + if ((context->movie_time_sec != -1) && - ((context->movie_time_sec != context->current_time_sec) || - (context->movie_time_nsec != context->current_time_nsec))) { + ((context->movie_time_sec != cur_sec) || + (context->movie_time_nsec != cur_nsec))) { - emit alertToShow(QString("Movie length mismatch. Metadata stores %1.%2 seconds but end time is %3.%4 seconds.").arg(context->movie_time_sec).arg(context->movie_time_nsec, 9, 10, QChar('0')).arg(context->current_time_sec).arg(context->current_time_nsec, 9, 10, QChar('0'))); + emit alertToShow(QString("Movie length mismatch. Metadata stores %1.%2 seconds but end time is %3.%4 seconds.").arg(context->movie_time_sec).arg(context->movie_time_nsec, 9, 10, QChar('0')).arg(cur_sec).arg(cur_nsec, 9, 10, QChar('0'))); } - context->movie_time_sec = context->current_time_sec; - context->movie_time_nsec = context->current_time_nsec; + context->movie_time_sec = cur_sec; + context->movie_time_nsec = cur_nsec; } } diff --git a/src/program/movie/MovieFileHeader.cpp b/src/program/movie/MovieFileHeader.cpp index 2634d924..ea58cd0b 100644 --- a/src/program/movie/MovieFileHeader.cpp +++ b/src/program/movie/MovieFileHeader.cpp @@ -50,6 +50,8 @@ void MovieFileHeader::load() context->config.sc.nb_controllers = config.value("nb_controllers").toInt(); context->config.sc.initial_time_sec = config.value("initial_time_sec").toULongLong(); context->config.sc.initial_time_nsec = config.value("initial_time_nsec").toULongLong(); + context->config.sc.initial_monotonic_time_sec = config.value("initial_monotonic_time_sec").toULongLong(); + context->config.sc.initial_monotonic_time_nsec = config.value("initial_monotonic_time_nsec").toULongLong(); framerate_num = config.value("framerate_num").toUInt(); framerate_den = config.value("framerate_den").toUInt(); @@ -132,6 +134,8 @@ void MovieFileHeader::save(uint64_t tot_frames, uint64_t nb_frames) config.setValue("nb_controllers", context->config.sc.nb_controllers); config.setValue("initial_time_sec", static_cast(context->config.sc.initial_time_sec)); config.setValue("initial_time_nsec", static_cast(context->config.sc.initial_time_nsec)); + config.setValue("initial_monotonic_time_sec", static_cast(context->config.sc.initial_monotonic_time_sec)); + config.setValue("initial_monotonic_time_nsec", static_cast(context->config.sc.initial_monotonic_time_nsec)); config.setValue("length_sec", static_cast(context->movie_time_sec)); config.setValue("length_nsec", static_cast(context->movie_time_nsec)); config.setValue("framerate_num", framerate_num); diff --git a/src/program/ui/MainWindow.cpp b/src/program/ui/MainWindow.cpp index dcd8741d..114e34d9 100644 --- a/src/program/ui/MainWindow.cpp +++ b/src/program/ui/MainWindow.cpp @@ -200,11 +200,9 @@ MainWindow::MainWindow(Context* c) : QMainWindow(), context(c) elapsedTimeSec = new QSpinBox(); elapsedTimeSec->setMaximum(std::numeric_limits::max()); elapsedTimeSec->setMinimumWidth(50); - elapsedTimeSec->setReadOnly(true); elapsedTimeNsec = new QSpinBox(); elapsedTimeNsec->setMaximum(std::numeric_limits::max()); elapsedTimeNsec->setMinimumWidth(50); - elapsedTimeNsec->setReadOnly(true); realTimeSec = new QSpinBox(); realTimeSec->setMinimum(1); @@ -1004,8 +1002,10 @@ void MainWindow::updateStatus() fpsNumField->setReadOnly(false); fpsDenField->setReadOnly(false); - elapsedTimeSec->setValue(0); - elapsedTimeNsec->setValue(0); + elapsedTimeSec->setReadOnly(false); + elapsedTimeNsec->setReadOnly(false); + elapsedTimeSec->setValue(context->config.sc.initial_monotonic_time_sec); + elapsedTimeNsec->setValue(context->config.sc.initial_monotonic_time_nsec); realTimeSec->setValue(context->config.sc.initial_time_sec); realTimeNsec->setValue(context->config.sc.initial_time_nsec); @@ -1042,6 +1042,8 @@ void MainWindow::updateStatus() fpsNumField->setReadOnly(true); fpsDenField->setReadOnly(true); } + elapsedTimeSec->setReadOnly(true); + elapsedTimeNsec->setReadOnly(true); launchGdbButton->setEnabled(false); @@ -1152,7 +1154,8 @@ void MainWindow::updateUIFrequent() realTimeNsec->setValue(context->new_realtime_nsec); /* Update movie time */ - double sec = context->current_time_sec + ((double) context->current_time_nsec)/1000000000; + double sec = (context->current_time_sec - context->config.sc.initial_monotonic_time_sec) + + ((double) (context->current_time_nsec - context->config.sc.initial_monotonic_time_nsec))/1000000000; int imin = (int)(sec/60); double dsec = sec - 60*imin; currentLength->setText(QString("Current Time: %1m %2s").arg(imin).arg(dsec, 0, 'f', 2)); @@ -1297,6 +1300,8 @@ void MainWindow::updateMovieParams() setRadioFromList(joystickGroup, context->config.sc.nb_controllers); fpsNumField->setValue(context->config.sc.framerate_num); fpsDenField->setValue(context->config.sc.framerate_den); + elapsedTimeSec->setValue(context->config.sc.initial_monotonic_time_sec); + elapsedTimeNsec->setValue(context->config.sc.initial_monotonic_time_nsec); realTimeSec->setValue(context->config.sc.initial_time_sec); realTimeNsec->setValue(context->config.sc.initial_time_nsec); autoRestartAction->setChecked(context->config.auto_restart); @@ -1443,6 +1448,8 @@ void MainWindow::slotLaunch(bool attach_gdb) /* Set a few parameters */ context->config.sc.framerate_num = fpsNumField->value(); context->config.sc.framerate_den = fpsDenField->value(); + context->config.sc.initial_monotonic_time_sec = elapsedTimeSec->value(); + context->config.sc.initial_monotonic_time_nsec = elapsedTimeNsec->value(); context->config.sc.initial_time_sec = realTimeSec->value(); context->config.sc.initial_time_nsec = realTimeNsec->value(); diff --git a/src/shared/SharedConfig.h b/src/shared/SharedConfig.h index 815ef909..b2fe8663 100644 --- a/src/shared/SharedConfig.h +++ b/src/shared/SharedConfig.h @@ -156,8 +156,14 @@ struct __attribute__((packed, aligned(8))) SharedConfig { /* Initial system time at game startup */ /* We don't use struct timespec because it contains longs so the size * depends on the arch.*/ - int64_t initial_time_sec = 1; - int64_t initial_time_nsec = 0; + uint64_t initial_time_sec = 1; + uint64_t initial_time_nsec = 0; + + /* Initial monotonic time at game startup. */ + /* We don't use struct timespec because it contains longs so the size + * depends on the arch.*/ + uint64_t initial_monotonic_time_sec = 0; + uint64_t initial_monotonic_time_nsec = 0; /* Virtual monitor resolution */ int screen_width = 0;