Skip to content

Commit

Permalink
Add playback/record Lua functions
Browse files Browse the repository at this point in the history
  • Loading branch information
SineSwiper committed Nov 30, 2018
1 parent 47c8491 commit f9f35cb
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 4 deletions.
74 changes: 71 additions & 3 deletions src/lua-engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3098,6 +3098,72 @@ static int movie_replay (lua_State *L) {
return 0;
}

// bool movie.play(string filename, [bool read_only, [int pauseframe]])
//
// Loads and plays a movie.
int movie_playback(lua_State *L) {
int arg_count = lua_gettop(L);

if (arg_count == 0) {
luaL_error(L, "no parameters specified");
return 0;
}

const char *filename = luaL_checkstring(L,1);
if (filename == NULL) {
luaL_error(L, "Filename required");
return 0;
}

bool read_only = arg_count >= 2 ? (lua_toboolean(L,2) == 1) : 0;
int pauseframe = arg_count >= 3 ? lua_tointeger(L,3) : 0;

if (pauseframe < 0) pauseframe = 0;

// Load it!
bool loaded = FCEUI_LoadMovie(filename, read_only, pauseframe);

lua_pushboolean(L, loaded);
return 1;
}

// bool movie.record(string filename, [int save_type, [string author]])
//
// Saves and records a movie.
int movie_record(lua_State *L) {
int arg_count = lua_gettop(L);

if (arg_count == 0) {
luaL_error(L, "no parameters specified");
return 0;
}

const char *filename = luaL_checkstring(L,1);
if (filename == NULL) {
luaL_error(L, "Filename required");
return 0;
}

// No need to use the full functionality of the enum
int save_type = arg_count >= 2 ? lua_tointeger(L, 2) : 0;
EMOVIE_FLAG flags;
if (save_type == 1) flags = MOVIE_FLAG_NONE; // from savestate
else if (save_type == 2) flags = MOVIE_FLAG_FROM_SAVERAM;
else flags = MOVIE_FLAG_FROM_POWERON;

// XXX: Assuming UTF-8 strings in Lua
std::wstring author =
arg_count >= 3 ?
mbstowcs( (std::string)luaL_checkstring(L, 3) ) : L""
;

// Save it!
FCEUI_SaveMovie(filename, flags, author);

lua_pushboolean(L, 1);
return 1;
}

//movie.ispoweron
//
//If movie is recorded from power-on
Expand Down Expand Up @@ -5759,13 +5825,15 @@ static const struct luaL_reg movielib[] = {
{"readonly", movie_getreadonly},
{"setreadonly", movie_setreadonly},
{"replay", movie_replay},
// {"record", movie_record},
// {"play", movie_playback},
{"record", movie_record},
{"play", movie_playback},

// alternative names
{"close", movie_stop},
{"getname", movie_getname},
// {"playback", movie_playback},
{"load", movie_playback},
{"save", movie_record},
{"playback", movie_playback},
{"playbeginning", movie_replay},
{"getreadonly", movie_getreadonly},
{"ispoweron", movie_ispoweron}, //If movie recorded from power-on
Expand Down
8 changes: 7 additions & 1 deletion src/movie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1024,9 +1024,12 @@ bool FCEUI_LoadMovie(const char *fname, bool _read_only, int _pauseframe)
static void openRecordingMovie(const char* fname)
{
osRecordingMovie = FCEUD_UTF8_fstream(fname, "wb");
if(!osRecordingMovie)
if(!osRecordingMovie || osRecordingMovie->fail()) {
FCEU_PrintError("Error opening movie output file: %s",fname);
return;
}
strcpy(curMovieFilename, fname);

#ifdef WIN32
//Add to the recent movie menu
AddRecentMovieFile(fname);
Expand All @@ -1047,6 +1050,9 @@ void FCEUI_SaveMovie(const char *fname, EMOVIE_FLAG flags, std::wstring author)

openRecordingMovie(fname);

if(!osRecordingMovie || osRecordingMovie->fail())
return;

currFrameCounter = 0;
LagCounterReset();
FCEUMOV_CreateCleanMovie();
Expand Down
17 changes: 17 additions & 0 deletions web/help/LuaFunctionsList.html
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,23 @@ <h1>Lua Functions List</h1>
<p><span class="rvts37"><br/></span></p>
<p><span class="rvts71">Movie Library</span></p>
<p><span class="rvts37"><br/></span></p>
<p><span class="rvts63">bool movie.play(string filename, [bool read_only, [int pauseframe]])</span></p>
<p><span class="rvts63">bool movie.playback(...)</span></p>
<p><span class="rvts63">bool movie.load(...)</span></p>
<p><span class="rvts37"><br/></span></p>
<p><span class="rvts37">Loads and plays a movie from the directory relative to the Lua script or from the absolute path. If read_only is true, the movie will be loaded in read-only mode. The default is read+write.</span></p>
<p><span class="rvts37"><br/></span></p>
<p><span class="rvts37">A pauseframe can be specified, which controls which frame will auto-pause the movie. By default, this is off. A true value is returned if the movie loaded correctly.</span></p>
<p><span class="rvts37"><br/></span></p>
<p><span class="rvts63">bool movie.record(string filename, [int save_type, [string author]])</span></p>
<p><span class="rvts63">bool movie.save(...)</span></p>
<p><span class="rvts37"><br/></span></p>
<p><span class="rvts37">Starts recording a movie, using the filename, relative to the Lua script.</span></p>
<p><span class="rvts37"><br/></span></p>
<p><span class="rvts37">An optional save_type can be specified. If set to 0 (default), it will record from a power on state, and automatically do so. This is the recommended setting for creating movies. This can also be set to 1 for savestate or 2 for saveram movies.</span></p>
<p><span class="rvts37"><br/></span></p>
<p><span class="rvts37">A third parameter specifies an author string. If included, it will be recorded into the movie file.</span></p>
<p><span class="rvts37"><br/></span></p>
<p><span class="rvts63">bool movie.active()</span></p>
<p><span class="rvts37"><br/></span></p>
<p><span class="rvts37">Returns true if a movie is currently loaded and false otherwise. &nbsp;(This should be used to guard against Lua errors when attempting to retrieve movie information).</span></p>
Expand Down

0 comments on commit f9f35cb

Please sign in to comment.