refactoring; removed configuration setup; added consistent sprite batch; got rid of sprite batch settings; implemented newer setup;
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using RecrownedAthenaeum.Render;
|
||||
|
||||
namespace RecrownedAthenaeum.ScreenSystem
|
||||
{
|
||||
@@ -34,7 +35,7 @@ namespace RecrownedAthenaeum.ScreenSystem
|
||||
/// Called once every frame while transition is active. Meant to draw transition.
|
||||
/// </summary>
|
||||
/// <param name="spriteBatch"></param>
|
||||
void DrawTransition(SpriteBatch spriteBatch);
|
||||
void DrawTransition(ConsistentSpriteBatch spriteBatch);
|
||||
|
||||
/// <summary>
|
||||
/// Updates if the previous screen uses a render target for exit transition.
|
||||
|
@@ -1,6 +1,7 @@
|
||||
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using RecrownedAthenaeum.Render;
|
||||
using System;
|
||||
|
||||
namespace RecrownedAthenaeum.ScreenSystem
|
||||
@@ -62,10 +63,10 @@ namespace RecrownedAthenaeum.ScreenSystem
|
||||
public void InitiateTransition(Rectangle dimensions)
|
||||
{
|
||||
color = Color.White;
|
||||
textureBounds.Width = (int)(ScreenSize.Height * proportion);
|
||||
textureBounds.Height = (int)(ScreenSize.Height * proportion);
|
||||
textureBounds.X = (ScreenSize.Width) / 2;
|
||||
textureBounds.Y = (ScreenSize.Height) / 2;
|
||||
textureBounds.Width = (int)(height * proportion);
|
||||
textureBounds.Height = (int)(height * proportion);
|
||||
textureBounds.X = (width) / 2;
|
||||
textureBounds.Y = (height) / 2;
|
||||
origin.X = texture.Width / 2;
|
||||
origin.Y = texture.Height / 2;
|
||||
}
|
||||
@@ -83,7 +84,7 @@ namespace RecrownedAthenaeum.ScreenSystem
|
||||
/// Draws the transition.
|
||||
/// </summary>
|
||||
/// <param name="spriteBatch">Batch to use.</param>
|
||||
public void DrawTransition(SpriteBatch spriteBatch)
|
||||
public void DrawTransition(ConsistentSpriteBatch spriteBatch)
|
||||
{
|
||||
spriteBatch.Draw(texture, textureBounds, null, color, rotation, origin, SpriteEffects.None, 0f);
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using RecrownedAthenaeum.Camera;
|
||||
using RecrownedAthenaeum.Render;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace RecrownedAthenaeum.ScreenSystem
|
||||
@@ -34,7 +35,7 @@ namespace RecrownedAthenaeum.ScreenSystem
|
||||
public readonly List<ITransition> Transitions;
|
||||
|
||||
/// <summary>
|
||||
/// Whether or not to continue rendering this screen onto a buffer for the benifit of the next screen to use as a transition.
|
||||
/// Whether or not to continue rendering this screen onto a buffer for the benefit of the next screen to use as a transition.
|
||||
/// </summary>
|
||||
public bool UseRenderTargetForExitTransition { get; private set; }
|
||||
|
||||
@@ -49,19 +50,9 @@ namespace RecrownedAthenaeum.ScreenSystem
|
||||
public Screen NextScreen { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The current window size.
|
||||
/// The current window dimensions.
|
||||
/// </summary>
|
||||
public Rectangle ScreenSize { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Whether or not screen have been initiated.
|
||||
/// </summary>
|
||||
public bool Initiated { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The 2D camera to be used for the screen.
|
||||
/// </summary>
|
||||
public Camera2D Camera { get; private set; }
|
||||
public int width, height;
|
||||
|
||||
/// <summary>
|
||||
/// Current state of the screen.
|
||||
@@ -79,13 +70,14 @@ namespace RecrownedAthenaeum.ScreenSystem
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called only once after initialization to give required parameters.
|
||||
/// Called when screen size is set.
|
||||
/// </summary>
|
||||
public virtual void Initiate(Rectangle screenSize, Camera2D camera)
|
||||
/// <param name="width">The width of the screen.</param>
|
||||
/// <param name="height">The height of the screen.</param>
|
||||
public virtual void ApplySize(int width, int height)
|
||||
{
|
||||
this.Camera = camera;
|
||||
this.ScreenSize = screenSize;
|
||||
Initiated = true;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -101,7 +93,7 @@ namespace RecrownedAthenaeum.ScreenSystem
|
||||
/// Called to draw this screen.
|
||||
/// </summary>
|
||||
/// <param name="spriteBatch">SpriteBatch to use.</param>
|
||||
public virtual void Draw(SpriteBatch spriteBatch)
|
||||
public virtual void Draw(ConsistentSpriteBatch spriteBatch)
|
||||
{
|
||||
foreach (ITransition transition in Transitions)
|
||||
{
|
||||
@@ -167,7 +159,7 @@ namespace RecrownedAthenaeum.ScreenSystem
|
||||
{
|
||||
foreach (ITransition transition in Transitions)
|
||||
{
|
||||
transition.InitiateTransition(ScreenSize);
|
||||
transition.InitiateTransition(new Rectangle(0, 0, width, height));
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -11,7 +11,7 @@ namespace RecrownedAthenaeum.ScreenSystem
|
||||
/// Called when the first screen is being shown.
|
||||
/// </summary>
|
||||
/// <param name="screen">The screen to show after the loading screen.</param>
|
||||
public delegate void ShowFirstScreen(Screen screen);
|
||||
public delegate void NeedNextScreen(Screen screen);
|
||||
|
||||
/// <summary>
|
||||
/// A manager for screens. Helps with transitions and updating screens as well as resizes.
|
||||
@@ -23,18 +23,15 @@ namespace RecrownedAthenaeum.ScreenSystem
|
||||
/// <summary>
|
||||
/// Called when the first loading screen is done, and needs to show the landing screen.
|
||||
/// </summary>
|
||||
public event ShowFirstScreen ShowFirstScreenEvent;
|
||||
public event NeedNextScreen NeedNextScreen;
|
||||
|
||||
/// <summary>
|
||||
/// The settings this manager will use to begin a sprite batch.
|
||||
/// </summary>
|
||||
public SpriteBatchSettings batchSettings;
|
||||
|
||||
private GraphicsDeviceManager graphics;
|
||||
private Screen previousScreen;
|
||||
private RenderTarget2D previousScreenRenderTarget;
|
||||
private Screen currentScreen;
|
||||
private Camera2D camera;
|
||||
private bool firstScreenChangeComplete;
|
||||
private bool resizing;
|
||||
/// <summary>
|
||||
@@ -50,9 +47,9 @@ namespace RecrownedAthenaeum.ScreenSystem
|
||||
{
|
||||
previousScreen = currentScreen;
|
||||
currentScreen = value;
|
||||
if (!Screen.Initiated)
|
||||
if (Screen.width != graphics.PreferredBackBufferWidth || Screen.height != graphics.PreferredBackBufferHeight)
|
||||
{
|
||||
Screen.Initiate(new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight), camera);
|
||||
Screen.ApplySize(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight);
|
||||
}
|
||||
if (previousScreen != null && previousScreenRenderTarget == null && previousScreen.UseRenderTargetForExitTransition)
|
||||
{
|
||||
@@ -68,27 +65,12 @@ namespace RecrownedAthenaeum.ScreenSystem
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a screen manager that helps manage multiple screens and their transitions.
|
||||
/// Creates a screen manager that helps update, draw, and manage multiple screens and their transitions. Uses its own <see cref="SpriteBatch"/>.
|
||||
/// </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>
|
||||
/// <param name="spriteBatchSettings">The settings to begin spritebatch with. Will use the built-in one in screen manager if not provided.</param>
|
||||
public ScreenManager(Camera2D camera = null, GraphicsDeviceManager graphicsDeviceManager = null, SpriteBatchSettings? spriteBatchSettings = null)
|
||||
/// <param name="graphicsDeviceManager">The graphics device manager to be used.</param>
|
||||
public ScreenManager(GraphicsDeviceManager graphicsDeviceManager)
|
||||
{
|
||||
|
||||
if (camera == null) camera = Configuration.Camera2D;
|
||||
if (!spriteBatchSettings.HasValue)
|
||||
{
|
||||
SpriteBatchSettings basicSettings = new SpriteBatchSettings();
|
||||
basicSettings.effect = camera.BasicEffect;
|
||||
basicSettings.samplerState = null;
|
||||
spriteBatchSettings = basicSettings;
|
||||
}
|
||||
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.");
|
||||
batchSettings = spriteBatchSettings.Value;
|
||||
graphics = graphicsDeviceManager ?? throw new ArgumentNullException("Graphics device manager argument cannot be null.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -98,6 +80,7 @@ namespace RecrownedAthenaeum.ScreenSystem
|
||||
/// <param name="waiting">Whether or not there is something a transition should be waiting for. Usually used to wait for assets to complete loading.</param>
|
||||
public void UpdateCurrentScreen(GameTime gameTime, bool waiting)
|
||||
{
|
||||
waiting = !waiting;
|
||||
switch (Screen.State)
|
||||
{
|
||||
case ScreenState.EnterTransition:
|
||||
@@ -113,7 +96,7 @@ namespace RecrownedAthenaeum.ScreenSystem
|
||||
if (!firstScreenChangeComplete)
|
||||
{
|
||||
firstScreenChangeComplete = true;
|
||||
OnFirstScreenChange(Screen);
|
||||
OnNeedNextScreen(Screen);
|
||||
}
|
||||
if (Screen.NextScreen != null)
|
||||
{
|
||||
@@ -137,24 +120,34 @@ namespace RecrownedAthenaeum.ScreenSystem
|
||||
|
||||
/// <summary>
|
||||
/// Renders screen into window.
|
||||
/// Starts and ends the given <paramref name="consistentSpriteBatch"/>.
|
||||
/// </summary>
|
||||
/// <param name="spriteBatch">Uses this batch to render.</param>
|
||||
public void DrawCurrentScreen(SpriteBatch spriteBatch)
|
||||
/// <param name="consistentSpriteBatch">The consistent sprite batch to use. Needs to be consistent as changing render targets requires ending and beginning the sprite batch.</param>
|
||||
public void DrawScreen(ConsistentSpriteBatch consistentSpriteBatch)
|
||||
{
|
||||
if (consistentSpriteBatch == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(consistentSpriteBatch));
|
||||
}
|
||||
if (Screen == null) return;
|
||||
|
||||
graphics.GraphicsDevice.Clear(Screen.BackgroundColor);
|
||||
if (Screen.State == ScreenState.EnterTransition && previousScreen != null && previousScreen.UseRenderTargetForExitTransition)
|
||||
{
|
||||
graphics.GraphicsDevice.SetRenderTarget(previousScreenRenderTarget);
|
||||
graphics.GraphicsDevice.Clear(previousScreen.BackgroundColor);
|
||||
batchSettings.BeginSpriteBatch(spriteBatch);
|
||||
previousScreen.Draw(spriteBatch);
|
||||
spriteBatch.End();
|
||||
|
||||
consistentSpriteBatch.Begin();
|
||||
previousScreen.Draw(consistentSpriteBatch);
|
||||
consistentSpriteBatch.End();
|
||||
|
||||
graphics.GraphicsDevice.SetRenderTarget(null);
|
||||
Screen.UpdatePreviousScreenFrame(previousScreenRenderTarget);
|
||||
}
|
||||
batchSettings.BeginSpriteBatch(spriteBatch);
|
||||
Screen.Draw(spriteBatch);
|
||||
spriteBatch.End();
|
||||
|
||||
consistentSpriteBatch.Begin();
|
||||
Screen.Draw(consistentSpriteBatch);
|
||||
consistentSpriteBatch.End();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -181,9 +174,9 @@ namespace RecrownedAthenaeum.ScreenSystem
|
||||
Screen.AssetLoadStateChange(true);
|
||||
}
|
||||
|
||||
private void OnFirstScreenChange(Screen screen)
|
||||
private void OnNeedNextScreen(Screen screen)
|
||||
{
|
||||
ShowFirstScreenEvent?.Invoke(screen);
|
||||
NeedNextScreen?.Invoke(screen);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -201,12 +194,12 @@ namespace RecrownedAthenaeum.ScreenSystem
|
||||
/// <param name="disposing">True of user invoked dispose called.</param>
|
||||
public virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (disposed) return;
|
||||
if (disposed) throw new ObjectDisposedException(GetType().Name);
|
||||
if (disposing)
|
||||
{
|
||||
previousScreenRenderTarget?.Dispose();
|
||||
}
|
||||
disposing = true;
|
||||
disposed = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
Reference in New Issue
Block a user