Browse Source

Revamped ResourceManager system to directly use objects that reference resource IDs.

improved_timing
Ian Burgmyer 7 years ago
parent
commit
67a7e5117f
  1. 39
      DotSDL/Graphics/SdlWindow.cs
  2. 22
      DotSDL/IResourceObject.cs
  3. 24
      DotSDL/Resource.cs
  4. 61
      DotSDL/ResourceManager.cs
  5. 7
      DotSDL/Sdl/Video.cs

39
DotSDL/Graphics/SdlWindow.cs

@ -1,12 +1,13 @@
using System;
using DotSDL.Sdl;
using DotSDL.Sdl;
using System;
namespace DotSDL.Graphics {
/// <summary>
/// Represents an SDL window.
/// </summary>
public class SdlWindow {
public class SdlWindow : IResourceObject {
private readonly SdlInit _sdlInit = SdlInit.Instance;
private readonly ResourceManager _resources = ResourceManager.Instance;
private readonly IntPtr _window;
private readonly IntPtr _renderer;
@ -18,6 +19,9 @@ namespace DotSDL.Graphics {
private uint _nextVideoUpdate;
private uint _nextGameUpdate;
/// <summary><c>true</c> if this <see cref="SdlWindow"/> instance has been destroyed, othersize <c>false</c>.</summary>
public bool IsDestroyed { get; set; }
/// <summary>The width of the user window.</summary>
public int WindowWidth { get; }
/// <summary>The height of the user window.</summary>
@ -96,12 +100,24 @@ namespace DotSDL.Graphics {
TextureWidth = textureWidth;
TextureHeight = textureHeight;
IsDestroyed = false;
_resources.RegisterResource(this);
}
/// <summary>
/// Releases resources used by the <see cref="SdlWindow"/> instance.
/// </summary>
~SdlWindow() {
DestroyObject();
_resources.UnregisterResource(this);
}
/// <summary>
/// Handles calling the user draw function and passing the CLR objects to SDL2.
/// </summary>
private unsafe void BaseDraw() {
if(IsDestroyed) return;
OnDraw(ref _canvas); // Call the overridden Draw function.
fixed(void* pixelsPtr = _canvas.Pixels) {
@ -124,9 +140,26 @@ namespace DotSDL.Graphics {
/// Handles updating the application logic for the <see cref="SdlWindow"/>.
/// </summary>
private void BaseUpdate() {
if(IsDestroyed) return;
OnUpdate(); // Call the overridden Update function.
}
/// <summary>
/// Destroys this <see cref="SdlWindow"/>.
/// </summary>
public void DestroyObject() {
Video.DestroyWindow(_window);
IsDestroyed = true;
}
/// <summary>
/// Retrieves the SDL resource ID for this <see cref="SdlWindow"/>.
/// </summary>
/// <returns></returns>
public uint GetResourceId() {
return Video.GetWindowId(_window);
}
/// <summary>
/// A game loop that calls the <see cref="SdlWindow"/> update and draw functions.
/// </summary>

22
DotSDL/IResourceObject.cs

@ -0,0 +1,22 @@
namespace DotSDL {
/// <summary>
/// Specifies an object that contains a resource.
/// </summary>
internal interface IResourceObject {
/// <summary>
/// <c>true</c> if the underlying <see cref="IResourceObject"/> has been destoryed, otherwise <c>false</c>.
/// </summary>
bool IsDestroyed { get; set; }
/// <summary>
/// Destroys this <see cref="IResourceObject"/>.
/// </summary>
void DestroyObject();
/// <summary>
/// Retrieves the SDL resource ID for the object instance..
/// </summary>
/// <returns>The reousrce ID for the object instance.</returns>
uint GetResourceId();
}
}

24
DotSDL/Resource.cs

@ -1,24 +0,0 @@
using System;
namespace DotSDL {
/// <summary>
/// Describes an SDL resource. These objects are tracked using the
/// <see cref="ResourceManager"/>.
/// </summary>
internal class Resource {
/// <summary>
/// The type of resource represeented by this instance.
/// </summary>
internal ResourceType Type { get; set; }
/// <summary>
/// The numeric ID of the resource.
/// </summary>
internal uint ResourceId { get; set; }
/// <summary>
/// The pointer to the resource's SDL object.
/// </summary>
internal IntPtr ResourcePtr { get; set; }
}
}

61
DotSDL/ResourceManager.cs

@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace DotSDL {
/// <summary>
@ -10,6 +12,65 @@ namespace DotSDL {
private static readonly Lazy<ResourceManager> Singleton = new Lazy<ResourceManager>(() => new ResourceManager());
internal static ResourceManager Instance => Singleton.Value;
private readonly List<IResourceObject> _resources = new List<IResourceObject>();
~ResourceManager() {
UnregisterAllResources();
}
/// <summary>
/// Retrieves a resources matching a given ID.
/// </summary>
/// <param name="id">The ID number of the <see cref="IResourceObject"/> to retrieve.</param>
/// <returns>The <see cref="IResourceObject"/> with a matching ID, or null if it doesn't exist.</returns>
internal IResourceObject GetResourceById(uint id) {
return _resources.FirstOrDefault(e => e.GetResourceId() == id);
}
/// <summary>
/// Retrieves all resources matching a given type.
/// </summary>
/// <param name="type">The <see cref="ResourceType"/> that should be returned.</param>
/// <returns>A collection of <see cref="Resource"/>s matching the type given in <paramref name="type"/>.</returns>
internal IEnumerable<IResourceObject> GetResourceByType(string type) {
//return _resources.Where(e => e is typeof(type));
throw new NotImplementedException();
}
/// <summary>
/// Registers a resource with the <see cref="ResourceManager"/>.
/// </summary>
/// <param name="resource">The resource to register.</param>
internal void RegisterResource(IResourceObject resource) {
_resources.Add(resource);
}
/// <summary>
/// Destroys and unregisters all <see cref="IResourceObject"/>s from the <see cref="ResourceManager"/>.
/// </summary>
internal void UnregisterAllResources() {
foreach(var resource in _resources) {
if(!resource.IsDestroyed)
resource.DestroyObject();
}
_resources.Clear();
}
/// <summary>
/// Unregisters a resource from the <see cref="ResourceManager"/>.
/// </summary>
/// <param name="resource">The resource to unregister.</param>
/// <returns><c>true</c> if the resource was successfully removed, otherwise <c>false</c>.</returns>
internal bool UnregisterResource(IResourceObject resource) {
var resourceObject = _resources.FirstOrDefault(e => e == resource);
if(resourceObject is null) return false;
if(!resourceObject.IsDestroyed)
resourceObject.DestroyObject();
_resources.Remove(resourceObject);
return true;
}
}
}

7
DotSDL/Sdl/Video.cs

@ -88,6 +88,13 @@ namespace DotSDL.Sdl {
[DllImport(Meta.DllName, EntryPoint = "SDL_CreateWindow", CallingConvention = CallingConvention.Cdecl)]
internal static extern IntPtr CreateWindow(string title, int x, int y, int w, int h, WindowFlags flags);
/// <summary>
/// Destroys an SDL window.
/// </summary>
/// <param name="window">The window to destroy.</param>
[DllImport(Meta.DllName, EntryPoint = "SDL_DestroyWindow", CallingConvention = CallingConvention.Cdecl)]
internal static extern void DestroyWindow(IntPtr window);
/// <summary>
/// Gets the numberic ID of a window.
/// </summary>

Loading…
Cancel
Save