Skip to content
This repository has been archived by the owner on Jan 4, 2023. It is now read-only.

Commit

Permalink
Breaking up DisplayTarget into MonoGame and MonoVision partials and p…
Browse files Browse the repository at this point in the history
…reparing SoundChannel for similar split.
  • Loading branch information
jessefreeman committed Mar 19, 2021
1 parent 9afb47a commit e383b12
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 263 deletions.
11 changes: 4 additions & 7 deletions Projects/PixelVision8/PixelVision8.CoreDesktop.csproj
Expand Up @@ -45,6 +45,9 @@
</ItemGroup>

<ItemGroup>
<Compile Include="..\..\SDK\Player\Chips\Audio\SoundChannel.MonoGame.cs">
<Link>SDK\Player\Chips\Audio\SoundChannel.MonoGame.cs</Link>
</Compile>
<Compile Include="..\..\SDK\Player\Chips\Game\GameChip.Log.cs">
<Link>SDK\Player\Chips\Game\GameChip.Log.cs</Link>
</Compile>
Expand Down Expand Up @@ -397,14 +400,11 @@
<Compile Include="..\..\SDK\Runner\Data\DisplayTarget.MonoVision.cs">
<Link>SDK\Runner\Data\DisplayTarget.MonoVision.cs</Link>
</Compile>
<Compile Include="..\..\SDK\Runner\Data\ShaderDisplayTarget.cs">
<Link>SDK\Runner\Data\ShaderDisplayTarget.cs</Link>
</Compile>
<Compile Include="..\..\SDK\Runner\Data\DisplayTarget.cs">
<Link>SDK\Runner\Data\DisplayTarget.cs</Link>
</Compile>
<Compile Include="..\..\SDK\Player\Data\ImageData.cs">
<Link>SDK\Runner\SDK\Player\Data\ImageData.cs</Link>
<Link>SDK\Player\Data\ImageData.cs</Link>
</Compile>
<Compile Include="..\..\SDK\Runner\DesktopRunner.cs">
<Link>SDK\Runner\DesktopRunner.cs</Link>
Expand Down Expand Up @@ -496,9 +496,6 @@
<Compile Include="..\..\SDK\Runner\Services\SaveFlags.cs">
<Link>SDK\Runner\Services\SaveFlags.cs</Link>
</Compile>
<Compile Include="..\..\SDK\Runner\Utils\FileLoadHelper.cs">
<Link>SDK\Runner\Utils\FileLoadHelper.cs</Link>
</Compile>
<Compile Include="..\..\SDK\Runner\Utils\GifUtils\Data\GifFrame.cs">
<Link>SDK\Runner\Utils\GifUtils\Data\GifFrame.cs</Link>
</Compile>
Expand Down
3 changes: 3 additions & 0 deletions Projects/PixelVision8Lite/PixelVision8Lite.CoreDesktop.csproj
Expand Up @@ -142,6 +142,9 @@

<!-- Parser -->
<ItemGroup>
<Compile Include="..\..\SDK\Player\Chips\Audio\SoundChannel.MonoGame.cs">
<Link>SDK\Player\Chips\Audio\SoundChannel.MonoGame.cs</Link>
</Compile>
<Compile Include="..\..\SDK\Player\Data\Point.cs">
<Link>SDK\Player\Data\Point.cs</Link>
</Compile>
Expand Down
1 change: 0 additions & 1 deletion SDK/Player/Chips/Audio/Sfxr/SfxrParams.cs
Expand Up @@ -21,7 +21,6 @@
//
// @author Zeh Fernando

using Microsoft.Xna.Framework;
using System;

namespace PixelVision8.Player.Audio
Expand Down
35 changes: 35 additions & 0 deletions SDK/Player/Chips/Audio/SoundChannel.MonoGame.cs
@@ -0,0 +1,35 @@
using System.Collections.Generic;
using System.IO;
using Microsoft.Xna.Framework.Audio;

namespace PixelVision8.Player
{
public partial class SoundChannel
{
private SoundEffectInstance _soundInstance;
protected readonly Dictionary<string, SoundEffectInstance> SoundInstanceCache = new Dictionary<string, SoundEffectInstance>();

public bool IsPlaying()
{

if (_soundInstance == null) return false;

return _soundInstance.State == SoundState.Playing;

}

protected void CreateSoundEffect(byte[] bytes, float? frequency = null)
{
using (var stream = new MemoryStream(bytes))
{
var soundEffect = SoundEffect.FromStream(stream);

_soundInstance = soundEffect.CreateInstance();

// TODO need to make sure this is correct and we can use the frequency to manipulate the pitch
if (frequency.HasValue)
_soundInstance.Pitch = frequency.Value;
}
}
}
}
33 changes: 3 additions & 30 deletions SDK/Player/Chips/Audio/SoundChannel.cs
Expand Up @@ -18,27 +18,10 @@
// Shawn Rakowski - @shwany
//

using System.Collections.Generic;
using Microsoft.Xna.Framework.Audio;
using System.IO;

