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>
public Matrix Matrix { get; private set; }
private GraphicsDevice graphicsDevice = Configuration.graphicsDeviceManager.GraphicsDevice;
private GraphicsDevice graphicsDevice;
/// <summary>
/// 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>
public Camera2D(GraphicsDevice graphicsDevice = null)
{
if (graphicsDevice != null) this.graphicsDevice = 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.");
this.graphicsDevice = graphicsDevice ?? (Configuration.GraphicsDeviceManager.GraphicsDevice);
Zoom = 1f;
Position.X = this.graphicsDevice.Viewport.Width * 0.5f;

View File

@ -1,4 +1,6 @@
using Microsoft.Xna.Framework;
using RecrownedAthenaeum.Camera;
using System;
namespace RecrownedAthenaeum
{
@ -7,9 +9,19 @@ namespace RecrownedAthenaeum
/// </summary>
public static class Configuration
{
private static GraphicsDeviceManager graphicsDeviceManager;
/// <summary>
/// The graphics device that will be used by default.
/// </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;
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())));
NinePatch ninePatch = new NinePatch(texture, ninePatchData.left, ninePatchData.right, ninePatchData.bottom, ninePatchData.top);

View File

@ -16,7 +16,7 @@ namespace RecrownedAthenaeum.ContentReaders
Texture2D atlasTexture;
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()));
atlasData = JsonConvert.DeserializeObject<TextureAtlasData>(serialized);

View File

@ -20,21 +20,22 @@ namespace RecrownedAthenaeum.Render
BasicEffect basicEffect;
PrimitiveType primitiveType;
int verticesPerPrimitive;
GraphicsDevice graphicsDevice = Configuration.graphicsDeviceManager.GraphicsDevice;
GraphicsDevice graphicsDevice;
bool began;
bool disposed;
/// <summary>
/// Creates a batch used to draw primitives.
/// </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="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;
if (this.graphicsDevice == null) throw new ArgumentNullException("Graphics device can't be null in setup and argument. One must not be null for use.");
basicEffect = new BasicEffect(graphicsDevice);
this.graphicsDevice = graphicsDevice ?? (Configuration.GraphicsDeviceManager.GraphicsDevice);
camera = camera ?? (Configuration.Camera2D);
basicEffect = new BasicEffect(this.graphicsDevice);
basicEffect.VertexColorEnabled = true;
basicEffect.View = camera.Matrix;
@ -93,9 +94,8 @@ namespace RecrownedAthenaeum.Render
throw new InvalidOperationException("Buffer size isn't large enough.");
}
}
vertices[bufferPosition] = new VertexPositionColor(new Vector3(vertex, 0), color);
bufferPosition++;
vertices.Add(new VertexPositionColor(new Vector3(vertex, 0), color));
bufferPosition = vertices.Count;
}
/// <summary>

View File

@ -29,7 +29,7 @@ namespace RecrownedAthenaeum.Render
/// Begins the render batch.
/// </summary>
/// <param name="filled">Whether or not to fill the rectangle.</param>
public void Begin(bool filled)
public void Begin(bool filled = false)
{
filling = filled;
if (began) throw new InvalidOperationException("Cannot begin twice.");
@ -75,7 +75,8 @@ namespace RecrownedAthenaeum.Render
primitiveBatch.AddVertex(corners[2], color);
primitiveBatch.AddVertex(corners[0], color);
primitiveBatch.AddVertex(corners[3], color);
} else
}
else
{
primitiveBatch.AddVertex(corners[0], 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.
/// </summary>
public event ShowFirstScreen ShowFirstScreenEvent;
private GraphicsDeviceManager graphics = Configuration.graphicsDeviceManager;
private GraphicsDeviceManager graphics;
private Screen previousScreen;
private RenderTarget2D previousScreenRenderTarget;
private Screen currentScreen;
@ -63,13 +63,15 @@ namespace RecrownedAthenaeum.ScreenSystem
/// <summary>
/// Creates a screen manager that helps manage multiple screens and their transitions.
/// </summary>
/// <param name="camera">The camera to be used to perform the correct translations and transformations.</param>
/// <param name="graphicsDeviceManager">The graphics device manager to be used.</param>
public ScreenManager(Camera2D camera, GraphicsDeviceManager graphicsDeviceManager = null)
/// <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. Will use default set in <see cref="Configuration"/> if left null.</param>
public ScreenManager(Camera2D camera = null, GraphicsDeviceManager graphicsDeviceManager = null)
{
if (graphicsDeviceManager != null) this.graphics = graphicsDeviceManager;
if (graphics == null) throw new ArgumentNullException("Graphics device manager argument cannot be null if setup's graphics device manager is also null.");
this.camera = camera;
if (camera == null) camera = Configuration.Camera2D;
if (graphicsDeviceManager == null) graphicsDeviceManager = Configuration.GraphicsDeviceManager;
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>

View File

@ -69,7 +69,7 @@ namespace RecrownedAthenaeum.SpecialTypes
/// <returns>The texture from the region.</returns>
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);
}
@ -194,7 +194,7 @@ namespace RecrownedAthenaeum.SpecialTypes
if (regionTexture == null)
{
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);
atlasTexture.GetData(0, sourceRectangle, data, 0, sourceRectangle.Width * sourceRectangle.Height);
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); } }
/// <summary>
/// Overall scale.
/// Sets scale of X and Y.
/// </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>
/// Constructs an image given a texture.

View File

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

View File

@ -2,6 +2,7 @@
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using RecrownedAthenaeum.Input;
using RecrownedAthenaeum.Render;
using System;
namespace RecrownedAthenaeum.UI.Modular
@ -12,6 +13,16 @@ namespace RecrownedAthenaeum.UI.Modular
/// </summary>
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>
/// Bounds of this module.
/// </summary>
@ -51,7 +62,7 @@ namespace RecrownedAthenaeum.UI.Modular
/// <param name="batch">Batch used to draw.</param>
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>

View File

@ -15,23 +15,25 @@ namespace RecrownedAthenaeum.UI.Modular
{
List<UIModule> modules = new List<UIModule>();
Rectangle scissorBounds;
RasterizerState scissorRasterizer = new RasterizerState();
RasterizerState scissorRasterizer;
/// <summary>
/// Camera used by the module for cropping.
/// </summary>
public Camera2D Camera { get; set; }
public Camera2D camera;
/// <summary>
/// Creates a module group.
/// </summary>
/// <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)
{
if (crop && camera == null) throw new ArgumentException("Cannot crop without camera.");
Camera = camera;
if (crop && camera == null) camera = Configuration.Camera2D;
this.camera = camera;
if (crop)
{
scissorRasterizer = new RasterizerState();
scissorRasterizer.ScissorTestEnable = true;
scissorBounds = new Rectangle();
}
@ -46,7 +48,7 @@ namespace RecrownedAthenaeum.UI.Modular
if (scissorBounds != null)
{
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.Height = bounds.Height;
scissorBounds.X = bounds.X;
@ -70,7 +72,7 @@ namespace RecrownedAthenaeum.UI.Modular
{
batch.GraphicsDevice.ScissorRectangle = scissorBounds;
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>
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;
this.graphicsDevice = graphicsDevice;
this.selectedSkinPath = path;