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

Commit

Permalink
Adding new DisplayTargetLite with core render logic for DisplayTarget…
Browse files Browse the repository at this point in the history
… to build on top of.
  • Loading branch information
jessefreeman committed Oct 4, 2020
1 parent dfd5f79 commit 32bcfb0
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 150 deletions.
4 changes: 2 additions & 2 deletions Disks/PixelVisionOS/System/Tools/WorkspaceTool/saves.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"GameChip":
{
"savedData":{
"lastPath": "/Workspace/Projects/SaveTest/",
"lastPath": "/Workspace/SpriteStressDemo/",
"selection": "0",
"scrollPos": "0",
"sessionID": "202010041020342055"
"sessionID": "202010041030367237"
}
}
}
164 changes: 32 additions & 132 deletions SDK/Runner/Data/DisplayTarget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,35 +27,15 @@

namespace PixelVision8.Runner.Data
{
public class DisplayTarget : IDisplayTarget
public class DisplayTarget : DisplayTargetLite
{
private readonly int _monitorHeight;
private readonly int _monitorWidth;
private readonly GraphicsDeviceManager graphicManager;
public readonly SpriteBatch spriteBatch;
private int _monitorScale = 1;
private bool _useCRT;
public bool cropScreen = true;
private Effect crtShader;
public bool fullscreen = false;
public Vector2 offset;
public Texture2D renderTexture;
public Vector2 scale = new Vector2(1, 1);
// private Effect shaderEffect;
public bool stretchScreen;
private Rectangle visibleRect;

// TODO think we just need to pass in the active game and not the entire runner?
public DisplayTarget(GraphicsDeviceManager graphicManager, int width, int height)
private Texture2D _colorPalette;
private readonly int paletteWidth = 256;

public DisplayTarget(GraphicsDeviceManager graphicManager, int width, int height): base(graphicManager, width, height)
{
this.graphicManager = graphicManager;

this.graphicManager.HardwareModeSwitch = false;

spriteBatch = new SpriteBatch(graphicManager.GraphicsDevice);

_monitorWidth = MathHelper.Clamp(width, 64, 640);
_monitorHeight = MathHelper.Clamp(height, 64, 480);
}

public bool useCRT
Expand Down Expand Up @@ -100,140 +80,60 @@ public Stream shaderPath

using (var reader = new BinaryReader(value))
{
crtShader = new Effect(graphicManager.GraphicsDevice,
crtShader = new Effect(GraphicManager.GraphicsDevice,
reader.ReadBytes((int) reader.BaseStream.Length));
}

useCRT = true;
}
}

public int monitorScale
{
get => _monitorScale;
set
{
var maxWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
var maxHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;

var fits = false;

while (fits == false)
{
var newWidth = _monitorWidth * value;
var newHeight = _monitorHeight * value;

if (newWidth < maxWidth && newHeight < maxHeight)
{
fits = true;
_monitorScale = value;
}
else
{
value--;
}
}
}
}

public void ResetResolution(int gameWidth, int gameHeight, int overScanX = 0, int overScanY = 0)
public override void ResetResolution(int gameWidth, int gameHeight, int overScanX = 0, int overScanY = 0)
{
if (renderTexture == null || renderTexture.Width != gameWidth || renderTexture.Height != gameHeight)
{
renderTexture = new Texture2D(graphicManager.GraphicsDevice, gameWidth, gameHeight);
renderTexture = new Texture2D(GraphicManager.GraphicsDevice, gameWidth, gameHeight);

crtShader?.Parameters["textureSize"].SetValue(new Vector2(gameWidth, gameHeight));
crtShader?.Parameters["videoSize"].SetValue(new Vector2(gameWidth, gameHeight));

}

// Calculate the game's resolution
visibleRect.Width = renderTexture.Width - overScanX;
visibleRect.Height = renderTexture.Height - overScanY;

var tmpMonitorScale = fullscreen ? 1 : monitorScale;

// Calculate the monitor's resolution
var displayWidth = fullscreen
? GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width
: _monitorWidth *
tmpMonitorScale;
var displayHeight = fullscreen
? GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height
: _monitorHeight * tmpMonitorScale;

// Calculate the game scale
// TODO need to figure out scale
scale.X = (float) displayWidth / visibleRect.Width;
scale.Y = (float) displayHeight / visibleRect.Height;

if (!stretchScreen)
{
// To preserve the aspect ratio,
// use the smaller scale factor.
scale.X = Math.Min(scale.X, scale.Y);
scale.Y = scale.X;
}

offset.X = (displayWidth - visibleRect.Width * scale.X) * .5f;
offset.Y = (displayHeight - visibleRect.Height * scale.Y) * .5f;

if (cropScreen && !fullscreen)
{
displayWidth = Math.Min(displayWidth, (int) (visibleRect.Width * scale.X));
displayHeight = Math.Min(displayHeight, (int) (visibleRect.Height * scale.Y));
offset.X = 0;
offset.Y = 0;
}

// Apply changes
graphicManager.IsFullScreen = fullscreen;

if (graphicManager.PreferredBackBufferWidth != displayWidth ||
graphicManager.PreferredBackBufferHeight != displayHeight)
{
graphicManager.PreferredBackBufferWidth = displayWidth;
graphicManager.PreferredBackBufferHeight = displayHeight;
graphicManager.ApplyChanges();
}

base.ResetResolution(gameWidth, gameHeight, overScanX, overScanY);

}

private Texture2D _colorPalette;
private readonly int paletteWidth = 256;
public void RebuildColorPalette(ColorChip colorChip)

public override void RebuildColorPalette(ColorChip colorChip)
{

base.RebuildColorPalette(colorChip);

var colors = ColorUtils.ConvertColors(colorChip.hexColors, colorChip.maskColor, colorChip.debugMode,
colorChip.backgroundColor);

var width = paletteWidth;
var height = (int)Math.Ceiling(colors.Length / (double)width);

_colorPalette = new Texture2D(graphicManager.GraphicsDevice, width, height);

var fullPalette = new Color[_colorPalette.Width];
for (int i = 0; i < fullPalette.Length; i++) { fullPalette[i] = i < colors.Length ? colors[i] : colors[0]; }

_colorPalette.SetData(colors);
// TODO do we need to recreate these two things each time?
_colorPalette = new Texture2D(GraphicManager.GraphicsDevice, paletteWidth, (int)Math.Ceiling(CachedColors.Length / (double)paletteWidth));
_colorPalette.SetData(CachedColors);

colorChip.ResetValidation();

// Set palette total
// crtShader.Parameters["maskColor"].SetValue(Color.Magenta.ToVector4());

}

public void Render(int[] pixels)
public override void Render(int[] pixels, int backgroundColor)
{

renderTexture.SetData(pixels);
spriteBatch.Begin(SpriteSortMode.Immediate, null, SamplerState.PointClamp);
crtShader.CurrentTechnique.Passes[0].Apply();
graphicManager.GraphicsDevice.Textures[1] = _colorPalette;
graphicManager.GraphicsDevice.SamplerStates[1] = SamplerState.PointClamp;
spriteBatch.Draw(renderTexture, offset, visibleRect, Color.White, 0f, Vector2.Zero, scale, SpriteEffects.None, 1f);
spriteBatch.End();
if (crtShader == null)
{
base.Render(pixels, backgroundColor);
}
else
{
renderTexture.SetData(pixels);
SpriteBatch.Begin(SpriteSortMode.Immediate, null, SamplerState.PointClamp);
crtShader.CurrentTechnique.Passes[0].Apply();
GraphicManager.GraphicsDevice.Textures[1] = _colorPalette;
GraphicManager.GraphicsDevice.SamplerStates[1] = SamplerState.PointClamp;
SpriteBatch.Draw(renderTexture, offset, VisibleRect, Color.White, 0f, Vector2.Zero, Scale, SpriteEffects.None, 1f);
SpriteBatch.End();
}

}

Expand Down

0 comments on commit 32bcfb0

Please sign in to comment.