Camera in functional state.

This commit is contained in:
Harrison Deng 2018-11-11 23:05:51 -06:00
parent 3c44a52a8f
commit a2defc5d11
6 changed files with 40 additions and 21 deletions

View File

@ -5,6 +5,7 @@ using RhythmBullet.Zer01HD.Game.ContentResolvers;
using RhythmBullet.Zer01HD.Game.Preferences; using RhythmBullet.Zer01HD.Game.Preferences;
using RhythmBullet.Zer01HD.Game.Screens; using RhythmBullet.Zer01HD.Game.Screens;
using RhythmBullet.Zer01HD.Utilities; using RhythmBullet.Zer01HD.Utilities;
using RhythmBullet.Zer01HD.Utilities.Camera;
using RhythmBullet.Zer01HD.Utilities.ContentSystem; using RhythmBullet.Zer01HD.Utilities.ContentSystem;
using RhythmBullet.Zer01HD.Utilities.DataTypes; using RhythmBullet.Zer01HD.Utilities.DataTypes;
using RhythmBullet.Zer01HD.Utilities.Input; using RhythmBullet.Zer01HD.Utilities.Input;
@ -32,6 +33,7 @@ namespace RhythmBullet
private RenderTarget2D previousScreenRenderTarget; private RenderTarget2D previousScreenRenderTarget;
private Screen currentScreen; private Screen currentScreen;
private bool resizing; private bool resizing;
private Camera2D camera;
public Screen Screen public Screen Screen
{ {
get get
@ -44,7 +46,7 @@ namespace RhythmBullet
currentScreen = value; currentScreen = value;
if (!Screen.Initiated) if (!Screen.Initiated)
{ {
Screen.Initiate(graphics.GraphicsDevice, new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight)); Screen.Initiate(graphics.GraphicsDevice, new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight), camera);
} }
if (previousScreen != null && previousScreenRenderTarget == null && previousScreen.UseRenderTargetForExitTransition) if (previousScreen != null && previousScreenRenderTarget == null && previousScreen.UseRenderTargetForExitTransition)
{ {
@ -64,6 +66,7 @@ namespace RhythmBullet
graphics = new GraphicsDeviceManager(this); graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content"; Content.RootDirectory = "Content";
Assets = new ContentSystem(Content); Assets = new ContentSystem(Content);
resolutionContentResolver = new ResolutionContentResolver(); resolutionContentResolver = new ResolutionContentResolver();
FontContentResolver fcr = new FontContentResolver(resolutionContentResolver); FontContentResolver fcr = new FontContentResolver(resolutionContentResolver);
Assets.contentPathModifier.Add(typeof(Texture2D), resolutionContentResolver); Assets.contentPathModifier.Add(typeof(Texture2D), resolutionContentResolver);
@ -100,6 +103,7 @@ namespace RhythmBullet
{ {
// Create a new SpriteBatch, which can be used to draw textures. // Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice); spriteBatch = new SpriteBatch(GraphicsDevice);
camera = new Camera2D(graphics.GraphicsDevice);
QueueContent(); QueueContent();
Screen = new LoadingScreen(Content.Load<Texture2D>("RhythmBullet"), 0.7f); Screen = new LoadingScreen(Content.Load<Texture2D>("RhythmBullet"), 0.7f);
} }
@ -160,6 +164,7 @@ namespace RhythmBullet
} }
InputUtilities.Update(); InputUtilities.Update();
camera.Update();
base.Update(gameTime); base.Update(gameTime);
} }
@ -174,16 +179,15 @@ namespace RhythmBullet
{ {
graphics.GraphicsDevice.SetRenderTarget(previousScreenRenderTarget); graphics.GraphicsDevice.SetRenderTarget(previousScreenRenderTarget);
GraphicsDevice.Clear(previousScreen.BackgroundColor); GraphicsDevice.Clear(previousScreen.BackgroundColor);
spriteBatch.Begin(); spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, camera.TransformMatrix);
previousScreen.Draw(spriteBatch); previousScreen.Draw(spriteBatch);
spriteBatch.End(); spriteBatch.End();
graphics.GraphicsDevice.SetRenderTarget(null); graphics.GraphicsDevice.SetRenderTarget(null);
Screen.UpdatePreviousScreenFrame(previousScreenRenderTarget); Screen.UpdatePreviousScreenFrame(previousScreenRenderTarget);
} }
spriteBatch.Begin(); spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, camera.TransformMatrix);
Screen.Draw(spriteBatch); Screen.Draw(spriteBatch);
spriteBatch.End(); spriteBatch.End();
base.Draw(gameTime); base.Draw(gameTime);
} }

View File

