mirror of
https://github.com/TheRavenwoodArts/LunaLightXMG.git
synced 2026-07-30 22:55:03 -07:00
Implemented RenderHandler class with zoom. This zoom is pretty garbage but on the right track I think.
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace LunaLightXMG
|
||||
{
|
||||
public class RenderHandler
|
||||
{
|
||||
// fields
|
||||
private GraphicsDevice graphicsDevice;
|
||||
private SpriteBatch spriteBatch;
|
||||
private RenderTarget2D renderTarget;
|
||||
private Rectangle renderDestination;
|
||||
|
||||
private int retroWidth;
|
||||
private int retroHeight;
|
||||
private float zoomLevel = 1.0f;
|
||||
private float zoomTarget = 1.0f;
|
||||
private readonly float zoomMin = 0.75f;
|
||||
private readonly float zoomNorm = 1.0f;
|
||||
private readonly float zoomMax = 1.25f;
|
||||
private readonly float zoomSpeed = 0.1f;
|
||||
public float ZoomLevel {get => zoomLevel;}
|
||||
|
||||
// constructor
|
||||
public RenderHandler(GraphicsDevice graphicsDevice, SpriteBatch spriteBatch, int retroWidth, int retroHeight)
|
||||
{
|
||||
this.graphicsDevice = graphicsDevice;
|
||||
this.spriteBatch = spriteBatch;
|
||||
this.retroWidth = retroWidth;
|
||||
this.retroHeight = retroHeight;
|
||||
|
||||
renderTarget = new RenderTarget2D(graphicsDevice, retroWidth, retroHeight);
|
||||
|
||||
CalculateRenderDestination();
|
||||
}
|
||||
|
||||
public void ZoomIn()
|
||||
{
|
||||
zoomTarget = zoomMin;
|
||||
}
|
||||
|
||||
public void ZoomNormalize()
|
||||
{
|
||||
zoomTarget = zoomNorm;
|
||||
}
|
||||
|
||||
public void ZoomOut()
|
||||
{
|
||||
zoomTarget = zoomMax;
|
||||
}
|
||||
|
||||
public void UpdateZoom()
|
||||
{
|
||||
zoomLevel = MathHelper.Lerp(zoomLevel, zoomTarget, zoomSpeed);
|
||||
zoomLevel = MathHelper.Clamp(zoomLevel, zoomMin, zoomMax);
|
||||
CalculateRenderDestination();
|
||||
}
|
||||
|
||||
public void CalculateRenderDestination()
|
||||
{
|
||||
Point screenSize = graphicsDevice.Viewport.Bounds.Size;
|
||||
// Calculate the integer scale factor
|
||||
int scaleX = screenSize.X / (int)(retroWidth * zoomLevel);
|
||||
int scaleY = screenSize.Y / (int)(retroHeight * zoomLevel);
|
||||
|
||||
// Set renderDestination
|
||||
renderDestination.Width = retroWidth * scaleX;
|
||||
renderDestination.Height = retroHeight * scaleY;
|
||||
renderDestination.X = (screenSize.X - renderDestination.Width) / 2;
|
||||
renderDestination.Y = (screenSize.Y - renderDestination.Height) / 2;
|
||||
}
|
||||
|
||||
public void BeginRenderTarget(Matrix cameraMatrix)
|
||||
{
|
||||
graphicsDevice.SetRenderTarget(renderTarget);
|
||||
graphicsDevice.Clear(Color.Black);
|
||||
|
||||
spriteBatch.Begin(transformMatrix: cameraMatrix, samplerState: SamplerState.PointClamp);
|
||||
}
|
||||
|
||||
public void EndRenderTarget()
|
||||
{
|
||||
spriteBatch.End();
|
||||
graphicsDevice.SetRenderTarget(null);
|
||||
}
|
||||
|
||||
public void DrawRenderTarget()
|
||||
{
|
||||
spriteBatch.Begin(samplerState: SamplerState.PointClamp);
|
||||
spriteBatch.Draw(renderTarget, renderDestination, Color.White);
|
||||
spriteBatch.End();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user