2019-12-04 18:16:38 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
|
|
|
|
using Microsoft.Xna.Framework.Input;
|
|
|
|
|
|
2019-12-10 15:56:51 +00:00
|
|
|
|
using System;
|
2019-12-04 18:16:38 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Jumpy {
|
|
|
|
|
public class JumpyGame : Game {
|
|
|
|
|
GraphicsDeviceManager graphics;
|
2019-12-09 22:21:22 +00:00
|
|
|
|
|
2019-12-10 15:56:51 +00:00
|
|
|
|
// TODO: use a History<RenderTarget2D> but implement functions that let us re-use the
|
|
|
|
|
// RenderTargets instead of re-creating them every frame?
|
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;
|
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-10 16:12:06 +00:00
|
|
|
|
History<KeyboardState> keyboard = new History<KeyboardState>(2);
|
|
|
|
|
History<GamePadState> gamePad = new History<GamePadState>(2);
|
2019-12-10 15:56:51 +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-10 21:09:43 +00:00
|
|
|
|
Texture2D grassland;
|
2019-12-10 21:37:49 +00:00
|
|
|
|
Texture2D grasslandBg1;
|
|
|
|
|
Texture2D grasslandBg2;
|
2019-12-08 14:38:26 +00:00
|
|
|
|
|
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-10 21:09:43 +00:00
|
|
|
|
grassland = Content.Load<Texture2D>("grassland");
|
2019-12-10 21:37:49 +00:00
|
|
|
|
grasslandBg1 = Content.Load<Texture2D>("grassland_bg1");
|
|
|
|
|
grasslandBg2 = Content.Load<Texture2D>("grassland_bg2");
|
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) {
|
2019-12-10 16:12:06 +00:00
|
|
|
|
gamePad.Add(GamePad.GetState(PlayerIndex.One));
|
|
|
|
|
keyboard.Add(Keyboard.GetState());
|
2019-12-04 18:16:38 +00:00
|
|
|
|
|
2019-12-10 16:12:06 +00:00
|
|
|
|
if (keyboard[0].IsKeyDown(Keys.Escape) || gamePad[0].IsButtonDown(Buttons.Start)) {
|
2019-12-10 15:56:51 +00:00
|
|
|
|
Exit();
|
2019-12-04 20:32:07 +00:00
|
|
|
|
}
|
2019-12-04 18:16:38 +00:00
|
|
|
|
|
2019-12-10 16:12:06 +00:00
|
|
|
|
if (keyboard[0].IsKeyDown(Keys.F12) && keyboard[1].IsKeyUp(Keys.F12) ||
|
|
|
|
|
gamePad[0].IsButtonDown(Buttons.Back) && gamePad[1].IsButtonUp(Buttons.Back)) {
|
2019-12-10 15:56:51 +00:00
|
|
|
|
fullScreen = !fullScreen;
|
|
|
|
|
display.SetFullScreen(fullScreen);
|
2019-12-04 18:16:38 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-12-10 16:12:06 +00:00
|
|
|
|
player.Update(gameTime, gamePad);
|
2019-12-08 14:38:26 +00:00
|
|
|
|
|
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-10 21:09:43 +00:00
|
|
|
|
|
2019-12-10 21:37:49 +00:00
|
|
|
|
// Draw background.
|
|
|
|
|
Rectangle bgSource = new Rectangle(0, grasslandBg1.Height - Camera.Height, Camera.Width, Camera.Height);
|
|
|
|
|
Rectangle bgTarget = new Rectangle(0, 0, Camera.Width, Camera.Height);
|
|
|
|
|
spriteBatch.Draw(grasslandBg2, bgTarget, bgSource, Color.White);
|
|
|
|
|
spriteBatch.Draw(grasslandBg1, bgTarget, bgSource, Color.White);
|
|
|
|
|
|
2019-12-10 21:09:43 +00:00
|
|
|
|
// Draw player.
|
2019-12-08 14:38:26 +00:00
|
|
|
|
player.Draw(gameTime, spriteBatch);
|
2019-12-10 21:09:43 +00:00
|
|
|
|
|
|
|
|
|
// Draw foreground tiles.
|
|
|
|
|
int size = 16;
|
|
|
|
|
Rectangle textureSource = new Rectangle(3 * size, 0 * size, size, size);
|
|
|
|
|
for (int i = 0; i < Camera.Width / size; i++) {
|
|
|
|
|
Vector2 drawPos = new Vector2(i * size, Camera.Height - size);
|
|
|
|
|
spriteBatch.Draw(grassland, drawPos, textureSource, Color.White);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Aaaaand we're done.
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|