From c59252dbbf74c91b1664f4740375f68acc12d0de Mon Sep 17 00:00:00 2001 From: Recrown Date: Sat, 9 Mar 2019 02:03:05 -0600 Subject: [PATCH] Added better system for beginning sprite batches for easier consistency. --- RecrownedAthenaeum/Configuration.cs | 11 +++++--- .../ScreenSystem/ScreenManager.cs | 26 ++++++++++++++++--- .../UI/Modular/UIModuleGroup.cs | 11 +++++--- 3 files changed, 39 insertions(+), 9 deletions(-) diff --git a/RecrownedAthenaeum/Configuration.cs b/RecrownedAthenaeum/Configuration.cs index c87ec2c..416134e 100644 --- a/RecrownedAthenaeum/Configuration.cs +++ b/RecrownedAthenaeum/Configuration.cs @@ -1,5 +1,6 @@ using Microsoft.Xna.Framework; using RecrownedAthenaeum.Camera; +using RecrownedAthenaeum.ScreenSystem; using System; namespace RecrownedAthenaeum @@ -9,19 +10,23 @@ namespace RecrownedAthenaeum /// public static class Configuration { - private static GraphicsDeviceManager graphicsDeviceManager; - /// /// The graphics device that will be used by default. /// 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; + 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; } } + + private static BeginBatch beginBatchFunction; + /// + /// The begin sprite batch to use for custom begins and consistency. + /// + public static BeginBatch BeginBatchFunction { set { beginBatchFunction = value; } get { if (beginBatchFunction == null) throw new InvalidOperationException("No default begin batch has been set yet has been requested."); return beginBatchFunction; } } } } diff --git a/RecrownedAthenaeum/ScreenSystem/ScreenManager.cs b/RecrownedAthenaeum/ScreenSystem/ScreenManager.cs index a4c9d80..50a389a 100644 --- a/RecrownedAthenaeum/ScreenSystem/ScreenManager.cs +++ b/RecrownedAthenaeum/ScreenSystem/ScreenManager.cs @@ -12,6 +12,12 @@ namespace RecrownedAthenaeum.ScreenSystem /// The screen to show after the loading screen. public delegate void ShowFirstScreen(Screen screen); + /// + /// Custom spritebatch begin call. + /// + /// + public delegate void BeginBatch(SpriteBatch spriteBatch); + /// /// A manager for screens. Helps with transitions and updating screens as well as resizes. /// @@ -23,6 +29,12 @@ namespace RecrownedAthenaeum.ScreenSystem /// Called when the first loading screen is done, and needs to show the landing screen. /// public event ShowFirstScreen ShowFirstScreenEvent; + + /// + /// The function to call that begins the batch. + /// + public BeginBatch beginBatchFunc; + private GraphicsDeviceManager graphics; private Screen previousScreen; private RenderTarget2D previousScreenRenderTarget; @@ -65,13 +77,16 @@ namespace RecrownedAthenaeum.ScreenSystem /// /// 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) + /// The function to call to begin a batch to be used generally. Will use the built-in one in screen manager if not provided. + public ScreenManager(Camera2D camera = null, GraphicsDeviceManager graphicsDeviceManager = null, BeginBatch beginBatchFunction = null) { + if (beginBatchFunction == null) beginBatchFunction = BasicBeginBatch; 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."); + beginBatchFunc = beginBatchFunction; } /// @@ -129,13 +144,13 @@ namespace RecrownedAthenaeum.ScreenSystem { graphics.GraphicsDevice.SetRenderTarget(previousScreenRenderTarget); graphics.GraphicsDevice.Clear(previousScreen.BackgroundColor); - spriteBatch.Begin(effect: camera.BasicEffect); + beginBatchFunc(spriteBatch); previousScreen.Draw(spriteBatch); spriteBatch.End(); graphics.GraphicsDevice.SetRenderTarget(null); Screen.UpdatePreviousScreenFrame(previousScreenRenderTarget); } - spriteBatch.Begin(effect: camera.BasicEffect); + beginBatchFunc(spriteBatch); Screen.Draw(spriteBatch); spriteBatch.End(); } @@ -199,5 +214,10 @@ namespace RecrownedAthenaeum.ScreenSystem { Dispose(false); } + + private void BasicBeginBatch(SpriteBatch spriteBatch) + { + spriteBatch.Begin(effect: camera.BasicEffect, blendState: BlendState.NonPremultiplied); + } } } diff --git a/RecrownedAthenaeum/UI/Modular/UIModuleGroup.cs b/RecrownedAthenaeum/UI/Modular/UIModuleGroup.cs index 27caebf..b680700 100644 --- a/RecrownedAthenaeum/UI/Modular/UIModuleGroup.cs +++ b/RecrownedAthenaeum/UI/Modular/UIModuleGroup.cs @@ -4,6 +4,7 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using RecrownedAthenaeum.Camera; +using RecrownedAthenaeum.ScreenSystem; namespace RecrownedAthenaeum.UI.Modular { @@ -16,6 +17,7 @@ namespace RecrownedAthenaeum.UI.Modular List modules = new List(); Rectangle scissorBounds; RasterizerState scissorRasterizer; + BeginBatch beginBatch; /// /// Camera used by the module for cropping. @@ -27,9 +29,12 @@ namespace RecrownedAthenaeum.UI.Modular /// /// Whether or not to crop out of bounds. Default is false. /// 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) + /// The function to be called that begins the batch. + public UIModuleGroup(bool crop = false, Camera2D camera = null, BeginBatch beginBatchFunction = null) { + if (beginBatchFunction == null) beginBatchFunction = Configuration.BeginBatchFunction; if (crop && camera == null) camera = Configuration.Camera2D; + this.beginBatch = beginBatchFunction; this.camera = camera; if (crop) { @@ -48,7 +53,7 @@ namespace RecrownedAthenaeum.UI.Modular if (scissorBounds != null) { batch.End(); - batch.Begin(effect: camera?.BasicEffect); + beginBatch(batch); scissorBounds.Width = situation.Width; scissorBounds.Height = situation.Height; scissorBounds.X = situation.X; @@ -73,7 +78,7 @@ namespace RecrownedAthenaeum.UI.Modular { batch.GraphicsDevice.ScissorRectangle = scissorBounds; batch.End(); - batch.Begin(effect: camera?.BasicEffect); + beginBatch(batch); } }