Files
fs2open.github.com/code/scripting/api/objs/movie_player.cpp
T
asarium 06a21a0f9b Add scripting API for displaying movie files
This adds an API for the scripting system which allows it to load and
display movie files using the standard movie player code which includes
multi-threaded frame decoding.
2018-07-13 21:54:41 +02:00

191 lines
5.2 KiB
C++

#include "movie_player.h"
#include "time_obj.h"
namespace scripting {
namespace api {
movie_player_h::movie_player_h(std::unique_ptr<cutscene::Player>&& player) : _player(std::move(player)) {}
bool movie_player_h::isValid() const { return (bool)_player; }
cutscene::Player* movie_player_h::player() { return _player.get(); }
ADE_OBJ(l_MoviePlayer, movie_player_h*, "movie_player", "A movie player instance");
ADE_FUNC(__gc, l_MoviePlayer, nullptr, "Cleans up the allocated resources. Should only be used by Lua.", "nothing",
nullptr)
{
movie_player_h* ph = nullptr;
if (!ade_get_args(L, "o", l_MoviePlayer.Get(&ph)))
return ADE_RETURN_NIL;
if (ph == nullptr) {
return ADE_RETURN_NIL;
}
// Clean up the pointer
delete ph;
return ADE_RETURN_NIL;
}
ADE_VIRTVAR(Width, l_MoviePlayer, "number", "Determines the width in pixels of this movie <b>Read-only</b>", "number",
"The width of the movie or -1 if handle is invalid")
{
movie_player_h* ph = nullptr;
if (!ade_get_args(L, "o", l_MoviePlayer.Get(&ph)))
return ade_set_args(L, "i", -1);
if (ph == nullptr || !ph->isValid()) {
return ade_set_args(L, "i", -1);
}
if (ADE_SETTING_VAR) {
LuaError(L, "Tried to modify read-only member!");
}
return ade_set_args(L, "i", (int)ph->player()->getMovieProperties().size.width);
}
ADE_VIRTVAR(Height, l_MoviePlayer, "number", "Determines the height in pixels of this movie <b>Read-only</b>", "number",
"The height of the movie or -1 if handle is invalid")
{
movie_player_h* ph = nullptr;
if (!ade_get_args(L, "o", l_MoviePlayer.Get(&ph)))
return ade_set_args(L, "i", -1);
if (ph == nullptr || !ph->isValid()) {
return ade_set_args(L, "i", -1);
}
if (ADE_SETTING_VAR) {
LuaError(L, "Tried to modify read-only member!");
}
return ade_set_args(L, "i", (int)ph->player()->getMovieProperties().size.height);
}
ADE_VIRTVAR(FPS, l_MoviePlayer, "number", "Determines the frames per second of this movie <b>Read-only</b>", "number",
"The FPS of the movie or -1 if handle is invalid")
{
movie_player_h* ph = nullptr;
if (!ade_get_args(L, "o", l_MoviePlayer.Get(&ph)))
return ade_set_args(L, "i", -1);
if (ph == nullptr || !ph->isValid()) {
return ade_set_args(L, "i", -1);
}
if (ADE_SETTING_VAR) {
LuaError(L, "Tried to modify read-only member!");
}
return ade_set_args(L, "f", (int)ph->player()->getMovieProperties().fps);
}
ADE_FUNC(update, l_MoviePlayer, "timespan step_time",
"Updates the current state of the movie and moves the internal timer forward by the specified time span.",
"boolean", "true if there is more to display, false otherwise")
{
movie_player_h* ph = nullptr;
int64_t time_diff = 0;
if (!ade_get_args(L, "oo", l_MoviePlayer.Get(&ph), l_TimeSpan.Get(&time_diff)))
return ADE_RETURN_FALSE;
if (ph == nullptr || !ph->isValid()) {
return ADE_RETURN_FALSE;
}
auto ret = ph->player()->update(static_cast<uint64_t>(time_diff / 1000));
return ade_set_args(L, "b", ret);
}
ADE_FUNC(isPlaybackReady, l_MoviePlayer, nullptr,
"Determines if the player is ready to display the movie. Since the movie frames are loaded asynchronously "
"there is a short delay between the creation of a player and when it is possible to start displaying the "
"movie. This function can be used to determine if playback is possible at the moment.",
"boolean", "true if playback is ready, false otherwise")
{
movie_player_h* ph = nullptr;
if (!ade_get_args(L, "o", l_MoviePlayer.Get(&ph)))
return ADE_RETURN_FALSE;
if (ph == nullptr || !ph->isValid()) {
return ADE_RETURN_FALSE;
}
auto ret = ph->player()->isPlaybackReady();
return ade_set_args(L, "b", ret);
}
ADE_FUNC(drawMovie, l_MoviePlayer, "number x1, number y1[, number x2, number y2]",
"Draws the current frame of the movie at the specified coordinates.", "nothing", "Returns nothing")
{
movie_player_h* ph = nullptr;
float x1;
float y1;
float x2 = -1.f;
float y2 = -1.f;
if (!ade_get_args(L, "off|ff", l_MoviePlayer.Get(&ph), &x1, &y1, &x2, &y2))
return ADE_RETURN_NIL;
if (ph == nullptr || !ph->isValid()) {
return ADE_RETURN_NIL;
}
auto& props = ph->player()->getMovieProperties();
// Use equality here to check if it has been changed
if (x2 == -1.f) {
x2 = x1 + props.size.width;
}
if (y2 == -1.f) {
y2 = y1 + props.size.height;
}
ph->player()->draw(x1, y1, x2, y2);
return ADE_RETURN_NIL;
}
ADE_FUNC(stop, l_MoviePlayer, nullptr,
"Explicitly stops playback. This function should be called when the player isn't needed anymore to free up "
"some resources.",
"nothing", "Returns nothing")
{
movie_player_h* ph = nullptr;
if (!ade_get_args(L, "o", l_MoviePlayer.Get(&ph)))
return ADE_RETURN_NIL;
if (ph == nullptr || !ph->isValid()) {
return ADE_RETURN_NIL;
}
ph->player()->stopPlayback();
return ADE_RETURN_NIL;
}
ADE_FUNC(isValid, l_MoviePlayer, nullptr, "Determines if this handle is valid", "boolean",
"true if valid, false otherwise")
{
movie_player_h* ph = nullptr;
if (!ade_get_args(L, "o", l_MoviePlayer.Get(&ph)))
return ADE_RETURN_FALSE;
if (ph == nullptr) {
return ADE_RETURN_FALSE;
}
if (!ph->isValid()) {
return ADE_RETURN_FALSE;
}
return ADE_RETURN_TRUE;
}
} // namespace api
} // namespace scripting