using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using System; using System.Collections.Generic; namespace Jumpy { public class JumpyGame : Game { GraphicsDeviceManager graphics; // TODO: use a History but implement functions that let us re-use the // RenderTargets instead of re-creating them every frame? const int numRenderTargets = 1; RenderTarget2D[] renderTargets = new RenderTarget2D[numRenderTargets]; int renderTargetIdx = 0; SpriteBatch spriteBatch; SpriteFont font; bool fullScreen = false; IDisplay display; History keyboardHistory = new History(2); History gamePadHistory = new History(2); FpsCounter fpsCounter = new FpsCounter(); Player player; public JumpyGame() { graphics = new GraphicsDeviceManager(this); IsMouseVisible = true; Content.RootDirectory = "Content"; } // Performs initialization that's needed before starting to run. protected override void Initialize() { display = (IDisplay) Services.GetService(typeof(IDisplay)); display.Initialize(Window, graphics); display.SetFullScreen(fullScreen); for (int i = 0; i < renderTargets.Length; i++) { renderTargets[i] = new RenderTarget2D( GraphicsDevice, Camera.Width, Camera.Height, false /* mipmap */, GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24); } base.Initialize(); } // Called once per game. Loads all game content. protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); font = Content.Load("font"); player = new Player(Content.Load("player_1x")); } // Called once per game. Unloads all game content. protected override void UnloadContent() { // TODO: Unload any non ContentManager content here. } // Updates the game world. protected override void Update(GameTime gameTime) { gamePadHistory.Add(GamePad.GetState(PlayerIndex.One)); keyboardHistory.Add(Keyboard.GetState()); if (gamePadHistory[0].Buttons.Start == ButtonState.Pressed || keyboardHistory[0].IsKeyDown(Keys.Escape)) { Exit(); } if (keyboardHistory[0].IsKeyDown(Keys.F12) && keyboardHistory[1].IsKeyUp(Keys.F12)) { fullScreen = !fullScreen; display.SetFullScreen(fullScreen); } player.Update(gameTime, gamePadHistory); base.Update(gameTime); } // Called when the game should draw itself. protected override void Draw(GameTime gameTime) { // We need to update the FPS counter in Draw() since Update() might get called more // frequently, especially when gameTime.IsRunningSlowly. fpsCounter.Update(); // Draw scene to RenderTarget. RenderTarget2D renderTarget = renderTargets[renderTargetIdx]; renderTargetIdx = (renderTargetIdx + 1) % renderTargets.Length; GraphicsDevice.SetRenderTarget(renderTarget); GraphicsDevice.Clear(Color.CornflowerBlue); spriteBatch.Begin(); player.Draw(gameTime, spriteBatch); spriteBatch.End(); // Draw RenderTarget to screen. GraphicsDevice.SetRenderTarget(null); GraphicsDevice.Clear(Color.Black); spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, SamplerState.PointClamp, DepthStencilState.Default, RasterizerState.CullNone); Rectangle drawRect = new Rectangle( 0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height); spriteBatch.Draw(renderTarget, drawRect, Color.White); string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " + $"{fpsCounter.Fps} FPS"; spriteBatch.DrawString(font, fpsText, new Vector2(10, 10), Color.White); spriteBatch.End(); base.Draw(gameTime); } } }