控制器输入实现

This commit is contained in:
ALIENJACK\alien 2024-07-18 15:14:45 +08:00
parent 34286f4263
commit 8b3b54f731
22 changed files with 364 additions and 87 deletions

View File

@ -13,7 +13,7 @@ namespace MyNes.Core
public override void Load(string fileName, bool loadDumps)
{
var fileStream = MyNesMain.FileManager.OpenRomFile(fileName);
var fileStream = MyNesMain.Supporter.OpenRomFile(fileName);
if (fileStream.Length < 16)
{
fileStream.Close();

View File

@ -9,7 +9,7 @@ namespace MyNes.Core
{
public static EmuSettings EmuSettings { get; private set; }
public static RendererSettings RendererSettings { get; private set; }
public static IFileManager FileManager { get; private set; }
public static IExternalSupporter Supporter { get; private set; }
public static string WorkingFolder { get; private set; }
internal static List<Board> Boards { get; private set; }
@ -20,10 +20,10 @@ namespace MyNes.Core
public static WaveRecorder WaveRecorder { get; private set; }
public static void Initialize(IFileManager fileManager, IVideoProvider videoProvider, IAudioProvider audioProvider)
public static void Initialize(IExternalSupporter fileManager, IVideoProvider videoProvider, IAudioProvider audioProvider)
{
Tracer.WriteLine("Initializing My Nes Core ....");
FileManager = fileManager;
Supporter = fileManager;
WorkingFolder = fileManager.GetWorkingFolderPath();
Tracer.WriteLine("Loading emu settings ...");
EmuSettings = new EmuSettings(Path.Combine(WorkingFolder, "emusettings.ini"));
@ -211,11 +211,5 @@ namespace MyNes.Core
}
}
}
public interface IFileManager
{
string GetWorkingFolderPath();
public Stream OpenDatabaseFile();
public Stream OpenPaletteFile();
public Stream OpenRomFile(string path);
}
}

View File

@ -28,7 +28,7 @@ namespace MyNes.Core
Ready = false;
_databaseRoms.Clear();
var stream = MyNesMain.FileManager.OpenDatabaseFile();
var stream = MyNesMain.Supporter.OpenDatabaseFile();
XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
xmlReaderSettings.DtdProcessing = DtdProcessing.Ignore;
xmlReaderSettings.IgnoreWhitespace = true;

View File

