Began trying to fix primitive batch. Added a default camera to the configuration.

This commit is contained in:
Harrison Deng 2019-01-30 07:46:58 -06:00
parent 8387cbc604
commit e1e2bbb3d7
13 changed files with 68 additions and 40 deletions

View File

@ -24,7 +24,7 @@ namespace RecrownedAthenaeum.Camera
/// </summary> /// </summary>
public Matrix Matrix { get; private set; } public Matrix Matrix { get; private set; }
private GraphicsDevice graphicsDevice = Configuration.graphicsDeviceManager.GraphicsDevice; private GraphicsDevice graphicsDevice;
/// <summary> /// <summary>
/// Constructs 2D camera. /// Constructs 2D camera.
@ -32,8 +32,7 @@ namespace RecrownedAthenaeum.Camera
/// <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 Camera2D(GraphicsDevice graphicsDevice = null) public Camera2D(GraphicsDevice graphicsDevice = null)
{ {
if (graphicsDevice != null) this.graphicsDevice = graphicsDevice; this.graphicsDevice = graphicsDevice ?? (Configuration.GraphicsDeviceManager.GraphicsDevice);
if (this.graphicsDevice == null) throw new ArgumentNullException("Graphics device can't be null in setup and argument. One must not be null for use.");
Zoom = 1f; Zoom = 1f;
Position.X = this.graphicsDevice.Viewport.Width * 0.5f; Position.X = this.graphicsDevice.Viewport.Width * 0.5f;

View File

@ -1,4 +1,6 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using RecrownedAthenaeum.Camera;
using System;
namespace RecrownedAthenaeum namespace RecrownedAthenaeum
{ {
@ -7,9 +9,19 @@ namespace RecrownedAthenaeum
/// </summary> /// </summary>
public static class Configuration public static class Configuration
{ {
private static GraphicsDeviceManager graphicsDeviceManager;
/// <summary> /// <summary>
/// The graphics device that will be used by default. /// The graphics device that will be used by default.
/// </summary> /// </summary>
public static GraphicsDeviceManager graphicsDeviceManager; public static GraphicsDeviceManager GraphicsDeviceManager { set { graphicsDeviceManager = value; } get { if (graphicsDeviceManager == null) throw new InvalidOperationException("Graphics device manager property requested as substitute but configuration does not have one."); return graphicsDeviceManager; } }
private static Camera2D camera2D;
/// <summary>
/// 2D camera to be used by default.
/// </summary>
public static Camera2D Camera2D { set { camera2D = value; } get { if (camera2D == null) throw new InvalidOperationException("2D camera property requested as substitute but configuration does not have one."); return camera2D; } }
} }
} }

View File

@ -15,7 +15,7 @@ namespace RecrownedAthenaeum.ContentReaders
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(Configuration.GraphicsDeviceManager.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

@ -16,7 +16,7 @@ namespace RecrownedAthenaeum.ContentReaders
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(Configuration.GraphicsDeviceManager.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

@ -20,21 +20,22 @@ namespace RecrownedAthenaeum.Render
BasicEffect basicEffect; BasicEffect basicEffect;
PrimitiveType primitiveType; PrimitiveType primitiveType;
int verticesPerPrimitive; int verticesPerPrimitive;
GraphicsDevice graphicsDevice = Configuration.graphicsDeviceManager.GraphicsDevice; GraphicsDevice graphicsDevice;
bool began; bool began;
bool disposed; bool disposed;
/// <summary> /// <summary>
/// Creates a batch used to draw primitives. /// Creates a batch used to draw primitives.
/// </summary> /// </summary>
/// <param name="camera">The current camera being used.</param> /// <param name="camera">The current camera being used. Will use default set in <see cref="Configuration"/> if left null.</param>
/// <param name="graphicsDevice">The graphics device used to draw the primitives. Will be using <see cref="Configuration"/>'s graphics device from graphics device manager if null. Default is null.</param> /// <param name="graphicsDevice">The graphics device used to draw the primitives. Will be using <see cref="Configuration"/>'s graphics device from graphics device manager if null. Default is null.</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> /// <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(Camera2D camera, GraphicsDevice graphicsDevice = null, int verticesPerBatch = 500) public PrimitiveBatch(Camera2D camera = null, GraphicsDevice graphicsDevice = null, int verticesPerBatch = 500)
{ {
if (graphicsDevice != null) this.graphicsDevice = graphicsDevice; this.graphicsDevice = graphicsDevice ?? (Configuration.GraphicsDeviceManager.GraphicsDevice);
if (this.graphicsDevice == null) throw new ArgumentNullException("Graphics device can't be null in setup and argument. One must not be null for use."); camera = camera ?? (Configuration.Camera2D);
basicEffect = new BasicEffect(graphicsDevice);
basicEffect = new BasicEffect(this.graphicsDevice);
basicEffect.VertexColorEnabled = true; basicEffect.VertexColorEnabled = true;
basicEffect.View = camera.Matrix; basicEffect.View = camera.Matrix;
@ -93,9 +94,8 @@ namespace RecrownedAthenaeum.Render
throw new InvalidOperationException("Buffer size isn't large enough."); throw new InvalidOperationException("Buffer size isn't large enough.");
} }
} }
vertices[bufferPosition] = new VertexPositionColor(new Vector3(vertex, 0), color); vertices.Add(new VertexPositionColor(new Vector3(vertex, 0), color));
bufferPosition = vertices.Count;
bufferPosition++;
} }
/// <summary> /// <summary>

