| 1 |
/* |
| 2 |
Copyright (c) 2012 Stephen Baker |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 |
this software and associated documentation files (the "Software"), to deal in |
| 6 |
the Software without restriction, including without limitation the rights to |
| 7 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 9 |
so, subject to the following conditions: |
| 10 |
|
| 11 |
The above copyright notice and this permission notice shall be included in all |
| 12 |
copies or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 |
SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
#include "config.h" |
| 24 |
|
| 25 |
#include <SDL_rect.h> |
| 26 |
|
| 27 |
#ifdef WITH_TRACY |
| 28 |
#include <tracy/Tracy.hpp> |
| 29 |
#endif |
| 30 |
|
| 31 |
#include "lua.hpp" |
| 32 |
#include "th_gfx_sdl.h" |
| 33 |
#include "th_lua.h" |
| 34 |
#include "th_lua_internal.h" |
| 35 |
#include "th_movie.h" |
| 36 |
|
| 37 |
namespace { |
| 38 |
|
| 39 |
int l_movie_new(lua_State* L) { |
| 40 |
luaT_stdnew<movie_player>(L, luaT_environindex, true); |
| 41 |
return 1; |
| 42 |
} |
| 43 |
|
| 44 |
int l_movie_set_renderer(lua_State* L) { |
| 45 |
movie_player* pMovie = luaT_testuserdata<movie_player>(L); |
| 46 |
render_target* pRenderTarget = luaT_testuserdata<render_target>(L, 2); |
| 47 |
pMovie->set_renderer(pRenderTarget->get_renderer()); |
| 48 |
return 0; |
| 49 |
} |
| 50 |
|
| 51 |
int l_movie_enabled(lua_State* L) { |
| 52 |
movie_player* pMovie = luaT_testuserdata<movie_player>(L); |
| 53 |
lua_pushboolean(L, pMovie->movies_enabled()); |
| 54 |
return 1; |
| 55 |
} |
| 56 |
|
| 57 |
int l_movie_load(lua_State* L) { |
| 58 |
ZoneScoped; |
| 59 |
|
| 60 |
bool loaded; |
| 61 |
const char* warning; |
| 62 |
movie_player* pMovie = luaT_testuserdata<movie_player>(L); |
| 63 |
const char* filepath = lua_tolstring(L, 2, nullptr); |
| 64 |
pMovie->clear_last_error(); |
| 65 |
loaded = pMovie->load(filepath); |
| 66 |
warning = pMovie->get_last_error(); |
| 67 |
lua_pushboolean(L, loaded); |
| 68 |
lua_pushstring(L, warning); |
| 69 |
return 2; |
| 70 |
} |
| 71 |
|
| 72 |
int l_movie_unload(lua_State* L) { |
| 73 |
ZoneScoped; |
| 74 |
|
| 75 |
movie_player* pMovie = luaT_testuserdata<movie_player>(L); |
| 76 |
pMovie->unload(); |
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
int l_movie_play(lua_State* L) { |
| 81 |
ZoneScoped; |
| 82 |
|
| 83 |
const char* warning; |
| 84 |
movie_player* pMovie = luaT_testuserdata<movie_player>(L); |
| 85 |
pMovie->clear_last_error(); |
| 86 |
pMovie->play(static_cast<int>(luaL_checkinteger(L, 2))); |
| 87 |
warning = pMovie->get_last_error(); |
| 88 |
lua_pushstring(L, warning); |
| 89 |
return 1; |
| 90 |
} |
| 91 |
|
| 92 |
int l_movie_stop(lua_State* L) { |
| 93 |
ZoneScoped; |
| 94 |
|
| 95 |
movie_player* pVideo = luaT_testuserdata<movie_player>(L); |
| 96 |
pVideo->stop(); |
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
int l_movie_toggle_pause(lua_State* L) { |
| 101 |
ZoneScoped; |
| 102 |
|
| 103 |
movie_player* pVideo = luaT_testuserdata<movie_player>(L); |
| 104 |
pVideo->togglePause(); |
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
int l_movie_get_native_height(lua_State* L) { |
| 109 |
movie_player* pMovie = luaT_testuserdata<movie_player>(L); |
| 110 |
lua_pushinteger(L, pMovie->get_native_height()); |
| 111 |
return 1; |
| 112 |
} |
| 113 |
|
| 114 |
int l_movie_get_native_width(lua_State* L) { |
| 115 |
movie_player* pMovie = luaT_testuserdata<movie_player>(L); |
| 116 |
lua_pushinteger(L, pMovie->get_native_width()); |
| 117 |
return 1; |
| 118 |
} |
| 119 |
|
| 120 |
int l_movie_has_audio_track(lua_State* L) { |
| 121 |
movie_player* pMovie = luaT_testuserdata<movie_player>(L); |
| 122 |
lua_pushboolean(L, pMovie->has_audio_track()); |
| 123 |
return 1; |
| 124 |
} |
| 125 |
|
| 126 |
int l_movie_get_length(lua_State* L) { |
| 127 |
movie_player* pMovie = luaT_testuserdata<movie_player>(L); |
| 128 |
lua_pushnumber(L, pMovie->get_movie_length()); |
| 129 |
return 1; |
| 130 |
} |
| 131 |
|
| 132 |
int l_movie_refresh(lua_State* L) { |
| 133 |
ZoneScoped; |
| 134 |
|
| 135 |
movie_player* pMovie = luaT_testuserdata<movie_player>(L); |
| 136 |
double pts = |
| 137 |
pMovie->refresh(SDL_Rect{static_cast<int>(luaL_checkinteger(L, 2)), |
| 138 |
static_cast<int>(luaL_checkinteger(L, 3)), |
| 139 |
static_cast<int>(luaL_checkinteger(L, 4)), |
| 140 |
static_cast<int>(luaL_checkinteger(L, 5))}); |
| 141 |
|
| 142 |
lua_pushnumber(L, pts); |
| 143 |
return 1; |
| 144 |
} |
| 145 |
|
| 146 |
int l_movie_allocate_picture_buffer(lua_State* L) { |
| 147 |
ZoneScoped; |
| 148 |
|
| 149 |
movie_player* pMovie = luaT_testuserdata<movie_player>(L); |
| 150 |
pMovie->allocate_picture_buffer(); |
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
int l_movie_deallocate_picture_buffer(lua_State* L) { |
| 155 |
ZoneScoped; |
| 156 |
|
| 157 |
movie_player* pMovie = luaT_testuserdata<movie_player>(L); |
| 158 |
pMovie->deallocate_picture_buffer(); |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
} // namespace |
| 163 |
|
| 164 |
void lua_register_movie(const lua_register_state* pState) { |
| 165 |
lua_class_binding<movie_player> lcb(pState, "moviePlayer", l_movie_new, |
| 166 |
lua_metatable::movie); |
| 167 |
lcb.add_function(l_movie_set_renderer, "setRenderer", lua_metatable::surface); |
| 168 |
lcb.add_function(l_movie_enabled, "getEnabled"); |
| 169 |
lcb.add_function(l_movie_load, "load"); |
| 170 |
lcb.add_function(l_movie_unload, "unload"); |
| 171 |
lcb.add_function(l_movie_play, "play"); |
| 172 |
lcb.add_function(l_movie_stop, "stop"); |
| 173 |
lcb.add_function(l_movie_toggle_pause, "togglePause"); |
| 174 |
lcb.add_function(l_movie_get_native_height, "getNativeHeight"); |
| 175 |
lcb.add_function(l_movie_get_native_width, "getNativeWidth"); |
| 176 |
lcb.add_function(l_movie_has_audio_track, "hasAudioTrack"); |
| 177 |
lcb.add_function(l_movie_get_length, "getLength"); |
| 178 |
lcb.add_function(l_movie_refresh, "refresh"); |
| 179 |
lcb.add_function(l_movie_allocate_picture_buffer, "allocatePictureBuffer"); |
| 180 |
lcb.add_function(l_movie_deallocate_picture_buffer, |
| 181 |
"deallocatePictureBuffer"); |
| 182 |
} |