@ -8,18 +8,28 @@ using System.Threading.Tasks;
namespace RhythmBullet.Zer01HD.Utilities.Camera namespace RhythmBullet.Zer01HD.Utilities.Camera
{ {
class Camera2D public class Camera2D
{ {
public float Zoom; public float Zoom;
public Vector2 Position; public Vector2 Position;
public Matrix Transform { get; private set; } public Matrix TransformMatrix { get; private set; }
public GraphicsDevice graphicsDevice;
public void Update(Viewport viewport) public Camera2D(GraphicsDevice graphicsDevice)
{ {
Transform = this.graphicsDevice = graphicsDevice;
Matrix.CreateTranslation(new Vector3(Position.X, Position.Y, 0)) * Zoom = 1f;
Position.X = this.graphicsDevice.Viewport.Width * 0.5f;
Position.Y = this.graphicsDevice.Viewport.Height * 0.5f;
}
public void Update()
{
Rectangle bounds = graphicsDevice.Viewport.Bounds;
TransformMatrix =
Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0)) *
Matrix.CreateScale(Zoom) * Matrix.CreateScale(Zoom) *
Matrix.CreateTranslation(new Vector3(viewport.Width/2f, viewport.Height/2f, 0f)); Matrix.CreateTranslation(new Vector3(bounds.Width * 0.5f, bounds.Height * 0.5f, 0f));
} }
} }
} }

View File

@ -1,5 +1,6 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using RhythmBullet.Zer01HD.Utilities.Camera;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -19,15 +20,16 @@ namespace RhythmBullet.Zer01HD.Utilities.ScreenSystem
public Screen NextScreen { get; protected set; } public Screen NextScreen { get; protected set; }
public Rectangle ScreenSize { get; protected set; } public Rectangle ScreenSize { get; protected set; }
public bool Initiated { get; private set; } public bool Initiated { get; private set; }
public Camera2D Camera { get; private set; }
public Screen(bool useEnterTransition = false) public Screen(bool useEnterTransition = false)
{ {
State = useEnterTransition ? ScreenState.EnterTransition : ScreenState.Normal; State = useEnterTransition ? ScreenState.EnterTransition : ScreenState.Normal;
Transitions = new List<ITransition>(); Transitions = new List<ITransition>();
} }
public void Initiate(GraphicsDevice graphicsDevice, Rectangle screenSize) public void Initiate(GraphicsDevice graphicsDevice, Rectangle screenSize, Camera2D camera)
{ {
this.Camera = camera;
this.ScreenSize = screenSize; this.ScreenSize = screenSize;
GraphicsDevice = graphicsDevice; GraphicsDevice = graphicsDevice;
Initiated = true; Initiated = true;

View File

@ -1,5 +1,6 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using RhythmBullet.Zer01HD.Utilities.Camera;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -32,12 +33,12 @@ namespace RhythmBullet.Zer01HD.UI.Modular
this.font = font; this.font = font;
} }
public override void Draw(SpriteBatch batch) public override void Draw(SpriteBatch batch, Camera2D camera)
{ {
position.X = Bounds.X; position.X = Bounds.X;
position.Y = Bounds.Y; position.Y = Bounds.Y;
batch.DrawString(font, DisplayedText, position, Color); batch.DrawString(font, DisplayedText, position, Color);
base.Draw(batch); base.Draw(batch, camera);
} }
} }
} }

View File

@ -1,6 +1,7 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using RhythmBullet.Zer01HD.Utilities.Camera;
using RhythmBullet.Zer01HD.Utilities.Input; using RhythmBullet.Zer01HD.Utilities.Input;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -21,7 +22,7 @@ namespace RhythmBullet.Zer01HD.UI.Modular
{ {
} }
public virtual void Draw(SpriteBatch batch) public virtual void Draw(SpriteBatch batch, Camera2D camera)
{ {
} }

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using RhythmBullet.Zer01HD.Utilities.Camera;
using RhythmBullet.Zer01HD.Utilities.Input; using RhythmBullet.Zer01HD.Utilities.Input;
namespace RhythmBullet.Zer01HD.UI.Modular namespace RhythmBullet.Zer01HD.UI.Modular
@ -24,12 +25,12 @@ namespace RhythmBullet.Zer01HD.UI.Modular
} }
} }
public override void Draw(SpriteBatch batch) public override void Draw(SpriteBatch batch, Camera2D camera)
{ {
if (scissorBounds != null) if (scissorBounds != null)
{ {
batch.End(); batch.End();
batch.Begin(SpriteSortMode.Deferred, null, null, null, scissorRasterizer); batch.Begin(SpriteSortMode.Deferred, null, null, null, scissorRasterizer, null, camera.TransformMatrix);
scissorBounds.Width = Bounds.Width; scissorBounds.Width = Bounds.Width;
scissorBounds.Height = Bounds.Height; scissorBounds.Height = Bounds.Height;
scissorBounds.X = Bounds.X; scissorBounds.X = Bounds.X;
@ -44,7 +45,7 @@ namespace RhythmBullet.Zer01HD.UI.Modular
int offsetY = module.Bounds.Y; int offsetY = module.Bounds.Y;
module.Bounds.X = Bounds.X + offsetX; module.Bounds.X = Bounds.X + offsetX;
module.Bounds.Y = Bounds.Y + offsetY; module.Bounds.Y = Bounds.Y + offsetY;
module.Draw(batch); module.Draw(batch, camera);
module.Bounds.X = offsetX; module.Bounds.X = offsetX;
module.Bounds.Y = offsetY; module.Bounds.Y = offsetY;
} }
@ -53,7 +54,7 @@ namespace RhythmBullet.Zer01HD.UI.Modular
{ {
batch.GraphicsDevice.ScissorRectangle = scissorBounds; batch.GraphicsDevice.ScissorRectangle = scissorBounds;
batch.End(); batch.End();
batch.Begin(); batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, camera.TransformMatrix);
} }
} }