Lua - move threading code into the frame loops, now emu.frameadvance works per frame not per emulator loop

This commit is contained in:
andres.delikat 2012-01-28 21:43:55 +00:00
parent 33ca14a344
commit 41708a8579
2 changed files with 63 additions and 59 deletions

View File

@ -13,23 +13,23 @@ namespace BizHawk.MultiClient
{ {
public class LuaImplementation public class LuaImplementation
{ {
Lua lua = new Lua(); Lua lua = new Lua();
LuaConsole Caller; LuaConsole Caller;
public String LuaLibraryList = ""; public String LuaLibraryList = "";
public EventWaitHandle LuaWait; public EventWaitHandle LuaWait;
public bool isRunning; public bool isRunning;
private Thread LuaThread; private Thread LuaThread;
private int CurrentMemoryDomain = 0; //Main memory by default private int CurrentMemoryDomain = 0; //Main memory by default
public LuaImplementation(LuaConsole passed) public LuaImplementation(LuaConsole passed)
{ {
LuaWait = new AutoResetEvent(false); LuaWait = new AutoResetEvent(false);
LuaLibraryList = ""; LuaLibraryList = "";
Caller = passed.get(); Caller = passed.get();
LuaRegister(lua); LuaRegister(lua);
} }
public void LuaRegister(Lua lua) public void LuaRegister(Lua lua)
{ {
lua.RegisterFunction("print", this, this.GetType().GetMethod("print")); lua.RegisterFunction("print", this, this.GetType().GetMethod("print"));
//Register libraries //Register libraries
@ -82,26 +82,26 @@ namespace BizHawk.MultiClient
LuaLibraryList += "client." + MultiClientFunctions[i] + "\n"; LuaLibraryList += "client." + MultiClientFunctions[i] + "\n";
} }
} }
private void LuaThreadFunction(object File) private void LuaThreadFunction(object File)
{ {
string F = File.ToString(); string F = File.ToString();
isRunning = true; isRunning = true;
try try
{ {
lua.DoFile(F); lua.DoFile(F);
} }
catch (Exception e) catch (Exception e)
{ {
MessageBox.Show("Exception caught. " + e.ToString()); MessageBox.Show("Exception caught. " + e.ToString());
} }
isRunning = false; isRunning = false;
LuaWait.Set(); LuaWait.Set();
} }
public void DoLuaFile(string File) public void DoLuaFile(string File)
{ {
LuaThread = new Thread(new ParameterizedThreadStart(LuaThreadFunction)); LuaThread = new Thread(new ParameterizedThreadStart(LuaThreadFunction));
LuaThread.Start(File); LuaThread.Start(File);
} }
@ -207,8 +207,8 @@ namespace BizHawk.MultiClient
//---------------------------------------------------- //----------------------------------------------------
public void emu_frameadvance() public void emu_frameadvance()
{ {
LuaWait.Set(); LuaWait.Set();
Global.MainForm.MainWait.WaitOne(); Global.MainForm.MainWait.WaitOne();
} }
public void emu_pause() public void emu_pause()

View File

@ -37,7 +37,7 @@ namespace BizHawk.MultiClient
bool runloop_frameProgress; bool runloop_frameProgress;
DateTime FrameAdvanceTimestamp = DateTime.MinValue; DateTime FrameAdvanceTimestamp = DateTime.MinValue;
public bool EmulatorPaused; public bool EmulatorPaused;
public EventWaitHandle MainWait; public EventWaitHandle MainWait;
int runloop_fps; int runloop_fps;
int runloop_last_fps; int runloop_last_fps;
bool runloop_frameadvance; bool runloop_frameadvance;
@ -68,7 +68,7 @@ namespace BizHawk.MultiClient
{ {
Global.MovieSession = new MovieSession(); Global.MovieSession = new MovieSession();
Global.MovieSession.Movie = new Movie(); Global.MovieSession.Movie = new Movie();
MainWait = new AutoResetEvent(false); MainWait = new AutoResetEvent(false);
Icon = BizHawk.MultiClient.Properties.Resources.logo; Icon = BizHawk.MultiClient.Properties.Resources.logo;
InitializeComponent(); InitializeComponent();
Global.Game = GameInfo.GetNullGame(); Global.Game = GameInfo.GetNullGame();
@ -332,11 +332,7 @@ namespace BizHawk.MultiClient
{ {
for (; ; ) for (; ; )
{ {
//client input-related duties
if (LuaConsole1.LuaImp.isRunning)
{
LuaConsole1.LuaImp.LuaWait.WaitOne();
}
Input.Instance.Update(); Input.Instance.Update();
//handle events and dispatch as a hotkey action, or a hotkey button, or an input button //handle events and dispatch as a hotkey action, or a hotkey button, or an input button
@ -352,10 +348,7 @@ namespace BizHawk.MultiClient
StepRunLoop_Throttle(); StepRunLoop_Throttle();
Render(); Render();
if (LuaConsole1.LuaImp.isRunning)
{
Global.MainForm.MainWait.Set();
}
CheckMessages(); CheckMessages();
if (exit) if (exit)
break; break;
@ -656,8 +649,8 @@ namespace BizHawk.MultiClient
genControls.BindMulti("P1 Start", Global.Config.GenesisController[0].Start); genControls.BindMulti("P1 Start", Global.Config.GenesisController[0].Start);
Global.GenControls = genControls; Global.GenControls = genControls;
var agenControls = new AutofireController(Genesis.GenesisController); var agenControls = new AutofireController(Genesis.GenesisController);
agbControls.Autofire = true; agbControls.Autofire = true;
genControls.BindMulti("P1 Up", Global.Config.GenesisAutoController[0].Up); genControls.BindMulti("P1 Up", Global.Config.GenesisAutoController[0].Up);
genControls.BindMulti("P1 Left", Global.Config.GenesisAutoController[0].Left); genControls.BindMulti("P1 Left", Global.Config.GenesisAutoController[0].Left);
genControls.BindMulti("P1 Right", Global.Config.GenesisAutoController[0].Right); genControls.BindMulti("P1 Right", Global.Config.GenesisAutoController[0].Right);
@ -666,7 +659,7 @@ namespace BizHawk.MultiClient
genControls.BindMulti("P1 B", Global.Config.GenesisAutoController[0].B); genControls.BindMulti("P1 B", Global.Config.GenesisAutoController[0].B);
genControls.BindMulti("P1 C", Global.Config.GenesisAutoController[0].C); genControls.BindMulti("P1 C", Global.Config.GenesisAutoController[0].C);
genControls.BindMulti("P1 Start", Global.Config.GenesisAutoController[0].Start); genControls.BindMulti("P1 Start", Global.Config.GenesisAutoController[0].Start);
Global.AutofireGenControls = agenControls; Global.AutofireGenControls = agenControls;
var TI83Controls = new Controller(TI83.TI83Controller); var TI83Controls = new Controller(TI83.TI83Controller);
TI83Controls.BindMulti("0", Global.Config.TI83Controller[0]._0); TI83Controls.BindMulti("0", Global.Config.TI83Controller[0]._0);
@ -1226,9 +1219,9 @@ namespace BizHawk.MultiClient
private void CloseGame() private void CloseGame()
{ {
if (Global.Config.AutoSavestates && Global.Emulator is NullEmulator == false) if (Global.Config.AutoSavestates && Global.Emulator is NullEmulator == false)
SaveState("Auto"); SaveState("Auto");
if (Global.Emulator.SaveRamModified) if (Global.Emulator.SaveRamModified)
SaveRam(); SaveRam();
Global.Emulator.Dispose(); Global.Emulator.Dispose();
Global.Emulator = new NullEmulator(); Global.Emulator = new NullEmulator();
@ -1381,14 +1374,14 @@ namespace BizHawk.MultiClient
Global.RenderPanel.AddMessage("Unthrottled: " + unthrottled); Global.RenderPanel.AddMessage("Unthrottled: " + unthrottled);
break; break;
case "Hard Reset": case "Hard Reset":
{ {
bool autoSaveState = Global.Config.AutoSavestates; bool autoSaveState = Global.Config.AutoSavestates;
Global.Config.AutoSavestates = false; Global.Config.AutoSavestates = false;
LoadRom(CurrentlyOpenRom); LoadRom(CurrentlyOpenRom);
Global.Config.AutoSavestates = autoSaveState; Global.Config.AutoSavestates = autoSaveState;
break; break;
} }
case "Screenshot": case "Screenshot":
TakeScreenshot(); TakeScreenshot();
@ -1626,6 +1619,17 @@ namespace BizHawk.MultiClient
bool genSound = false; bool genSound = false;
if (runFrame) if (runFrame)
{ {
//client input-related duties
if (LuaConsole1.LuaImp.isRunning)
{
LuaConsole1.LuaImp.LuaWait.WaitOne();
}
if (LuaConsole1.LuaImp.isRunning)
{
Global.MainForm.MainWait.Set();
}
runloop_fps++; runloop_fps++;
bool ff = Global.ClientControls["Fast Forward"]; bool ff = Global.ClientControls["Fast Forward"];
bool updateFpsString = (runloop_last_ff != ff); bool updateFpsString = (runloop_last_ff != ff);