From fe9abb1c2b0221c560b085d2ac186a73b6edbf5c Mon Sep 17 00:00:00 2001 From: Sean Fausett Date: Sun, 5 Dec 2010 11:18:48 +1300 Subject: [PATCH] Added UseShiftKeyMod to GamePort settings. Added signature to machine state. Changed floating point precision from double to single. --- Library/DirectSound.cs | 2 +- Library/MathHelpers.cs | 5 ----- Virtu/GamePort.cs | 11 +++++++---- Virtu/Machine.cs | 15 ++++++++++----- Virtu/Services/AudioService.cs | 2 +- .../Services/SilverlightAudioService.cs | 2 +- Virtu/Speaker.cs | 10 +++++----- Virtu/Wpf/Services/WpfAudioService.cs | 2 +- Virtu/Xna/Services/XnaAudioService.cs | 4 ++-- Virtu/Xna/Services/XnaGamePortService.cs | 18 +++++++++--------- 10 files changed, 37 insertions(+), 34 deletions(-) diff --git a/Library/DirectSound.cs b/Library/DirectSound.cs index 28c24da..f679d97 100644 --- a/Library/DirectSound.cs +++ b/Library/DirectSound.cs @@ -26,7 +26,7 @@ public void Dispose() _stopEvent.Close(); } - public void SetVolume(double volume) + public void SetVolume(float volume) { int attenuation = (volume < 0.01) ? (int)BufferVolume.Min : (int)Math.Floor(100 * 20 * Math.Log10(volume)); // 100 db lock (_bufferLock) diff --git a/Library/MathHelpers.cs b/Library/MathHelpers.cs index 937ac31..3c31801 100644 --- a/Library/MathHelpers.cs +++ b/Library/MathHelpers.cs @@ -7,11 +7,6 @@ public static int Clamp(int value, int min, int max) return (value < min) ? min : (value > max) ? max : value; } - public static double Clamp(double value, double min, double max) - { - return (value < min) ? min : (value > max) ? max : value; - } - public static int ClampByte(int value) { return Clamp(value, byte.MinValue, byte.MaxValue); diff --git a/Virtu/GamePort.cs b/Virtu/GamePort.cs index 8d420a1..830483b 100644 --- a/Virtu/GamePort.cs +++ b/Virtu/GamePort.cs @@ -22,7 +22,7 @@ public override void Initialize() _keyboardService = Machine.Services.GetService(); _gamePortService = Machine.Services.GetService(); - JoystickDeadZone = 0.4; + JoystickDeadZone = 0.4f; } public override void LoadState(BinaryReader reader, Version version) @@ -32,7 +32,8 @@ public override void LoadState(BinaryReader reader, Version version) throw new ArgumentNullException("reader"); } - JoystickDeadZone = reader.ReadDouble(); + JoystickDeadZone = reader.ReadSingle(); + UseShiftKeyMod = reader.ReadBoolean(); UseKeyboard = reader.ReadBoolean(); Joystick0UpLeftKey = reader.ReadInt32(); @@ -64,6 +65,7 @@ public override void SaveState(BinaryWriter writer) } writer.Write(JoystickDeadZone); + writer.Write(UseShiftKeyMod); writer.Write(UseKeyboard); writer.Write(Joystick0UpLeftKey); @@ -101,7 +103,7 @@ public bool ReadButton1() public bool ReadButton2() { - return (_gamePortService.IsButton2Down || !_keyboardService.IsShiftKeyDown || // Shift' [TN9] + return (_gamePortService.IsButton2Down || (UseShiftKeyMod && !_keyboardService.IsShiftKeyDown) || // Shift' [TN9] (UseKeyboard && (Button2Key > 0) && _keyboardService.IsKeyDown(Button2Key))); } @@ -196,7 +198,8 @@ private void ResetPaddle3StrobeEvent() Paddle3Strobe = false; } - public double JoystickDeadZone { get; set; } + public float JoystickDeadZone { get; set; } + public bool UseShiftKeyMod { get; set; } public bool UseKeyboard { get; set; } public int Joystick0UpLeftKey { get; set; } diff --git a/Virtu/Machine.cs b/Virtu/Machine.cs index a8e4557..2e371f5 100644 --- a/Virtu/Machine.cs +++ b/Virtu/Machine.cs @@ -110,11 +110,14 @@ private void LoadState() { try { - var version = new Version(Version); using (var reader = new BinaryReader(stream)) { - version = new Version(reader.ReadString()); - Components.ForEach(component => component.LoadState(reader, version)); + string stateSignature = reader.ReadString(); + var stateVersion = new Version(reader.ReadString()); + if ((stateSignature == StateSignature) && (stateVersion == new Version(Machine.Version))) // avoid state version mismatch (for now) + { + Components.ForEach(component => component.LoadState(reader, stateVersion)); + } } } catch (Exception) @@ -139,7 +142,8 @@ private void SaveState() { using (var writer = new BinaryWriter(stream)) { - writer.Write(Version); + writer.Write(StateSignature); + writer.Write(Machine.Version); Components.ForEach(component => component.SaveState(writer)); } }); @@ -172,7 +176,7 @@ private void Run() // machine thread Uninitialize(); } - public const string Version = "0.9.0.0"; + public const string Version = "0.9.1.0"; public MachineEvents Events { get; private set; } public MachineServices Services { get; private set; } @@ -201,6 +205,7 @@ private void Run() // machine thread public Thread Thread { get; private set; } private const string LastStateFileName = "LastState.bin"; + private const string StateSignature = "Virtu"; private AutoResetEvent _pauseEvent = new AutoResetEvent(false); private AutoResetEvent _unpauseEvent = new AutoResetEvent(false); diff --git a/Virtu/Services/AudioService.cs b/Virtu/Services/AudioService.cs index 8465389..8f457e2 100644 --- a/Virtu/Services/AudioService.cs +++ b/Virtu/Services/AudioService.cs @@ -39,7 +39,7 @@ public void Reset() Buffer.BlockCopy(SampleZero, 0, _buffer, 0, SampleSize); } - public abstract void SetVolume(double volume); + public abstract void SetVolume(float volume); protected void Update() // audio thread { diff --git a/Virtu/Silverlight/Services/SilverlightAudioService.cs b/Virtu/Silverlight/Services/SilverlightAudioService.cs index 802aed2..f254b8c 100644 --- a/Virtu/Silverlight/Services/SilverlightAudioService.cs +++ b/Virtu/Silverlight/Services/SilverlightAudioService.cs @@ -28,7 +28,7 @@ public SilverlightAudioService(Machine machine, UserControl page, MediaElement m #endif } - public override void SetVolume(double volume) + public override void SetVolume(float volume) { _media.Dispatcher.Send(() => _media.Volume = volume); } diff --git a/Virtu/Speaker.cs b/Virtu/Speaker.cs index 7a55033..189d8fb 100644 --- a/Virtu/Speaker.cs +++ b/Virtu/Speaker.cs @@ -17,9 +17,9 @@ public override void Initialize() _audioService = Machine.Services.GetService(); #if WINDOWS_PHONE - Volume = 0.85; + Volume = 0.85f; #else - Volume = 0.5; + Volume = 0.5f; #endif Machine.Events.AddEvent(CyclesPerFlush * Machine.Cpu.Multiplier, _flushOutputEvent); } @@ -38,7 +38,7 @@ public override void LoadState(BinaryReader reader, Version version) throw new ArgumentNullException("reader"); } - Volume = reader.ReadDouble(); + Volume = reader.ReadSingle(); } public override void SaveState(BinaryWriter writer) @@ -77,7 +77,7 @@ private void UpdateCycles() _lastCycles = Machine.Cpu.Cycles; } - public double Volume { get { return _volume; } set { _volume = value; _audioService.SetVolume(_volume); } } + public float Volume { get { return _volume; } set { _volume = value; _audioService.SetVolume(_volume); } } private const int CyclesPerFlush = 23; @@ -89,6 +89,6 @@ private void UpdateCycles() private int _highCycles; private int _totalCycles; private long _lastCycles; - private double _volume; + private float _volume; } } diff --git a/Virtu/Wpf/Services/WpfAudioService.cs b/Virtu/Wpf/Services/WpfAudioService.cs index ad41c61..3f100e3 100644 --- a/Virtu/Wpf/Services/WpfAudioService.cs +++ b/Virtu/Wpf/Services/WpfAudioService.cs @@ -28,7 +28,7 @@ public WpfAudioService(Machine machine, UserControl page) : }; } - public override void SetVolume(double volume) + public override void SetVolume(float volume) { _directSound.SetVolume(volume); } diff --git a/Virtu/Xna/Services/XnaAudioService.cs b/Virtu/Xna/Services/XnaAudioService.cs index f8a7c87..fac643c 100644 --- a/Virtu/Xna/Services/XnaAudioService.cs +++ b/Virtu/Xna/Services/XnaAudioService.cs @@ -23,9 +23,9 @@ public XnaAudioService(Machine machine, GameBase game) : _dynamicSoundEffect.Play(); } - public override void SetVolume(double volume) + public override void SetVolume(float volume) { - _dynamicSoundEffect.Volume = (float)volume; + _dynamicSoundEffect.Volume = volume; } protected override void Dispose(bool disposing) diff --git a/Virtu/Xna/Services/XnaGamePortService.cs b/Virtu/Xna/Services/XnaGamePortService.cs index 6c2b40e..979521c 100644 --- a/Virtu/Xna/Services/XnaGamePortService.cs +++ b/Virtu/Xna/Services/XnaGamePortService.cs @@ -20,22 +20,22 @@ public override void Update() // main thread var left = _state.ThumbSticks.Left; var right = _state.ThumbSticks.Right; var dpad = _state.DPad; - float joystickDeadZone = (float)Machine.GamePort.JoystickDeadZone; + var gamePort = Machine.GamePort; Paddle0 = (int)((1 + left.X) * PaddleScale); Paddle1 = (int)((1 - left.Y) * PaddleScale); // invert y Paddle2 = (int)((1 + right.X) * PaddleScale); Paddle3 = (int)((1 - right.Y) * PaddleScale); // invert y - IsJoystick0Up = ((left.Y > joystickDeadZone) || (dpad.Up == ButtonState.Pressed)); - IsJoystick0Left = ((left.X < -joystickDeadZone) || (dpad.Left == ButtonState.Pressed)); - IsJoystick0Right = ((left.X > joystickDeadZone) || (dpad.Right == ButtonState.Pressed)); - IsJoystick0Down = ((left.Y < -joystickDeadZone) || (dpad.Down == ButtonState.Pressed)); + IsJoystick0Up = ((left.Y > gamePort.JoystickDeadZone) || (dpad.Up == ButtonState.Pressed)); + IsJoystick0Left = ((left.X < -gamePort.JoystickDeadZone) || (dpad.Left == ButtonState.Pressed)); + IsJoystick0Right = ((left.X > gamePort.JoystickDeadZone) || (dpad.Right == ButtonState.Pressed)); + IsJoystick0Down = ((left.Y < -gamePort.JoystickDeadZone) || (dpad.Down == ButtonState.Pressed)); - IsJoystick1Up = (right.Y > joystickDeadZone); - IsJoystick1Left = (right.X < -joystickDeadZone); - IsJoystick1Right = (right.X > joystickDeadZone); - IsJoystick1Down = (right.Y < -joystickDeadZone); + IsJoystick1Up = (right.Y > gamePort.JoystickDeadZone); + IsJoystick1Left = (right.X < -gamePort.JoystickDeadZone); + IsJoystick1Right = (right.X > gamePort.JoystickDeadZone); + IsJoystick1Down = (right.Y < -gamePort.JoystickDeadZone); IsButton0Down = ((_state.Buttons.A == ButtonState.Pressed) || (_state.Buttons.LeftShoulder == ButtonState.Pressed)); IsButton1Down = ((_state.Buttons.B == ButtonState.Pressed) || (_state.Buttons.RightShoulder == ButtonState.Pressed));