Skip to content

Commit

Permalink
Users can set the initial monotonic time (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
clementgallet committed May 6, 2022
1 parent 1619d80 commit 6a0621a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 17 additions & 9 deletions src/program/GameLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/program/movie/MovieFileHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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<unsigned long long>(context->config.sc.initial_time_sec));
config.setValue("initial_time_nsec", static_cast<unsigned long long>(context->config.sc.initial_time_nsec));
config.setValue("initial_monotonic_time_sec", static_cast<unsigned long long>(context->config.sc.initial_monotonic_time_sec));
config.setValue("initial_monotonic_time_nsec", static_cast<unsigned long long>(context->config.sc.initial_monotonic_time_nsec));
config.setValue("length_sec", static_cast<unsigned long long>(context->movie_time_sec));
config.setValue("length_nsec", static_cast<unsigned long long>(context->movie_time_nsec));
config.setValue("framerate_num", framerate_num);
Expand Down
17 changes: 12 additions & 5 deletions src/program/ui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,9 @@ MainWindow::MainWindow(Context* c) : QMainWindow(), context(c)
elapsedTimeSec = new QSpinBox();
elapsedTimeSec->setMaximum(std::numeric_limits<int>::max());
elapsedTimeSec->setMinimumWidth(50);
elapsedTimeSec->setReadOnly(true);
elapsedTimeNsec = new QSpinBox();
elapsedTimeNsec->setMaximum(std::numeric_limits<int>::max());
elapsedTimeNsec->setMinimumWidth(50);
elapsedTimeNsec->setReadOnly(true);

realTimeSec = new QSpinBox();
realTimeSec->setMinimum(1);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -1042,6 +1042,8 @@ void MainWindow::updateStatus()
fpsNumField->setReadOnly(true);
fpsDenField->setReadOnly(true);
}
elapsedTimeSec->setReadOnly(true);
elapsedTimeNsec->setReadOnly(true);

launchGdbButton->setEnabled(false);

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand Down
10 changes: 8 additions & 2 deletions src/shared/SharedConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6a0621a

Please sign in to comment.