From a277dfa1e59fd04a8e3f96160b0d2859d25dd569 Mon Sep 17 00:00:00 2001 From: goyuken Date: Sun, 9 Sep 2012 14:17:57 +0000 Subject: [PATCH] wire controls into gambatte. --- .../Consoles/Nintendo/Gameboy/Gambatte.cs | 51 +++++++++++++++++-- .../Consoles/Nintendo/Gameboy/LibGambatte.cs | 9 +++- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/BizHawk.Emulation/Consoles/Nintendo/Gameboy/Gambatte.cs b/BizHawk.Emulation/Consoles/Nintendo/Gameboy/Gambatte.cs index 07585fe52b..442fe2f2c2 100644 --- a/BizHawk.Emulation/Consoles/Nintendo/Gameboy/Gambatte.cs +++ b/BizHawk.Emulation/Consoles/Nintendo/Gameboy/Gambatte.cs @@ -16,6 +16,15 @@ namespace BizHawk.Emulation.Consoles.GB /// IntPtr GambatteState = IntPtr.Zero; + /// + /// keep a copy of the input callback delegate so it doesn't get GCed + /// + LibGambatte.InputGetter InputCallback; + + /// + /// whatever keys are currently depressed + /// + LibGambatte.Buttons CurrentButtons = 0; public Gameboy(byte[] romdata) { @@ -36,6 +45,13 @@ namespace BizHawk.Emulation.Consoles.GB InitSound(); + + Frame = 0; + LagCount = 0; + + InputCallback = new LibGambatte.InputGetter(ControllerCallback); + + LibGambatte.gambatte_setinputgetter(GambatteState, InputCallback); } public IVideoProvider VideoProvider @@ -67,22 +83,49 @@ namespace BizHawk.Emulation.Consoles.GB public IController Controller { get; set; } - + public void FrameAdvance(bool render) { uint nsamp = 35112; + Controller.UpdateControls(Frame++); + + // update our local copy of the controller data + CurrentButtons = 0; + + if (Controller["Up"]) + CurrentButtons |= LibGambatte.Buttons.UP; + if (Controller["Down"]) + CurrentButtons |= LibGambatte.Buttons.DOWN; + if (Controller["Left"]) + CurrentButtons |= LibGambatte.Buttons.LEFT; + if (Controller["Right"]) + CurrentButtons |= LibGambatte.Buttons.RIGHT; + if (Controller["A"]) + CurrentButtons |= LibGambatte.Buttons.A; + if (Controller["B"]) + CurrentButtons |= LibGambatte.Buttons.B; + if (Controller["Select"]) + CurrentButtons |= LibGambatte.Buttons.SELECT; + if (Controller["Start"]) + CurrentButtons |= LibGambatte.Buttons.START; + LibGambatte.gambatte_runfor(GambatteState, VideoBuffer, 160, soundbuff, ref nsamp); soundbuffcontains = (int)nsamp; } - public int Frame + // can when this is called (or not called) be used to give information about lagged frames? + LibGambatte.Buttons ControllerCallback() { - get { return 0; } + return CurrentButtons; } + + + public int Frame { get; set; } + public int LagCount { get; set; } public bool IsLagFrame @@ -218,7 +261,7 @@ namespace BizHawk.Emulation.Consoles.GB int soundbuffcontains = 0; Sound.Utilities.SpeexResampler resampler; - Sound.MetaspuSoundProvider metaspu; + Sound.MetaspuSoundProvider metaspu; void InitSound() { diff --git a/BizHawk.Emulation/Consoles/Nintendo/Gameboy/LibGambatte.cs b/BizHawk.Emulation/Consoles/Nintendo/Gameboy/LibGambatte.cs index b2f5822a67..89386f61d5 100644 --- a/BizHawk.Emulation/Consoles/Nintendo/Gameboy/LibGambatte.cs +++ b/BizHawk.Emulation/Consoles/Nintendo/Gameboy/LibGambatte.cs @@ -114,13 +114,20 @@ namespace BizHawk.Emulation.Consoles.GB DOWN = 0x80 } + /// + /// type of the callback for input state + /// + /// bitfield combination of pressed buttons + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + public delegate Buttons InputGetter(); + /// /// Sets the callback used for getting input state. /// /// opaque state pointer /// [DllImport("libgambatte.dll", CallingConvention = CallingConvention.Cdecl)] - public static extern void gambatte_setinputgetter(IntPtr core, Func getinput); + public static extern void gambatte_setinputgetter(IntPtr core, InputGetter getinput); /// /// Sets the directory used for storing save data. The default is the same directory as the ROM Image file.