using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using System; namespace LunaLightXMG { public class Game1 : Game { private GraphicsDeviceManager graphics; private SpriteBatch spriteBatch; // Test Render Target private RenderTarget2D renderTarget; private Rectangle renderDestination; bool resizing; // Test sprites Texture2D sprRo; Texture2D sprCave1Background; Texture2D sprCave1Foreground; Texture2D sprCave1Walls; // Test player input and movement with abstracted player class private Player player; // Player input private PlayerInput playerInput; // Native retro scale private int retroWidth = 320; private int retroHeight = 180; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; IsMouseVisible = true; Window.ClientSizeChanged += OnClientSizeChanged; Window.AllowUserResizing = true; IsMouseVisible = true; // Test player input and movement with abstracted player class player = new Player(); playerInput = new PlayerInput(); } 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() { // Set initial window size graphics.PreferredBackBufferWidth = 1920; graphics.PreferredBackBufferHeight = 1080; graphics.ApplyChanges(); playerInput.HasControl = true; // May need to find a new home for this base.Initialize(); CalculateRenderDestination(); } 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; } protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); sprRo = Content.Load("sprRo_Wall"); sprCave1Background = Content.Load("spr_cave1_background"); sprCave1Foreground = Content.Load("spr_Cave1_foreground"); sprCave1Walls = Content.Load("spr_Cave1_walls"); // Test Render Target renderTarget = new RenderTarget2D(GraphicsDevice, retroWidth, retroHeight); } protected override void Update(GameTime gameTime) { // Temporary quit game - eventually handle in a pause menu class if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit(); // Test player input and movement with abstracted player class playerInput.Update(); player.Update(gameTime, playerInput); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // Test Render Target - Draw sprites to the render target GraphicsDevice.SetRenderTarget(renderTarget); // Test Sprite spriteBatch.Begin(samplerState: SamplerState.PointClamp); // Draw sprites here spriteBatch.Draw(sprCave1Background,new Vector2(0,0),Color.White); // Test player input and movement with abstracted player class player.Draw(spriteBatch, sprRo); spriteBatch.Draw(sprCave1Walls, new Vector2(0, 0), Color.White); spriteBatch.Draw(sprCave1Foreground, new Vector2(0, 0), Color.White); 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(); base.Draw(gameTime); } } }