@ -453,8 +453,6 @@ namespace MyNes.Core
private static IJoypadConnecter joypad4;
private static IShortcutsHandler shortucts;
public static bool IsFourPlayers;
private static byte[] reverseLookup = new byte[256]
@ -663,24 +661,10 @@ namespace MyNes.Core
private static Thread mainThread;
private static double fps_time_last;
private static double fps_time_start;
private static double fps_time_token;
private static double fps_time_dead;
private static double fps_time_period;
private static double fps_time_frame_time;
private static double emu_time_target_fps = 60.0;
private static bool emu_frame_clocking_mode;
private static bool emu_frame_done;
private static bool render_initialized;
private static RenderVideoFrame render_video;
@ -3887,15 +3871,6 @@ namespace MyNes.Core
{
joypad4 = new BlankJoypad();
}
if (shortucts == null)
{
shortucts = new BlankShortuctsHandler();
}
}
public static void SetupShortcutsHandler(IShortcutsHandler hh)
{
shortucts = hh;
}
public static void SetupControllers(IJoypadConnecter joy1, IJoypadConnecter joy2, IJoypadConnecter joy3, IJoypadConnecter joy4)
@ -5162,7 +5137,6 @@ namespace MyNes.Core
hardReset();
Tracer.WriteLine("EMU is ready.");
success = true;
emu_frame_clocking_mode = !useThread;
ON = true;
PAUSED = false;
if (useThread)
@ -5274,7 +5248,7 @@ namespace MyNes.Core
private static Stopwatch sw = new Stopwatch();
private static double fixTime;
public static int currentFrame;
public static ulong currentFrame;
private static void EmuClock()
{
while (ON)
@ -5294,6 +5268,7 @@ namespace MyNes.Core
fixTime = waitTime - GetTime();
};
currentFrame++;
continue;
}
@ -5303,7 +5278,6 @@ namespace MyNes.Core
render_audio_toggle_pause(paused: true);
}
Thread.Sleep(100);
shortucts.Update();
switch (emu_request_mode)
{
case RequestMode.HardReset:
@ -5370,7 +5344,6 @@ namespace MyNes.Core
}
isPaused = false;
ppu_frame_finished = false;
emu_frame_done = true;
joypad1.Update();
joypad2.Update();
if (IsFourPlayers)
@ -5378,7 +5351,6 @@ namespace MyNes.Core
joypad3.Update();
joypad4.Update();
}
shortucts.Update();
if (SoundEnabled)
{
render_audio_get_is_playing(out render_audio_is_playing);
@ -5391,8 +5363,6 @@ namespace MyNes.Core
audio_samples_added = 0;
audio_timer = 0.0;
}
fps_time_token = GetTime() - fps_time_start;
fps_time_start = GetTime();
}
private static double GetTime()
@ -5400,11 +5370,6 @@ namespace MyNes.Core
return (double)Stopwatch.GetTimestamp() / (double)Stopwatch.Frequency;
}
public static void GetSpeedValues(out double frame_time, out double immediate_frame_time)
{
frame_time = fps_time_token;
immediate_frame_time = fps_time_frame_time;
}
public static void SetFramePeriod(ref double period)
{
@ -5490,7 +5455,7 @@ namespace MyNes.Core
{
Tracer.WriteLine("Palette set to load from file.");
var paletteFileStream = MyNesMain.FileManager.OpenPaletteFile();
var paletteFileStream = MyNesMain.Supporter.OpenPaletteFile();
if (paletteFileStream != null)
{
PaletteFileWrapper.LoadFile(paletteFileStream, out var palette);

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: f2c1e2b9170060a4081ad7befba87bc0
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,7 @@
namespace MyNes.Core
{
public enum EnumJoyIndex : byte
{
P1, P2, P3, P4
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d0185abce4b430d478095a8d5621aef1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,7 @@
namespace MyNes.Core
{
public enum EnumKeyKind
{
Up, Down, Left, Right, Select, Start, B, A, TurboB, TurboA
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b25aa577a22fb0046afd4f72cd99daff
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,13 @@
using System.IO;
namespace MyNes.Core
{
public interface IExternalSupporter
{
string GetWorkingFolderPath();
public Stream OpenDatabaseFile();
public Stream OpenPaletteFile();
public Stream OpenRomFile(string path);
public bool IsKeyPressing(EnumJoyIndex index,EnumKeyKind key);
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c9fd20bbd0bd8ff46b6ddef7dfb345a3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,5 +1,48 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1 &319390252125274553
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 3543948837876491845}
- component: {fileID: 2496653285840897638}
m_Layer: 0
m_Name: Input
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &3543948837876491845
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 319390252125274553}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 8662582775964487076}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &2496653285840897638
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 319390252125274553}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d79c33962dea7dc48b2c5fcd45afe1ad, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &8662582774585465456
GameObject:
m_ObjectHideFlags: 0
@ -25,11 +68,11 @@ RectTransform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8662582774585465456}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: -1, z: 1}
m_Children: []
m_Father: {fileID: 8662582775350046794}
m_Father: {fileID: 8662582775439058149}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
@ -80,7 +123,7 @@ GameObject:
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 8662582774971523581}
- component: {fileID: 3209572454846341542}
- component: {fileID: 8662582774971523580}
- component: {fileID: 8662582774971523579}
m_Layer: 5
@ -90,8 +133,8 @@ GameObject:
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &8662582774971523581
RectTransform:
--- !u!4 &3209572454846341542
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
@ -99,16 +142,11 @@ RectTransform:
m_GameObject: {fileID: 8662582774971523582}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: -1, z: 1}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 8662582775439058149}
m_Father: {fileID: 8662582775964487076}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!82 &8662582774971523580
AudioSource:
m_ObjectHideFlags: 0
@ -227,18 +265,18 @@ GameObject:
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 8662582775350046794}
- component: {fileID: 7667066390141474019}
- component: {fileID: 8662582775350046791}
- component: {fileID: 8662582775350046790}
m_Layer: 5
m_Name: video
m_Name: Video
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &8662582775350046794
RectTransform:
--- !u!4 &7667066390141474019
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
@ -248,16 +286,10 @@ RectTransform:
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 8662582774585465455}
- {fileID: 8662582775359084755}
m_Father: {fileID: 8662582775439058149}
- {fileID: 8662582775439058149}
m_Father: {fileID: 8662582775964487076}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 1, y: 1}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0.5, y: 0.5}
--- !u!222 &8662582775350046791
CanvasRenderer:
m_ObjectHideFlags: 0
@ -306,11 +338,11 @@ RectTransform:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8662582775359084756}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 8662582775350046794}
m_Father: {fileID: 8662582775439058149}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 1, y: 0}
@ -390,9 +422,9 @@ RectTransform:
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0, y: 0, z: 0}
m_Children:
- {fileID: 8662582775350046794}
- {fileID: 8662582774971523581}
m_Father: {fileID: 8662582775964487076}
- {fileID: 8662582774585465455}
- {fileID: 8662582775359084755}
m_Father: {fileID: 7667066390141474019}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
@ -489,7 +521,9 @@ Transform:
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children:
- {fileID: 8662582775439058149}
- {fileID: 7667066390141474019}
- {fileID: 3209572454846341542}
- {fileID: 3543948837876491845}
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@ -507,3 +541,4 @@ MonoBehaviour:
m_EditorClassIdentifier:
VideoCom: {fileID: 8662582775350046790}
AudioCom: {fileID: 8662582774971523579}
InputManager: {fileID: 2496653285840897638}

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: cf0f3c8610629184e9c18af05d977830
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,50 @@
using MyNes.Core;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace AxibugEmuOnline.Client.Input
{
public class InputManager : MonoBehaviour
{
private KeyMapper m_p1Mapper = new KeyMapper();
private KeyMapper m_p2Mapper = new KeyMapper();
private KeyMapper m_p3Mapper = new KeyMapper();
private KeyMapper m_p4Mapper = new KeyMapper();
private void Awake()
{
m_p1Mapper.SetKeyMapper(KeyCode.W, EnumKeyKind.Up);
m_p1Mapper.SetKeyMapper(KeyCode.S, EnumKeyKind.Down);
m_p1Mapper.SetKeyMapper(KeyCode.A, EnumKeyKind.Left);
m_p1Mapper.SetKeyMapper(KeyCode.D, EnumKeyKind.Right);
m_p1Mapper.SetKeyMapper(KeyCode.V, EnumKeyKind.Select);
m_p1Mapper.SetKeyMapper(KeyCode.B, EnumKeyKind.Start);
m_p1Mapper.SetKeyMapper(KeyCode.J, EnumKeyKind.B);
m_p1Mapper.SetKeyMapper(KeyCode.K, EnumKeyKind.A);
m_p1Mapper.SetKeyMapper(KeyCode.U, EnumKeyKind.TurboB);
m_p1Mapper.SetKeyMapper(KeyCode.I, EnumKeyKind.TurboA);
m_p1Mapper.SetComplete();
}
private void Update()
{
m_p1Mapper.Update();
m_p2Mapper.Update();
m_p3Mapper.Update();
m_p4Mapper.Update();
}
public bool IsKeyPress(EnumJoyIndex joyIndex, EnumKeyKind keyKind)
{
switch (joyIndex)
{
case EnumJoyIndex.P1: return m_p1Mapper.IsPressing(keyKind);
case EnumJoyIndex.P2: return m_p2Mapper.IsPressing(keyKind);
case EnumJoyIndex.P3: return m_p3Mapper.IsPressing(keyKind);
case EnumJoyIndex.P4: return m_p4Mapper.IsPressing(keyKind);
default: return default;
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d79c33962dea7dc48b2c5fcd45afe1ad
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,57 @@
using MyNes.Core;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace AxibugEmuOnline.Client.Input
{
public class KeyMapper
{
private Dictionary<KeyCode, EnumKeyKind> m_mapper = new Dictionary<KeyCode, EnumKeyKind>();
private Dictionary<EnumKeyKind, KeyCode> m_mapperOpp = new Dictionary<EnumKeyKind, KeyCode>();
private Dictionary<EnumKeyKind, int> m_keyIndexTable = new Dictionary<EnumKeyKind, int>();
private EnumKeyKind[] m_focusKeys;
private bool[] m_keyStates;
public void SetKeyMapper(KeyCode inputKeycode, EnumKeyKind joyKey)
{
if (m_mapperOpp.TryGetValue(joyKey, out KeyCode keyCode))//如果该映射已设置过,移除之前的映射
{
m_mapperOpp.Remove(joyKey);
m_mapper.Remove(keyCode);
}
m_mapper[inputKeycode] = joyKey;
m_mapperOpp[joyKey] = inputKeycode;
}
public void SetComplete()
{
m_focusKeys = m_mapperOpp.Keys.ToArray();
m_keyStates = new bool[m_focusKeys.Length];
m_keyIndexTable.Clear();
for (int i = 0; i < m_focusKeys.Length; i++)
{
m_keyIndexTable[m_focusKeys[i]] = i;
}
}
public void Update()
{
if (m_focusKeys == null) return;
for (int i = 0; i < m_focusKeys.Length; i++)
{
var keyCode = m_mapperOpp[m_focusKeys[i]];
m_keyStates[i] = UnityEngine.Input.GetKey(keyCode);
}
}
public bool IsPressing(EnumKeyKind keyKind)
{
if (!m_keyIndexTable.TryGetValue(keyKind, out int index)) return false;//没有设置映射,直接false
return m_keyStates[index];
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 61e49dbcce97ff74fb117da5d69f844d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,53 @@
using MyNes.Core;
using Unity.VisualScripting.YamlDotNet.Core.Tokens;
namespace AxibugEmuOnline.Client
{
public class NesJoyController : IJoypadConnecter
{
private EnumJoyIndex m_joyIndex;
public NesJoyController(EnumJoyIndex joyIndex)
{
m_joyIndex = joyIndex;
}
public override void Update()
{
DATA = 0;
var state = MyNesMain.Supporter;
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.A))
{
DATA |= 1;
}
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.B))
{
DATA |= 2;
}
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.Select))
{
DATA |= 4;
}
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.Start))
{
DATA |= 8;
}
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.Up))
{
DATA |= 16;
}
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.Down))
{
DATA |= 32;
}
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.Left))
{
DATA |= 64;
}
if (state.IsKeyPressing(m_joyIndex, EnumKeyKind.Right))
{
DATA |= 128;
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a4c763168d739ee409c2723564c2113b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -32,7 +32,6 @@ namespace AxibugEmuOnline.Client
public void Initialize()
{
m_rawBufferWarper = new Texture2D(256, 240);
//m_drawCanvas.texture = RenderTexture.GetTemporary(256, 240, 0, UnityEngine.Experimental.Rendering.GraphicsFormat.B8G8R8A8_SRGB);
}
public void GetColor(uint value, ref Color res)

View File

@ -1,16 +1,24 @@
using MyNes;
using AxibugEmuOnline.Client.Input;
using MyNes.Core;
using System.IO;
using UnityEngine;
namespace AxibugEmuOnline.Client.Manager
{
public class AppEmu : IFileManager
public class AppEmu : IExternalSupporter
{
public void Init(IVideoProvider videoCom, IAudioProvider audioCom)
{
MyNesMain.Initialize(this, videoCom, audioCom);
private InputManager m_inputMgr;
public void Init(IVideoProvider videoCom, IAudioProvider audioCom, InputManager inputManager)
{
m_inputMgr = inputManager;
MyNesMain.Initialize(this, videoCom, audioCom);
NesEmu.SetupControllers(
new NesJoyController(EnumJoyIndex.P1),
new NesJoyController(EnumJoyIndex.P2),
new NesJoyController(EnumJoyIndex.P3),
new NesJoyController(EnumJoyIndex.P4));
NesEmu.LoadGame("kirby.nes", out var successed, true);
}
@ -44,5 +52,10 @@ namespace AxibugEmuOnline.Client.Manager
MemoryStream ms = new MemoryStream(ta.bytes);
return ms;
}
public bool IsKeyPressing(EnumJoyIndex index, EnumKeyKind key)
{
return m_inputMgr.IsKeyPress(index, key);
}
}
}

View File

@ -1,3 +1,4 @@
using AxibugEmuOnline.Client.Input;
using AxibugEmuOnline.Client.Manager;
using MyNes.Core;
using System.IO;
@ -10,12 +11,13 @@ namespace AxibugEmuOnline.Client
{
public UguiVideoProvider VideoCom;
public AudioProvider AudioCom;
public InputManager InputManager;
private AppEmu m_appEnum = new AppEmu();
private void Start()
{
m_appEnum.Init(VideoCom, AudioCom);
m_appEnum.Init(VideoCom, AudioCom, InputManager);
}
private void OnDestroy()