Skip to content

Commit ab7a1d8

Browse files
committed
some fixes for playing movies via script
The movie player code was missing some important functionality, in particular: 1) There was only one timing method to get real-time elapsed time, which was not suitable for pausing or time compression 2) There was an improper cast for the FPS property, causing a script error 3) The `getSeconds` method lost precision during casting This patches those deficiencies.
1 parent e8d379a commit ab7a1d8

File tree

5 files changed

+14
-2
lines changed

5 files changed

+14
-2
lines changed

code/io/timer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,3 +642,8 @@ fix timestamp_get_mission_time()
642642

643643
return static_cast<fix>(time / MICROSECONDS_PER_SECOND);
644644
}
645+
646+
uint64_t timestamp_get_mission_time_in_microseconds()
647+
{
648+
return timestamp_get_microseconds() - Timestamp_microseconds_at_mission_start;
649+
}

code/io/timer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ extern int timer_get_seconds(); // seconds since program started... not accur
106106
constexpr int MILLISECONDS_PER_SECOND = 1000;
107107
constexpr uint64_t MICROSECONDS_PER_SECOND = 1000000;
108108
constexpr uint64_t NANOSECONDS_PER_SECOND = 1000000000;
109+
constexpr uint64_t NANOSECONDS_PER_MICROSECOND = 1000;
109110

110111
// use this call to get the current counter value (which represents the time at the time
111112
// this function is called). I.e. it doesn't return a count that would be in the future,
@@ -225,5 +226,6 @@ void timestamp_start_mission();
225226

226227
// Calculate the current mission time using the timestamps
227228
fix timestamp_get_mission_time();
229+
uint64_t timestamp_get_mission_time_in_microseconds();
228230

229231
#endif

code/scripting/api/libs/time_lib.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,10 @@ ADE_FUNC(getCurrentTime, l_Time, nullptr, "Gets the current real-time timestamp.
1515
return ade_set_args(L, "o", l_Timestamp.Set(timer_get_nanoseconds()));
1616
}
1717

18+
ADE_FUNC(getCurrentMissionTime, l_Time, nullptr, "Gets the current mission-time timestamp.", "timestamp", "The current mission time")
19+
{
20+
return ade_set_args(L, "o", l_Timestamp.Set(timestamp_get_mission_time_in_microseconds() * NANOSECONDS_PER_MICROSECOND));
21+
}
22+
1823
} // namespace api
1924
} // namespace scripting

code/scripting/api/objs/movie_player.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ ADE_VIRTVAR(FPS, l_MoviePlayer, "number", "Determines the frames per second of t
6363
LuaError(L, "Tried to modify read-only member!");
6464
}
6565

66-
return ade_set_args(L, "f", (int)ph->player()->getMovieProperties().fps);
66+
return ade_set_args(L, "f", ph->player()->getMovieProperties().fps);
6767
}
6868

6969
ADE_FUNC(update, l_MoviePlayer, "timespan step_time",

code/scripting/api/objs/time_obj.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ ADE_FUNC(getSeconds, l_TimeSpan, nullptr, "Gets the value of this timestamp in s
3131
}
3232

3333
// Timestamps and spans are stored in nanoseconds
34-
return ade_set_args(L, "f", (float)value / 1000000000.f);
34+
return ade_set_args(L, "f", (float)((long double)value / (long double)NANOSECONDS_PER_SECOND));
3535
}
3636

3737
} // namespace api

0 commit comments

Comments
 (0)