#region usings using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; using OpenTK.Graphics.OpenGL4; #endregion namespace SM.OGL { /// /// Specifies default object behaviour. /// public abstract class GLObject { private static List _disposableObjects = new List(); protected bool ReportAsNotCompiled; /// /// Contains the OpenGL ID /// protected int _id = -1; /// /// If true, the system will call "Compile()", when "ID" is tried to get, but the id is still -1. /// protected virtual bool AutoCompile { get; } = false; /// /// Checks if the object was compiled. /// public bool WasCompiled => _id > 0 && !ReportAsNotCompiled; /// /// Returns the id for this object. /// It will auto compile, if needed and allowed. /// public virtual int ID { get { if (AutoCompile && !WasCompiled) PerformCompile(); return _id; } } /// /// Identifies the object. /// public abstract ObjectLabelIdentifier TypeIdentifier { get; } [DebuggerStepThrough] private void PerformCompile() { Compile(); } /// /// The action, that is called, when "ID" tries to compile something. /// [DebuggerStepThrough] public virtual void Compile() { } /// /// Is triggered, when something want to dispose this object. /// public virtual void Dispose() { _id = -1; } /// /// Re-compiles the object. /// public void Recompile() { if (!WasCompiled) return; Dispose(); Compile(); } /// /// Names the object for debugging. /// /// public void Name(string name) { if (GLSystem.Debugging) GL.ObjectLabel(TypeIdentifier, _id, name.Length, name); } public static void DisposeMarkedObjects() { foreach (GLObject o in _disposableObjects) { o.Dispose(); } _disposableObjects.Clear(); } /// /// Returns the ID for the object. /// /// public static implicit operator int(GLObject glo) { return glo.ID; } ~GLObject() { if (WasCompiled) _disposableObjects.Add(this); } } }