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:
+46
-56
@@ -24,9 +24,8 @@ namespace LunaLightXMG
|
||||
private int retroWidth = 320;
|
||||
private int retroHeight = 180;
|
||||
|
||||
// Test Render Target
|
||||
private RenderTarget2D renderTarget;
|
||||
private Rectangle renderDestination;
|
||||
// Test Render Target Handler
|
||||
private RenderHandler renderHandler;
|
||||
bool resizing;
|
||||
|
||||
// Test sprites - eventually handle in a sprite manager?
|
||||
@@ -59,7 +58,7 @@ namespace LunaLightXMG
|
||||
IsMouseVisible = true;
|
||||
|
||||
// Test player input and movement with abstracted player class
|
||||
Vector2 initPlayerPosition = new Vector2(163, 0);
|
||||
Vector2 initPlayerPosition = new Vector2(163, 0);
|
||||
player = new Player(initPlayerPosition);
|
||||
playerInput = new PlayerInput();
|
||||
enemies = new List<Enemy>();
|
||||
@@ -85,9 +84,9 @@ namespace LunaLightXMG
|
||||
// Initialize camera
|
||||
camera2D = new Camera2D(retroWidth, retroHeight);
|
||||
// camera test - set level bounds
|
||||
camera2D.SetLevelBounds(new Rectangle(0,0,480,270)); // default 320x180
|
||||
camera2D.SetLevelBounds(new Rectangle(0,0,480,270)); // Testing - default 320x180
|
||||
|
||||
CalculateRenderDestination();
|
||||
renderHandler.CalculateRenderDestination();
|
||||
// Play music
|
||||
// PlayBackgroundMusic(sinkBeats);
|
||||
|
||||
@@ -95,14 +94,17 @@ namespace LunaLightXMG
|
||||
platforms = PlatformGenerator.CreatePlatforms(numOfPlatforms);
|
||||
}
|
||||
|
||||
// Testing camera ------------------------
|
||||
// Testing camera and render target handler ------------------------
|
||||
private void TestCameraWhilePlaying()
|
||||
{
|
||||
KeyboardState keyboardState = Keyboard.GetState();
|
||||
var KeyI = keyboardState.IsKeyDown(Keys.I) ? true : false;
|
||||
var KeyO = keyboardState.IsKeyDown(Keys.O) ? true : false;
|
||||
var KeyK = keyboardState.IsKeyDown(Keys.K) ? true : false;
|
||||
var KeyL = keyboardState.IsKeyDown(Keys.L) ? true : false;
|
||||
var KeyI = keyboardState.IsKeyDown(Keys.I);
|
||||
var KeyO = keyboardState.IsKeyDown(Keys.O);
|
||||
var KeyK = keyboardState.IsKeyDown(Keys.K);
|
||||
var KeyL = keyboardState.IsKeyDown(Keys.L);
|
||||
var KeyB = keyboardState.IsKeyDown(Keys.B);
|
||||
var KeyN = keyboardState.IsKeyDown(Keys.N);
|
||||
var KeyM = keyboardState.IsKeyDown(Keys.M);
|
||||
|
||||
if (KeyI)
|
||||
{
|
||||
@@ -120,6 +122,18 @@ namespace LunaLightXMG
|
||||
{
|
||||
camera2D.Rotation += 0.01f;
|
||||
}
|
||||
if (KeyB)
|
||||
{
|
||||
renderHandler.ZoomIn();
|
||||
}
|
||||
if (KeyN)
|
||||
{
|
||||
renderHandler.ZoomNormalize();
|
||||
}
|
||||
if (KeyM)
|
||||
{
|
||||
renderHandler.ZoomOut();
|
||||
}
|
||||
}
|
||||
// ---------------------------------------
|
||||
protected override void LoadContent()
|
||||
@@ -137,8 +151,8 @@ namespace LunaLightXMG
|
||||
// Spawn 10 enemies at the start of the level
|
||||
enemies = SpawnEnemies(numOfEnemies);
|
||||
|
||||
// Test Render Target
|
||||
renderTarget = new RenderTarget2D(GraphicsDevice, retroWidth, retroHeight);
|
||||
// Test Render Target Handler
|
||||
renderHandler = new RenderHandler(GraphicsDevice, spriteBatch, retroWidth, retroHeight);
|
||||
|
||||
// Debugging
|
||||
debugFont = Content.Load<SpriteFont>("fonts/debugFont");
|
||||
@@ -161,7 +175,8 @@ namespace LunaLightXMG
|
||||
foreach (var enemy in enemies) {
|
||||
enemy.Update(gameTime, player, platforms);
|
||||
}
|
||||
|
||||
// test render handler zoom
|
||||
renderHandler.UpdateZoom();
|
||||
// Update camera position
|
||||
camera2D.Follow(player.position);
|
||||
// camera testing
|
||||
@@ -174,18 +189,8 @@ namespace LunaLightXMG
|
||||
{
|
||||
GraphicsDevice.Clear(Color.CornflowerBlue);
|
||||
|
||||
// Test Render Target - Draw sprites to the render target
|
||||
GraphicsDevice.SetRenderTarget(renderTarget);
|
||||
|
||||
// Add camera to draw batch
|
||||
spriteBatch.Begin(
|
||||
transformMatrix: camera2D.GetViewMatrix(),
|
||||
samplerState: SamplerState.PointClamp
|
||||
);
|
||||
|
||||
// Debug Grid
|
||||
// spriteBatch.Draw(debugGrid, new Vector2(0, 0), Color.Blue);
|
||||
// spriteBatch.Draw(sinkArea, new Vector2(0, 0), Color.White);
|
||||
// // Test Render Target Handler - Draw sprites to the render (target
|
||||
renderHandler.BeginRenderTarget(camera2D.GetViewMatrix());
|
||||
|
||||
// Test draw tiles
|
||||
foreach (var platform in platforms)
|
||||
@@ -200,15 +205,9 @@ namespace LunaLightXMG
|
||||
{
|
||||
enemy.Draw(spriteBatch);
|
||||
}
|
||||
|
||||
spriteBatch.End();
|
||||
|
||||
|
||||
// Test Render Target - Draw render target to the screen
|
||||
GraphicsDevice.SetRenderTarget(null);
|
||||
spriteBatch.Begin(samplerState: SamplerState.PointClamp);
|
||||
spriteBatch.Draw(renderTarget, renderDestination, Color.White);
|
||||
spriteBatch.End();
|
||||
|
||||
renderHandler.EndRenderTarget();
|
||||
renderHandler.DrawRenderTarget();
|
||||
|
||||
// Debugging
|
||||
DrawDebugInfo();
|
||||
@@ -249,30 +248,10 @@ namespace LunaLightXMG
|
||||
if (!resizing && Window.ClientBounds.Width > 0 && Window.ClientBounds.Height > 0)
|
||||
{
|
||||
resizing = true;
|
||||
CalculateRenderDestination();
|
||||
renderHandler.CalculateRenderDestination();
|
||||
resizing = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void CalculateRenderDestination()
|
||||
{
|
||||
Point size = GraphicsDevice.Viewport.Bounds.Size;
|
||||
|
||||
// Calculate the integer scale factor
|
||||
int scaleX = size.X / renderTarget.Width;
|
||||
int scaleY = size.Y / renderTarget.Height;
|
||||
int scale = Math.Min(scaleX, scaleY);
|
||||
|
||||
// Ensure the scale is at least 1
|
||||
scale = Math.Max(scale, 1);
|
||||
|
||||
// Set renderDestination
|
||||
renderDestination.Width = (int)(renderTarget.Width * scale);
|
||||
renderDestination.Height = (int)(renderTarget.Height * scale);
|
||||
|
||||
renderDestination.X = (size.X - renderDestination.Width) / 2;
|
||||
renderDestination.Y = (size.Y - renderDestination.Height) / 2;
|
||||
}
|
||||
|
||||
private void DrawDebugInfo()
|
||||
{
|
||||
@@ -286,6 +265,13 @@ namespace LunaLightXMG
|
||||
spriteBatch.DrawString(debugFont, $"Camera Position: {camera2D.Position}", new Vector2(10, 90), Color.White);
|
||||
spriteBatch.DrawString(debugFont, $"Camera Zoom: {camera2D.Zoom}", new Vector2(10, 110), Color.White);
|
||||
spriteBatch.DrawString(debugFont, $"Camera Rotation: {camera2D.Rotation}", new Vector2(10, 130), Color.White);
|
||||
spriteBatch.DrawString(debugFont, $"Render Target Zoom: {renderHandler.ZoomLevel}", new Vector2(10, 150), Color.White);
|
||||
|
||||
|
||||
spriteBatch.DrawString(debugFont, $"Camera Zoom Control - Zoom in: I, Zoom out: O", new Vector2(10, 170), Color.White);
|
||||
spriteBatch.DrawString(debugFont, $"Camera Rotation Control - Rotate left: K, Rotate right: L", new Vector2(10, 190), Color.White);
|
||||
|
||||
spriteBatch.DrawString(debugFont, $"Render Target Zoom Control - Zoom in: B, Zoom normal: N, Zoom out: M", new Vector2(10, 210), Color.White);
|
||||
|
||||
spriteBatch.End();
|
||||
}
|
||||
@@ -296,6 +282,10 @@ namespace LunaLightXMG
|
||||
}
|
||||
private void ResetGame()
|
||||
{
|
||||
// reset cam
|
||||
camera2D.Rotation = 0;
|
||||
camera2D.Zoom = 1f;
|
||||
|
||||
// Reset player position
|
||||
player.Reset(new Vector2(163, 0));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user