diff --git a/Projects/PixelVision8/PixelVision8.CoreDesktop.csproj b/Projects/PixelVision8/PixelVision8.CoreDesktop.csproj index 51697609..f652f1f8 100755 --- a/Projects/PixelVision8/PixelVision8.CoreDesktop.csproj +++ b/Projects/PixelVision8/PixelVision8.CoreDesktop.csproj @@ -180,6 +180,9 @@ SDK\Runner\Parsers\SystemParser.TilemapChip.cs + + SDK\Runner\Utils\ColorUtils.MonoGame.cs + SDK\Editor\Audio\SfxrMusicGeneratorChip.cs @@ -358,17 +361,17 @@ SDK\Player\Chips\IDraw.cs - - SDK\Player\Chips\Input\ControllerChip.cs + + SDK\Player\Chips\Input\ControllerChip.MonogGame.cs SDK\Player\Chips\Input\InputMap.cs - - SDK\Player\Chips\Input\KeyboardInputChip.cs + + SDK\Player\Chips\Input\KeyboardInputChip.MonoGame.cs - - SDK\Player\Chips\Input\MouseInputChip.cs + + SDK\Player\Chips\Input\MouseInputChip.MonoGame.cs SDK\Player\Chips\IUpdate.cs @@ -1244,10 +1247,5 @@ - - - SDK\Player\Chips\Audio\Sfxr - - diff --git a/Projects/PixelVision8Lite/PixelVision8Lite.CoreDesktop.csproj b/Projects/PixelVision8Lite/PixelVision8Lite.CoreDesktop.csproj index 0b174f2a..ed30ec6d 100755 --- a/Projects/PixelVision8Lite/PixelVision8Lite.CoreDesktop.csproj +++ b/Projects/PixelVision8Lite/PixelVision8Lite.CoreDesktop.csproj @@ -145,6 +145,15 @@ SDK\Player\Chips\Audio\SoundChannel.MonoGame.cs + + SDK\Player\Chips\Input\ControllerChip.MonogGame.cs + + + SDK\Player\Chips\Input\KeyboardInputChip.MonoGame.cs + + + SDK\Player\Chips\Input\MouseInputChip.MonoGame.cs + SDK\Player\Data\Point.cs @@ -252,15 +261,6 @@ SDK/Player/Chips/IDraw.cs - - SDK/Player/Chips/Input/ControllerChip.cs - - - SDK/Player/Chips/Input/KeyboardInputChip.cs - - - SDK/Player/Chips/Input/MouseInputChip.cs - SDK/Player/Chips/IUpdate.cs @@ -325,6 +325,9 @@ + + SDK\Runner\Utils\ColorUtils.MonoGame.cs + SDK/Runner/Data/DisplayTarget.cs diff --git a/SDK/Editor/Exporters/ColorPaletteExporter.cs b/SDK/Editor/Exporters/ColorPaletteExporter.cs index 49037747..655db3b5 100755 --- a/SDK/Editor/Exporters/ColorPaletteExporter.cs +++ b/SDK/Editor/Exporters/ColorPaletteExporter.cs @@ -101,7 +101,7 @@ public void Dispose() public virtual void ConfigureColors() { - colors = DisplayTarget.ConvertColors(colorChip.HexColors, colorChip.MaskColor, true).Select(c=> new ColorData(c.R, c.G, c.B)).ToArray(); + colors = ColorUtils.ConvertColors(colorChip.HexColors, colorChip.MaskColor, true).Select(c=> new ColorData(c.R, c.G, c.B)).ToArray(); // // colorChip.colors; total = colors.Length; diff --git a/SDK/Editor/Exporters/GifExporter.cs b/SDK/Editor/Exporters/GifExporter.cs index 17c8671b..3cb234bb 100755 --- a/SDK/Editor/Exporters/GifExporter.cs +++ b/SDK/Editor/Exporters/GifExporter.cs @@ -102,7 +102,7 @@ public ColorData[] VisiblePixels() // TODO there might be a better way to do this like grabbing the pixel data from somewhere else? var pixels = DisplayChip.Pixels; - var cachedColors = DisplayTarget.ConvertColors(engine.ColorChip.HexColors, engine.ColorChip.MaskColor, + var cachedColors = ColorUtils.ConvertColors(engine.ColorChip.HexColors, engine.ColorChip.MaskColor, engine.ColorChip.DebugMode, engine.ColorChip.BackgroundColor).Select(c=> new ColorData(c.R, c.G, c.B)).ToArray(); // var cachedColors = engine.ColorChip.colors; diff --git a/SDK/Editor/Exporters/SpriteExporter.cs b/SDK/Editor/Exporters/SpriteExporter.cs index 60e80e4c..7d744be8 100755 --- a/SDK/Editor/Exporters/SpriteExporter.cs +++ b/SDK/Editor/Exporters/SpriteExporter.cs @@ -48,7 +48,7 @@ public class SpriteExporter : IExporter // var colorMapChip = engine.GetChip(ColorMapParser.chipName, false) as ColorChip; - colors = DisplayTarget.ConvertColors(engine.ColorChip.HexColors, engine.ColorChip.MaskColor, true).Select(c=>new ColorData(c.R, c.G, c.B)).ToArray(); + colors = ColorUtils.ConvertColors(engine.ColorChip.HexColors, engine.ColorChip.MaskColor, true).Select(c=>new ColorData(c.R, c.G, c.B)).ToArray(); // TODO removing the color map chip dependency when exporting moving forward // colors = colorMapChip == null ? engine.ColorChip.colors : colorMapChip.colors; diff --git a/SDK/Editor/Services/GameDataExportService.cs b/SDK/Editor/Services/GameDataExportService.cs index 682d8f1f..21549513 100755 --- a/SDK/Editor/Services/GameDataExportService.cs +++ b/SDK/Editor/Services/GameDataExportService.cs @@ -137,7 +137,7 @@ public void ExportImage(string path, int[] pixelData, PixelVision engine, int wi var imageExporter = new PNGWriter(); // TODO need to double check that we should force this into debug so transparent images have the mask color in them by default - var colors = DisplayTarget.ConvertColors(engine.ColorChip.HexColors, engine.ColorChip.MaskColor, true).Select(c=> new ColorData(c.R, c.G, c.B)).ToArray(); + var colors = ColorUtils.ConvertColors(engine.ColorChip.HexColors, engine.ColorChip.MaskColor, true).Select(c=> new ColorData(c.R, c.G, c.B)).ToArray(); AddExporter(new PixelDataExporter(path, pixelData, width, height, colors, imageExporter, diff --git a/SDK/Editor/Services/ScreenshotService.cs b/SDK/Editor/Services/ScreenshotService.cs index 44c9395b..2ec774dd 100755 --- a/SDK/Editor/Services/ScreenshotService.cs +++ b/SDK/Editor/Services/ScreenshotService.cs @@ -100,7 +100,7 @@ public bool TakeScreenshot(PixelVision engine) { // var cachedColors = engine.ColorChip.colors; - var cachedColors = DisplayTarget.ConvertColors(engine.ColorChip.HexColors, "#FF00FF", true).Select(c=> new ColorData(c.R, c.G, c.B)).ToArray(); + var cachedColors = ColorUtils.ConvertColors(engine.ColorChip.HexColors, "#FF00FF", true).Select(c=> new ColorData(c.R, c.G, c.B)).ToArray(); var pixels = engine.DisplayChip.Pixels; diff --git a/SDK/Lua/Chips/Game/LuaGameChip.Debug.cs b/SDK/Lua/Chips/Game/LuaGameChip.Debug.cs index ce2d2209..2544a8cd 100644 --- a/SDK/Lua/Chips/Game/LuaGameChip.Debug.cs +++ b/SDK/Lua/Chips/Game/LuaGameChip.Debug.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using Microsoft.Xna.Framework.Input; namespace PixelVision8.Player { diff --git a/SDK/Lua/Chips/Game/LuaGameChip.Math.cs b/SDK/Lua/Chips/Game/LuaGameChip.Math.cs index dd1ddbe3..b6267c2f 100644 --- a/SDK/Lua/Chips/Game/LuaGameChip.Math.cs +++ b/SDK/Lua/Chips/Game/LuaGameChip.Math.cs @@ -1,5 +1,4 @@ using System; -using Microsoft.Xna.Framework.Input; namespace PixelVision8.Player { diff --git a/SDK/Lua/Chips/Game/LuaGameChip.Sound.cs b/SDK/Lua/Chips/Game/LuaGameChip.Sound.cs index 5daa8bf4..096ab2d4 100644 --- a/SDK/Lua/Chips/Game/LuaGameChip.Sound.cs +++ b/SDK/Lua/Chips/Game/LuaGameChip.Sound.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; -using Microsoft.Xna.Framework.Input; using MoonSharp.Interpreter; -using PixelVision8.Player; namespace PixelVision8.Player { diff --git a/SDK/Lua/Chips/Game/LuaGameChip.Sprites.cs b/SDK/Lua/Chips/Game/LuaGameChip.Sprites.cs index f312d023..d0888a01 100644 --- a/SDK/Lua/Chips/Game/LuaGameChip.Sprites.cs +++ b/SDK/Lua/Chips/Game/LuaGameChip.Sprites.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using Microsoft.Xna.Framework.Input; using MoonSharp.Interpreter; namespace PixelVision8.Player diff --git a/SDK/Lua/Chips/Game/LuaGameChip.Tilemap.cs b/SDK/Lua/Chips/Game/LuaGameChip.Tilemap.cs index 75e89c45..ac7e66ee 100644 --- a/SDK/Lua/Chips/Game/LuaGameChip.Tilemap.cs +++ b/SDK/Lua/Chips/Game/LuaGameChip.Tilemap.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using Microsoft.Xna.Framework.Input; using MoonSharp.Interpreter; namespace PixelVision8.Player diff --git a/SDK/Lua/Chips/Game/LuaGameChip.Utils.cs b/SDK/Lua/Chips/Game/LuaGameChip.Utils.cs index 3e324aab..b6992115 100644 --- a/SDK/Lua/Chips/Game/LuaGameChip.Utils.cs +++ b/SDK/Lua/Chips/Game/LuaGameChip.Utils.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using Microsoft.Xna.Framework.Input; namespace PixelVision8.Player { diff --git a/SDK/Lua/Services/LuaService.Boot.cs b/SDK/Lua/Services/LuaService.Boot.cs index 99324149..23c6b4cb 100644 --- a/SDK/Lua/Services/LuaService.Boot.cs +++ b/SDK/Lua/Services/LuaService.Boot.cs @@ -1,6 +1,5 @@ using System; using MoonSharp.Interpreter; -using PixelVision8.Workspace; namespace PixelVision8.Runner { diff --git a/SDK/Lua/Services/LuaService.Debug.cs b/SDK/Lua/Services/LuaService.Debug.cs index 9a5b582a..77abf40a 100644 --- a/SDK/Lua/Services/LuaService.Debug.cs +++ b/SDK/Lua/Services/LuaService.Debug.cs @@ -1,6 +1,5 @@ using System; using MoonSharp.Interpreter; -using PixelVision8.Workspace; namespace PixelVision8.Runner { diff --git a/SDK/Lua/Services/LuaService.Disks.cs b/SDK/Lua/Services/LuaService.Disks.cs index 7cd854d8..021ef139 100644 --- a/SDK/Lua/Services/LuaService.Disks.cs +++ b/SDK/Lua/Services/LuaService.Disks.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.IO; using MoonSharp.Interpreter; using PixelVision8.Workspace; diff --git a/SDK/Lua/Services/LuaService.Editor.cs b/SDK/Lua/Services/LuaService.Editor.cs index eeb477b5..b4bedd6d 100644 --- a/SDK/Lua/Services/LuaService.Editor.cs +++ b/SDK/Lua/Services/LuaService.Editor.cs @@ -1,7 +1,5 @@ -using System; using MoonSharp.Interpreter; using PixelVision8.Editor; -using PixelVision8.Workspace; namespace PixelVision8.Runner { diff --git a/SDK/Lua/Services/LuaService.Export.cs b/SDK/Lua/Services/LuaService.Export.cs index c40ddadd..978cd41d 100644 --- a/SDK/Lua/Services/LuaService.Export.cs +++ b/SDK/Lua/Services/LuaService.Export.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using MoonSharp.Interpreter; -using PixelVision8.Workspace; namespace PixelVision8.Runner { diff --git a/SDK/Lua/Services/LuaService.Input.cs b/SDK/Lua/Services/LuaService.Input.cs index 7e922a85..07ea42bc 100644 --- a/SDK/Lua/Services/LuaService.Input.cs +++ b/SDK/Lua/Services/LuaService.Input.cs @@ -1,7 +1,5 @@ using System; -using System.Collections.Generic; using MoonSharp.Interpreter; -using PixelVision8.Workspace; namespace PixelVision8.Runner { diff --git a/SDK/Lua/Services/LuaService.Load.cs b/SDK/Lua/Services/LuaService.Load.cs index 5b533aee..02909849 100644 --- a/SDK/Lua/Services/LuaService.Load.cs +++ b/SDK/Lua/Services/LuaService.Load.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using MoonSharp.Interpreter; -using PixelVision8.Workspace; namespace PixelVision8.Runner { diff --git a/SDK/Lua/Services/LuaService.Preload.cs b/SDK/Lua/Services/LuaService.Preload.cs index e90c725d..025dcf59 100644 --- a/SDK/Lua/Services/LuaService.Preload.cs +++ b/SDK/Lua/Services/LuaService.Preload.cs @@ -1,8 +1,6 @@ using System; -using System.Collections.Generic; using MoonSharp.Interpreter; using PixelVision8.Player; -using PixelVision8.Workspace; namespace PixelVision8.Runner { diff --git a/SDK/Lua/Services/LuaService.SystemIO.cs b/SDK/Lua/Services/LuaService.SystemIO.cs index b50690a8..61067599 100644 --- a/SDK/Lua/Services/LuaService.SystemIO.cs +++ b/SDK/Lua/Services/LuaService.SystemIO.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using Microsoft.Xna.Framework; using MoonSharp.Interpreter; using PixelVision8.Editor; using PixelVision8.Player; diff --git a/SDK/Lua/Services/LuaService.Threading.cs b/SDK/Lua/Services/LuaService.Threading.cs index 3286af05..9239a00c 100644 --- a/SDK/Lua/Services/LuaService.Threading.cs +++ b/SDK/Lua/Services/LuaService.Threading.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using MoonSharp.Interpreter; using PixelVision8.Editor; -using PixelVision8.Workspace; namespace PixelVision8.Runner { diff --git a/SDK/Player/Chips/Game/GameChip.Sound.cs b/SDK/Player/Chips/Game/GameChip.Sound.cs index 826ae726..fcf2dfb9 100644 --- a/SDK/Player/Chips/Game/GameChip.Sound.cs +++ b/SDK/Player/Chips/Game/GameChip.Sound.cs @@ -41,12 +41,7 @@ public void PlaySound(int id, int channel = 0) { SoundChip.PlaySound(id, channel); } - - public void PlaySample(string name, int channel = 0) - { - SoundChip.PlaySound(name, channel); - } - + /// /// Use StopSound() to stop any sound playing on a specific channel. /// diff --git a/SDK/Player/Chips/Input/ControllerChip.cs b/SDK/Player/Chips/Input/ControllerChip.MonogGame.cs similarity index 100% rename from SDK/Player/Chips/Input/ControllerChip.cs rename to SDK/Player/Chips/Input/ControllerChip.MonogGame.cs diff --git a/SDK/Player/Chips/Input/KeyboardInputChip.cs b/SDK/Player/Chips/Input/KeyboardInputChip.MonoGame.cs similarity index 100% rename from SDK/Player/Chips/Input/KeyboardInputChip.cs rename to SDK/Player/Chips/Input/KeyboardInputChip.MonoGame.cs diff --git a/SDK/Player/Chips/Input/MouseInputChip.cs b/SDK/Player/Chips/Input/MouseInputChip.MonoGame.cs similarity index 100% rename from SDK/Player/Chips/Input/MouseInputChip.cs rename to SDK/Player/Chips/Input/MouseInputChip.MonoGame.cs diff --git a/SDK/Player/Data/SoundData.Sfxr.cs b/SDK/Player/Data/SoundData.Sfxr.cs index b56ddcd7..72d33f4c 100755 --- a/SDK/Player/Data/SoundData.Sfxr.cs +++ b/SDK/Player/Data/SoundData.Sfxr.cs @@ -19,7 +19,6 @@ // using System; -using PixelVision8.Workspace; namespace PixelVision8.Player { diff --git a/SDK/Player/Data/TrackerData.cs b/SDK/Player/Data/TrackerData.cs index 9c140ce9..7a7ba023 100755 --- a/SDK/Player/Data/TrackerData.cs +++ b/SDK/Player/Data/TrackerData.cs @@ -19,7 +19,6 @@ // using System; -using System.Text; namespace PixelVision8.Player { diff --git a/SDK/Runner/Data/ColorData.cs b/SDK/Runner/Data/ColorData.cs index a208f148..cd917faa 100644 --- a/SDK/Runner/Data/ColorData.cs +++ b/SDK/Runner/Data/ColorData.cs @@ -1,116 +1,51 @@ -// MIT License - Copyright (C) The Mono.Xna Team -// This file is subject to the terms and conditions defined in -// file 'LICENSE.txt', which is part of this source code package. - using System; +using System.Globalization; using System.Text; -// using System.Runtime.Serialization; -// using System.Diagnostics; - namespace PixelVision8.Runner { - /// - /// Describes a 32-bit packed color. - /// - // [DataContract] - // [DebuggerDisplay("{DebugDisplayString,nq}")] - public struct ColorData + + public readonly struct ColorData { + public readonly byte B; + public readonly byte G; + public readonly byte R; + public readonly byte A; - // Stored as RGBA with R in the least significant octet: - // |-------|-------|-------|------- - // A B G R - private readonly uint _packedValue; - /// - /// Constructs an RGBA color from scalars representing red, green, blue and alpha values. + /// Static method for converting a HEX color into an RGB value. /// - /// - /// This overload sets the values directly without clamping, and may therefore be faster than the other overloads. - /// + /// /// /// /// - /// - public ColorData(byte r, byte g, byte b, byte alpha = Byte.MaxValue) + public static void HexToRgb(string hex, out byte r, out byte g, out byte b) { - _packedValue = ((uint)alpha << 24) | ((uint)b << 16) | ((uint)g << 8) | (r); - } + hex ??= "FF00FF"; - public ColorData(string hex) - { - DisplayTarget.HexToRgb(hex, out var r, out var g, out var b); + if (hex[0] == '#') hex = hex.Substring(1); - _packedValue = ((uint)Byte.MaxValue << 24) | ((uint)b << 16) | ((uint)g << 8) | (r); - + r = byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber); + g = byte.Parse(hex.Substring(2, 2), NumberStyles.HexNumber); + b = byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber); } - - /// - /// Gets or sets the blue component. - /// - // [DataMember] - public byte B - { - get - { - unchecked - { - return (byte) (_packedValue >> 16); - } - } - } - - /// - /// Gets or sets the green component. - /// - // [DataMember] - public byte G - { - get - { - unchecked - { - return (byte)(_packedValue >> 8); - } - } - } - - /// - /// Gets or sets the red component. - /// - // [DataMember] - public byte R + + public ColorData(byte r, byte g, byte b, byte alpha = Byte.MaxValue) { - get - { - unchecked - { - return (byte) _packedValue; - } - } + R = r; + G = g; + B = b; + A = alpha; } - /// - /// Gets or sets the alpha component. - /// - // [DataMember] - public byte A + public ColorData(string hex) { - get - { - unchecked - { - return (byte)(_packedValue >> 24); - } - } + HexToRgb(hex, out R, out G, out B); + + A = Byte.MaxValue; } - /// - /// Returns a representation of this in the format: - /// {R:[red] G:[green] B:[blue] A:[alpha]} - /// - /// representation of this . + public override string ToString () { StringBuilder sb = new StringBuilder(25); diff --git a/SDK/Runner/Data/DisplayTarget.MonoGame.cs b/SDK/Runner/Data/DisplayTarget.MonoGame.cs index b275360d..034e14d6 100644 --- a/SDK/Runner/Data/DisplayTarget.MonoGame.cs +++ b/SDK/Runner/Data/DisplayTarget.MonoGame.cs @@ -1,3 +1,4 @@ +using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; @@ -7,12 +8,36 @@ namespace PixelVision8.Runner public partial class DisplayTarget { + private static int RealWidth => GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width; + private static int RealHeight => GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height; + private int _defaultColor; + private Vector2 _offset; + private Texture2D _renderTexture; + private GraphicsDeviceManager _graphicManager; + private SpriteBatch _spriteBatch; + private Color[] _cachedColors; + private Color[] _pixelData = new Color[0]; + private Rectangle _renderRect; + + public Vector2 Scale = new Vector2(1, 1); + + public GraphicsDeviceManager GraphicsManager + { + set + { + _graphicManager = value; + + _graphicManager.HardwareModeSwitch = false; + + _spriteBatch = new SpriteBatch(_graphicManager.GraphicsDevice); + } + } public void RebuildColorPalette(string[] hexColors, int bgColorId = 0, string maskColor = "#FF00FF", bool debugMode = false) { - CachedColors = ConvertColors( + _cachedColors = ColorUtils.ConvertColors( hexColors, maskColor, debugMode, @@ -23,38 +48,76 @@ public void RebuildColorPalette(string[] hexColors, int bgColorId = 0, string ma } - public virtual void Render(int[] pixels) + private void Apply() { - if (Invalid) + // Apply changes + _graphicManager.IsFullScreen = Fullscreen; + + if (_graphicManager.PreferredBackBufferWidth != _visibleWidth || + _graphicManager.PreferredBackBufferHeight != _visibleHeight) { + _graphicManager.PreferredBackBufferWidth = _visibleWidth; + _graphicManager.PreferredBackBufferHeight = _visibleHeight; + _graphicManager.ApplyChanges(); + } + + if (_renderTexture == null || _renderTexture.Width != _gameWidth || _renderTexture.Height != _gameHeight) + { + _renderTexture = new Texture2D(_graphicManager.GraphicsDevice, _gameWidth, _gameHeight); - CalculateResolution(); - - CalculateDisplayScale(); + _totalPixels = _renderTexture.Width * _renderTexture.Height; - CalculateDisplayOffset(); - - Apply(); + if (_pixelData.Length != _totalPixels) + { + Array.Resize(ref _pixelData, _totalPixels); + } - ResetValidation(); + // Calculate the game's resolution + _renderRect.Width = _renderTexture.Width; + _renderRect.Height = _renderTexture.Height; } + _offset.X = _offsetX; + _offset.Y = _offsetY; + + Scale.X = _scaleX; + Scale.Y = _scaleY; + + } + + public void ConfigureDisplay() + { + CalculateResolution(); + + CalculateDisplayScale(); + + CalculateDisplayOffset(); + + Apply(); + + ResetValidation(); + } + + public virtual void Render(int[] pixels) + { + if (Invalid) + ConfigureDisplay(); + // We can only update the display if the pixel lengths match up if (pixels.Length != _totalPixels) return; - SpriteBatch.Begin(SpriteSortMode.Immediate, null, SamplerState.PointClamp); + _spriteBatch.Begin(SpriteSortMode.Immediate, null, SamplerState.PointClamp); - for (_i = 0; _i < _totalPixels; _i++) + for (var i = 0; i < _totalPixels; i++) { - _colorId = pixels[_i]; - _pixelData[_i] = CachedColors[_colorId < 0 ? _defaultColor : _colorId]; + _pixelData[i] = _cachedColors[pixels[i] < 0 ? _defaultColor : pixels[i]]; } - RenderTexture.SetData(_pixelData); - SpriteBatch.Draw(RenderTexture, Offset, VisibleRect, Color.White, 0f, Vector2.Zero, Scale, SpriteEffects.None, 1f); - SpriteBatch.End(); + _renderTexture.SetData(_pixelData); + _spriteBatch.Draw(_renderTexture, _offset, _renderRect, Color.White, 0f, Vector2.Zero, Scale, SpriteEffects.None, 1f); + _spriteBatch.End(); } } } \ No newline at end of file diff --git a/SDK/Runner/Data/DisplayTarget.MonoVision.cs b/SDK/Runner/Data/DisplayTarget.MonoVision.cs index 8b3032f3..522e9df7 100644 --- a/SDK/Runner/Data/DisplayTarget.MonoVision.cs +++ b/SDK/Runner/Data/DisplayTarget.MonoVision.cs @@ -7,123 +7,191 @@ namespace PixelVision8.Runner { public partial class DisplayTarget { - private bool _useCRT; - private Effect crtShader; + private static int RealWidth => GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width; + private static int RealHeight => GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height; + + private readonly int _paletteWidth = 256; + private Vector2 _offset; + private Texture2D _renderTexture; + private GraphicsDeviceManager _graphicManager; + private SpriteBatch _spriteBatch; + private Color[] _cachedColors; + private Color[] _pixelData = new Color[0]; + private Rectangle _renderRect; + private bool _useCrt; + private Effect _crtShader; private Texture2D _colorPalette; - private readonly int paletteWidth = 256; + + public Vector2 Scale = new Vector2(1, 1); + + + public GraphicsDeviceManager GraphicsManager + { + set + { + _graphicManager = value; + + _graphicManager.HardwareModeSwitch = false; + + _spriteBatch = new SpriteBatch(_graphicManager.GraphicsDevice); + } + } + + private void Apply() + { + // Apply changes + _graphicManager.IsFullScreen = Fullscreen; + + if (_graphicManager.PreferredBackBufferWidth != _visibleWidth || + _graphicManager.PreferredBackBufferHeight != _visibleHeight) + { + _graphicManager.PreferredBackBufferWidth = _visibleWidth; + _graphicManager.PreferredBackBufferHeight = _visibleHeight; + _graphicManager.ApplyChanges(); + } + + if (_renderTexture == null || _renderTexture.Width != _gameWidth || _renderTexture.Height != _gameHeight) + { + _renderTexture = new Texture2D(_graphicManager.GraphicsDevice, _gameWidth, _gameHeight); + + _totalPixels = _renderTexture.Width * _renderTexture.Height; + + if (_pixelData.Length != _totalPixels) + { + Array.Resize(ref _pixelData, _totalPixels); + } + + // Calculate the game's resolution + _renderRect.Width = _renderTexture.Width; + _renderRect.Height = _renderTexture.Height; + + } + + _offset.X = _offsetX; + _offset.Y = _offsetY; + + Scale.X = _scaleX; + Scale.Y = _scaleY; + + } + + public bool CropScreen { get; set; } = true; - public bool useCRT + public bool UseCrt { - get { return _useCRT; } + get => _useCrt; set { - if (crtShader == null) return; + if (_crtShader == null) return; - _useCRT = value; + _useCrt = value; - crtShader?.Parameters["crtOn"].SetValue(value ? 1f : 0f); - crtShader?.Parameters["warp"].SetValue(value ? new Vector2(0.008f, 0.01f) : Vector2.Zero); + _crtShader?.Parameters["crtOn"].SetValue(value ? 1f : 0f); + _crtShader?.Parameters["warp"].SetValue(value ? new Vector2(0.008f, 0.01f) : Vector2.Zero); } } - public float brightness + public float Brightness { - get => crtShader?.Parameters["brightboost"]?.GetValueSingle() ?? 0; - set => crtShader?.Parameters["brightboost"]?.SetValue(MathHelper.Clamp(value, .255f, 1.5f)); + get => _crtShader?.Parameters["brightboost"]?.GetValueSingle() ?? 0; + set => _crtShader?.Parameters["brightboost"]?.SetValue(MathHelper.Clamp(value, .255f, 1.5f)); } - public float sharpness + public float Sharpness { - get => crtShader?.Parameters["hardPix"]?.GetValueSingle() ?? 0; - set => crtShader?.Parameters["hardPix"]?.SetValue(value); + get => _crtShader?.Parameters["hardPix"]?.GetValueSingle() ?? 0; + set => _crtShader?.Parameters["hardPix"]?.SetValue(value); } public bool HasShader() { - return crtShader != null; + return _crtShader != null; } - public Stream shaderPath + public Stream ShaderPath { set { using (var reader = new BinaryReader(value)) { - crtShader = new Effect(_graphicManager.GraphicsDevice, + _crtShader = new Effect(_graphicManager.GraphicsDevice, reader.ReadBytes((int) reader.BaseStream.Length)); } - useCRT = true; + UseCrt = true; } } public void RebuildColorPalette(string[] hexColors, int bgColorId = 0, string maskColor = "#FF00FF", bool debugMode = false) { - CachedColors = ConvertColors( + _cachedColors = ColorUtils.ConvertColors( hexColors, maskColor, debugMode, bgColorId ); - _colorPalette = new Texture2D(_graphicManager.GraphicsDevice, paletteWidth, - (int) Math.Ceiling(CachedColors.Length / (double) paletteWidth)); + _colorPalette = new Texture2D(_graphicManager.GraphicsDevice, _paletteWidth, + (int) Math.Ceiling(_cachedColors.Length / (double) _paletteWidth)); // We need at least 256 colors for the shader to work so pad the array - if (CachedColors.Length < 256) + if (_cachedColors.Length < 256) { - Array.Resize(ref CachedColors, 256); + Array.Resize(ref _cachedColors, 256); } - _colorPalette.SetData(CachedColors); + _colorPalette.SetData(_cachedColors); } private void UpdateShader() { - crtShader?.Parameters["textureSize"].SetValue(new Vector2(RenderTexture.Width, RenderTexture.Height)); - crtShader?.Parameters["videoSize"].SetValue(new Vector2(RenderTexture.Width, RenderTexture.Height)); + _crtShader?.Parameters["textureSize"].SetValue(new Vector2(_renderTexture.Width, _renderTexture.Height)); + _crtShader?.Parameters["videoSize"].SetValue(new Vector2(_renderTexture.Width, _renderTexture.Height)); } private void CalculateCrop() { if (CropScreen && !Fullscreen) { - DisplayWidth = Math.Min(DisplayWidth, (int) (GameWidth * Scale.X)); - DisplayHeight = Math.Min(DisplayHeight, (int) (GameHeight * Scale.Y)); - OffsetX = 0; - OffsetY = 0; + _visibleWidth = Math.Min(_visibleWidth, (int) (_gameWidth * _scaleX)); + _visibleHeight = Math.Min(_visibleHeight, (int) (_gameHeight * _scaleY)); + _offsetX = 0; + _offsetY = 0; } } - public void Render(int[] pixels) + public void ConfigureDisplay() { - - if (Invalid) - { - CalculateResolution(); + CalculateResolution(); - CalculateDisplayScale(); + CalculateDisplayScale(); - CalculateDisplayOffset(); + CalculateDisplayOffset(); - CalculateCrop(); + CalculateCrop(); - Apply(); + Apply(); - UpdateShader(); + UpdateShader(); - ResetValidation(); - } + ResetValidation(); + } + + public void Render(int[] pixels) + { + + if (Invalid) + ConfigureDisplay(); - RenderTexture.SetData(pixels); - SpriteBatch.Begin(); - crtShader.CurrentTechnique.Passes[0].Apply(); + _renderTexture.SetData(pixels); + _spriteBatch.Begin(); + _crtShader.CurrentTechnique.Passes[0].Apply(); _graphicManager.GraphicsDevice.Textures[1] = _colorPalette; _graphicManager.GraphicsDevice.SamplerStates[1] = SamplerState.PointClamp; - SpriteBatch.Draw(RenderTexture, Offset, VisibleRect, Color.White, Vector2.Zero, Scale); - SpriteBatch.End(); + _spriteBatch.Draw(_renderTexture, _offset, _renderRect, Color.White, Vector2.Zero, Scale); + _spriteBatch.End(); } } diff --git a/SDK/Runner/Data/DisplayTarget.cs b/SDK/Runner/Data/DisplayTarget.cs index e8deacd5..16559444 100755 --- a/SDK/Runner/Data/DisplayTarget.cs +++ b/SDK/Runner/Data/DisplayTarget.cs @@ -19,115 +19,34 @@ // using System; -using System.Globalization; -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; using PixelVision8.Player; -using Rectangle = Microsoft.Xna.Framework.Rectangle; namespace PixelVision8.Runner { public partial class DisplayTarget : AbstractData { - public static Color HexToColor(string hex) - { - HexToRgb(hex, out var r, out var g, out var b); - - return new Color(r, g, b); - } - - /// - /// Static method for converting a HEX color into an RGB value. - /// - /// - /// - /// - /// - public static void HexToRgb(string hex, out byte r, out byte g, out byte b) - { - if (hex == null) hex = "FF00FF"; - - if (hex[0] == '#') hex = hex.Substring(1); - - r = byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber); // / (float) byte.MaxValue; - g = byte.Parse(hex.Substring(2, 2), NumberStyles.HexNumber); // / (float) byte.MaxValue; - b = byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber); // / (float) byte.MaxValue; - } - - /// - /// The display target is in charge of converting system colors into MonoGame colors. This utility method - /// can be used by any external runner class to correctly convert hex colors into Colors. - /// - /// - /// - /// - /// - /// - public static Color[] ConvertColors(string[] hexColors, string maskColor = "#FF00FF", bool debugMode = false, int backgroundColor = 0) - { - var t = hexColors.Length; - var colors = new Color[t]; - - for (var i = 0; i < t; i++) - { - var colorHex = hexColors[i]; - - if (colorHex == maskColor && debugMode == false) colorHex = hexColors[backgroundColor]; - - var color = HexToColor(colorHex); - colors[i] = color; - } - - return colors; - } - - protected Vector2 Offset; - protected Texture2D RenderTexture; - protected GraphicsDeviceManager _graphicManager; - protected SpriteBatch SpriteBatch; - protected Color[] CachedColors; - public Vector2 Scale = new Vector2(1, 1); - private Color[] _pixelData = new Color[0]; - public int RealWidth => GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width; - public int RealHeight => GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height; - protected Rectangle VisibleRect; - - public GraphicsDeviceManager GraphicsManager - { - set - { - _graphicManager = value; - - _graphicManager.HardwareModeSwitch = false; - - SpriteBatch = new SpriteBatch(_graphicManager.GraphicsDevice); - } - } - - - - protected int GameWidth; - protected int GameHeight; - private readonly int _monitorHeight; - private readonly int _monitorWidth; + private int _gameWidth; + private int _gameHeight; + private readonly int _displayHeight; + private readonly int _displayWidth; private int _monitorScale = 1; private int _totalPixels; - private int _colorId; - private int _i; - protected int DisplayWidth; - protected int DisplayHeight; - protected float OffsetX; - protected float OffsetY; + private int _visibleWidth; + private int _visibleHeight; + private float _offsetX; + private float _offsetY; + private float _scaleX; + private float _scaleY; public bool Fullscreen { get; set; } public DisplayTarget(int width, int height) { - _monitorWidth = Utilities.Clamp(width, 64, 640); - _monitorHeight = Utilities.Clamp(height, 64, 480); + _displayWidth = Utilities.Clamp(width, 64, 640); + _displayHeight = Utilities.Clamp(height, 64, 480); - ResetResolution(_monitorWidth, _monitorHeight); + ResetResolution(_displayWidth, _displayHeight); } public int MonitorScale @@ -139,8 +58,8 @@ public int MonitorScale while (fits == false) { - var newWidth = _monitorWidth * value; - var newHeight = _monitorHeight * value; + var newWidth = _displayWidth * value; + var newHeight = _displayHeight * value; if (newWidth < RealWidth && newHeight < RealHeight) @@ -158,93 +77,44 @@ public int MonitorScale public void ResetResolution(int gameWidth, int gameHeight) { - GameWidth = gameWidth; - GameHeight = gameHeight; + _gameWidth = gameWidth; + _gameHeight = gameHeight; Invalidate(); } - protected void CalculateResolution() + private void CalculateResolution() { var tmpMonitorScale = Fullscreen ? 1 : MonitorScale; // Calculate the monitor's resolution - DisplayWidth = Fullscreen + _visibleWidth = Fullscreen ? RealWidth - : _monitorWidth * + : _displayWidth * tmpMonitorScale; - DisplayHeight = Fullscreen + _visibleHeight = Fullscreen ? RealHeight - : _monitorHeight * tmpMonitorScale; + : _displayHeight * tmpMonitorScale; } - - protected void CalculateDisplayScale() + + private void CalculateDisplayScale() { // Calculate the game scale - Scale.X = (float) DisplayWidth / GameWidth; - Scale.Y = (float) DisplayHeight / GameHeight; + _scaleX = (float) _visibleWidth / _gameWidth; + _scaleY = (float) _visibleHeight / _gameHeight; // To preserve the aspect ratio, // use the smaller scale factor. - Scale.X = Math.Min(Scale.X, Scale.Y); - Scale.Y = Scale.X; + _scaleX = Math.Min(_scaleX, _scaleY); + _scaleY = _scaleX; } - - protected void CalculateDisplayOffset() - { - OffsetX = (DisplayWidth - GameWidth * Scale.X) * .5f; - OffsetY = (DisplayHeight - GameHeight * Scale.Y) * .5f; - } - - public void ConfigureDisplay() - { - CalculateResolution(); - - CalculateDisplayScale(); - - CalculateDisplayOffset(); - - Apply(); - - ResetValidation(); - } - - protected void Apply() + private void CalculateDisplayOffset() { - // Apply changes - _graphicManager.IsFullScreen = Fullscreen; - - if (_graphicManager.PreferredBackBufferWidth != DisplayWidth || - _graphicManager.PreferredBackBufferHeight != DisplayHeight) - { - _graphicManager.PreferredBackBufferWidth = DisplayWidth; - _graphicManager.PreferredBackBufferHeight = DisplayHeight; - _graphicManager.ApplyChanges(); - } - - if (RenderTexture == null || RenderTexture.Width != GameWidth || RenderTexture.Height != GameHeight) - { - RenderTexture = new Texture2D(_graphicManager.GraphicsDevice, GameWidth, GameHeight); - - _totalPixels = RenderTexture.Width * RenderTexture.Height; - - if (_pixelData.Length != _totalPixels) - { - Array.Resize(ref _pixelData, _totalPixels); - } - - // Calculate the game's resolution - VisibleRect.Width = RenderTexture.Width; - VisibleRect.Height = RenderTexture.Height; - - } - - Offset.X = OffsetX; - Offset.Y = OffsetY; - + _offsetX = (_visibleWidth - _gameWidth * _scaleX) * .5f; + _offsetY = (_visibleHeight - _gameHeight * _scaleY) * .5f; } } diff --git a/SDK/Runner/DesktopRunner.cs b/SDK/Runner/DesktopRunner.cs index 0fe8310c..65ca78e6 100755 --- a/SDK/Runner/DesktopRunner.cs +++ b/SDK/Runner/DesktopRunner.cs @@ -282,7 +282,7 @@ public override void ConfigureDisplayTarget() "/App/Effects/crt-lottes-mg.ogl.mgfxo")); if (workspaceService.Exists(shaderPath)) - DisplayTarget.shaderPath = + DisplayTarget.ShaderPath = workspaceService.OpenFile(shaderPath, FileAccess.Read); } @@ -1977,34 +1977,34 @@ public bool EnableCRT(bool? toggle) { if (toggle.HasValue) { - DisplayTarget.useCRT = toggle.Value; + DisplayTarget.UseCrt = toggle.Value; bios.UpdateBiosData(CRTSettings.CRT.ToString(), toggle.Value.ToString()); InvalidateResolution(); } - return DisplayTarget.useCRT; + return DisplayTarget.UseCrt; } public float Brightness(float? brightness = null) { if (brightness.HasValue) { - DisplayTarget.brightness = brightness.Value; + DisplayTarget.Brightness = brightness.Value; bios.UpdateBiosData(CRTSettings.Brightness.ToString(), (brightness * 100).ToString()); } - return DisplayTarget.brightness; + return DisplayTarget.Brightness; } public float Sharpness(float? sharpness = null) { if (sharpness.HasValue) { - DisplayTarget.sharpness = sharpness.Value; + DisplayTarget.Sharpness = sharpness.Value; bios.UpdateBiosData(CRTSettings.Sharpness.ToString(), sharpness.ToString()); } - return DisplayTarget.sharpness; + return DisplayTarget.Sharpness; } #endregion diff --git a/SDK/Runner/Game/GameRunner.Sound.cs b/SDK/Runner/Game/GameRunner.Sound.cs index c671d9af..c136bcad 100644 --- a/SDK/Runner/Game/GameRunner.Sound.cs +++ b/SDK/Runner/Game/GameRunner.Sound.cs @@ -4,9 +4,9 @@ namespace PixelVision8.Runner { public partial class GameRunner { - protected static bool _mute; - protected static int _lastVolume; - protected static int _muteVolume; + private static bool _mute; + private static int _lastVolume; + private static int _muteVolume; public virtual int Volume(int? value = null) { diff --git a/SDK/Runner/Parsers/ColorParser.cs b/SDK/Runner/Parsers/ColorParser.cs index 1327b1b1..b27bb888 100755 --- a/SDK/Runner/Parsers/ColorParser.cs +++ b/SDK/Runner/Parsers/ColorParser.cs @@ -25,25 +25,25 @@ namespace PixelVision8.Runner { public class ColorParser : ImageParser { - protected readonly List colors = new List(); + private readonly List _colors = new List(); - protected ColorChip colorChip; + private readonly ColorChip _colorChip; - protected ColorData magenta; - protected ColorData tmpColor; - protected int totalColors; + private readonly ColorData _magenta; + private ColorData _tmpColor; + private int _totalColors; public ColorParser(string sourceFile, IImageParser parser, ColorChip colorChip) : base(parser) { SourcePath = sourceFile; - this.colorChip = colorChip; + this._colorChip = colorChip; // unique = colorChip.unique; - magenta = new ColorData(colorChip.MaskColor); + _magenta = new ColorData(colorChip.MaskColor); } public override void CalculateSteps() { - colorChip.Clear(); + _colorChip.Clear(); base.CalculateSteps(); @@ -64,27 +64,27 @@ public virtual void ReadColors() for (var i = 0; i < total; i++) { // Get the current color - tmpColor = srcColors[i]; //pixels[i]); + _tmpColor = srcColors[i]; //pixels[i]); - if (tmpColor.A < 1) // && !ignoreTransparent) - tmpColor = magenta; + if (_tmpColor.A < 1) // && !ignoreTransparent) + _tmpColor = _magenta; - colors.Add(tmpColor); + _colors.Add(_tmpColor); } - totalColors = colors.Count; + _totalColors = _colors.Count; CurrentStep++; } public void UpdateColors() { - for (var i = 0; i < totalColors; i++) + for (var i = 0; i < _totalColors; i++) { - var tmpColor = colors[i]; + var tmpColor = _colors[i]; var hex = SpriteImageParser.RgbToHex(tmpColor.R, tmpColor.G, tmpColor.B); - colorChip.UpdateColorAt(i, hex); + _colorChip.UpdateColorAt(i, hex); } CurrentStep++; diff --git a/SDK/Runner/Parsers/SpriteImageParser.cs b/SDK/Runner/Parsers/SpriteImageParser.cs index b85271f8..b3913e09 100755 --- a/SDK/Runner/Parsers/SpriteImageParser.cs +++ b/SDK/Runner/Parsers/SpriteImageParser.cs @@ -110,7 +110,7 @@ public virtual void PrepareSprites() public virtual void CreateImage() { // Get the chip colors and replace any transparent ones with the first color so we don't parse transparency - var colorData = DisplayTarget.ConvertColors(colorChip.HexColors, colorChip.MaskColor); + var colorData = ColorUtils.ConvertColors(colorChip.HexColors, colorChip.MaskColor); // colorData = colorChip.colors; diff --git a/SDK/Runner/Services/LoadService.cs b/SDK/Runner/Services/LoadService.cs index 70b1b07d..af9ed361 100755 --- a/SDK/Runner/Services/LoadService.cs +++ b/SDK/Runner/Services/LoadService.cs @@ -18,11 +18,8 @@ // Shawn Rakowski - @shwany // -using Microsoft.Xna.Framework; using PixelVision8.Player; -using PixelVision8.Runner; using System; -using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Threading; diff --git a/SDK/Runner/Utils/ColorUtils.MonoGame.cs b/SDK/Runner/Utils/ColorUtils.MonoGame.cs new file mode 100644 index 00000000..1e391147 --- /dev/null +++ b/SDK/Runner/Utils/ColorUtils.MonoGame.cs @@ -0,0 +1,36 @@ +using Microsoft.Xna.Framework; + +namespace PixelVision8.Runner +{ + public class ColorUtils + { + /// + /// The display target is in charge of converting system colors into MonoGame colors. This utility method + /// can be used by any external runner class to correctly convert hex colors into Colors. + /// + /// + /// + /// + /// + /// + public static Color[] ConvertColors(string[] hexColors, string maskColor = "#FF00FF", bool debugMode = false, int backgroundColor = 0) + { + var t = hexColors.Length; + var colors = new Color[t]; + + for (var i = 0; i < t; i++) + { + var colorHex = hexColors[i]; + + if (colorHex == maskColor && debugMode == false) colorHex = hexColors[backgroundColor]; + + ColorData.HexToRgb(colorHex, out var r, out var g, out var b); + + colors[i] = new Color(r, g, b); + + } + + return colors; + } + } +} \ No newline at end of file