View File

@ -29,7 +29,7 @@ namespace RecrownedAthenaeum.Render
/// Begins the render batch. /// Begins the render batch.
/// </summary> /// </summary>
/// <param name="filled">Whether or not to fill the rectangle.</param> /// <param name="filled">Whether or not to fill the rectangle.</param>
public void Begin(bool filled) public void Begin(bool filled = false)
{ {
filling = filled; filling = filled;
if (began) throw new InvalidOperationException("Cannot begin twice."); if (began) throw new InvalidOperationException("Cannot begin twice.");
@ -75,7 +75,8 @@ namespace RecrownedAthenaeum.Render
primitiveBatch.AddVertex(corners[2], color); primitiveBatch.AddVertex(corners[2], color);
primitiveBatch.AddVertex(corners[0], color); primitiveBatch.AddVertex(corners[0], color);
primitiveBatch.AddVertex(corners[3], color); primitiveBatch.AddVertex(corners[3], color);
} else }
else
{ {
primitiveBatch.AddVertex(corners[0], color); primitiveBatch.AddVertex(corners[0], color);
primitiveBatch.AddVertex(corners[1], color); primitiveBatch.AddVertex(corners[1], color);

View File

@ -23,7 +23,7 @@ namespace RecrownedAthenaeum.ScreenSystem
/// Called when the first loading screen is done, and needs to show the landing screen. /// Called when the first loading screen is done, and needs to show the landing screen.
/// </summary> /// </summary>
public event ShowFirstScreen ShowFirstScreenEvent; public event ShowFirstScreen ShowFirstScreenEvent;
private GraphicsDeviceManager graphics = Configuration.graphicsDeviceManager; private GraphicsDeviceManager graphics;
private Screen previousScreen; private Screen previousScreen;
private RenderTarget2D previousScreenRenderTarget; private RenderTarget2D previousScreenRenderTarget;
private Screen currentScreen; private Screen currentScreen;
@ -63,13 +63,15 @@ namespace RecrownedAthenaeum.ScreenSystem
/// <summary> /// <summary>
/// Creates a screen manager that helps manage multiple screens and their transitions. /// Creates a screen manager that helps manage multiple screens and their transitions.
/// </summary> /// </summary>
/// <param name="camera">The camera to be used to perform the correct translations and transformations.</param> /// <param name="camera">The camera to be used to perform the correct translations and transformations. Will use default set in <see cref="Configuration"/> if left null.</param>
/// <param name="graphicsDeviceManager">The graphics device manager to be used.</param> /// <param name="graphicsDeviceManager">The graphics device manager to be used. Will use default set in <see cref="Configuration"/> if left null.</param>
public ScreenManager(Camera2D camera, GraphicsDeviceManager graphicsDeviceManager = null) public ScreenManager(Camera2D camera = null, GraphicsDeviceManager graphicsDeviceManager = null)
{ {
if (graphicsDeviceManager != null) this.graphics = graphicsDeviceManager; if (camera == null) camera = Configuration.Camera2D;
if (graphics == null) throw new ArgumentNullException("Graphics device manager argument cannot be null if setup's graphics device manager is also null."); if (graphicsDeviceManager == null) graphicsDeviceManager = Configuration.GraphicsDeviceManager;
this.camera = camera;
graphics = graphicsDeviceManager ?? throw new ArgumentNullException("Graphics device manager argument cannot be null if setup's graphics device manager is also null.");
this.camera = camera ?? throw new ArgumentNullException("2d camera argument cannot be null if setup's 2d camera is also null.");
} }
/// <summary> /// <summary>

View File

@ -69,7 +69,7 @@ namespace RecrownedAthenaeum.SpecialTypes
/// <returns>The texture from the region.</returns> /// <returns>The texture from the region.</returns>
public Texture2D ObtainRegionAsTexture(string name, GraphicsDevice graphicsDevice = null) public Texture2D ObtainRegionAsTexture(string name, GraphicsDevice graphicsDevice = null)
{ {
if (graphicsDevice == null) graphicsDevice = Configuration.graphicsDeviceManager.GraphicsDevice; if (graphicsDevice == null) graphicsDevice = Configuration.GraphicsDeviceManager.GraphicsDevice;
return dictionaryOfRegions[name].AsTexture2D(graphicsDevice); return dictionaryOfRegions[name].AsTexture2D(graphicsDevice);
} }
@ -194,7 +194,7 @@ namespace RecrownedAthenaeum.SpecialTypes
if (regionTexture == null) if (regionTexture == null)
{ {
Color[] data = new Color[sourceRectangle.Width * sourceRectangle.Height]; Color[] data = new Color[sourceRectangle.Width * sourceRectangle.Height];
if (graphicsDevice == null) graphicsDevice = Configuration.graphicsDeviceManager.GraphicsDevice; if (graphicsDevice == null) graphicsDevice = Configuration.GraphicsDeviceManager.GraphicsDevice;
regionTexture = new Texture2D(graphicsDevice, sourceRectangle.Width, sourceRectangle.Height); regionTexture = new Texture2D(graphicsDevice, sourceRectangle.Width, sourceRectangle.Height);
atlasTexture.GetData(0, sourceRectangle, data, 0, sourceRectangle.Width * sourceRectangle.Height); atlasTexture.GetData(0, sourceRectangle, data, 0, sourceRectangle.Width * sourceRectangle.Height);
regionTexture.SetData(data); regionTexture.SetData(data);

View File

@ -31,9 +31,9 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
public float ScaleY { get { return (float)bounds.Height / Texture.Height; } set { bounds.Height = (int)(Texture.Height * value); } } public float ScaleY { get { return (float)bounds.Height / Texture.Height; } set { bounds.Height = (int)(Texture.Height * value); } }
/// <summary> /// <summary>
/// Overall scale. /// Sets scale of X and Y.
/// </summary> /// </summary>
public float Scale { set { bounds.Height = (int)(Texture.Height * value); bounds.Width = (int)(Texture.Width * value); } } public float Scale { set { ScaleY = value; ScaleX = value; } }
/// <summary> /// <summary>
/// Constructs an image given a texture. /// Constructs an image given a texture.

View File

@ -2,6 +2,7 @@
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.UI.SkinSystem; using RecrownedAthenaeum.UI.SkinSystem;
using RecrownedAthenaeum.UI.SkinSystem.Definitions; using RecrownedAthenaeum.UI.SkinSystem.Definitions;
using System;
using System.Text; using System.Text;
namespace RecrownedAthenaeum.UI.Modular.Modules namespace RecrownedAthenaeum.UI.Modular.Modules
@ -41,7 +42,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
/// <summary> /// <summary>
/// The string to be displayed. /// The string to be displayed.
/// </summary> /// </summary>
public string Content { get { return originalText; } set { if (value == null) value = ellipsis; modifiedTextSize = font.MeasureString(value); originalText = value; displayedText = value; } } public string Content { get { return originalText; } set { originalText = value; if (value == null) value = ellipsis; modifiedTextSize = font.MeasureString(value); displayedText = value; } }
/// <summary> /// <summary>
/// Creates a UI text object. /// Creates a UI text object.
@ -50,8 +51,8 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
/// <param name="content">The string for the text.</param> /// <param name="content">The string for the text.</param>
public Text(SpriteFont font, string content = null) public Text(SpriteFont font, string content = null)
{ {
this.font = font ?? throw new ArgumentNullException("Font cannot be null.");
Content = content; Content = content;
this.font = font;
} }
/// <summary> /// <summary>
@ -129,7 +130,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
/// </summary> /// </summary>
public void ResetToOriginalText() public void ResetToOriginalText()
{ {
ModifiedText = originalText; Content = originalText;
} }
/// <summary> /// <summary>

View File

@ -2,6 +2,7 @@
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Input;
using RecrownedAthenaeum.Input; using RecrownedAthenaeum.Input;
using RecrownedAthenaeum.Render;
using System; using System;
namespace RecrownedAthenaeum.UI.Modular namespace RecrownedAthenaeum.UI.Modular
@ -12,6 +13,16 @@ namespace RecrownedAthenaeum.UI.Modular
/// </summary> /// </summary>
public class UIModule : IInputListener public class UIModule : IInputListener
{ {
private RectangleRenderer renderer;
private PrimitiveBatch primitiveBatch;
private bool debug = false;
/// <summary>
/// Draws rectangle using the bounds of this module.
/// </summary>
public bool Debugging { set { if (value) { if (renderer == null) renderer = new RectangleRenderer(primitiveBatch = new PrimitiveBatch()); } else { primitiveBatch.Dispose(); primitiveBatch = null; renderer = null; } } get { return debug; } }
/// <summary> /// <summary>
/// Bounds of this module. /// Bounds of this module.
/// </summary> /// </summary>
@ -51,7 +62,7 @@ namespace RecrownedAthenaeum.UI.Modular
/// <param name="batch">Batch used to draw.</param> /// <param name="batch">Batch used to draw.</param>
public virtual void Draw(SpriteBatch batch) public virtual void Draw(SpriteBatch batch)
{ {
if (Debugging) { renderer.Begin(false); renderer.DrawRectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height, Color.Red); renderer.End(); }
} }
/// <summary> /// <summary>

View File

@ -15,23 +15,25 @@ namespace RecrownedAthenaeum.UI.Modular
{ {
List<UIModule> modules = new List<UIModule>(); List<UIModule> modules = new List<UIModule>();
Rectangle scissorBounds; Rectangle scissorBounds;
RasterizerState scissorRasterizer = new RasterizerState(); RasterizerState scissorRasterizer;
/// <summary> /// <summary>
/// Camera used by the module for cropping. /// Camera used by the module for cropping.
/// </summary> /// </summary>
public Camera2D Camera { get; set; } public Camera2D camera;
/// <summary> /// <summary>
/// Creates a module group. /// Creates a module group.
/// </summary> /// </summary>
/// <param name="crop">Whether or not to crop out of bounds. Default is false.</param> /// <param name="crop">Whether or not to crop out of bounds. Default is false.</param>
/// <param name="camera">What camera to use for cropping. Default is null.</param> /// <param name="camera">What camera to use for cropping. Default is null and will use <see cref="Configuration"/>'s camera if crop is enabled.</param>
public UIModuleGroup(bool crop = false, Camera2D camera = null) public UIModuleGroup(bool crop = false, Camera2D camera = null)
{ {
if (crop && camera == null) throw new ArgumentException("Cannot crop without camera."); if (crop && camera == null) camera = Configuration.Camera2D;
Camera = camera; this.camera = camera;
if (crop) if (crop)
{ {
scissorRasterizer = new RasterizerState();
scissorRasterizer.ScissorTestEnable = true; scissorRasterizer.ScissorTestEnable = true;
scissorBounds = new Rectangle(); scissorBounds = new Rectangle();
} }
@ -46,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?.Matrix); batch.Begin(SpriteSortMode.Deferred, null, null, null, scissorRasterizer, null, camera?.Matrix);
scissorBounds.Width = bounds.Width; scissorBounds.Width = bounds.Width;
scissorBounds.Height = bounds.Height; scissorBounds.Height = bounds.Height;
scissorBounds.X = bounds.X; scissorBounds.X = bounds.X;
@ -70,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?.Matrix); batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, camera?.Matrix);
} }
} }

View File

@ -116,7 +116,7 @@ namespace RecrownedAthenaeum.UI.SkinSystem
/// <param name="path">The path pointing to the file with the extension "<see cref="EXTENSION"/>".</param> /// <param name="path">The path pointing to the file with the extension "<see cref="EXTENSION"/>".</param>
public void LoadSkin(SkinData skinData, string path, GraphicsDevice graphicsDevice = null) public void LoadSkin(SkinData skinData, string path, GraphicsDevice graphicsDevice = null)
{ {
if (graphicsDevice == null) graphicsDevice = Configuration.graphicsDeviceManager.GraphicsDevice; if (graphicsDevice == null) graphicsDevice = Configuration.GraphicsDeviceManager.GraphicsDevice;
action = Action.LOAD; action = Action.LOAD;
this.graphicsDevice = graphicsDevice; this.graphicsDevice = graphicsDevice;
this.selectedSkinPath = path; this.selectedSkinPath = path;