2019-12-04 18:16:38 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Jumpy {
|
|
|
|
|
public class JumpyGame : Game {
|
|
|
|
|
GraphicsDeviceManager graphics;
|
2019-12-09 22:21:22 +00:00
|
|
|
|
|
|
|
|
|
const int numRenderTargets = 1;
|
|
|
|
|
RenderTarget2D[] renderTargets = new RenderTarget2D[numRenderTargets];
|
|
|
|
|
int renderTargetIdx = 0;
|
|
|
|
|
|
2019-12-04 18:16:38 +00:00
|
|
|
|
SpriteBatch spriteBatch;
|
|
|
|
|
SpriteFont font;
|
|
|
|
|
KeyboardInput keyboardInput = new KeyboardInput();
|
2019-12-04 20:32:07 +00:00
|
|
|
|
bool fullScreen = false;
|
2019-12-04 22:15:50 +00:00
|
|
|
|
IDisplay display;
|
2019-12-04 18:16:38 +00:00
|
|
|
|
|
2019-12-09 02:19:50 +00:00
|
|
|
|
FpsCounter fpsCounter = new FpsCounter();
|
2019-12-08 14:38:26 +00:00
|
|
|
|
Player player;
|
|
|
|
|
|
2019-12-04 18:16:38 +00:00
|
|
|
|
public JumpyGame() {
|
|
|
|
|
graphics = new GraphicsDeviceManager(this);
|
2019-12-04 20:48:38 +00:00
|
|
|
|
IsMouseVisible = true;
|
2019-12-04 18:16:38 +00:00
|
|
|
|
Content.RootDirectory = "Content";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Performs initialization that's needed before starting to run.
|
|
|
|
|
protected override void Initialize() {
|
2019-12-04 22:15:50 +00:00
|
|
|
|
display = (IDisplay) Services.GetService(typeof(IDisplay));
|
2019-12-04 22:09:02 +00:00
|
|
|
|
display.Initialize(Window, graphics);
|
|
|
|
|
display.SetFullScreen(fullScreen);
|
2019-12-09 22:21:22 +00:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < renderTargets.Length; i++) {
|
|
|
|
|
renderTargets[i] = new RenderTarget2D(
|
|
|
|
|
GraphicsDevice, Camera.Width, Camera.Height, false /* mipmap */,
|
|
|
|
|
GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
|
|
|
|
|
}
|
2019-12-08 23:51:23 +00:00
|
|
|
|
|
2019-12-04 18:16:38 +00:00
|
|
|
|
base.Initialize();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called once per game. Loads all game content.
|
|
|
|
|
protected override void LoadContent() {
|
|
|
|
|
spriteBatch = new SpriteBatch(GraphicsDevice);
|
|
|
|
|
font = Content.Load<SpriteFont>("font");
|
2019-12-08 23:51:23 +00:00
|
|
|
|
player = new Player(Content.Load<Texture2D>("player_1x"));
|
2019-12-04 18:16:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
|
keyboardInput.Update();
|
2019-12-08 14:38:26 +00:00
|
|
|
|
GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
|
2019-12-04 18:16:38 +00:00
|
|
|
|
List<Keys> keysDown = keyboardInput.NewKeysDown();
|
|
|
|
|
|
2019-12-04 20:32:07 +00:00
|
|
|
|
if (keysDown.Contains(Keys.F12)) {
|
|
|
|
|
fullScreen = !fullScreen;
|
2019-12-04 22:09:02 +00:00
|
|
|
|
display.SetFullScreen(fullScreen);
|
2019-12-04 20:32:07 +00:00
|
|
|
|
}
|
2019-12-04 18:16:38 +00:00
|
|
|
|
|
2019-12-08 14:38:26 +00:00
|
|
|
|
if (gamePadState.Buttons.Start == ButtonState.Pressed ||
|
2019-12-04 18:16:38 +00:00
|
|
|
|
keysDown.Contains(Keys.Escape)) {
|
|
|
|
|
Exit();
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-08 14:38:26 +00:00
|
|
|
|
player.Update(gameTime, gamePadState);
|
|
|
|
|
|
2019-12-04 18:16:38 +00:00
|
|
|
|
base.Update(gameTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called when the game should draw itself.
|
|
|
|
|
protected override void Draw(GameTime gameTime) {
|
2019-12-09 02:19:50 +00:00
|
|
|
|
// We need to update the FPS counter in Draw() since Update() might get called more
|
|
|
|
|
// frequently, especially when gameTime.IsRunningSlowly.
|
|
|
|
|
fpsCounter.Update();
|
|
|
|
|
|
2019-12-08 23:51:23 +00:00
|
|
|
|
// Draw scene to RenderTarget.
|
2019-12-09 22:21:22 +00:00
|
|
|
|
RenderTarget2D renderTarget = renderTargets[renderTargetIdx];
|
|
|
|
|
renderTargetIdx = (renderTargetIdx + 1) % renderTargets.Length;
|
2019-12-08 23:51:23 +00:00
|
|
|
|
GraphicsDevice.SetRenderTarget(renderTarget);
|
2019-12-04 18:16:38 +00:00
|
|
|
|
GraphicsDevice.Clear(Color.CornflowerBlue);
|
|
|
|
|
spriteBatch.Begin();
|
2019-12-08 14:38:26 +00:00
|
|
|
|
player.Draw(gameTime, spriteBatch);
|
2019-12-04 18:16:38 +00:00
|
|
|
|
spriteBatch.End();
|
|
|
|
|
|
2019-12-08 23:51:23 +00:00
|
|
|
|
// Draw RenderTarget to screen.
|
|
|
|
|
GraphicsDevice.SetRenderTarget(null);
|
|
|
|
|
GraphicsDevice.Clear(Color.Black);
|
2019-12-09 01:33:20 +00:00
|
|
|
|
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
|
2019-12-08 23:51:23 +00:00
|
|
|
|
SamplerState.PointClamp, DepthStencilState.Default,
|
|
|
|
|
RasterizerState.CullNone);
|
|
|
|
|
Rectangle drawRect = new Rectangle(
|
|
|
|
|
0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
|
|
|
|
|
spriteBatch.Draw(renderTarget, drawRect, Color.White);
|
2019-12-09 02:38:05 +00:00
|
|
|
|
string fpsText = $"{GraphicsDevice.Viewport.Width}x{GraphicsDevice.Viewport.Height}, " +
|
2019-12-09 04:02:22 +00:00
|
|
|
|
$"{fpsCounter.Fps} FPS";
|
2019-12-09 02:19:50 +00:00
|
|
|
|
spriteBatch.DrawString(font, fpsText, new Vector2(10, 10), Color.White);
|
2019-12-08 23:51:23 +00:00
|
|
|
|
spriteBatch.End();
|
2019-12-07 01:00:42 +00:00
|
|
|
|
|
2019-12-04 18:16:38 +00:00
|
|
|
|
base.Draw(gameTime);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|