From 05a779e1fbb1da4281e264791eeea1125ab1552e Mon Sep 17 00:00:00 2001 From: Colin McMillen Date: Sun, 8 Dec 2019 18:51:23 -0500 Subject: [PATCH] draw to a RenderTarget & scale that to screen GitOrigin-RevId: b4635c5fabc03e6f19be595c68e3aa94203aa454 --- Jumpy.Shared/JumpyGame.cs | 23 ++++++++++++++++++++--- Jumpy.Shared/Player.cs | 27 ++++++++++++++------------- 2 files changed, 34 insertions(+), 16 deletions(-) diff --git a/Jumpy.Shared/JumpyGame.cs b/Jumpy.Shared/JumpyGame.cs index 7c53af4..abfcc78 100644 --- a/Jumpy.Shared/JumpyGame.cs +++ b/Jumpy.Shared/JumpyGame.cs @@ -7,6 +7,7 @@ using System.Collections.Generic; namespace Jumpy { public class JumpyGame : Game { GraphicsDeviceManager graphics; + RenderTarget2D renderTarget; SpriteBatch spriteBatch; SpriteFont font; KeyboardInput keyboardInput = new KeyboardInput(); @@ -27,6 +28,10 @@ namespace Jumpy { display.Initialize(Window, graphics); display.SetFullScreen(fullScreen); + renderTarget = new RenderTarget2D( + GraphicsDevice, 320, 180, false /* mipmap */, + GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24); + base.Initialize(); } @@ -34,7 +39,7 @@ namespace Jumpy { protected override void LoadContent() { spriteBatch = new SpriteBatch(GraphicsDevice); font = Content.Load("font"); - player = new Player(Content.Load("player")); + player = new Player(Content.Load("player_1x")); } @@ -66,13 +71,25 @@ namespace Jumpy { // Called when the game should draw itself. protected override void Draw(GameTime gameTime) { + // Draw scene to RenderTarget. + GraphicsDevice.SetRenderTarget(renderTarget); + // GraphicsDevice.DepthStencilState = new DepthStencilState() { DepthBufferEnable = true }; GraphicsDevice.Clear(Color.CornflowerBlue); - spriteBatch.Begin(); player.Draw(gameTime, spriteBatch); 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); } diff --git a/Jumpy.Shared/Player.cs b/Jumpy.Shared/Player.cs index f394065..6aa99b1 100644 --- a/Jumpy.Shared/Player.cs +++ b/Jumpy.Shared/Player.cs @@ -9,16 +9,16 @@ namespace Jumpy { enum Pose { Walking, Standing, Crouching, Stretching, SwordSwing, Jumping }; enum AirState { Jumping, Ground }; - private const int spriteSize = 144; - private const int spriteWidth = 20; - private const int moveSpeed = 600; - private const int jumpSpeed = 1800; - private const int gravity = 6000; + private const int spriteSize = 48; + private const int spriteWidth = 8; + private const int moveSpeed = 200; + private const int jumpSpeed = 600; + private const int gravity = 2000; private Texture2D texture; // TODO: stop assuming 1920x1080. - private const int groundLevel = 1080 - spriteSize / 2 - 200; - private Vector2 position = new Vector2(200, groundLevel); + private const int groundLevel = 180 - spriteSize / 2 - 10; + private Point position = new Point(200, groundLevel); private Facing facing = Facing.Right; private Pose pose = Pose.Standing; private AirState airState = AirState.Ground; @@ -48,11 +48,11 @@ namespace Jumpy { if (gamePad.DPad.Left == ButtonState.Pressed || leftStick.X < -0.5) { facing = Facing.Left; 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) { facing = Facing.Right; 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) { pose = Pose.Crouching; } else if (gamePad.DPad.Up == ButtonState.Pressed || leftStick.Y > 0.5) { @@ -70,8 +70,8 @@ namespace Jumpy { } if (airState == AirState.Jumping) { - position.Y += (float)(ySpeed * gameTime.ElapsedGameTime.TotalSeconds); - ySpeed += gravity * (float)gameTime.ElapsedGameTime.TotalSeconds; + position.Y += (int) (ySpeed * gameTime.ElapsedGameTime.TotalSeconds); + ySpeed += gravity * (float) gameTime.ElapsedGameTime.TotalSeconds; if (position.Y > groundLevel) { position.Y = groundLevel; ySpeed = 0; @@ -82,7 +82,7 @@ namespace Jumpy { 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) { @@ -126,7 +126,8 @@ namespace Jumpy { Vector2 spriteCenter = new Vector2(spriteSize / 2, spriteSize / 2); SpriteEffects effect = facing == Facing.Right ? 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); } }