Removed code from when still using Monogame.

This commit is contained in:
Harrison Deng 2020-02-17 00:35:14 -05:00
parent 9ac8e94530
commit 60fa54ccf9
5 changed files with 0 additions and 555 deletions

View File

@ -1,90 +0,0 @@
using OpenTK;
namespace RecrownedGTK.Graphics.Render
{
/// <summary>
/// A generic camera. Functions in 3D.
/// </summary>
public class BasicCamera
{
/// <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 camera.
/// </summary>
public Vector3 upDirection;
/// <summary>
/// The transform matrix representing the world (rotation and translations of the original world).
/// </summary>
public Matrix3 worldMatrix;
/// <summary>
/// The view matrix that describes where the camera looks.
/// </summary>
public Matrix3 ViewMatrix { get; protected set; }
/// <summary>
/// The projection matrix.
/// </summary>
public Matrix3 projectionMatrix;
/// <summary>
/// A basic effect that will be updated with the correct matrice information everytime <see cref="Apply"/> is called. Can be null and thus will not be used.
/// </summary>
public BasicEffect basicEffect;
/// <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="basicEffect">A basic effect that will be updated with the correct matrice information everytime <see cref="Apply"/> is called. Can be null and thus will not be used.</param>
public BasicCamera(BasicEffect basicEffect = null)
{
worldMatrix = Matrix.Identity;
lookAt = Vector3.Forward;
upDirection = Vector3.Up;
projectionMatrix = Matrix.Identity;
this.basicEffect = basicEffect;
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);
if (basicEffect != null)
{
basicEffect.World = worldMatrix;
basicEffect.Projection = projectionMatrix;
basicEffect.View = ViewMatrix;
}
}
/// <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();
}
}
}

View File

