File: movie_player.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-3
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,740 kB
  • sloc: cpp: 595,005; ansic: 21,741; python: 1,174; sh: 457; makefile: 243; xml: 181
file content (193 lines) | stat: -rw-r--r-- 5,648 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193

#include "movie_player.h"
#include "time_obj.h"

namespace scripting {
namespace api {

movie_player_h::movie_player_h() = default;
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_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.GetPtr(&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.GetPtr(&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.GetPtr(&ph)))
		return ade_set_args(L, "f", -1.0f);

	if (ph == nullptr || !ph->isValid()) {
		return ade_set_args(L, "f", -1.0f);
	}

	if (ADE_SETTING_VAR) {
		LuaError(L, "Tried to modify read-only member!");
	}

	return ade_set_args(L, "f", ph->player()->getMovieProperties().fps);
}

ADE_VIRTVAR(Duration, l_MoviePlayer, "number", "Determines the duration in seconds of this movie <b>Read-only</b>", "number",
            "The duration of the movie or -1 if handle is invalid")
{
	movie_player_h* ph = nullptr;
	if (!ade_get_args(L, "o", l_MoviePlayer.GetPtr(&ph)))
		return ade_set_args(L, "f", -1.0f);

	if (ph == nullptr || !ph->isValid()) {
		return ade_set_args(L, "f", -1.0f);
	}

	if (ADE_SETTING_VAR) {
		LuaError(L, "Tried to modify read-only member!");
	}

	return ade_set_args(L, "f", ph->player()->getMovieProperties().duration);
}

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.GetPtr(&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.GetPtr(&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.", nullptr, "Returns nothing")
{
	movie_player_h* ph = nullptr;
	float x1;
	float y1;
	float x2 = -1.f;
	float y2 = -1.f;
	float alpha = 1.f;

	if (!ade_get_args(L, "off|fff", l_MoviePlayer.GetPtr(&ph), &x1, &y1, &x2, &y2, &alpha))
		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, alpha);

	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.",
         nullptr, "Returns nothing")
{
	movie_player_h* ph = nullptr;
	if (!ade_get_args(L, "o", l_MoviePlayer.GetPtr(&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.GetPtr(&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