This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Breaking up DisplayTarget into MonoGame and MonoVision partials and p…
…reparing SoundChannel for similar split.
- Loading branch information
1 parent
9afb47a
commit e383b12
Showing
14 changed files
with
247 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
|
||
} | ||
} | ||
} |
Oops, something went wrong.