@ -1,84 +0,0 @@
using OpenTK;
using System;
namespace RecrownedGTK.Graphics.Render
{
/// <summary>
/// A virtual 2D camera that wraps the normal <see cref="BasicCamera"/>. Default projection is orthographic.
/// </summary>
public class Camera2D : BasicCamera
{
/// <summary>
/// The width of the view of the camera.
/// </summary>
public int viewWidth;
/// <summary>
/// The height of the view of the camera.
/// </summary>
public int viewHeight;
/// <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 ConrnerPosition { set { position.X = value.X + viewWidth / 2f; position.Y = value.Y + viewHeight / 2f; } }
/// <summary>
/// A 2D camera from the generic <see cref="BasicCamera"/>.
/// </summary>
/// <param name="height">Width of camera view.</param>
/// <param name="width">Height of camera view.</param>
/// <param name="basicEffect">A basic effect that will be updated with the correct matrice information everytime <see cref="Apply"/> is called. Can be null and thus will not be used.</param>
public Camera2D(int width, int height, BasicEffect basicEffect = null) : base(basicEffect)
{
this.viewWidth = width;
this.viewHeight = height;
upDirection = Vector3.Down;
ConrnerPosition = new Vector2(0, 0);
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()
{
projectionMatrix = Matrix.CreateOrthographic(viewWidth, viewHeight, 0, 1);
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;
}
}
}

View File

@ -1,104 +0,0 @@
namespace RecrownedGTK.Graphics.Render
{
/// <summary>
/// A <see cref="SpriteBatch"/> that keeps it's settings through begin and end unless manually changed either by the <see cref="Begin(SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, Matrix?)"/> or through changing the fields. Note that changing the fields while the batch has begun will not take effect until the next time the batch is started.
/// Casting this as a <see cref="SpriteBatch"/> will not persist the configuration and will call the normal <see cref="SpriteBatch.Begin(SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, Matrix?)"/>.
/// </summary>
public class ConsistentSpriteBatch : SpriteBatch
{
/// <summary>
/// How to blend the colors. Uses <see cref="SpriteBatch"/>'s default if not set before or during <see cref="Begin(SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, Matrix?)"/> call.
/// Changes will only take effect on <see cref="Begin(SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, Matrix?)"/> call.
/// </summary>
public BlendState blendState;
/// <summary>
/// The state of sampler to use for the spritebatch. Uses <see cref="SpriteBatch"/>'s default if not set before or during <see cref="Begin(SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, Matrix?)"/> call.
/// Changes will only take effect on <see cref="Begin(SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, Matrix?)"/> call.
/// </summary>
public SamplerState samplerState;
/// <summary>
/// The state of the depth-stencil buffer. Uses <see cref="SpriteBatch"/>'s defaultdefault if not set before or during <see cref="Begin(SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, Matrix?)"/> call.
/// Changes will only take effect on <see cref="Begin(SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, Matrix?)"/> call.
/// </summary>
public DepthStencilState depthStencilState;
/// <summary>
/// The state of rasterizer to use. Uses <see cref="SpriteBatch"/>'s default if not set before or during <see cref="Begin(SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, Matrix?)"/> call.
/// Changes will only take effect on <see cref="Begin(SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, Matrix?)"/> call.
/// </summary>
public RasterizerState rasterizerState;
/// <summary>
/// An effect to apply to the batch. Uses <see cref="SpriteBatch"/>'s default if not set before or during <see cref="Begin(SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, Matrix?)"/> call.
/// Changes will only take effect on <see cref="Begin(SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, Matrix?)"/> call.
/// </summary>
public Effect effect;
/// <summary>
/// A matrix to be applied to transform the sprites geometry. An identity matrix is used if not provided before or during <see cref="Begin(SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, Matrix?)"/> call.
/// Changes will only take effect on <see cref="Begin(SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, Matrix?)"/> call.
/// </summary>
public Matrix? transformMatrix;
/// <summary>
/// Creates a consistent sprite batch with default values.
/// </summary>
/// <param name="graphicsDevice">The graphics device to use to create a <see cref="SpriteBatch"/>.</param>
public ConsistentSpriteBatch(GraphicsDevice graphicsDevice) : base(graphicsDevice)
{
}
/// <summary>
/// Begins the consistent sprite batch. The configuration passed to this function is saved for later begin calls.
/// </summary>
/// <param name="sortMode">Defines the spritebatch's method of sorting the items in each batch. Uses <see cref="SpriteBatch"/>'s default.</param>
/// <param name="blendState">How to blend the colors. Uses <see cref="SpriteBatch"/>'s default if not set and field is also not set.</param>
/// <param name="samplerState">The state of sampler to use for the spritebatch. Uses <see cref="SpriteBatch"/>'s default if not set and field is also not set.</param>
/// <param name="depthStencilState">What type of rasterization to use. Uses <see cref="SpriteBatch"/>'s default if not set and field is also not set.</param>
/// <param name="rasterizerState">What type of rasterization to use. Uses <see cref="SpriteBatch"/>'s default if not set and field is also not set.</param>
/// <param name="effect">An effect to apply to the batch. Uses <see cref="SpriteBatch"/>'s default if not set and field is also not set.</param>
/// <param name="transformMatrix">A matrix to be applied to transform the sprites geometry. Uses <see cref="SpriteBatch"/>'s default if not set and field is also not set.</param>
public new void Begin(SpriteSortMode sortMode = SpriteSortMode.Deferred, BlendState blendState = null, SamplerState samplerState = null, DepthStencilState depthStencilState = null, RasterizerState rasterizerState = null, Effect effect = null, Matrix? transformMatrix = null)
{
if (blendState != null) this.blendState = blendState;
if (samplerState != null) this.samplerState = samplerState;
if (depthStencilState != null) this.depthStencilState = depthStencilState;
if (rasterizerState != null) this.rasterizerState = rasterizerState;
if (effect != null) this.effect = effect;
if (transformMatrix != null) this.transformMatrix = transformMatrix;
base.Begin(
sortMode: sortMode,
blendState: this.blendState,
samplerState: this.samplerState,
depthStencilState: this.depthStencilState,
rasterizerState: this.rasterizerState,
effect: this.effect,
transformMatrix: this.transformMatrix);
}
/// <summary>
/// Begins the consistent sprite batch without saving the configuration. Useful for one time changes to one portion of the configuration.
/// </summary>
/// <param name="sortMode">Defines the spritebatch's method of sorting the items in each batch. Uses <see cref="SpriteBatch"/>'s default.</param>
/// <param name="blendState">How to blend the colors. Uses <see cref="SpriteBatch"/>'s default if not set and field is also not set.</param>
/// <param name="samplerState">The state of sampler to use for the spritebatch. Uses <see cref="SpriteBatch"/>'s default if not set and field is also not set.</param>
/// <param name="depthStencilState">What type of rasterization to use. Uses <see cref="SpriteBatch"/>'s default if not set and field is also not set.</param>
/// <param name="rasterizerState">What type of rasterization to use. Uses <see cref="SpriteBatch"/>'s default if not set and field is also not set.</param>
/// <param name="effect">An effect to apply to the batch. Uses <see cref="SpriteBatch"/>'s default if not set and field is also not set.</param>
/// <param name="transformMatrix">A matrix to be applied to transform the sprites geometry. Uses <see cref="SpriteBatch"/>'s default if not set and field is also not set.</param>
public void BeginWithoutSaving(SpriteSortMode sortMode = SpriteSortMode.Deferred, BlendState blendState = null, SamplerState samplerState = null, DepthStencilState depthStencilState = null, RasterizerState rasterizerState = null, Effect effect = null, Matrix? transformMatrix = null)
{
base.Begin(
sortMode: sortMode,
blendState: blendState ?? this.blendState,
samplerState: samplerState ?? this.samplerState,
depthStencilState: depthStencilState ?? this.depthStencilState,
rasterizerState: rasterizerState ?? this.rasterizerState,
effect: effect ?? this.effect,
transformMatrix: transformMatrix ?? this.transformMatrix);
}
}
}

View File

@ -1,158 +0,0 @@
using OpenTK.Graphics;
using OpenTK;
using System;
namespace RecrownedGTK.Graphics.Render
{
/// <summary>
/// A batch used to draw primitive shapes by batching together vertices.
/// </summary>
public class PrimitiveBatch : IDisposable
{
VertexPositionColor[] vertices;
int bufferPosition;
Effect effect;
PrimitiveType primitiveType;
private int verticesPerPrimitive;
/// <summary>
/// Amount of primitives.
/// </summary>
public int primitiveCount;
GraphicsDevice graphicsDevice;
bool began;
bool disposed;
BasicEffect basicEffect;
/// <summary>
/// Creates a batch used to draw primitives.
/// </summary>
/// <param name="graphicsDevice">The graphics device used to draw the primitives.</param>
/// <param name="verticesPerBatch">The amount of vertices every batch can hold before flushing. Default is 450. Should be changed to be the most optimal number if possible to prevent unnecessary resizing. Especially if using strip primitive types.</param>
public PrimitiveBatch(GraphicsDevice graphicsDevice, int verticesPerBatch = 500)
{
vertices = new VertexPositionColor[verticesPerBatch + 1];
this.graphicsDevice = graphicsDevice;
basicEffect = new BasicEffect(graphicsDevice);
basicEffect.VertexColorEnabled = true;
basicEffect.Projection = Matrix.CreateOrthographicOffCenter(0, graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height, 0, 0, 1);
basicEffect.World = Matrix.Identity;
basicEffect.View = Matrix.CreateLookAt(Vector3.Zero, Vector3.Forward, Vector3.Up);
}
/// <summary>
/// Starts the batch. Batch cannot be started twice.
/// </summary>
/// <param name="primitiveType">The type of primitive this batch would be drawing.</param>
/// <param name="effect">Effect to use to render the primitives. Default will use a <see cref="BasicEffect"/> with parameters set up during creation.</param>
public void Begin(PrimitiveType primitiveType, Effect effect = null)
{
if (began) throw new InvalidOperationException("Begin is being called twice before being ended.");
if (disposed) throw new ObjectDisposedException(this.GetType().Name);
this.primitiveType = primitiveType;
switch (primitiveType)
{
case PrimitiveType.LineList: verticesPerPrimitive = 2; break;
case PrimitiveType.TriangleList: verticesPerPrimitive = 3; break;
case PrimitiveType.LineStrip: verticesPerPrimitive = 1; break;
case PrimitiveType.TriangleStrip: verticesPerPrimitive = 3; break;
}
this.effect = effect ?? basicEffect;
began = true;
}
/// <summary>
/// Ends the batch. Begin needs to be called before end.
/// </summary>
public void End()
{
if (disposed) throw new ObjectDisposedException(this.GetType().Name);
if (!began) throw new InvalidOperationException("Begin must be called before ending.");
Flush();
began = false;
}
/// <summary>
/// Adds a vertex position for the primitive being drawn. The batch needs to have beens started before this.
/// </summary>
/// <param name="vertex">The vector that represents the vertex.</param>
/// <param name="color">The color of that vertex.</param>
public void AddVertex(Vector2 vertex, Color4 color)
{
if (!began) throw new InvalidOperationException("Begin needs to be called before adding vertex.");
if (disposed) throw new ObjectDisposedException(this.GetType().Name);
if (primitiveType != PrimitiveType.LineStrip && primitiveType != PrimitiveType.TriangleStrip)
{
if (bufferPosition + verticesPerPrimitive >= vertices.Length)
{
if ((bufferPosition % vertices.Length == 0))
{
Flush();
}
else
{
throw new InvalidOperationException("Buffer size doesn't match with primitive type.");
}
}
}
else if (bufferPosition + verticesPerPrimitive > vertices.Length)
{
throw new InvalidOperationException("Buffer size isn't large enough.");
}
vertices[bufferPosition] = new VertexPositionColor(new Vector3(vertex, 0), color);
bufferPosition++;
}
/// <summary>
/// Flushes the batch. Automatically called if required if using <see cref="PrimitiveType.LineList"/> or <see cref="PrimitiveType.TriangleList"/>. Otherwise, manual flushing is required.
/// <see cref="primitiveCount"/> is used if not zero. Is reset to zero every flush.
/// </summary>
public void Flush()
{
if (!began) throw new InvalidOperationException("Begin needs to be called before flushing.");
if (disposed) throw new ObjectDisposedException(this.GetType().Name);
if (bufferPosition == 0) return;
if (primitiveCount == 0) primitiveCount = bufferPosition / verticesPerPrimitive;
foreach (EffectPass effectPass in effect.CurrentTechnique.Passes)
{
effectPass.Apply();
graphicsDevice.DrawUserPrimitives(primitiveType, vertices, 0, primitiveCount);
}
bufferPosition = 0;
primitiveCount = 0;
}
/// <summary>
/// Disposes this.
/// </summary>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Overridable dispose.
/// </summary>
/// <param name="disposing">True for when user called for this to be disposed. False otherwise.</param>
public virtual void Dispose(bool disposing)
{
if (disposed) throw new ObjectDisposedException(this.GetType().Name);
disposed = true;
if (disposing && !disposed)
{
basicEffect.Dispose();
}
}
/// <summary>
/// Destructor.
/// </summary>
~PrimitiveBatch()
{
Dispose(false);
}
}
}

View File

@ -1,119 +0,0 @@
using OpenTK.Graphics;
using OpenTK;
using RecrownedGTK.Types;
using System;
namespace RecrownedGTK.Graphics.Render
{
/// <summary>
/// Renders rectangles using the <see cref="PrimitiveBatch"/>.
/// </summary>
public class RectangleRenderer : IDisposable
{
private bool filled;
private bool disposed;
/// <summary>
/// The <see cref="PrimitiveBatch"/> used. Needs to be disposed.
/// </summary>
private readonly PrimitiveBatch primitiveBatch;
/// <summary>
/// Creates a rectangle renderer with the given <see cref="PrimitiveBatch"/>.
/// </summary>
/// <param name="graphicsDevice">Graphics device to use.</param>
public RectangleRenderer(GraphicsDevice graphicsDevice)
{
primitiveBatch = new PrimitiveBatch(graphicsDevice);
}
/// <summary>
/// Disposes the rectangle renderer.
/// </summary>
public void Dispose()
{
Dispose(true);
}
/// <summary>
/// Overridable for dispose.
/// </summary>
/// <param name="disposing">True when its a player calling the dispose.</param>
public virtual void Dispose(bool disposing)
{
if (disposed) throw new ObjectDisposedException(GetType().Name);
if (disposing)
{
primitiveBatch.Dispose();
}
disposed = true;
}
/// <summary>
/// Begins a batch for rectangles.
/// </summary>
/// <param name="filled">Whether or not this batch should be filled rectangles.</param>
public void Begin(bool filled = false)
{
this.filled = filled;
primitiveBatch.Begin(filled ? PrimitiveType.TriangleStrip : PrimitiveType.LineStrip);
}
/// <summary>
/// Ends the batch.
/// </summary>
public void End()
{
primitiveBatch.End();
}
/// <summary>
/// Draws a basic rectangle given bottom left and top right.
/// </summary>
/// <param name="x">X coordinate of bottom left.</param>
/// <param name="y">Y coordinate of bottom left.</param>
/// <param name="width">Width of rectangle.</param>
/// <param name="height">Height of rectangle.</param>
/// <param name="color">Color of all vertices of this rectangle.</param>
/// <param name="rotation">Rotation of rectangle. Default is 0 radians.</param>
public void Draw(int x, int y, int width, int height, Color4 color, float rotation = 0)
{
primitiveBatch.primitiveCount = filled ? 3 : 4;
Vector2[] corners = new Vector2[4];
corners[1] = new Vector2(width, 0);
corners[2] = new Vector2(width, height);
corners[3] = new Vector2(0, height);
if (rotation != 0)
{
Matrix rotMat = Matrix.CreateRotationZ(rotation);
for (int i = 0; i < corners.Length; i++)
{
corners[i] = Vector2.Transform(corners[i], rotMat);
}
}
for (int i = 0; i < corners.Length; i++)
{
corners[i].X += x;
corners[i].Y += y;
}
primitiveBatch.AddVertex(corners[0], color);
primitiveBatch.AddVertex(corners[1], color);
primitiveBatch.AddVertex(corners[2], color);
primitiveBatch.AddVertex(corners[3], color);
primitiveBatch.AddVertex(corners[0], color);
primitiveBatch.Flush();
}
/// <summary>
/// Draws the given rectangle.
/// </summary>
/// <param name="rectangle">Uses the x, y and dimensions to draw a rectangle.</param>
/// <param name="color">The color of the rectangle.</param>
public void Draw(Rectangle rectangle, Color4 color)
{
Draw(rectangle.X, rectangle.Y, rectangle.Width, rectangle.Height, color);
}
}
}