draw to a RenderTarget & scale that to screen

GitOrigin-RevId: b4635c5fabc03e6f19be595c68e3aa94203aa454
This commit is contained in:
Colin McMillen 2019-12-08 18:51:23 -05:00
parent 7f4ce7e854
commit 05a779e1fb
2 changed files with 34 additions and 16 deletions

View File

@ -7,6 +7,7 @@ using System.Collections.Generic;
namespace Jumpy { namespace Jumpy {
public class JumpyGame : Game { public class JumpyGame : Game {
GraphicsDeviceManager graphics; GraphicsDeviceManager graphics;
RenderTarget2D renderTarget;
SpriteBatch spriteBatch; SpriteBatch spriteBatch;
SpriteFont font; SpriteFont font;
KeyboardInput keyboardInput = new KeyboardInput(); KeyboardInput keyboardInput = new KeyboardInput();
@ -27,6 +28,10 @@ namespace Jumpy {
display.Initialize(Window, graphics); display.Initialize(Window, graphics);
display.SetFullScreen(fullScreen); display.SetFullScreen(fullScreen);
renderTarget = new RenderTarget2D(
GraphicsDevice, 320, 180, false /* mipmap */,
GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
base.Initialize(); base.Initialize();
} }
@ -34,7 +39,7 @@ namespace Jumpy {
protected override void LoadContent() { protected override void LoadContent() {
spriteBatch = new SpriteBatch(GraphicsDevice); spriteBatch = new SpriteBatch(GraphicsDevice);
font = Content.Load<SpriteFont>("font"); font = Content.Load<SpriteFont>("font");
player = new Player(Content.Load<Texture2D>("player")); player = new Player(Content.Load<Texture2D>("player_1x"));
} }
@ -66,13 +71,25 @@ namespace Jumpy {
// Called when the game should draw itself. // Called when the game should draw itself.
protected override void Draw(GameTime gameTime) { protected override void Draw(GameTime gameTime) {
// Draw scene to RenderTarget.
GraphicsDevice.SetRenderTarget(renderTarget);
// GraphicsDevice.DepthStencilState = new DepthStencilState() { DepthBufferEnable = true };
GraphicsDevice.Clear(Color.CornflowerBlue); GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin(); spriteBatch.Begin();
player.Draw(gameTime, spriteBatch); player.Draw(gameTime, spriteBatch);
spriteBatch.End(); spriteBatch.End();
// spriteBatch.DrawString(font, "hello world", new Vector2(100, 100), Color.Black); // Draw RenderTarget to screen.
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.Black);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque,
SamplerState.PointClamp, DepthStencilState.Default,
RasterizerState.CullNone);
Rectangle drawRect = new Rectangle(
0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
spriteBatch.Draw(renderTarget, drawRect, Color.White);
// spriteBatch.DrawString(font, "hello world", new Vector2(10, 10), Color.Black);
spriteBatch.End();
base.Draw(gameTime); base.Draw(gameTime);
} }

View File

@ -9,16 +9,16 @@ namespace Jumpy {
enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping }; enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping };
enum AirState { Jumping, Ground }; enum AirState { Jumping, Ground };
private const int spriteSize = 144; private const int spriteSize = 48;
private const int spriteWidth = 20; private const int spriteWidth = 8;
private const int moveSpeed = 600; private const int moveSpeed = 200;
private const int jumpSpeed = 1800; private const int jumpSpeed = 600;
private const int gravity = 6000; private const int gravity = 2000;
private Texture2D texture; private Texture2D texture;
// TODO: stop assuming 1920x1080. // TODO: stop assuming 1920x1080.
private const int groundLevel = 1080 - spriteSize / 2 - 200; private const int groundLevel = 180 - spriteSize / 2 - 10;
private Vector2 position = new Vector2(200, groundLevel); private Point position = new Point(200, groundLevel);
private Facing facing = Facing.Right; private Facing facing = Facing.Right;
private Pose pose = Pose.Standing; private Pose pose = Pose.Standing;
private AirState airState = AirState.Ground; private AirState airState = AirState.Ground;
@ -48,11 +48,11 @@ namespace Jumpy {
if (gamePad.DPad.Left == ButtonState.Pressed || leftStick.X < -0.5) { if (gamePad.DPad.Left == ButtonState.Pressed || leftStick.X < -0.5) {
facing = Facing.Left; facing = Facing.Left;
pose = Pose.Walking; pose = Pose.Walking;
position.X -= moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; position.X -= (int) (moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds);
} else if (gamePad.DPad.Right == ButtonState.Pressed || leftStick.X > 0.5) { } else if (gamePad.DPad.Right == ButtonState.Pressed || leftStick.X > 0.5) {
facing = Facing.Right; facing = Facing.Right;
pose = Pose.Walking; pose = Pose.Walking;
position.X += moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds; position.X += (int) (moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds);
} else if (gamePad.DPad.Down == ButtonState.Pressed || leftStick.Y < -0.5) { } else if (gamePad.DPad.Down == ButtonState.Pressed || leftStick.Y < -0.5) {
pose = Pose.Crouching; pose = Pose.Crouching;
} else if (gamePad.DPad.Up == ButtonState.Pressed || leftStick.Y > 0.5) { } else if (gamePad.DPad.Up == ButtonState.Pressed || leftStick.Y > 0.5) {
@ -70,8 +70,8 @@ namespace Jumpy {
} }
if (airState == AirState.Jumping) { if (airState == AirState.Jumping) {
position.Y += (float)(ySpeed * gameTime.ElapsedGameTime.TotalSeconds); position.Y += (int) (ySpeed * gameTime.ElapsedGameTime.TotalSeconds);
ySpeed += gravity * (float)gameTime.ElapsedGameTime.TotalSeconds; ySpeed += gravity * (float) gameTime.ElapsedGameTime.TotalSeconds;
if (position.Y > groundLevel) { if (position.Y > groundLevel) {
position.Y = groundLevel; position.Y = groundLevel;
ySpeed = 0; ySpeed = 0;
@ -82,7 +82,7 @@ namespace Jumpy {
pose = Pose.Jumping; pose = Pose.Jumping;
} }
position.X = Math.Min(Math.Max(position.X, 0 + spriteWidth), 1920 - spriteWidth); position.X = (int) Math.Min(Math.Max(position.X, 0 + spriteWidth), 320 - spriteWidth);
} }
private Point spritePosition(Pose pose, GameTime time) { private Point spritePosition(Pose pose, GameTime time) {
@ -126,7 +126,8 @@ namespace Jumpy {
Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2); Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2);
SpriteEffects effect = facing == Facing.Right ? SpriteEffects effect = facing == Facing.Right ?
SpriteEffects.FlipHorizontally : SpriteEffects.None; SpriteEffects.FlipHorizontally : SpriteEffects.None;
spriteBatch.Draw(texture, position, textureSource, Color.White, 0f, spriteCenter, Vector2 drawPos = new Vector2(position.X, position.Y);
spriteBatch.Draw(texture, drawPos, textureSource, Color.White, 0f, spriteCenter,
Vector2.One, effect, 0f); Vector2.One, effect, 0f);
} }
} }