namespace PixelVision8.Player.Audio
namespace PixelVision8.Player
{
public class SoundChannel
public partial class SoundChannel
{

private SoundEffectInstance _soundInstance;
protected readonly Dictionary<string, SoundEffectInstance> SoundInstanceCache = new Dictionary<string, SoundEffectInstance>();

public bool Playing
{
get
{
if (_soundInstance == null) return false;

return _soundInstance.State == SoundState.Playing;
}
}

/// <summary>
/// Plays the sound. If the parameters are dirty, synthesises sound as it plays, caching it for later.
Expand All @@ -61,17 +44,7 @@ public virtual void Play(SoundData soundData, float? frequency = null)
if (soundData.bytes != null)
{
// if (waveLock == WaveType.Sample || waveLock == WaveType.None)
using (var stream = new MemoryStream(soundData.bytes))
{
var soundEffect = SoundEffect.FromStream(stream);

_soundInstance = soundEffect.CreateInstance();

// TODO need to make sure this is correct and we can use the frequency to manipulate the pitch
if (frequency.HasValue)
_soundInstance.Pitch = frequency.Value;

}
CreateSoundEffect(soundData.bytes, frequency);
}

SoundInstanceCache[soundData.name] = _soundInstance;
Expand Down
5 changes: 2 additions & 3 deletions SDK/Player/Chips/Audio/SoundChip.cs
Expand Up @@ -19,7 +19,6 @@
//

using System;
using PixelVision8.Player.Audio;

namespace PixelVision8.Player
{
Expand Down Expand Up @@ -154,7 +153,7 @@ protected int FindSoundId(string name)

public bool IsChannelPlaying(int channelId)
{
return Channels[channelId] != null && Channels[channelId].Playing;
return Channels[channelId] != null && Channels[channelId].IsPlaying();
}

public void StopSound(int channel)
Expand All @@ -180,7 +179,7 @@ public void AddSample(string name, byte[] bytes)
public override void Shutdown()
{
foreach (var channel in Channels)
if (channel.Playing)
if (channel.IsPlaying())
channel.Stop();

base.Shutdown();
Expand Down
22 changes: 20 additions & 2 deletions SDK/Runner/Data/DisplayTarget.MonoGame.cs
Expand Up @@ -3,9 +3,27 @@

namespace PixelVision8.Runner
{


public partial class DisplayTarget
{
public virtual void Render(int[] pixels, int defaultColor)
private int _defaultColor;

public void RebuildColorPalette(string[] hexColors, int bgColorId = 0, string maskColor = "#FF00FF", bool debugMode = false)
{

CachedColors = ConvertColors(
hexColors,
maskColor,
debugMode,
bgColorId
);

_defaultColor = bgColorId;

}

public virtual void Render(int[] pixels)
{
if (Invalid)
{
Expand All @@ -31,7 +49,7 @@ public virtual void Render(int[] pixels, int defaultColor)
for (_i = 0; _i < _totalPixels; _i++)
{
_colorId = pixels[_i];
_pixelData[_i] = CachedColors[_colorId < 0 ? defaultColor : _colorId];
_pixelData[_i] = CachedColors[_colorId < 0 ? _defaultColor : _colorId];
}

RenderTexture.SetData(_pixelData);
Expand Down
124 changes: 114 additions & 10 deletions SDK/Runner/Data/DisplayTarget.MonoVision.cs
@@ -1,26 +1,130 @@
using System;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace PixelVision8.Runner
{
public partial class DisplayTarget
{
public virtual void Render(int[] pixels, int defaultColor)
private bool _useCRT;
private Effect crtShader;
private Texture2D _colorPalette;
private readonly int paletteWidth = 256;
public bool CropScreen { get; set; } = true;

public bool useCRT
{

// We can only update the display if the pixel lengths match up
if (pixels.Length != _totalPixels)
return;
get { return _useCRT; }
set
{
if (crtShader == null) return;

_useCRT = value;

crtShader?.Parameters["crtOn"].SetValue(value ? 1f : 0f);
crtShader?.Parameters["warp"].SetValue(value ? new Vector2(0.008f, 0.01f) : Vector2.Zero);
}
}

SpriteBatch.Begin(); //SpriteSortMode.Immediate
public float brightness
{
get => crtShader?.Parameters["brightboost"]?.GetValueSingle() ?? 0;
set => crtShader?.Parameters["brightboost"]?.SetValue(MathHelper.Clamp(value, .255f, 1.5f));
}

for (_i = 0; _i < _totalPixels; _i++)
public float sharpness
{
get => crtShader?.Parameters["hardPix"]?.GetValueSingle() ?? 0;
set => crtShader?.Parameters["hardPix"]?.SetValue(value);
}

public bool HasShader()
{
return crtShader != null;
}

public Stream shaderPath
{
set
{
_colorId = pixels[_i];
_pixelData[_i] = CachedColors[_colorId < 0 ? defaultColor : _colorId];
using (var reader = new BinaryReader(value))
{
crtShader = new Effect(_graphicManager.GraphicsDevice,
reader.ReadBytes((int) reader.BaseStream.Length));
}

useCRT = true;
}
}

public void RebuildColorPalette(string[] hexColors, int bgColorId = 0, string maskColor = "#FF00FF", bool debugMode = false)
{
CachedColors = ConvertColors(
hexColors,
maskColor,
debugMode,
bgColorId
);

RenderTexture.SetData(_pixelData);
_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)
{
Array.Resize(ref CachedColors, 256);
}

_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));
}

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;
}
}

public void Render(int[] pixels)
{

if (Invalid)
{
CalculateResolution();

CalculateDisplayScale();

CalculateDisplayOffset();

CalculateCrop();

Apply();

UpdateShader();

ResetValidation();
}

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();

}
}
}

0 comments on commit e383b12

Please sign in to comment.