From 82236acfa7d066f3fc9a484fa36e132a619faf98 Mon Sep 17 00:00:00 2001 From: gered Date: Sat, 17 Aug 2013 23:08:09 -0400 Subject: [PATCH] add GraphicsDevice object to the application object. minor adjustments to make it easier to clean up platform-independant "system" objects --- Blarg.GameFramework.SDL2/SDLApplication.cs | 15 +++--- Blarg.GameFramework/BaseApplication.cs | 23 +++++++- Blarg.GameFramework/IApplication.cs | 2 + Blarg.GameFramework/Platform.cs | 62 ++++++++++++++++------ 4 files changed, 78 insertions(+), 24 deletions(-) diff --git a/Blarg.GameFramework.SDL2/SDLApplication.cs b/Blarg.GameFramework.SDL2/SDLApplication.cs index d023b9c..6cb52dd 100644 --- a/Blarg.GameFramework.SDL2/SDLApplication.cs +++ b/Blarg.GameFramework.SDL2/SDLApplication.cs @@ -22,8 +22,8 @@ namespace Blarg.GameFramework SDLKeyboard _keyboard; SDLMouse _mouse; SDLFileSystem _filesystem; - SDLGL20 _gl; SDLWindow _windowInfo; + SDLGL20 _gl; PlatformOS _os; bool _isWindowActive; @@ -146,6 +146,7 @@ namespace Blarg.GameFramework Platform.Set(this); + OnInit(); OnNewContext(); OnResize(ScreenOrientation.Rotation0, _windowInfo.ClientRectangle); OnLoad(); @@ -161,7 +162,7 @@ namespace Blarg.GameFramework GameApp.Dispose(); GameApp = null; - ReleaseSDL(); + Release(); } public override void Quit() @@ -611,13 +612,15 @@ namespace Blarg.GameFramework #region IDisposable - private void ReleaseSDL() + protected override void Release() { if (!_isSDLinited) return; - Logger.Info(LOG_TAG, "Releasing SDL."); + Logger.Info(LOG_TAG, "Releasing SDL application object."); + base.Release(); + Logger.Info(LOG_TAG, "Releasing SDL."); DestroyOpenGLContext(); DestroyWindow(); SDL.SDL_Quit(); @@ -628,14 +631,14 @@ namespace Blarg.GameFramework ~SDLApplication() { - ReleaseSDL(); + Release(); } public override void Dispose() { base.Dispose(); Logger.Info(LOG_TAG, "Disposing."); - ReleaseSDL(); + Release(); GC.SuppressFinalize(this); } diff --git a/Blarg.GameFramework/BaseApplication.cs b/Blarg.GameFramework/BaseApplication.cs index c964f10..8c7ea52 100644 --- a/Blarg.GameFramework/BaseApplication.cs +++ b/Blarg.GameFramework/BaseApplication.cs @@ -10,6 +10,8 @@ namespace Blarg.GameFramework { const string LOG_TAG = "BASE_APP"; + private bool _isReleased = false; + protected IGameApp GameApp { get; set; } public abstract PlatformOS OperatingSystem { get; } @@ -22,6 +24,7 @@ namespace Blarg.GameFramework public abstract ITouchScreen TouchScreen { get; } public abstract IPlatformWindow Window { get; } public abstract GL20 GL { get; } + public GraphicsDevice GraphicsDevice { get; private set; } public int FPS { get; protected set; } public float FrameTime { get; protected set; } @@ -34,6 +37,12 @@ namespace Blarg.GameFramework public abstract void Run(IGameApp gameApp, IPlatformConfiguration config); public abstract void Quit(); + protected void OnInit() + { + Logger.Info(LOG_TAG, "Initializing application objects."); + GraphicsDevice = new GraphicsDevice(); + } + protected void OnAppGainFocus() { Logger.Info(LOG_TAG, "OnAppGainFocus"); @@ -98,10 +107,22 @@ namespace Blarg.GameFramework GameApp.OnUpdate(delta); } + #region Disposable + + protected virtual void Release() + { + if (_isReleased) + return; + + Logger.Info(LOG_TAG, "Releasing resources."); + GraphicsDevice.Dispose(); + } + public virtual void Dispose() { Logger.Info(LOG_TAG, "Disposing."); } + + #endregion } } - diff --git a/Blarg.GameFramework/IApplication.cs b/Blarg.GameFramework/IApplication.cs index 81390fa..05d6946 100644 --- a/Blarg.GameFramework/IApplication.cs +++ b/Blarg.GameFramework/IApplication.cs @@ -1,5 +1,6 @@ using System; using PortableGL; +using Blarg.GameFramework.Graphics; using Blarg.GameFramework.Input; using Blarg.GameFramework.IO; @@ -16,6 +17,7 @@ namespace Blarg.GameFramework IMouse Mouse { get; } ITouchScreen TouchScreen { get; } IPlatformWindow Window { get; } + GraphicsDevice GraphicsDevice { get; } GL20 GL { get; } int FPS { get; } diff --git a/Blarg.GameFramework/Platform.cs b/Blarg.GameFramework/Platform.cs index e6b87b3..55999a1 100644 --- a/Blarg.GameFramework/Platform.cs +++ b/Blarg.GameFramework/Platform.cs @@ -23,16 +23,52 @@ namespace Blarg.GameFramework public static class Platform { - public static PlatformOS OperatingSystem { get; private set; } - public static PlatformType Type { get; private set; } - public static IApplication Application { get; private set; } - public static ILogger Logger { get; private set; } - public static IFileSystem FileSystem { get; private set; } - public static IKeyboard Keyboard { get; private set; } - public static IMouse Mouse { get; private set; } - public static ITouchScreen TouchScreen { get; private set; } - public static GL20 GL { get; private set; } + + public static PlatformOS OperatingSystem + { + get { return Application.OperatingSystem; } + } + + public static PlatformType Type + { + get { return Application.Type; } + } + + public static ILogger Logger + { + get { return Application.Logger; } + } + + public static IFileSystem FileSystem + { + get { return Application.FileSystem; } + } + + public static IKeyboard Keyboard + { + get { return Application.Keyboard; } + } + + public static IMouse Mouse + { + get { return Application.Mouse; } + } + + public static ITouchScreen TouchScreen + { + get { return Application.TouchScreen; } + } + + public static GraphicsDevice GraphicsDevice + { + get { return Application.GraphicsDevice; } + } + + public static GL20 GL + { + get { return Application.GL; } + } public static void Set(IApplication application) { @@ -40,14 +76,6 @@ namespace Blarg.GameFramework throw new InvalidOperationException(); Application = application; - OperatingSystem = Application.OperatingSystem; - Type = Application.Type; - Logger = Application.Logger; - FileSystem = Application.FileSystem; - Keyboard = Application.Keyboard; - Mouse = Application.Mouse; - TouchScreen = Application.TouchScreen; - GL = Application.GL; } } }