Skip to content

Commit

Permalink
Managed InputProcessing.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
ogamespec committed Aug 24, 2023
1 parent d2a7506 commit 631fe33
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
1 change: 1 addition & 0 deletions Breaknes/Breaknes/Breaknes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<ItemGroup>
<PackageReference Include="Be.Windows.Forms.HexBox.Net5" Version="1.8.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SharpDX.DirectInput" Version="4.2.0" />
</ItemGroup>

<ItemGroup>
Expand Down
13 changes: 10 additions & 3 deletions Breaknes/Breaknes/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ public partial class FormMain : Form
static extern bool AllocConsole();

private BoardControl board = new();
private VideoRender vid_out = null;
private AudioRender snd_out = null;
private string original_title;
private VideoRender? vid_out = null;
private AudioRender? snd_out = null;
private InputProcessor? input = null;
private string original_title = "";
private List<FormDebugger> debuggers = new();

public FormMain()
Expand Down Expand Up @@ -74,6 +75,7 @@ private void loadROMDumpToolStripMenuItem_Click(object sender, EventArgs e)
vid_out = new(OnRenderField, settings.DumpVideo, settings.DumpVideoDir, rom_name);
vid_out.SetOutputPictureBox(pictureBox1);
snd_out = new(Handle, settings.DumpAudio, settings.DumpAudioDir, rom_name, settings.IIR, settings.CutoffFrequency);
input = new InputProcessor();
board.Paused = debuggers.Count != 0;

foreach (var inst in debuggers)
Expand Down Expand Up @@ -125,6 +127,11 @@ private void Debugger_FormClosed(object? sender, FormClosedEventArgs e)

private void OnRenderField()
{
if (input != null)
{
input.PollDevices();
}

foreach (var inst in debuggers)
{
inst.UpdateOnRenderField();
Expand Down
78 changes: 78 additions & 0 deletions Breaknes/Breaknes/InputProcessing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using SharpDX.DirectInput;

// Demo:
// https://github.com/sharpdx/SharpDX-Samples/blob/master/Desktop/DirectInput/JoystickApp/Program.cs

namespace Breaknes
{

public class InputProcessor
{
Keyboard? keyboard = null;
Joystick? joystick = null;

public InputProcessor()
{
var directInput = new DirectInput();

// Find keyboard
keyboard = new Keyboard(directInput);
keyboard.Properties.BufferSize = 128;
keyboard.Acquire();

// Find a Joystick Guid
var joystickGuid = Guid.Empty;

foreach (var deviceInstance in directInput.GetDevices(DeviceType.Gamepad, DeviceEnumerationFlags.AllDevices))
joystickGuid = deviceInstance.InstanceGuid;

// If Gamepad not found, look for a Joystick
if (joystickGuid == Guid.Empty)
foreach (var deviceInstance in directInput.GetDevices(DeviceType.Joystick, DeviceEnumerationFlags.AllDevices))
joystickGuid = deviceInstance.InstanceGuid;

// If Joystick not found, throws an error
if (joystickGuid == Guid.Empty)
{
return;
}

// Instantiate the joystick
joystick = new Joystick(directInput, joystickGuid);

Console.WriteLine("Found Joystick/Gamepad with GUID: {0}", joystickGuid);

// Query all suported ForceFeedback effects
var allEffects = joystick.GetEffects();
foreach (var effectInfo in allEffects)
Console.WriteLine("Effect available {0}", effectInfo.Name);

// Set BufferSize in order to use buffered data.
joystick.Properties.BufferSize = 128;

// Acquire the joystick
joystick.Acquire();
}

public void PollDevices()
{
if (keyboard != null)
{
keyboard.Poll();
var datas = keyboard.GetBufferedData();
foreach (var state in datas)
Console.WriteLine(state);
}

if (joystick != null)
{
joystick.Poll();
var datas = joystick.GetBufferedData();
foreach (var state in datas)
Console.WriteLine(state);
}
}

}

}

0 comments on commit 631fe33

Please sign in to comment.