Merge pull request #2 from TheRavenwoodArts/AddingSpritesTest

Added test sprites to test game window scaling
This commit is contained in:
J. C. Lynch
2024-05-15 13:23:49 -07:00
committed by GitHub
+43 -2
View File
@@ -1,6 +1,7 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using System;
namespace LunaLightXMG namespace LunaLightXMG
{ {
@@ -12,6 +13,13 @@ namespace LunaLightXMG
// Test Render Target // Test Render Target
private RenderTarget2D renderTarget; private RenderTarget2D renderTarget;
private Rectangle renderDestination; private Rectangle renderDestination;
bool resizing;
// Test sprites
Texture2D sprRo;
Texture2D sprCave1_background;
Texture2D sprCave1_foreground;
Texture2D sprCave1_walls;
// Native retro scale // Native retro scale
private int retroWidth = 320; private int retroWidth = 320;
@@ -23,11 +31,21 @@ namespace LunaLightXMG
Content.RootDirectory = "Content"; Content.RootDirectory = "Content";
IsMouseVisible = true; IsMouseVisible = true;
//Window.ClientSizeChanged += OnClientSizeChanged; // Jensen can implement Window.ClientSizeChanged += OnClientSizeChanged; // Jensen can implement
Window.AllowUserResizing = true; Window.AllowUserResizing = true;
IsMouseVisible = true; IsMouseVisible = true;
} }
private void OnClientSizeChanged(object sender, EventArgs eventArgs)
{
if (!resizing && Window.ClientBounds.Width > 0 && Window.ClientBounds.Height > 0)
{
resizing = true;
CalculateRenderDestination();
resizing = false;
}
}
protected override void Initialize() protected override void Initialize()
{ {
// Set initial window size // Set initial window size
@@ -36,12 +54,30 @@ namespace LunaLightXMG
graphics.ApplyChanges(); graphics.ApplyChanges();
base.Initialize(); base.Initialize();
//CalculateRenderDestination(); // Jensen can implement CalculateRenderDestination(); // Jensen can implement
} }
private void CalculateRenderDestination()
{
Point size = GraphicsDevice.Viewport.Bounds.Size;
float scaleX = (float)size.X / renderTarget.Width;
float scaleY = (float)size.Y / renderTarget.Height;
float scale = Math.Min(scaleX, scaleY);
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;
}
protected override void LoadContent() protected override void LoadContent()
{ {
spriteBatch = new SpriteBatch(GraphicsDevice); spriteBatch = new SpriteBatch(GraphicsDevice);
sprRo = Content.Load<Texture2D>("sprRo_Wall");
sprCave1_background = Content.Load<Texture2D>("spr_cave1_background");
sprCave1_foreground = Content.Load<Texture2D>("spr_Cave1_foreground");
sprCave1_walls = Content.Load<Texture2D>("spr_Cave1_walls");
// Test Render Target // Test Render Target
renderTarget = new RenderTarget2D(GraphicsDevice, retroWidth, retroHeight); renderTarget = new RenderTarget2D(GraphicsDevice, retroWidth, retroHeight);
@@ -68,8 +104,13 @@ namespace LunaLightXMG
// Test Sprite // Test Sprite
spriteBatch.Begin(samplerState: SamplerState.PointClamp); spriteBatch.Begin(samplerState: SamplerState.PointClamp);
// Draw sprites here // Draw sprites here
spriteBatch.Draw(sprCave1_background,new Vector2(0,0),Color.White);
spriteBatch.Draw(sprRo, new Vector2(163, 63), Color.White);
spriteBatch.Draw(sprCave1_walls, new Vector2(0, 0), Color.White);
spriteBatch.Draw(sprCave1_foreground, new Vector2(0, 0), Color.White);
spriteBatch.End(); spriteBatch.End();
// Test Render Target - Draw render target to the screen // Test Render Target - Draw render target to the screen
GraphicsDevice.SetRenderTarget(null); GraphicsDevice.SetRenderTarget(null);
spriteBatch.Begin(samplerState: SamplerState.PointClamp); spriteBatch.Begin(samplerState: SamplerState.PointClamp);