From e1e2bbb3d728552acf33b01d59732a5c8b469829 Mon Sep 17 00:00:00 2001 From: Recrown Date: Wed, 30 Jan 2019 07:46:58 -0600 Subject: [PATCH] Began trying to fix primitive batch. Added a default camera to the configuration. --- RecrownedAthenaeum/Camera/Camera2D.cs | 5 ++--- RecrownedAthenaeum/Configuration.cs | 14 +++++++++++++- .../ContentReaders/NinePatchDataReader.cs | 2 +- .../ContentReaders/TextureAtlasDataReader.cs | 2 +- RecrownedAthenaeum/Render/PrimitiveBatch.cs | 18 +++++++++--------- RecrownedAthenaeum/Render/RectangleRenderer.cs | 5 +++-- .../ScreenSystem/ScreenManager.cs | 16 +++++++++------- .../SpecialTypes/TextureAtlas.cs | 4 ++-- RecrownedAthenaeum/UI/Modular/Modules/Image.cs | 4 ++-- RecrownedAthenaeum/UI/Modular/Modules/Text.cs | 7 ++++--- RecrownedAthenaeum/UI/Modular/UIModule.cs | 13 ++++++++++++- RecrownedAthenaeum/UI/Modular/UIModuleGroup.cs | 16 +++++++++------- .../UI/SkinSystem/SkinManager.cs | 2 +- 13 files changed, 68 insertions(+), 40 deletions(-) diff --git a/RecrownedAthenaeum/Camera/Camera2D.cs b/RecrownedAthenaeum/Camera/Camera2D.cs index 2a6679d..98a980e 100644 --- a/RecrownedAthenaeum/Camera/Camera2D.cs +++ b/RecrownedAthenaeum/Camera/Camera2D.cs @@ -24,7 +24,7 @@ namespace RecrownedAthenaeum.Camera /// public Matrix Matrix { get; private set; } - private GraphicsDevice graphicsDevice = Configuration.graphicsDeviceManager.GraphicsDevice; + private GraphicsDevice graphicsDevice; /// /// Constructs 2D camera. @@ -32,8 +32,7 @@ namespace RecrownedAthenaeum.Camera /// The graphics device to use. Will use graphics device from 's graphics device manager if this is null which it is by default. 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; diff --git a/RecrownedAthenaeum/Configuration.cs b/RecrownedAthenaeum/Configuration.cs index a1c05c6..c87ec2c 100644 --- a/RecrownedAthenaeum/Configuration.cs +++ b/RecrownedAthenaeum/Configuration.cs @@ -1,4 +1,6 @@ using Microsoft.Xna.Framework; +using RecrownedAthenaeum.Camera; +using System; namespace RecrownedAthenaeum { @@ -7,9 +9,19 @@ namespace RecrownedAthenaeum /// public static class Configuration { + + private static GraphicsDeviceManager graphicsDeviceManager; + /// /// The graphics device that will be used by default. /// - 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; + + /// + /// 2D camera to be used by default. + /// + 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; } } } } diff --git a/RecrownedAthenaeum/ContentReaders/NinePatchDataReader.cs b/RecrownedAthenaeum/ContentReaders/NinePatchDataReader.cs index 38ae1f4..0ea3058 100644 --- a/RecrownedAthenaeum/ContentReaders/NinePatchDataReader.cs +++ b/RecrownedAthenaeum/ContentReaders/NinePatchDataReader.cs @@ -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(Encoding.ASCII.GetString(input.ReadBytes(input.ReadInt32()))); NinePatch ninePatch = new NinePatch(texture, ninePatchData.left, ninePatchData.right, ninePatchData.bottom, ninePatchData.top); diff --git a/RecrownedAthenaeum/ContentReaders/TextureAtlasDataReader.cs b/RecrownedAthenaeum/ContentReaders/TextureAtlasDataReader.cs index cbf9413..79d57b5 100644 --- a/RecrownedAthenaeum/ContentReaders/TextureAtlasDataReader.cs +++ b/RecrownedAthenaeum/ContentReaders/TextureAtlasDataReader.cs @@ -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(serialized); diff --git a/RecrownedAthenaeum/Render/PrimitiveBatch.cs b/RecrownedAthenaeum/Render/PrimitiveBatch.cs index ba12695..83cf156 100644 --- a/RecrownedAthenaeum/Render/PrimitiveBatch.cs +++ b/RecrownedAthenaeum/Render/PrimitiveBatch.cs @@ -20,21 +20,22 @@ namespace RecrownedAthenaeum.Render BasicEffect basicEffect; PrimitiveType primitiveType; int verticesPerPrimitive; - GraphicsDevice graphicsDevice = Configuration.graphicsDeviceManager.GraphicsDevice; + GraphicsDevice graphicsDevice; bool began; bool disposed; /// /// Creates a batch used to draw primitives. /// - /// The current camera being used. + /// The current camera being used. Will use default set in if left null. /// The graphics device used to draw the primitives. Will be using 's graphics device from graphics device manager if null. Default is null. /// 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. - 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; } /// diff --git a/RecrownedAthenaeum/Render/RectangleRenderer.cs b/RecrownedAthenaeum/Render/RectangleRenderer.cs index c3655e2..2e53330 100644 --- a/RecrownedAthenaeum/Render/RectangleRenderer.cs +++ b/RecrownedAthenaeum/Render/RectangleRenderer.cs @@ -29,7 +29,7 @@ namespace RecrownedAthenaeum.Render /// Begins the render batch. /// /// Whether or not to fill the rectangle. - 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); diff --git a/RecrownedAthenaeum/ScreenSystem/ScreenManager.cs b/RecrownedAthenaeum/ScreenSystem/ScreenManager.cs index 73f1936..a3e2859 100644 --- a/RecrownedAthenaeum/ScreenSystem/ScreenManager.cs +++ b/RecrownedAthenaeum/ScreenSystem/ScreenManager.cs @@ -23,7 +23,7 @@ namespace RecrownedAthenaeum.ScreenSystem /// Called when the first loading screen is done, and needs to show the landing screen. /// 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 /// /// Creates a screen manager that helps manage multiple screens and their transitions. /// - /// The camera to be used to perform the correct translations and transformations. - /// The graphics device manager to be used. - public ScreenManager(Camera2D camera, GraphicsDeviceManager graphicsDeviceManager = null) + /// The camera to be used to perform the correct translations and transformations. Will use default set in if left null. + /// The graphics device manager to be used. Will use default set in if left null. + 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."); } /// diff --git a/RecrownedAthenaeum/SpecialTypes/TextureAtlas.cs b/RecrownedAthenaeum/SpecialTypes/TextureAtlas.cs index 6d8af5c..274ac62 100644 --- a/RecrownedAthenaeum/SpecialTypes/TextureAtlas.cs +++ b/RecrownedAthenaeum/SpecialTypes/TextureAtlas.cs @@ -69,7 +69,7 @@ namespace RecrownedAthenaeum.SpecialTypes /// The texture from the region. 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); diff --git a/RecrownedAthenaeum/UI/Modular/Modules/Image.cs b/RecrownedAthenaeum/UI/Modular/Modules/Image.cs index dba5e22..9e21f03 100644 --- a/RecrownedAthenaeum/UI/Modular/Modules/Image.cs +++ b/RecrownedAthenaeum/UI/Modular/Modules/Image.cs @@ -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); } } /// - /// Overall scale. + /// Sets scale of X and Y. /// - public float Scale { set { bounds.Height = (int)(Texture.Height * value); bounds.Width = (int)(Texture.Width * value); } } + public float Scale { set { ScaleY = value; ScaleX = value; } } /// /// Constructs an image given a texture. diff --git a/RecrownedAthenaeum/UI/Modular/Modules/Text.cs b/RecrownedAthenaeum/UI/Modular/Modules/Text.cs index c90297e..55bf217 100644 --- a/RecrownedAthenaeum/UI/Modular/Modules/Text.cs +++ b/RecrownedAthenaeum/UI/Modular/Modules/Text.cs @@ -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 /// /// The string to be displayed. /// - 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; } } /// /// Creates a UI text object. @@ -50,8 +51,8 @@ namespace RecrownedAthenaeum.UI.Modular.Modules /// The string for the text. public Text(SpriteFont font, string content = null) { + this.font = font ?? throw new ArgumentNullException("Font cannot be null."); Content = content; - this.font = font; } /// @@ -129,7 +130,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules /// public void ResetToOriginalText() { - ModifiedText = originalText; + Content = originalText; } /// diff --git a/RecrownedAthenaeum/UI/Modular/UIModule.cs b/RecrownedAthenaeum/UI/Modular/UIModule.cs index f41445d..0e75698 100644 --- a/RecrownedAthenaeum/UI/Modular/UIModule.cs +++ b/RecrownedAthenaeum/UI/Modular/UIModule.cs @@ -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 /// public class UIModule : IInputListener { + private RectangleRenderer renderer; + private PrimitiveBatch primitiveBatch; + + private bool debug = false; + + /// + /// Draws rectangle using the bounds of this module. + /// + 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; } } + /// /// Bounds of this module. /// @@ -51,7 +62,7 @@ namespace RecrownedAthenaeum.UI.Modular /// Batch used to draw. 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(); } } /// diff --git a/RecrownedAthenaeum/UI/Modular/UIModuleGroup.cs b/RecrownedAthenaeum/UI/Modular/UIModuleGroup.cs index b56796a..5013f86 100644 --- a/RecrownedAthenaeum/UI/Modular/UIModuleGroup.cs +++ b/RecrownedAthenaeum/UI/Modular/UIModuleGroup.cs @@ -15,23 +15,25 @@ namespace RecrownedAthenaeum.UI.Modular { List modules = new List(); Rectangle scissorBounds; - RasterizerState scissorRasterizer = new RasterizerState(); + RasterizerState scissorRasterizer; + /// /// Camera used by the module for cropping. /// - public Camera2D Camera { get; set; } + public Camera2D camera; /// /// Creates a module group. /// /// Whether or not to crop out of bounds. Default is false. - /// What camera to use for cropping. Default is null. + /// What camera to use for cropping. Default is null and will use 's camera if crop is enabled. 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); } } diff --git a/RecrownedAthenaeum/UI/SkinSystem/SkinManager.cs b/RecrownedAthenaeum/UI/SkinSystem/SkinManager.cs index 2047c48..a20c608 100644 --- a/RecrownedAthenaeum/UI/SkinSystem/SkinManager.cs +++ b/RecrownedAthenaeum/UI/SkinSystem/SkinManager.cs @@ -116,7 +116,7 @@ namespace RecrownedAthenaeum.UI.SkinSystem /// The path pointing to the file with the extension "". 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;