Attempted at fixing 2D camera system.

This commit is contained in:
Harrison Deng 2019-02-24 22:44:02 -06:00
parent 5f04dfd73a
commit 051f3b04b5
9 changed files with 41 additions and 18 deletions

View File

@ -10,12 +10,15 @@ namespace RecrownedAthenaeum.ContentReaders
{ {
class NinePatchDataReader : ContentTypeReader<NinePatch> class NinePatchDataReader : ContentTypeReader<NinePatch>
{ {
public static GraphicsDevice graphicsDevice;
protected override NinePatch Read(ContentReader input, NinePatch existingInstance) protected override NinePatch Read(ContentReader input, NinePatch existingInstance)
{ {
if (graphicsDevice == null) graphicsDevice = Configuration.GraphicsDeviceManager.GraphicsDevice;
Texture2D texture; Texture2D texture;
using (MemoryStream stream = new MemoryStream(input.ReadBytes(input.ReadInt32()))) using (MemoryStream stream = new MemoryStream(input.ReadBytes(input.ReadInt32())))
{ {
texture = Texture2D.FromStream(Configuration.GraphicsDeviceManager.GraphicsDevice, stream); texture = Texture2D.FromStream(graphicsDevice, stream);
} }
NinePatchData ninePatchData = JsonConvert.DeserializeObject<NinePatchData>(Encoding.ASCII.GetString(input.ReadBytes(input.ReadInt32()))); NinePatchData ninePatchData = JsonConvert.DeserializeObject<NinePatchData>(Encoding.ASCII.GetString(input.ReadBytes(input.ReadInt32())));
NinePatch ninePatch = new NinePatch(texture, ninePatchData.left, ninePatchData.right, ninePatchData.bottom, ninePatchData.top); NinePatch ninePatch = new NinePatch(texture, ninePatchData.left, ninePatchData.right, ninePatchData.bottom, ninePatchData.top);

View File

@ -1,4 +1,5 @@
using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Newtonsoft.Json; using Newtonsoft.Json;
using RecrownedAthenaeum.Data; using RecrownedAthenaeum.Data;
@ -10,13 +11,16 @@ namespace RecrownedAthenaeum.ContentReaders
{ {
class TextureAtlasDataReader : ContentTypeReader<TextureAtlas> class TextureAtlasDataReader : ContentTypeReader<TextureAtlas>
{ {
public static GraphicsDevice graphicsDevice;
protected override TextureAtlas Read(ContentReader input, TextureAtlas existingInstance) protected override TextureAtlas Read(ContentReader input, TextureAtlas existingInstance)
{ {
if (graphicsDevice == null) graphicsDevice = Configuration.GraphicsDeviceManager.GraphicsDevice;
TextureAtlasData atlasData; TextureAtlasData atlasData;
Texture2D atlasTexture; Texture2D atlasTexture;
using (MemoryStream stream = new MemoryStream(input.ReadBytes(input.ReadInt32()))) using (MemoryStream stream = new MemoryStream(input.ReadBytes(input.ReadInt32())))
{ {
atlasTexture = Texture2D.FromStream(Configuration.GraphicsDeviceManager.GraphicsDevice, stream); atlasTexture = Texture2D.FromStream(graphicsDevice, stream);
} }
string serialized = Encoding.ASCII.GetString(input.ReadBytes(input.ReadInt32())); string serialized = Encoding.ASCII.GetString(input.ReadBytes(input.ReadInt32()));
atlasData = JsonConvert.DeserializeObject<TextureAtlasData>(serialized); atlasData = JsonConvert.DeserializeObject<TextureAtlasData>(serialized);

View File

@ -51,8 +51,8 @@
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Camera\Camera3D.cs" /> <Compile Include="View\Camera3D.cs" />
<Compile Include="Camera\Camera2D.cs" /> <Compile Include="View\Camera2D.cs" />
<Compile Include="ContentSystem\ContentData.cs" /> <Compile Include="ContentSystem\ContentData.cs" />
<Compile Include="ContentSystem\ContentManagerController.cs" /> <Compile Include="ContentSystem\ContentManagerController.cs" />
<Compile Include="ContentSystem\IContentPathResolver.cs" /> <Compile Include="ContentSystem\IContentPathResolver.cs" />

View File

@ -51,7 +51,6 @@ namespace RecrownedAthenaeum.Render
if (began) throw new InvalidOperationException("Begin is being called twice before being ended."); if (began) throw new InvalidOperationException("Begin is being called twice before being ended.");
if (disposed) throw new ObjectDisposedException(this.GetType().Name); if (disposed) throw new ObjectDisposedException(this.GetType().Name);
this.primitiveType = primitiveType; this.primitiveType = primitiveType;
verticesPerPrimitive = 0;
switch (primitiveType) switch (primitiveType)
{ {
case PrimitiveType.LineList: verticesPerPrimitive = 2; break; case PrimitiveType.LineList: verticesPerPrimitive = 2; break;

View File

@ -79,7 +79,7 @@ namespace RecrownedAthenaeum.Render
{ {
primitiveBatch.primitiveCount = filled ? 3 : 4; primitiveBatch.primitiveCount = filled ? 3 : 4;
Vector2[] corners = new Vector2[4]; Vector2[] corners = new Vector2[4];
corners[1] = new Vector2 (width, 0); corners[1] = new Vector2(width, 0);
corners[2] = new Vector2(width, height); corners[2] = new Vector2(width, height);
corners[3] = new Vector2(0, height); corners[3] = new Vector2(0, height);
@ -89,16 +89,22 @@ namespace RecrownedAthenaeum.Render
for (int i = 0; i < corners.Length; i++) for (int i = 0; i < corners.Length; i++)
{ {
corners[i] = Vector2.Transform(corners[i], rotMat); corners[i] = Vector2.Transform(corners[i], rotMat);
}
}
for (int i = 0; i < corners.Length; i++)
{
corners[i].X += x; corners[i].X += x;
corners[i].Y += y; corners[i].Y += y;
} }
}
primitiveBatch.AddVertex(corners[0], color); primitiveBatch.AddVertex(corners[0], color);
primitiveBatch.AddVertex(corners[1], color); primitiveBatch.AddVertex(corners[1], color);
primitiveBatch.AddVertex(corners[2], color); primitiveBatch.AddVertex(corners[2], color);
primitiveBatch.AddVertex(corners[3], color); primitiveBatch.AddVertex(corners[3], color);
primitiveBatch.AddVertex(corners[0], color); primitiveBatch.AddVertex(corners[0], color);
primitiveBatch.Flush();
} }
} }
} }

View File

@ -129,13 +129,13 @@ namespace RecrownedAthenaeum.ScreenSystem
{ {
graphics.GraphicsDevice.SetRenderTarget(previousScreenRenderTarget); graphics.GraphicsDevice.SetRenderTarget(previousScreenRenderTarget);
graphics.GraphicsDevice.Clear(previousScreen.BackgroundColor); graphics.GraphicsDevice.Clear(previousScreen.BackgroundColor);
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, camera.TransformationMatrix); spriteBatch.Begin(effect: camera.BasicEffect);
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(SpriteSortMode.Deferred, null, null, null, null, null, camera.TransformationMatrix); spriteBatch.Begin(effect: camera.BasicEffect);
Screen.Draw(spriteBatch); Screen.Draw(spriteBatch);
spriteBatch.End(); spriteBatch.End();
} }

View File

@ -48,7 +48,7 @@ namespace RecrownedAthenaeum.UI.Modular
if (scissorBounds != null) if (scissorBounds != null)
{ {
batch.End(); batch.End();
batch.Begin(SpriteSortMode.Deferred, null, null, null, scissorRasterizer, null, camera?.TransformationMatrix); batch.Begin(effect: camera?.BasicEffect);
scissorBounds.Width = bounds.Width; scissorBounds.Width = bounds.Width;
scissorBounds.Height = bounds.Height; scissorBounds.Height = bounds.Height;
scissorBounds.X = bounds.X; scissorBounds.X = bounds.X;
@ -72,7 +72,7 @@ namespace RecrownedAthenaeum.UI.Modular
{ {
batch.GraphicsDevice.ScissorRectangle = scissorBounds; batch.GraphicsDevice.ScissorRectangle = scissorBounds;
batch.End(); batch.End();
batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, camera?.TransformationMatrix); batch.Begin(effect: camera?.BasicEffect);
} }
} }

View File

@ -28,6 +28,7 @@ namespace RecrownedAthenaeum.Camera
{ {
projectionMatrix = Matrix.CreateOrthographic(this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height, 0, 1); projectionMatrix = Matrix.CreateOrthographic(this.graphicsDevice.Viewport.Width, this.graphicsDevice.Viewport.Height, 0, 1);
CenterPosition = new Vector2(0, 0); CenterPosition = new Vector2(0, 0);
upDirection = Vector3.Down;
Apply(); Apply();
} }
@ -37,6 +38,7 @@ namespace RecrownedAthenaeum.Camera
/// </summary> /// </summary>
public override void Apply() public override void Apply()
{ {
position.Z = 0;
lookAt = new Vector3(Position, 1f); lookAt = new Vector3(Position, 1f);
base.Apply(); base.Apply();
} }
@ -64,9 +66,7 @@ namespace RecrownedAthenaeum.Camera
/// <param name="move">Magnitude of how much to move per axis.</param> /// <param name="move">Magnitude of how much to move per axis.</param>
public void MoveCamera(Vector2 move) public void MoveCamera(Vector2 move)
{ {
Console.WriteLine(move);
Position += move; Position += move;
Console.WriteLine(Position);
} }
} }
} }

View File

@ -53,20 +53,27 @@ namespace RecrownedAthenaeum.Camera
/// </summary> /// </summary>
protected GraphicsDevice graphicsDevice; protected GraphicsDevice graphicsDevice;
/// <summary>
/// The basic effect that contains the transformations.
/// </summary>
public BasicEffect BasicEffect { get; private set; }
/// <summary> /// <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. /// 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> /// </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> /// <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) public Camera3D(GraphicsDevice graphicsDevice = null)
{ {
this.graphicsDevice = graphicsDevice ?? (Configuration.GraphicsDeviceManager.GraphicsDevice); graphicsDevice = graphicsDevice ?? (Configuration.GraphicsDeviceManager.GraphicsDevice);
this.graphicsDevice = graphicsDevice;
worldMatrix = Matrix.Identity; worldMatrix = Matrix.Identity;
lookAt = Vector3.Forward; lookAt = Vector3.Forward;
upDirection = Vector3.Up; upDirection = Vector3.Up;
GraphicsDevice gDevice = this.graphicsDevice;
projectionMatrix = Matrix.Identity; projectionMatrix = Matrix.Identity;
BasicEffect = new BasicEffect(graphicsDevice);
BasicEffect.TextureEnabled = true;
Apply(); Apply();
} }
@ -77,6 +84,10 @@ namespace RecrownedAthenaeum.Camera
{ {
ViewMatrix = Matrix.CreateLookAt(position, lookAt, upDirection); ViewMatrix = Matrix.CreateLookAt(position, lookAt, upDirection);
TransformationMatrix = projectionMatrix * ViewMatrix * worldMatrix; TransformationMatrix = projectionMatrix * ViewMatrix * worldMatrix;
BasicEffect.World = worldMatrix;
BasicEffect.View = ViewMatrix;
BasicEffect.Projection = projectionMatrix;
} }
/// <summary> /// <summary>