Previous 199869 Revisions Next

r34193 Thursday 25th December, 2014 at 20:06:30 UTC by Luca Bruno
luaengine: add frame hooking support

This commit adds a method to let LUA scripts register a callback
to be invoked before rendering each frame.
This callback typically makes use of screen drawing methods to
draw a custom HUD on top of each frame.

Signed-off-by: Luca Bruno <lucab@debian.org>
[src/emu]luaengine.c luaengine.h video.c

trunk/src/emu/luaengine.c
r242704r242705
337337   return 0;
338338}
339339
340int lua_engine::l_emu_set_hook(lua_State *L)
341{
342   luaThis->emu_set_hook(L);
343   return 0;
344}
345
346void lua_engine::emu_set_hook(lua_State *L)
347{
348   luaL_argcheck(L, lua_isfunction(L, 1) || lua_isnil(L, 1), 1, "callback function expected");
349   luaL_argcheck(L, lua_isstring(L, 2), 2, "message (string) expected");
350   const char *hookname = luaL_checkstring(L,2);
351
352   if (strcmp(hookname, "output") == 0) {
353      hook_output_cb.set(L, 1);
354      if (!output_notifier_set) {
355         output_set_notifier(NULL, s_output_notifier, this);
356         output_notifier_set = true;
357      }
358   } else if (strcmp(hookname, "frame") == 0) {
359      hook_frame_cb.set(L, 1);
360   } else {
361      luai_writestringerror("%s", "Unknown hook name, aborting.\n");
362   }
363}
364
340365//-------------------------------------------------
341366//  machine_get_screens - return table of available screens userdata
342367//  -> manager:machine().screens[":screen"]
r242704r242705
773798         .addCFunction ("romname",     l_emu_romname )
774799         .addCFunction ("keypost",     l_emu_keypost )
775800         .addCFunction ("hook_output", l_emu_hook_output )
801         .addCFunction ("sethook",     l_emu_set_hook )
776802         .addCFunction ("time",        l_emu_time )
777803         .addCFunction ("wait",        l_emu_wait )
778804         .addCFunction ("after",       l_emu_after )
r242704r242705
836862   mg_start_thread(::serve_lua, this);
837863}
838864
865//-------------------------------------------------
866//  frame_hook - called at each frame refresh, used to draw a HUD
867//-------------------------------------------------
868bool lua_engine::frame_hook()
869{
870   bool is_cb_hooked = false;
871   if (m_machine != NULL) {
872      // invoke registered callback (if any)
873      is_cb_hooked = hook_frame_cb.active();
874      if (is_cb_hooked) {
875         lua_State *L = hook_frame_cb.precall();
876         hook_frame_cb.call(this, L, 0);
877      }
878   }
879   return is_cb_hooked;
880}
881
839882void lua_engine::periodic_check()
840883{
841884   osd_lock_acquire(lock);
trunk/src/emu/luaengine.h
r242704r242705
4444
4545   void serve_lua();
4646   void periodic_check();
47   bool frame_hook();
4748
4849   void resume(lua_State *L, int nparam = 0, lua_State *root = NULL);
4950   void set_machine(running_machine *machine) { m_machine = machine; update_machine(); }
r242704r242705
6869   hook hook_output_cb;
6970   bool output_notifier_set;
7071
72   hook hook_frame_cb;
73
7174   static lua_engine*  luaThis;
7275
7376   std::map<lua_State *, std::pair<lua_State *, int> > thread_registry;
r242704r242705
8285   int emu_after(lua_State *L);
8386   int emu_wait(lua_State *L);
8487   void emu_hook_output(lua_State *L);
88   void emu_set_hook(lua_State *L);
8589
8690   static int l_ioport_write(lua_State *L);
8791   static int l_emu_after(lua_State *L);
r242704r242705
97101   static int l_emu_start(lua_State *L);
98102   static int l_emu_pause(lua_State *L);
99103   static int l_emu_unpause(lua_State *L);
104   static int l_emu_set_hook(lua_State *L);
100105
101106   // "emu.machine" namespace
102107   static luabridge::LuaRef l_machine_get_devices(const running_machine *r);
trunk/src/emu/video.c
r242704r242705
655655      if (screen->update_quads())
656656         anything_changed = true;
657657
658   // draw HUD from LUA callback (if any)
659   anything_changed |= machine().manager().lua()->frame_hook();
660
658661   // update our movie recording and burn-in state
659662   if (!machine().paused())
660663   {


Previous 199869 Revisions Next


© 1997-2024 The MAME Team