Added better system for beginning sprite batches for easier consistency.

This commit is contained in:
Harrison Deng 2019-03-09 02:03:05 -06:00
parent ed11d31100
commit c59252dbbf
3 changed files with 39 additions and 9 deletions

View File

@ -1,5 +1,6 @@
using Microsoft.Xna.Framework;
using RecrownedAthenaeum.Camera;
using RecrownedAthenaeum.ScreenSystem;
using System;
namespace RecrownedAthenaeum
@ -9,19 +10,23 @@ 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 { 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;
/// <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; } }
private static BeginBatch beginBatchFunction;
/// <summary>
/// The begin sprite batch to use for custom begins and consistency.
/// </summary>
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; } }
}
}

View File

@ -12,6 +12,12 @@ namespace RecrownedAthenaeum.ScreenSystem
/// <param name="screen">The screen to show after the loading screen.</param>
public delegate void ShowFirstScreen(Screen screen);
/// <summary>
/// Custom spritebatch begin call.
/// </summary>
/// <param name="spriteBatch"></param>
public delegate void BeginBatch(SpriteBatch spriteBatch);
/// <summary>
/// A manager for screens. Helps with transitions and updating screens as well as resizes.
/// </summary>
@ -23,6 +29,12 @@ namespace RecrownedAthenaeum.ScreenSystem
/// Called when the first loading screen is done, and needs to show the landing screen.
/// </summary>
public event ShowFirstScreen ShowFirstScreenEvent;
/// <summary>
/// The function to call that begins the batch.
/// </summary>
public BeginBatch beginBatchFunc;
private GraphicsDeviceManager graphics;
private Screen previousScreen;
private RenderTarget2D previousScreenRenderTarget;
@ -65,13 +77,16 @@ namespace RecrownedAthenaeum.ScreenSystem
/// </summary>
/// <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)
/// <param name="beginBatchFunction">The function to call to begin a batch to be used generally. Will use the built-in one in screen manager if not provided.</param>
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;
}
/// <summary>
@ -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);
}
}
}

View File

@ -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<UIModule> modules = new List<UIModule>();
Rectangle scissorBounds;
RasterizerState scissorRasterizer;
BeginBatch beginBatch;
/// <summary>
/// Camera used by the module for cropping.
@ -27,9 +29,12 @@ namespace RecrownedAthenaeum.UI.Modular
/// </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 and will use <see cref="Configuration"/>'s camera if crop is enabled.</param>
public UIModuleGroup(bool crop = false, Camera2D camera = null)
/// <param name="beginBatchFunction">The function to be called that begins the batch.</param>
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);
}
}