Added some documentation and progress on boilerplate.

This commit is contained in:
Harrison Deng 2020-05-23 23:52:41 -05:00
parent 7a6f709e9a
commit 065e6d878f
3 changed files with 116 additions and 35 deletions

View File

@ -49,7 +49,7 @@ namespace SlatedGameToolkit.Framework {
} }
private static void Loop(Object o) { private static void Loop(Object o) {
if (!(o is Manager)) throw new ArgumentException("The passed object needs to be of type manager."); if (!(o is Manager)) throw new InternalFrameworkException(String.Format("Expected manager object for asynchronous loop. Got {0}", o));
Manager manager = (Manager) o; Manager manager = (Manager) o;
long currentTime = DateTimeOffset.Now.ToUnixTimeMilliseconds(); long currentTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
long timePassedFromLastUpdate = 0; long timePassedFromLastUpdate = 0;
@ -113,6 +113,11 @@ namespace SlatedGameToolkit.Framework {
lock (ignitionLock) { lock (ignitionLock) {
if (!loopCompleted) return false; if (!loopCompleted) return false;
loopCompleted = false; loopCompleted = false;
if (SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION, 3) < 0) throw new FrameworkSDLException();
if (SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MINOR_VERSION, 1) < 0) throw new FrameworkSDLException();
if (SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_CORE) < 0) throw new FrameworkSDLException();
exit = false; exit = false;
if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO) != 0) { if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO) != 0) {
throw new FrameworkSDLException(); throw new FrameworkSDLException();

View File

@ -7,13 +7,39 @@ using SlatedGameToolkit.Framework.Exceptions;
namespace SlatedGameToolkit.Framework.Graphics.Window namespace SlatedGameToolkit.Framework.Graphics.Window
{ {
/// <summary>
/// A delegate to determine the type of interaction with a window.
/// The method should be extremely lightweight as it can be called many times.
/// </summary>
/// <param name="hitPoint">The window coordinates that are being interacted with.</param>
/// <returns>The region type.</returns>
public delegate WindowRegion WindowRegionHit(FloatVector2 hitPoint); public delegate WindowRegion WindowRegionHit(FloatVector2 hitPoint);
/// <summary>
/// A handle for a window.
/// This object itself is not thread safe.
/// </summary>
public sealed class WindowHandle : IDisposable public sealed class WindowHandle : IDisposable
{ {
/// <summary>
/// The pointer referencing the SDL window.
/// </summary>
public readonly IntPtr window; public readonly IntPtr window;
public event WindowRegionHit windowRegionHitEvent;
IntPtr windowSurfaceHandle;
/// <summary>
/// The pointer referencing the OpenGL context.
/// </summary>
public readonly IntPtr glContext;
/// <summary>
/// Event for when the window is being interacted with.
/// </summary>
public event WindowRegionHit windowRegionHitEvent;
/// <summary>
/// Whether or not to show this window.
/// </summary>
/// <value>True for showing.</value>
public bool Shown { public bool Shown {
set { set {
if (value) { if (value) {
@ -28,6 +54,10 @@ namespace SlatedGameToolkit.Framework.Graphics.Window
} }
} }
/// <summary>
/// The title displayed on the window.
/// </summary>
/// <value>A string that represents whats to be displayed as the title of the window.</value>
public string WindowTitle { public string WindowTitle {
set { set {
SDL.SDL_SetWindowTitle(window, value); SDL.SDL_SetWindowTitle(window, value);
@ -38,6 +68,10 @@ namespace SlatedGameToolkit.Framework.Graphics.Window
} }
} }
/// <summary>
/// Whether or not to display the default window border.
/// </summary>
/// <value>True if displaying borders.</value>
public bool WindowBordered { public bool WindowBordered {
set { set {
SDL.SDL_SetWindowBordered(window, value ? SDL.SDL_bool.SDL_TRUE : SDL.SDL_bool.SDL_FALSE); SDL.SDL_SetWindowBordered(window, value ? SDL.SDL_bool.SDL_TRUE : SDL.SDL_bool.SDL_FALSE);
@ -51,6 +85,10 @@ namespace SlatedGameToolkit.Framework.Graphics.Window
} }
} }
/// <summary>
/// Whether or not this window should be resizeable.
/// </summary>
/// <value>True if resizeable.</value>
public bool WindowResizable { public bool WindowResizable {
set { set {
SDL.SDL_SetWindowResizable(window, value ? SDL.SDL_bool.SDL_TRUE : SDL.SDL_bool.SDL_FALSE); SDL.SDL_SetWindowResizable(window, value ? SDL.SDL_bool.SDL_TRUE : SDL.SDL_bool.SDL_FALSE);
@ -60,6 +98,11 @@ namespace SlatedGameToolkit.Framework.Graphics.Window
} }
} }
/// <summary>
/// The boundaries of the window represent both the bottom left corner relative to the screen,
/// as well as the current width and height of this window.
/// </summary>
/// <value>Rectangle representing the boundaries of the window.</value>
public FloatRectangle WindowBoundaries { public FloatRectangle WindowBoundaries {
set { set {
SDL.SDL_SetWindowPosition(window, (int)value.X1, (int)value.Y1); SDL.SDL_SetWindowPosition(window, (int)value.X1, (int)value.Y1);
@ -79,6 +122,10 @@ namespace SlatedGameToolkit.Framework.Graphics.Window
} }
} }
/// <summary>
/// The maximum size the window can be at any given time.
/// </summary>
/// <value>A vector that represents the maximum width and height of the window with the x and y components respectively.</value>
public FloatVector2 MaximumSize { public FloatVector2 MaximumSize {
set { set {
SDL.SDL_SetWindowMaximumSize(window, (int)value.x, (int)value.y); SDL.SDL_SetWindowMaximumSize(window, (int)value.x, (int)value.y);
@ -93,19 +140,10 @@ namespace SlatedGameToolkit.Framework.Graphics.Window
} }
} }
public float Opacity { /// <summary>
set { /// The minimum size the window can be at any given time.
int errorCode = SDL.SDL_SetWindowOpacity(window, value); /// </summary>
if (errorCode < 0) throw new OptionalSDLException(); /// <value>A vector that represents the minimum width and height of the window with the x and y components respectively.</value>
}
get {
float value;
int errorCode = SDL.SDL_GetWindowOpacity(window, out value);
if (errorCode < 0) throw new OptionalSDLException();
return value;
}
}
public FloatVector2 MinimumSize { public FloatVector2 MinimumSize {
set { set {
SDL.SDL_SetWindowMinimumSize(window, (int)value.x, (int)value.y); SDL.SDL_SetWindowMinimumSize(window, (int)value.x, (int)value.y);
@ -120,6 +158,28 @@ namespace SlatedGameToolkit.Framework.Graphics.Window
} }
} }
/// <summary>
/// The opacity of the window itself.
/// This is not supported on all targetted platforms, and therefore, can throw an OptionalSDLException for when this is the case.
/// </summary>
/// <value>A float with bounds of [0, 1] representing the opacity of the window.</value>
public float Opacity {
set {
int errorCode = SDL.SDL_SetWindowOpacity(window, value);
if (errorCode < 0) throw new OptionalSDLException();
}
get {
float value;
int errorCode = SDL.SDL_GetWindowOpacity(window, out value);
if (errorCode < 0) throw new OptionalSDLException();
return value;
}
}
/// <summary>
/// Whether or not this window is grabbing the users input by making sure the mouse stays within this windows boundaries.
/// </summary>
/// <value>True if grabbing.</value>
public bool GrabbingInput { public bool GrabbingInput {
set { set {
SDL.SDL_SetWindowGrab(window, value ? SDL.SDL_bool.SDL_TRUE : SDL.SDL_bool.SDL_FALSE); SDL.SDL_SetWindowGrab(window, value ? SDL.SDL_bool.SDL_TRUE : SDL.SDL_bool.SDL_FALSE);
@ -134,10 +194,39 @@ namespace SlatedGameToolkit.Framework.Graphics.Window
if (window == null) { if (window == null) {
throw new FrameworkSDLException(); throw new FrameworkSDLException();
} }
glContext = SDL.SDL_GL_CreateContext(window);
if (glContext == null) {
throw new FrameworkSDLException();
}
if (SDL.SDL_SetWindowHitTest(window, SpecialRegionHit, IntPtr.Zero) < 0) { if (SDL.SDL_SetWindowHitTest(window, SpecialRegionHit, IntPtr.Zero) < 0) {
throw new OptionalSDLException(); throw new OptionalSDLException();
} }
windowSurfaceHandle = SDL.SDL_GetWindowSurface(window); }
/// <summary>
/// Makes this window the window that is currently being drawn to.
/// More specifically, it sets the OpenGL Context associated with this window to be the one
/// that is actively receiving all OpenGL calls.
/// </summary>
public void DrawToWindow() {
SDL.SDL_GL_MakeCurrent(window, glContext);
}
/// <summary>
/// Raises this window to be above other windows.
/// </summary>
public void RaiseToTop() {
SDL.SDL_RaiseWindow(window);
}
/// <summary>
/// Gets the index of the display that this window resides within.
/// </summary>
/// <returns>An integer representing the display this window resides within.</returns>
public int GetDisplayIndex() {
int index = SDL.SDL_GetWindowDisplayIndex(window);
if (index < 0) throw new FrameworkSDLException();
return index;
} }
private SDL.SDL_HitTestResult SpecialRegionHit(IntPtr window, IntPtr hitPtr, IntPtr data) { private SDL.SDL_HitTestResult SpecialRegionHit(IntPtr window, IntPtr hitPtr, IntPtr data) {
@ -147,19 +236,12 @@ namespace SlatedGameToolkit.Framework.Graphics.Window
return region.ToSDLHitTestResult(); return region.ToSDLHitTestResult();
} }
public void RaiseToTop() { /// <summary>
SDL.SDL_RaiseWindow(window); /// Disposes of this window and it's associated handles.
} /// </summary>
public int GetDisplayIndex() {
int index = SDL.SDL_GetWindowDisplayIndex(window);
if (index < 0) throw new FrameworkSDLException();
return index;
}
public void Dispose() public void Dispose()
{ {
SDL.SDL_FreeSurface(windowSurfaceHandle); SDL.SDL_GL_DeleteContext(glContext);
SDL.SDL_DestroyWindow(window); SDL.SDL_DestroyWindow(window);
} }

View File

@ -1,14 +1,12 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading; using System.Threading;
using SlatedGameToolkit.Framework.Graphics.Window;
using SlatedGameToolkit.Framework.StateSystem.States; using SlatedGameToolkit.Framework.StateSystem.States;
namespace SlatedGameToolkit.Framework.StateSystem namespace SlatedGameToolkit.Framework.StateSystem
{ {
public sealed class Manager : IDisposable { public sealed class Manager : IDisposable {
public Thread thread; public Thread thread;
public readonly WindowHandle window;
private IState currentState; private IState currentState;
private IState nextState; private IState nextState;
private Dictionary<string, IState> states; private Dictionary<string, IState> states;
@ -19,11 +17,9 @@ namespace SlatedGameToolkit.Framework.StateSystem
/// None of the parameters can be null, and the initial state must exist in initial set of states. /// None of the parameters can be null, and the initial state must exist in initial set of states.
/// </summary> /// </summary>
/// <param name="initialState">The name of the initial state.</param> /// <param name="initialState">The name of the initial state.</param>
/// <param name="window">The window handle being used for this set of states.</param>
/// <param name="states">The initial set of game states to be added.</param> /// <param name="states">The initial set of game states to be added.</param>
public Manager(string initialState, WindowHandle window, params IState[] states) { public Manager(string initialState, params IState[] states) {
if (initialState == null) throw new ArgumentNullException("initialState"); if (initialState == null) throw new ArgumentNullException("initialState");
this.window = window ?? throw new ArgumentNullException("window");
this.states = new Dictionary<string, IState>(); this.states = new Dictionary<string, IState>();
thread = Thread.CurrentThread; thread = Thread.CurrentThread;
addStates(states); addStates(states);
@ -117,12 +113,10 @@ namespace SlatedGameToolkit.Framework.StateSystem
/// <summary> /// <summary>
/// Disposes of this manager. /// Disposes of this manager.
/// This also disposes the window and all the individual states.
/// </summary> /// </summary>
public void Dispose() public void Dispose()
{ {
removeAllStates(); removeAllStates();
window.Dispose();
} }
~Manager() { ~Manager() {