Changed 9p and tatlas' to use a separately loaded texture.
This commit is contained in:
71
RecrownedAthenaeum/Render/Camera2D.cs
Normal file
71
RecrownedAthenaeum/Render/Camera2D.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using System;
|
||||
|
||||
namespace RecrownedAthenaeum.Camera
|
||||
{
|
||||
/// <summary>
|
||||
/// A virtual 2D camera that wraps the normal <see cref="Camera3D"/>. Default projection is orthographic.
|
||||
/// </summary>
|
||||
public class Camera2D : Camera3D
|
||||
{
|
||||
/// <summary>
|
||||
/// The 2D position.
|
||||
/// </summary>
|
||||
public Vector2 Position { get { return new Vector2(position.X, position.Y); } set { position.X = value.X; position.Y = value.Y; } }
|
||||
|
||||
/// <summary>
|
||||
/// Places camera in the center given the corner position.
|
||||
/// </summary>
|
||||
public Vector2 CenterPosition { set { position.X = value.X + graphicsDevice.Viewport.Width / 2f; position.Y = value.Y + graphicsDevice.Viewport.Height / 2f; } }
|
||||
|
||||
/// <summary>
|
||||
/// A 2D camera from the generic <see cref="Camera3D"/>.
|
||||
/// </summary>
|
||||
/// <param name="graphicsDevice">The graphics device to use if not using the one in <see cref="Configuration"/>.</param>
|
||||
public Camera2D(GraphicsDevice graphicsDevice = null) : base(graphicsDevice)
|
||||
{
|
||||
projectionMatrix = Matrix.CreateOrthographic(this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height, 0, 1);
|
||||
CenterPosition = new Vector2(0, 0);
|
||||
upDirection = Vector3.Down;
|
||||
Apply();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies for 2D.
|
||||
/// Sets where the camera is looking for the view matrix to the position of the camera.
|
||||
/// </summary>
|
||||
public override void Apply()
|
||||
{
|
||||
position.Z = 0;
|
||||
lookAt = new Vector3(Position, 1f);
|
||||
base.Apply();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lerps to the given position.
|
||||
/// </summary>
|
||||
/// <param name="alpha">The multiplier for difference in distance.</param>
|
||||
/// <param name="targetPosition">The target position to lerp to.</param>
|
||||
/// <param name="delta">Time between this frame and the previous frame.</param>
|
||||
public void LinearInterpolationToPosition(float alpha, Vector2 targetPosition, float delta)
|
||||
{
|
||||
if (alpha <= 0 && alpha > 1f) throw new ArgumentException("Alpha can't be greater than 1f, less than or equal to 0.");
|
||||
|
||||
Vector2 distance = targetPosition - Position;
|
||||
distance *= (float)(1.0f - Math.Pow(1 - alpha, delta / 0.02f));
|
||||
|
||||
Position += distance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Moves the camera.
|
||||
/// Apply needs to be called.
|
||||
/// </summary>
|
||||
/// <param name="move">Magnitude of how much to move per axis.</param>
|
||||
public void MoveCamera(Vector2 move)
|
||||
{
|
||||
Position += move;
|
||||
}
|
||||
}
|
||||
}
|
99
RecrownedAthenaeum/Render/Camera3D.cs
Normal file
99
RecrownedAthenaeum/Render/Camera3D.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace RecrownedAthenaeum.Camera
|
||||
{
|
||||
/// <summary>
|
||||
/// A generic 3D camera.
|
||||
/// </summary>
|
||||
public class Camera3D
|
||||
{
|
||||
/// <summary>
|
||||
/// The scale for the world.
|
||||
/// </summary>
|
||||
public float worldScale = 1f;
|
||||
|
||||
/// <summary>
|
||||
/// Current position in the world.
|
||||
/// </summary>
|
||||
public Vector3 position;
|
||||
|
||||
/// <summary>
|
||||
/// The place the 3D camera is looking at.
|
||||
/// </summary>
|
||||
public Vector3 lookAt;
|
||||
|
||||
/// <summary>
|
||||
/// The direction up is for the 3D camera.
|
||||
/// </summary>
|
||||
public Vector3 upDirection;
|
||||
|
||||
/// <summary>
|
||||
/// The transform matrix representing the world (rotation and translations of the original world).
|
||||
/// </summary>
|
||||
public Matrix worldMatrix;
|
||||
|
||||
/// <summary>
|
||||
/// The view matrix that describes where the camera looks.
|
||||
/// </summary>
|
||||
public Matrix ViewMatrix { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// The projection matrix.
|
||||
/// </summary>
|
||||
public Matrix projectionMatrix;
|
||||
|
||||
/// <summary>
|
||||
/// The graphics device used
|
||||
/// </summary>
|
||||
protected GraphicsDevice graphicsDevice;
|
||||
|
||||
/// <summary>
|
||||
/// The basic effect that contains the transformations.
|
||||
/// </summary>
|
||||
public BasicEffect BasicEffect { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// Constructs 3D camera with an orthographic projection matrix with dimensions of graphics devices viewport. All changes to matrices should have apply called after changes.
|
||||
/// </summary>
|
||||
/// <param name="graphicsDevice">The graphics device to use. Will use graphics device from <see cref="Configuration"/>'s graphics device manager if this is null which it is by default.</param>
|
||||
public Camera3D(GraphicsDevice graphicsDevice = null)
|
||||
{
|
||||
graphicsDevice = graphicsDevice ?? (Configuration.GraphicsDeviceManager.GraphicsDevice);
|
||||
this.graphicsDevice = graphicsDevice;
|
||||
worldMatrix = Matrix.Identity;
|
||||
lookAt = Vector3.Forward;
|
||||
upDirection = Vector3.Up;
|
||||
|
||||
projectionMatrix = Matrix.Identity;
|
||||
|
||||
BasicEffect = new BasicEffect(graphicsDevice);
|
||||
BasicEffect.TextureEnabled = true;
|
||||
BasicEffect.VertexColorEnabled = true;
|
||||
Apply();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Applies the changes to the fields and properties of the camera.
|
||||
/// </summary>
|
||||
public virtual void Apply()
|
||||
{
|
||||
ViewMatrix = Matrix.CreateLookAt(position, lookAt, upDirection);
|
||||
worldMatrix *= Matrix.CreateScale(worldScale);
|
||||
|
||||
BasicEffect.World = worldMatrix;
|
||||
BasicEffect.View = ViewMatrix;
|
||||
BasicEffect.Projection = projectionMatrix;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Moves camera by the given amount.
|
||||
/// </summary>
|
||||
/// <param name="move">A <see cref="Vector3"/> that contains how much in each direction to move.</param>
|
||||
public void MoveCamera(Vector3 move)
|
||||
{
|
||||
position += move;
|
||||
Apply();
|
||||
}
|
||||
}
|
||||
}
|
@@ -2,7 +2,6 @@
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using RecrownedAthenaeum.Camera;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace RecrownedAthenaeum.Render
|
||||
{
|
||||
|
41
RecrownedAthenaeum/Render/ScissorBox.cs
Normal file
41
RecrownedAthenaeum/Render/ScissorBox.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
|
||||
namespace RecrownedAthenaeum.Render
|
||||
{
|
||||
/// <summary>
|
||||
/// Box that crops off anything outside of the bounds.
|
||||
/// </summary>
|
||||
public class ScissorBox
|
||||
{
|
||||
|
||||
SpriteBatch spriteBatch;
|
||||
Rectangle currentScissorRect;
|
||||
|
||||
/// <summary>
|
||||
/// Starts spritebatch with scissoring enabled.
|
||||
/// </summary>
|
||||
/// <param name="rectangle"></param>
|
||||
/// <param name="spriteBatch"></param>
|
||||
/// <param name="spriteBatchSettings">The settings for using the <see cref="SpriteBatch"/> to perform the scissor.</param>
|
||||
public void Begin(Rectangle rectangle, SpriteBatch spriteBatch, SpriteBatchSettings? spriteBatchSettings = null)
|
||||
{
|
||||
if (spriteBatchSettings == null) spriteBatchSettings = Configuration.SpriteBatchSettings;
|
||||
|
||||
this.spriteBatch = spriteBatch;
|
||||
spriteBatchSettings.Value.rasterizerState.ScissorTestEnable = true;
|
||||
spriteBatchSettings.Value.BeginSpriteBatch(spriteBatch);
|
||||
currentScissorRect = spriteBatch.GraphicsDevice.ScissorRectangle;
|
||||
spriteBatch.GraphicsDevice.ScissorRectangle = rectangle;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ends the scissoring and spritebatch.
|
||||
/// </summary>
|
||||
public void End()
|
||||
{
|
||||
spriteBatch.GraphicsDevice.ScissorRectangle = currentScissorRect;
|
||||
spriteBatch.End();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,10 +1,5 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RecrownedAthenaeum.Render
|
||||
{
|
||||
@@ -46,8 +41,28 @@ namespace RecrownedAthenaeum.Render
|
||||
/// <summary>
|
||||
/// The transformation matrix to use.
|
||||
/// </summary>
|
||||
public Matrix transformMatrix;
|
||||
public Matrix? transformMatrix;
|
||||
|
||||
/// <summary>
|
||||
/// See <see cref="SpriteBatch.Begin(SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, Matrix?)"/> for uses and defaults.
|
||||
/// </summary>
|
||||
/// <param name="spriteSortMode"></param>
|
||||
/// <param name="blendState"></param>
|
||||
/// <param name="samplerState"></param>
|
||||
/// <param name="depthStencilState"></param>
|
||||
/// <param name="rasterizerState"></param>
|
||||
/// <param name="effect"></param>
|
||||
/// <param name="transformMatrix"></param>
|
||||
public SpriteBatchSettings(SpriteSortMode spriteSortMode = SpriteSortMode.Deferred, BlendState blendState = null, SamplerState samplerState = null, DepthStencilState depthStencilState = null, RasterizerState rasterizerState = null, Effect effect = null, Matrix? transformMatrix = null)
|
||||
{
|
||||
this.spriteSortMode = spriteSortMode;
|
||||
this.blendState = blendState;
|
||||
this.samplerState = samplerState;
|
||||
this.depthStencilState = depthStencilState;
|
||||
this.rasterizerState = rasterizerState;
|
||||
this.effect = effect;
|
||||
this.transformMatrix = transformMatrix;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Begins the given sprite batch with the current set of settings.
|
||||
|
Reference in New Issue
Block a user