From 97e638d9d9ed9ba6d0eb9701958219b7cbaa4b83 Mon Sep 17 00:00:00 2001 From: Michel Fedde Date: Thu, 1 Oct 2020 15:39:03 +0200 Subject: [PATCH] 01.10.2020 + Time controls (Stopwatch, Timers, Intervals) + Added smmeries to everything in SM.Base ~ Renamed Vectors to CVectors. --- SMCode/SM.Base/Controls/Mouse.cs | 8 + SMCode/SM.Base/Defaults.cs | 15 ++ SMCode/SM.Base/Drawing/DrawingBasis.cs | 6 + SMCode/SM.Base/Objects/Mesh.cs | 7 + SMCode/SM.Base/Objects/Static/Plate.cs | 4 + SMCode/SM.Base/SM.Base.csproj | 13 +- SMCode/SM.Base/Scene/GenericCamera.cs | 39 +++- SMCode/SM.Base/Scene/GenericItemCollection.cs | 37 +++- SMCode/SM.Base/Scene/GenericScene.cs | 45 ++++- SMCode/SM.Base/Scene/IBackgroundItem.cs | 3 + SMCode/SM.Base/Scene/IShowCollection.cs | 11 ++ SMCode/SM.Base/Scene/IShowItem.cs | 11 ++ SMCode/SM.Base/Text/CharParameter.cs | 25 ++- SMCode/SM.Base/Text/Font.cs | 79 ++++++-- SMCode/SM.Base/Text/FontCharStorage.cs | 14 ++ SMCode/SM.Base/Text/TextDrawingBasis.cs | 45 ++++- SMCode/SM.Base/Textures/Texture.cs | 49 ++++- SMCode/SM.Base/Time/Interval.cs | 48 +++++ SMCode/SM.Base/Time/Stopwatch.cs | 59 ++++++ SMCode/SM.Base/Time/Timer.cs | 77 ++++++++ SMCode/SM.Base/Types/CVector.cs | 176 ++++++++++++++++++ SMCode/SM.Base/Types/CVector2.cs | 56 ++++++ SMCode/SM.Base/Types/CVector3.cs | 46 +++++ SMCode/SM.Base/Types/CVector4.cs | 42 +++++ SMCode/SM.Base/Types/Vector.cs | 110 ----------- SMCode/SM.Base/Types/Vector2.cs | 33 ---- SMCode/SM.Base/Types/Vector3.cs | 23 --- SMCode/SM.Base/Types/Vector4.cs | 21 --- SMCode/SM.Base/Utility/Assembly.cs | 16 +- SMCode/SM.Base/Utility/BezierCurve.cs | 23 --- SMCode/SM.Base/Utility/Deltatime.cs | 25 +++ SMCode/SM.Base/Utility/Randomize.cs | 33 +++- SMCode/SM.Base/Window/Contexts/DrawContext.cs | 26 ++- .../SM.Base/Window/Contexts/UpdateContext.cs | 23 ++- SMCode/SM.Base/Window/GenericWindow.cs | 60 +++++- SMCode/SM.OGL/GLObject.cs | 16 ++ SMCode/SM.OGL/Texture/TextureBase.cs | 16 +- SMCode/SM2D/Drawing/DrawText.cs | 4 +- SMCode/SM2D/Drawing/DrawTexture.cs | 3 +- SMCode/SM2D/Scene/Camera.cs | 4 +- SMCode/SM2D/Scene/ItemCollection.cs | 2 +- SMCode/SM2D/Shader/Default2DShader.cs | 4 +- SMCode/SM2D/Types/Transformation.cs | 6 +- SM_TEST/Program.cs | 18 +- 44 files changed, 1092 insertions(+), 289 deletions(-) create mode 100644 SMCode/SM.Base/Time/Interval.cs create mode 100644 SMCode/SM.Base/Time/Stopwatch.cs create mode 100644 SMCode/SM.Base/Time/Timer.cs create mode 100644 SMCode/SM.Base/Types/CVector.cs create mode 100644 SMCode/SM.Base/Types/CVector2.cs create mode 100644 SMCode/SM.Base/Types/CVector3.cs create mode 100644 SMCode/SM.Base/Types/CVector4.cs delete mode 100644 SMCode/SM.Base/Types/Vector.cs delete mode 100644 SMCode/SM.Base/Types/Vector2.cs delete mode 100644 SMCode/SM.Base/Types/Vector3.cs delete mode 100644 SMCode/SM.Base/Types/Vector4.cs delete mode 100644 SMCode/SM.Base/Utility/BezierCurve.cs diff --git a/SMCode/SM.Base/Controls/Mouse.cs b/SMCode/SM.Base/Controls/Mouse.cs index f631755..ef72d90 100644 --- a/SMCode/SM.Base/Controls/Mouse.cs +++ b/SMCode/SM.Base/Controls/Mouse.cs @@ -25,11 +25,19 @@ namespace SM.Base.Controls /// public Vector2 InScreenNormalized { get; private set; } + /// + /// The constructor + /// + /// The window, its listen to. protected internal Mouse(TWindow window) { _window = window; } + /// + /// The event to update the values. + /// + /// The event args. protected void MouseMoveEvent(MouseMoveEventArgs mmea) { InScreen = new Vector2(mmea.X, mmea.Y); diff --git a/SMCode/SM.Base/Defaults.cs b/SMCode/SM.Base/Defaults.cs index 83b3a6b..cab5f23 100644 --- a/SMCode/SM.Base/Defaults.cs +++ b/SMCode/SM.Base/Defaults.cs @@ -2,12 +2,27 @@ using SM.Base.Objects.Static; using SM.Base.Scene; using SM.OGL.Mesh; +using SM.Utility; namespace SM.Base { + /// + /// The default options. + /// public class Defaults { + /// + /// The default shader. + /// public static IShader DefaultShader; + /// + /// The default mesh. + /// public static GenericMesh DefaultMesh = Plate.Object; + + /// + /// The default deltatime helper. + /// + public static Deltatime DefaultDeltatime = new Deltatime(); } } \ No newline at end of file diff --git a/SMCode/SM.Base/Drawing/DrawingBasis.cs b/SMCode/SM.Base/Drawing/DrawingBasis.cs index e51c212..7594647 100644 --- a/SMCode/SM.Base/Drawing/DrawingBasis.cs +++ b/SMCode/SM.Base/Drawing/DrawingBasis.cs @@ -17,11 +17,14 @@ namespace SM.Base.Scene /// The mesh it should use. /// protected GenericMesh _mesh = Defaults.DefaultMesh; + + /// public virtual void Update(UpdateContext context) { } + /// public virtual void Draw(DrawContext context) { } @@ -45,6 +48,9 @@ namespace SM.Base.Scene public abstract class DrawingBasis : DrawingBasis where TTransformation : GenericTransformation, new() { + /// + /// The current transformation. + /// public TTransformation Transform = new TTransformation(); } } \ No newline at end of file diff --git a/SMCode/SM.Base/Objects/Mesh.cs b/SMCode/SM.Base/Objects/Mesh.cs index e29c01a..b743026 100644 --- a/SMCode/SM.Base/Objects/Mesh.cs +++ b/SMCode/SM.Base/Objects/Mesh.cs @@ -2,10 +2,17 @@ namespace SM.Base.Objects { + /// public class Mesh : GenericMesh { + /// + /// Contains vertex colors + /// public virtual VBO Color { get; } + /// + /// While initializing, it will add the to the data index. + /// protected Mesh() { AttribDataIndex.Add(3, Color); diff --git a/SMCode/SM.Base/Objects/Static/Plate.cs b/SMCode/SM.Base/Objects/Static/Plate.cs index 05ce4ee..dd5445d 100644 --- a/SMCode/SM.Base/Objects/Static/Plate.cs +++ b/SMCode/SM.Base/Objects/Static/Plate.cs @@ -15,6 +15,7 @@ namespace SM.Base.Objects.Static /// public static Plate Object = new Plate(); + /// public override VBO Vertex { get; } = new VBO() { {-.5f, -.5f, 0}, @@ -23,6 +24,7 @@ namespace SM.Base.Objects.Static {.5f, -.5f, 0}, }; + /// public override VBO UVs { get; } = new VBO(pointerSize: 2) { {0, 0}, @@ -31,8 +33,10 @@ namespace SM.Base.Objects.Static {1, 0}, }; + /// public override PrimitiveType PrimitiveType { get; } = PrimitiveType.Quads; + /// public override BoundingBox BoundingBox { get; } = new BoundingBox(new Vector3(-.5f, -.5f, 0), new Vector3(.5f, .5f, 0)); //public override int[] Indices { get; set; } = new[] {0, 1, 2, 3}; diff --git a/SMCode/SM.Base/SM.Base.csproj b/SMCode/SM.Base/SM.Base.csproj index 2a8d9f5..4c63000 100644 --- a/SMCode/SM.Base/SM.Base.csproj +++ b/SMCode/SM.Base/SM.Base.csproj @@ -22,6 +22,7 @@ prompt 4 latest + bin\Debug\SM.Base.xml pdbonly @@ -64,12 +65,14 @@ - - - - + + + + + + + - diff --git a/SMCode/SM.Base/Scene/GenericCamera.cs b/SMCode/SM.Base/Scene/GenericCamera.cs index 70dc17d..e6d3887 100644 --- a/SMCode/SM.Base/Scene/GenericCamera.cs +++ b/SMCode/SM.Base/Scene/GenericCamera.cs @@ -2,25 +2,62 @@ namespace SM.Base.Scene { + /// + /// Controller for a camera + /// public abstract class GenericCamera { + /// + /// The matrix for the orthographic world. + /// public static Matrix4 OrthographicWorld { get; protected set; } + /// + /// The matrix for the perspective world. + /// public static Matrix4 PerspectiveWorld { get; protected set; } + /// + /// This defines what is up. (Normalized) + /// Default: + /// public static Vector3 UpVector { get; set; } = Vector3.UnitY; - public Matrix4 ViewMatrix { get; protected set; } + /// + /// Contains the view matrix of this camera. + /// Default: + /// + public Matrix4 ViewMatrix { get; protected set; } = Matrix4.Identity; + /// + /// Returns the world matrix that is connected to this camera. + /// public Matrix4 World => Orthographic ? OrthographicWorld : PerspectiveWorld; + /// + /// Calculates the view matrix. + /// + /// The calculated view matrix. Same as internal Matrix4 CalculateViewMatrix() { ViewMatrix = ViewCalculation(); return ViewMatrix; } + /// + /// This calculates the view matrix. + /// + /// The new view matrix. This is the returns for and the next value for . protected abstract Matrix4 ViewCalculation(); + /// + /// Represents if the camera is orthographic. + /// public abstract bool Orthographic { get; } + /// + /// This will calculate the world. + /// This is called on to calculate the world. + /// + /// The world scale + /// The aspect ratio from the window. public abstract void RecalculateWorld(Vector2 world, float aspect); } } \ No newline at end of file diff --git a/SMCode/SM.Base/Scene/GenericItemCollection.cs b/SMCode/SM.Base/Scene/GenericItemCollection.cs index cc117a0..84d081f 100644 --- a/SMCode/SM.Base/Scene/GenericItemCollection.cs +++ b/SMCode/SM.Base/Scene/GenericItemCollection.cs @@ -3,23 +3,50 @@ using SM.Base.Contexts; namespace SM.Base.Scene { - public abstract class GenericItemCollection : IShowItem, IShowCollection + /// + /// Contains a list of show items. + /// + /// The type of show items. + public abstract class GenericItemCollection : IShowItem, IShowCollection where TItem : IShowItem - where TTransformation : GenericTransformation, new() { + /// public List Objects { get; } = new List(); - public TTransformation Transform = new TTransformation(); + + /// public void Update(UpdateContext context) { throw new System.NotImplementedException(); } + /// public virtual void Draw(DrawContext context) { - context.View = Transform.GetMatrix() * context.View; - for (int i = 0; i < Objects.Count; i++) Objects[i].Draw(context); } } + + /// + /// Contains a list of show items with transformation. + /// + /// The type of show items. + /// The type of transformation. + public abstract class GenericItemCollection : GenericItemCollection + where TItem : IShowItem + where TTransformation : GenericTransformation, new() + { + /// + /// Transformation of the collection + /// + public TTransformation Transform = new TTransformation(); + + /// + public override void Draw(DrawContext context) + { + context.View = Transform.GetMatrix() * context.View; + + base.Draw(context); + } + } } \ No newline at end of file diff --git a/SMCode/SM.Base/Scene/GenericScene.cs b/SMCode/SM.Base/Scene/GenericScene.cs index afbda70..6b6cdf9 100644 --- a/SMCode/SM.Base/Scene/GenericScene.cs +++ b/SMCode/SM.Base/Scene/GenericScene.cs @@ -4,20 +4,44 @@ using SM.Base.Contexts; namespace SM.Base.Scene { - public abstract class GenericScene : IShowCollection + /// + /// A generic scene that imports different functions. + /// + /// The type of cameras. + /// The type of show items. + public abstract class GenericScene : GenericItemCollection where TCamera : GenericCamera, new() where TItem : IShowItem { - - protected IBackgroundItem _background; - public List HUD { get; } = new List(); - public List Objects { get; } = new List(); + /// + /// The active camera, that is used if the context doesn't force the viewport camera. + /// If none set, it automaticly uses the viewport camera. + /// public TCamera Camera { get; set; } + /// + /// A camera to control the background. + /// public TCamera BackgroundCamera { get; set; } = new TCamera(); + /// + /// A camera to control the HUD. + /// public TCamera HUDCamera { get; set; } = new TCamera(); + + /// + /// This contains the background. + /// + protected IBackgroundItem _background; + /// + /// This defines the HUD objects. + /// + public List HUD { get; } = new List(); + /// + /// A collection for cameras to switch easier to different cameras. + /// public Dictionary Cameras = new Dictionary(); - public virtual void Draw(DrawContext context) + /// + public override void Draw(DrawContext context) { if (!context.ForceViewport && Camera != null) context.View = Camera.CalculateViewMatrix(); @@ -25,19 +49,24 @@ namespace SM.Base.Scene backgroundDrawContext.View = BackgroundCamera.CalculateViewMatrix(); _background?.Draw(backgroundDrawContext); - for(int i = 0; i < Objects.Count; i++) - Objects[i].Draw(context); + base.Draw(context); context.View = HUDCamera.CalculateViewMatrix(); for (int i = 0; i < HUD.Count; i++) HUD[i].Draw(context); } + /// + /// Called, when the user activates the scene. + /// internal void Activate() { OnActivating(); } + /// + /// Called, when the user activates the scene. + /// protected virtual void OnActivating() { } } diff --git a/SMCode/SM.Base/Scene/IBackgroundItem.cs b/SMCode/SM.Base/Scene/IBackgroundItem.cs index bedd535..9b0c59f 100644 --- a/SMCode/SM.Base/Scene/IBackgroundItem.cs +++ b/SMCode/SM.Base/Scene/IBackgroundItem.cs @@ -1,5 +1,8 @@ namespace SM.Base.Scene { + /// + /// A iteration of to reduce clutter. + /// public interface IBackgroundItem : IShowItem { diff --git a/SMCode/SM.Base/Scene/IShowCollection.cs b/SMCode/SM.Base/Scene/IShowCollection.cs index 83d53a7..4af281f 100644 --- a/SMCode/SM.Base/Scene/IShowCollection.cs +++ b/SMCode/SM.Base/Scene/IShowCollection.cs @@ -3,10 +3,21 @@ using SM.Base.Contexts; namespace SM.Base.Scene { + /// + /// Adds functions, that is required for a collection. + /// + /// The type of show item. public interface IShowCollection where TItem : IShowItem { + /// + /// The object collection. + /// List Objects { get; } + /// + /// This draws the objects in the list. + /// + /// The context how the objects need to be drawn. void Draw(DrawContext context); } } \ No newline at end of file diff --git a/SMCode/SM.Base/Scene/IShowItem.cs b/SMCode/SM.Base/Scene/IShowItem.cs index c30f43f..41b021e 100644 --- a/SMCode/SM.Base/Scene/IShowItem.cs +++ b/SMCode/SM.Base/Scene/IShowItem.cs @@ -2,9 +2,20 @@ namespace SM.Base.Scene { + /// + /// Adds requirements to object, to be properly used as a update and/or draw item. + /// public interface IShowItem { + /// + /// Tells the object to update own systems. + /// + /// The update context void Update(UpdateContext context); + /// + /// Tells the object to draw its object. + /// + /// void Draw(DrawContext context); } } \ No newline at end of file diff --git a/SMCode/SM.Base/Text/CharParameter.cs b/SMCode/SM.Base/Text/CharParameter.cs index e21f2b2..cb9f3b3 100644 --- a/SMCode/SM.Base/Text/CharParameter.cs +++ b/SMCode/SM.Base/Text/CharParameter.cs @@ -1,16 +1,29 @@ using System; -using System.Configuration.Assemblies; -using OpenTK; -namespace SM.Data.Fonts +namespace SM.Base.Text { + /// + /// Contains information for a font character. + /// [Serializable] - public class CharParameter + public struct CharParameter { + /// + /// The position on the X-axis. + /// public int X; + /// + /// The width of the character. + /// public float Width; - public float RelativeX; - public float RelativeWidth; + /// + /// The normalized position inside the texture. + /// + public float NormalizedX; + /// + /// The normalized width inside the texture. + /// + public float NormalizedWidth; } } \ No newline at end of file diff --git a/SMCode/SM.Base/Text/Font.cs b/SMCode/SM.Base/Text/Font.cs index 71396e9..4225543 100644 --- a/SMCode/SM.Base/Text/Font.cs +++ b/SMCode/SM.Base/Text/Font.cs @@ -5,28 +5,50 @@ using System.Security.Policy; using OpenTK; using OpenTK.Graphics.OpenGL4; using OpenTK.Input; +using SM.Base.Textures; using SM.Data.Fonts; using SM.OGL.Texture; namespace SM.Base.Text { - public class Font : TextureBase + /// + /// Represents a font. + /// + public class Font : Texture { - public Dictionary Positions = new Dictionary(); - - public override TextureMinFilter Filter { get; set; } = TextureMinFilter.Linear; + /// public override TextureWrapMode WrapMode { get; set; } = TextureWrapMode.ClampToEdge; - + + /// + /// The font family, that is used to find the right font. + /// public FontFamily FontFamily; + /// + /// The font style. + /// Default: + /// public FontStyle FontStyle = FontStyle.Regular; + /// + /// The font size. + /// Default: 12 + /// public float FontSize = 12; + /// + /// The char set for the font. + /// Default: + /// public ICollection CharSet = FontCharStorage.SimpleUTF8; - public int Width { get; private set; } - public int Height { get; private set; } - public Bitmap Texture { get; private set; } + /// + /// This contains all information for the different font character. + /// + public Dictionary Positions = new Dictionary(); + /// + /// Generates a font from a font family from the specified path. + /// + /// The specified path public Font(string path) { PrivateFontCollection pfc = new PrivateFontCollection(); @@ -34,16 +56,26 @@ namespace SM.Base.Text FontFamily = pfc.Families[0]; } + /// + /// Generates a font from a specified font family. + /// + /// Font-Family public Font(FontFamily font) { FontFamily = font; } - + /// + /// Regenerates the texture. + /// public void RegenerateTexture() { - Bitmap map = new Bitmap(1000,20); Width = 0; Height = 0; + Positions = new Dictionary(); + + + Bitmap map = new Bitmap(1000, 20); + Dictionary charParams = new Dictionary(); using (System.Drawing.Font f = new System.Drawing.Font(FontFamily, FontSize, FontStyle)) { using (Graphics g = Graphics.FromImage(map)) @@ -56,7 +88,7 @@ namespace SM.Base.Text SizeF size = g.MeasureString(s, f); try { - Positions.Add(c, new CharParameter() { Width = size.Width, X = Width }); + charParams.Add(c, new[] {size.Width, Width }); } catch { @@ -71,25 +103,34 @@ namespace SM.Base.Text map = new Bitmap(Width, Height); using (Graphics g = Graphics.FromImage(map)) { - foreach (KeyValuePair keyValuePair in Positions) + foreach (KeyValuePair keyValuePair in charParams) { - keyValuePair.Value.RelativeX = (keyValuePair.Value.X + 0.00001f) / Width; - keyValuePair.Value.RelativeWidth = (keyValuePair.Value.Width) / Width; + float normalizedX = (keyValuePair.Value[1] + 0.00001f) / Width; + float normalizedWidth = (keyValuePair.Value[0]) / Width; - g.DrawString(keyValuePair.Key.ToString(), f, Brushes.White, keyValuePair.Value.X, 0); + CharParameter parameter; + Positions.Add(keyValuePair.Key, parameter = new CharParameter() + { + NormalizedWidth = normalizedWidth, + NormalizedX = normalizedX, + Width = keyValuePair.Value[0], + X = (int)keyValuePair.Value[1] + }); + + g.DrawString(keyValuePair.Key.ToString(), f, Brushes.White, parameter.X, 0); } } } - Texture = map; - - _id = SM.Base.Textures.Texture.GenerateTexture(map, Filter, WrapMode); + Map = map; + Recompile(); } + /// protected override void Compile() { - base.Compile(); RegenerateTexture(); + base.Compile(); } } } \ No newline at end of file diff --git a/SMCode/SM.Base/Text/FontCharStorage.cs b/SMCode/SM.Base/Text/FontCharStorage.cs index 38739d3..df95c98 100644 --- a/SMCode/SM.Base/Text/FontCharStorage.cs +++ b/SMCode/SM.Base/Text/FontCharStorage.cs @@ -1,7 +1,13 @@ namespace SM.Data.Fonts { + /// + /// Contains default char sets. + /// public class FontCharStorage { + /// + /// Contains the english alphabet and the common special character. + /// public static readonly char[] SimpleUTF8 = new char[] { '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', @@ -10,5 +16,13 @@ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~' }; + /// + /// Contains only numbers. + /// + public static readonly char[] Numbers = new[] + { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' + }; + } } \ No newline at end of file diff --git a/SMCode/SM.Base/Text/TextDrawingBasis.cs b/SMCode/SM.Base/Text/TextDrawingBasis.cs index 2301d0a..66cbfdf 100644 --- a/SMCode/SM.Base/Text/TextDrawingBasis.cs +++ b/SMCode/SM.Base/Text/TextDrawingBasis.cs @@ -7,12 +7,25 @@ using SM.Data.Fonts; namespace SM.Base.Text { + /// + /// Defines a basis for text drawing. + /// + /// Transformation type public abstract class TextDrawingBasis : DrawingBasis where TTransform : GenericTransformation, new() { - protected Instance[] _modelMatrixs; + /// + /// The different instances for drawing. + /// + protected Instance[] _instances; + /// + /// The text, that is drawn. + /// protected string _text; + /// + /// The font. + /// public Font Font { get => (Font) _material.Texture; @@ -23,6 +36,9 @@ namespace SM.Base.Text } } + /// + /// The text, that is drawn. + /// public string Text { get => _text; @@ -33,30 +49,47 @@ namespace SM.Base.Text } } + /// + /// The font color. + /// public Color4 Color { get => _material.Tint; set => _material.Tint = value; } + /// + /// The spacing between numbers. + /// Default: 1 + /// public float Spacing = 1; + /// + /// Creates a text object with a font. + /// + /// The font. protected TextDrawingBasis(Font font) { _material.Texture = font; } + + /// public override void Draw(DrawContext context) { base.Draw(context); - if (_modelMatrixs == null) GenerateMatrixes(); + if (_instances == null) GenerateMatrixes(); } + /// + /// This generates the instances. + /// + /// The font doesn't contain a character that is needed for the text. public void GenerateMatrixes() { if (!Font.WasCompiled) Font.RegenerateTexture(); - _modelMatrixs = new Instance[_text.Length]; + _instances = new Instance[_text.Length]; float x = 0; CharParameter _last = new CharParameter(); @@ -80,11 +113,11 @@ namespace SM.Base.Text Matrix4 matrix = Matrix4.CreateScale(parameter.Width, Font.Height, 1) * Matrix4.CreateTranslation(x, 0, 0); - _modelMatrixs[i] = new Instance + _instances[i] = new Instance { ModelMatrix = matrix, - TexturePosition = new Vector2(parameter.RelativeX, 0), - TextureScale = new Vector2(parameter.RelativeWidth, 1) + TexturePosition = new Vector2(parameter.NormalizedX, 0), + TextureScale = new Vector2(parameter.NormalizedWidth, 1) }; x += parameter.Width * Spacing; diff --git a/SMCode/SM.Base/Textures/Texture.cs b/SMCode/SM.Base/Textures/Texture.cs index 24c607d..020d2e7 100644 --- a/SMCode/SM.Base/Textures/Texture.cs +++ b/SMCode/SM.Base/Textures/Texture.cs @@ -6,13 +6,37 @@ using PixelFormat = System.Drawing.Imaging.PixelFormat; namespace SM.Base.Textures { + /// + /// Texture that can be drawn to an object. + /// public class Texture : TextureBase { + /// + /// The texture as bitmap. + /// public Bitmap Map; + /// + /// Decides if the bitmap will automatically dispose itself. + /// public bool AutoDispose = false; - public Texture(Bitmap map) : this(map, TextureMinFilter.Linear, TextureWrapMode.Repeat) {} + /// + /// Empty constructor + /// + protected Texture() {} + /// + /// Creates a texture with only the map. + /// Sets the filter to Linear and WrapMode to Repeat. + /// + /// The map + public Texture(Bitmap map) : this(map, TextureMinFilter.Linear, TextureWrapMode.Repeat) {} + /// + /// Creates the texture. + /// + /// The texture map + /// The filter + /// The wrap mode public Texture(Bitmap map, TextureMinFilter filter, TextureWrapMode wrapMode) { Map = map; @@ -20,6 +44,8 @@ namespace SM.Base.Textures WrapMode = wrapMode; } + + /// protected override void Compile() { base.Compile(); @@ -27,6 +53,22 @@ namespace SM.Base.Textures _id = GenerateTexture(Map, Filter, WrapMode, AutoDispose); } + /// + protected override void Dispose() + { + base.Dispose(); + + GL.DeleteTexture(this); + } + + /// + /// Generates a OpenGL-texture. + /// + /// The texture as bitmap + /// The filter + /// The wrap mode + /// Auto dispose of the bitmap? Default: false + /// public static int GenerateTexture(Bitmap map, TextureMinFilter filter, TextureWrapMode wrapMode, bool dispose = false) { int id = GL.GenTexture(); @@ -58,8 +100,9 @@ namespace SM.Base.Textures return id; } + /// + /// Converts a bitmap to a texture. + /// public static implicit operator Texture(Bitmap map) => new Texture(map); - public override TextureMinFilter Filter { get; set; } - public override TextureWrapMode WrapMode { get; set; } } } \ No newline at end of file diff --git a/SMCode/SM.Base/Time/Interval.cs b/SMCode/SM.Base/Time/Interval.cs new file mode 100644 index 0000000..bafd602 --- /dev/null +++ b/SMCode/SM.Base/Time/Interval.cs @@ -0,0 +1,48 @@ +using System; +using SM.Base.Contexts; + +namespace SM.Base.Time +{ + /// + /// Performs intervals. + /// + public class Interval : Timer + { + private bool _stop; + + /// + public Interval(float seconds) : base(seconds) + { + } + + /// + public Interval(TimeSpan timeSpan) : base(timeSpan) + { + } + + /// + protected override void Stopping(UpdateContext context) + { + TriggerEndAction(context); + if (_stop){base.Stop();} + else Reset(); + } + + /// + /// This will tell the interval to stop after the next iteration. + /// To stop immediately use + /// + public override void Stop() + { + _stop = true; + } + + /// + /// This will stop the interval immediately. + /// + public void Cancel() + { + base.Stop(); + } + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Time/Stopwatch.cs b/SMCode/SM.Base/Time/Stopwatch.cs new file mode 100644 index 0000000..bf1bab6 --- /dev/null +++ b/SMCode/SM.Base/Time/Stopwatch.cs @@ -0,0 +1,59 @@ +using System.Collections.Generic; +using SM.Base.Contexts; + +namespace SM.Base.Time +{ + /// + /// Represents a stopwatch. + /// + public class Stopwatch + { + private static List _activeStopwatches = new List(); + + /// + /// Contains how much time already has passed. (in seconds) + /// + public float Elapsed { get; private set; } + + /// + /// Starts the stopwatch. + /// + public virtual void Start() + { + _activeStopwatches.Add(this); + } + + /// + /// Performs a tick. + /// + /// + private protected virtual void Tick(UpdateContext context) + { + Elapsed += context.Deltatime; + } + + /// + /// Stops the stopwatch. + /// + public virtual void Stop() + { + _activeStopwatches.Remove(this); + } + + /// + /// Resets the stopwatch. + /// + public void Reset() + { + Elapsed = 0; + } + + internal static void PerformTicks(UpdateContext context) + { + for (var i = 0; i < _activeStopwatches.Count; i++) + { + _activeStopwatches[i].Tick(context); + } + } + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Time/Timer.cs b/SMCode/SM.Base/Time/Timer.cs new file mode 100644 index 0000000..473cf05 --- /dev/null +++ b/SMCode/SM.Base/Time/Timer.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using SM.Base.Contexts; + +namespace SM.Base.Time +{ + /// + /// Timer-System + /// + public class Timer : Stopwatch + { + + /// + /// The target time in seconds. + /// + public float Target { get; private set; } + /// + /// The already elapsed time but normalized to the target. + /// + public float ElapsedNormalized { get; private set; } + + /// + /// The event, that is triggered when the timer stops. + /// + public event Action EndAction; + + /// + /// Creates a timer with specified seconds. + /// + /// + public Timer(float seconds) + { + Target = seconds; + } + + /// + /// Creates a timer with a time span. + /// + /// + public Timer(TimeSpan timeSpan) + { + Target = (float)timeSpan.TotalSeconds; + } + + /// + public override void Start() + { + base.Start(); + Reset(); + } + + private protected override void Tick(UpdateContext context) + { + base.Tick(context); + + ElapsedNormalized = Elapsed / Target; + if (ElapsedNormalized >= 1) Stopping(context); + } + + /// + /// Occurs, when the timer tries to stop. + /// + protected virtual void Stopping(UpdateContext context) + { + EndAction?.Invoke(this, context); + Stop(); + } + /// + /// This will trigger + /// + /// + protected void TriggerEndAction(UpdateContext context) + { + EndAction?.Invoke(this, context); + } + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Types/CVector.cs b/SMCode/SM.Base/Types/CVector.cs new file mode 100644 index 0000000..12462a1 --- /dev/null +++ b/SMCode/SM.Base/Types/CVector.cs @@ -0,0 +1,176 @@ +using System; +using System.Configuration.Assemblies; +using System.Diagnostics; +using OpenTK; + +namespace SM.Base.Types +{ + /// + /// Represents a base vector-class + /// + public abstract class CVector + { + [DebuggerBrowsable(DebuggerBrowsableState.Never)] + private float _x = default; + [DebuggerBrowsable(DebuggerBrowsableState.Never)] + private float _y = default; + [DebuggerBrowsable(DebuggerBrowsableState.Never)] + private float _z = default; + [DebuggerBrowsable(DebuggerBrowsableState.Never)] + private float _w = default; + + /// + /// The X-component. + /// + [DebuggerBrowsable(DebuggerBrowsableState.Never)] + protected float _X + { + get => _x; + set => _x = value; + } + /// + /// The Y-component + /// + [DebuggerBrowsable(DebuggerBrowsableState.Never)] + protected float _Y + { + get => _y; + set => _y = value; + } + /// + /// The Z-component. + /// + [DebuggerBrowsable(DebuggerBrowsableState.Never)] + protected float _Z + { + get => _z; + set => _z = value; + } + /// + /// The W-component. + /// + [DebuggerBrowsable(DebuggerBrowsableState.Never)] + protected float _W + { + get => _w; + set => _w = value; + } + + /// + /// Creates a vector by setting every component to the same value. + /// + protected CVector(float uniform) : this(uniform, uniform, uniform, uniform) + { } + + /// + /// Creates a vector by setting values for each component. + /// + protected CVector(float x, float y, float z, float w) + { + _x = x; + _y = y; + _z = z; + _w = w; + } + + #region Set + /// + /// Sets the X and Y-component. + /// + protected void Set(float x, float y) + { + _X = x; + _Y = y; + } + /// + /// Sets the X and Y-component from a + /// + protected void Set(OpenTK.Vector2 vector) + { + Set(vector.X, vector.Y); + } + + /// + /// Sets the X, Y and Z-component. + /// + protected void Set(float x, float y, float z) + { + Set(x,y); + _Z = z; + } + + /// + /// Sets the X, Y and Z-component from a . + /// + /// + protected void Set(OpenTK.Vector3 vector) + { + Set(vector.X, vector.Y, vector.Z); + } + + /// + /// Sets the X, Y, Z and W-component. + /// + protected void Set(float x, float y, float z, float w) + { + Set(x,y,z); + _W = w; + } + + /// + /// Sets the X, Y, Z and W-component from a . + /// + protected void Set(OpenTK.Vector4 vector) + { + Set(vector.X, vector.Y, vector.Z, vector.W); + } + #endregion + + #region Add + + /// + /// Adds a to the X and Y-component. + /// + protected void Add(OpenTK.Vector2 vector) + { + _X += vector.X; + _Y += vector.Y; + } + + /// + /// Adds a to the X, Y and Z-component. + /// + protected void Add(OpenTK.Vector3 vector) + { + _X += vector.X; + _Y += vector.Y; + _Z += vector.Z; + } + + /// + /// Adds a to the X, Y, Z and W-component. + /// + protected void Add(OpenTK.Vector4 vector) + { + _X += vector.X; + _Y += vector.Y; + _Z += vector.Z; + _W += vector.W; + } + + #endregion + + /// + /// Transforms a to a + /// + public static implicit operator OpenTK.Vector2(CVector v) => new OpenTK.Vector2(v._x, v._y); + /// + /// Transforms a to a + /// + public static implicit operator OpenTK.Vector3(CVector v) => new OpenTK.Vector3(v._x, v._y, v._z); + /// + /// Transforms a to a + /// + public static implicit operator OpenTK.Vector4(CVector v) => new OpenTK.Vector4(v._x, v._y, v._z, v._w); + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Types/CVector2.cs b/SMCode/SM.Base/Types/CVector2.cs new file mode 100644 index 0000000..3e3d826 --- /dev/null +++ b/SMCode/SM.Base/Types/CVector2.cs @@ -0,0 +1,56 @@ +namespace SM.Base.Types +{ + /// + /// Represents a vector of two floats. + /// + public class CVector2 : CVector + { + /// + /// X-component + /// + public float X + { + get => _X; + set => _X = value; + } + /// + /// Y-component + /// + public float Y + { + get => _Y; + set => _Y = value; + } + + /// + public CVector2() : this(0) + {} + + /// + public CVector2(float uniform) : base(uniform) + { + + } + + /// + public CVector2(float x, float y) : base(x,y, default, default) {} + + /// + protected CVector2(float x, float y, float z, float w) : base(x, y, z, w) {} + + /// + /// Sets the X and Y-component. + /// + public new void Set(float x, float y) => base.Set(x, y); + + /// + /// Sets the X and Y-component from a + /// + public new void Set(OpenTK.Vector2 vector) => base.Set(vector); + + /// + /// Adds a to the X and Y-component. + /// + public new void Add(OpenTK.Vector2 vector) => base.Add(vector); + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Types/CVector3.cs b/SMCode/SM.Base/Types/CVector3.cs new file mode 100644 index 0000000..cc52788 --- /dev/null +++ b/SMCode/SM.Base/Types/CVector3.cs @@ -0,0 +1,46 @@ +using System.Drawing.Design; +using System.Runtime.InteropServices; +using OpenTK; + +namespace SM.Base.Types +{ + /// + /// Represents a Vector of three floats. + /// + public class CVector3 : CVector2 + { + /// + /// Z-component + /// + public float Z + { + get => _Z; + set => _Z = value; + } + + /// + public CVector3(float uniform) : base(uniform) + { } + + /// + public CVector3(float x, float y, float z) : base(x, y, z, default) + { } + + /// + protected CVector3(float x, float y, float z, float w) : base(x, y, z, w) { } + + /// + /// Sets the X, Y and Z-component. + /// + public new void Set(float x, float y, float z) => base.Set(x, y, z); + /// + /// Sets the X, Y and Z-component from a . + /// + public new void Set(Vector3 vector) => base.Set(vector); + + /// + /// Adds a to the X, Y and Z-component. + /// + public new void Add(Vector3 vector) => base.Add(vector); + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Types/CVector4.cs b/SMCode/SM.Base/Types/CVector4.cs new file mode 100644 index 0000000..b4cbf74 --- /dev/null +++ b/SMCode/SM.Base/Types/CVector4.cs @@ -0,0 +1,42 @@ +using OpenTK; + +namespace SM.Base.Types +{ + /// + /// Represents a vector of four floats. + /// + public class CVector4 : CVector3 + { + /// + /// W-component + /// + public float W + { + get => _W; + set => _W = value; + } + + /// + public CVector4(float uniform) : base(uniform) + { + } + + /// + public CVector4(float x, float y, float z, float w) : base(x, y, z, w) + { + } + + /// + /// Sets the X, Y, Z and W-component. + /// + public new void Set(float x, float y, float z, float w) => base.Set(x, y, z, w); + /// + /// Sets the X, Y, Z and W-component from a . + /// + public new void Set(Vector4 vector) => base.Set(vector); + /// + /// Adds a to the X, Y, Z and W-component. + /// + public new void Add(Vector4 vector) => base.Add(vector); + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Types/Vector.cs b/SMCode/SM.Base/Types/Vector.cs deleted file mode 100644 index f3ef9f3..0000000 --- a/SMCode/SM.Base/Types/Vector.cs +++ /dev/null @@ -1,110 +0,0 @@ -using System; -using System.Configuration.Assemblies; -using OpenTK; - -namespace SM.Base.Types -{ - public abstract class Vector - { - private float _x = default; - private float _y = default; - private float _z = default; - private float _w = default; - - protected float _X - { - get => _x; - set => _x = value; - } - protected float _Y - { - get => _y; - set => _y = value; - } - protected float _Z - { - get => _z; - set => _z = value; - } - protected float _W - { - get => _w; - set => _w = value; - } - - protected Vector(float uniform) : this(uniform, uniform, uniform, uniform) - { } - - protected Vector(float x, float y, float z, float w) - { - _x = x; - _y = y; - _z = z; - _w = w; - } - - #region Set - protected void Set(float x, float y) - { - _X = x; - _Y = y; - } - - protected void Set(OpenTK.Vector2 vector) - { - Set(vector.X, vector.Y); - } - - protected void Set(float x, float y, float z) - { - Set(x,y); - _Z = z; - } - - protected void Set(OpenTK.Vector3 vector) - { - Set(vector.X, vector.Y, vector.Z); - } - - protected void Set(float x, float y, float z, float w) - { - Set(x,y,z); - _W = w; - } - - protected void Set(OpenTK.Vector4 vector) - { - Set(vector.X, vector.Y, vector.Z, vector.W); - } - #endregion - - #region Add - - protected void Add(OpenTK.Vector2 vector) - { - _X += vector.X; - _Y += vector.Y; - } - protected void Add(OpenTK.Vector3 vector) - { - _X += vector.X; - _Y += vector.Y; - _Z += vector.Z; - } - protected void Add(OpenTK.Vector4 vector) - { - _X += vector.X; - _Y += vector.Y; - _Z += vector.Z; - _W += vector.W; - } - - #endregion - public static implicit operator OpenTK.Vector2(Vector v) => new OpenTK.Vector2(v._x, v._y); - public static implicit operator OpenTK.Vector3(Vector v) => new OpenTK.Vector3(v._x, v._y, v._z); - public static implicit operator OpenTK.Vector4(Vector v) => new OpenTK.Vector4(v._x, v._y, v._z, v._w); - - - - } -} \ No newline at end of file diff --git a/SMCode/SM.Base/Types/Vector2.cs b/SMCode/SM.Base/Types/Vector2.cs deleted file mode 100644 index 2dabb58..0000000 --- a/SMCode/SM.Base/Types/Vector2.cs +++ /dev/null @@ -1,33 +0,0 @@ -namespace SM.Base.Types -{ - public class Vector2 : Vector - { - public float X - { - get => _X; - set => _X = value; - } - public float Y - { - get => _Y; - set => _Y = value; - } - - public Vector2() : this(0) - {} - - public Vector2(float uniform) : base(uniform) - { - - } - public Vector2(float x, float y) : base(x,y, default, default) {} - protected Vector2(float x, float y, float z, float w) : base(x, y, z, w) {} - - public new void Set(float x, float y) => base.Set(x, y); - public new void Set(OpenTK.Vector2 vector) => base.Set(vector); - - public new void Add(OpenTK.Vector2 vector) => base.Add(vector); - - public static implicit operator Vector2(OpenTK.Vector2 v) => new Vector2(v.X, v.Y); - } -} \ No newline at end of file diff --git a/SMCode/SM.Base/Types/Vector3.cs b/SMCode/SM.Base/Types/Vector3.cs deleted file mode 100644 index 748b9d0..0000000 --- a/SMCode/SM.Base/Types/Vector3.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System.Runtime.InteropServices; - -namespace SM.Base.Types -{ - public class Vector3 : Vector2 - { - public float Z - { - get => _Z; - set => _Z = value; - } - - public Vector3(float uniform) : base(uniform) - { } - - public Vector3(float x, float y, float z) : base(x, y, z, default) - { } - - protected Vector3(float x, float y, float z, float w) : base(x, y, z, w) { } - - public static implicit operator Vector3(OpenTK.Vector3 v) => new Vector3(v.X, v.Y, v.Z); - } -} \ No newline at end of file diff --git a/SMCode/SM.Base/Types/Vector4.cs b/SMCode/SM.Base/Types/Vector4.cs deleted file mode 100644 index 5c8bf7f..0000000 --- a/SMCode/SM.Base/Types/Vector4.cs +++ /dev/null @@ -1,21 +0,0 @@ -namespace SM.Base.Types -{ - public class Vector4 : Vector3 - { - public float W - { - get => _W; - set => _W = value; - } - - public Vector4(float uniform) : base(uniform) - { - } - - public Vector4(float x, float y, float z, float w) : base(x, y, z, w) - { - } - - public static implicit operator Vector4(OpenTK.Vector4 v) => new Vector4(v.X, v.Y, v.Z, v.W); - } -} \ No newline at end of file diff --git a/SMCode/SM.Base/Utility/Assembly.cs b/SMCode/SM.Base/Utility/Assembly.cs index 1fe6639..5fb8d98 100644 --- a/SMCode/SM.Base/Utility/Assembly.cs +++ b/SMCode/SM.Base/Utility/Assembly.cs @@ -4,6 +4,9 @@ using System.Reflection; namespace SM.Utility { + /// + /// Contains utility functions for handling with assemblies. + /// public class AssemblyUtility { /// @@ -22,8 +25,19 @@ namespace SM.Utility public static string ReadAssemblyFile(string path) { return ReadAssemblyFile(Assembly.GetCallingAssembly(), path); } - public static Stream GetAssemblyStream(Assembly ass, string path) { return ass.GetManifestResourceStream(ass.GetName().Name + "." + path) ?? throw new InvalidOperationException("Assembly couldn't find resource at path '" + path + "'."); } + /// + /// Returns a stream of a requested resource. + /// + /// The assembly the resource is in. + /// The path to the resource. + /// The stream + public static Stream GetAssemblyStream(Assembly ass, string path) { return ass.GetManifestResourceStream(path) ?? throw new InvalidOperationException("Assembly couldn't find resource at path '" + path + "'."); } + /// + /// Returns a stream of a requested resource in the calling assembly. + /// + /// The path to the resource + /// The stream public static Stream GetAssemblyStream(string path) { return GetAssemblyStream(Assembly.GetCallingAssembly(), path); } } } \ No newline at end of file diff --git a/SMCode/SM.Base/Utility/BezierCurve.cs b/SMCode/SM.Base/Utility/BezierCurve.cs deleted file mode 100644 index 6076a86..0000000 --- a/SMCode/SM.Base/Utility/BezierCurve.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; - -namespace SM.Utility -{ - public class BezierCurve - { - public static float Calculate(float t, params float[] points) - { - int pointAmount = points.Length; - int itterations = pointAmount - 1; - - double x = Math.Pow(1 - t, itterations) * points[0]; - for (int i = 1; i < itterations; i++) - { - if (i % 2 == 0) x += itterations * (1 - t) * Math.Pow(t, itterations - 1) * points[i]; - else x += itterations * Math.Pow(1 - t, itterations - 1) * t * points[i]; - } - - x += Math.Pow(t, itterations) * points[pointAmount - 1]; - return (float)x; - } - } -} \ No newline at end of file diff --git a/SMCode/SM.Base/Utility/Deltatime.cs b/SMCode/SM.Base/Utility/Deltatime.cs index f8427be..2e46e6d 100644 --- a/SMCode/SM.Base/Utility/Deltatime.cs +++ b/SMCode/SM.Base/Utility/Deltatime.cs @@ -1,15 +1,40 @@ namespace SM.Utility { + /// + /// A assistant to control the delta time. + /// public class Deltatime { + /// + /// The current update delta time. + /// public static float UpdateDelta { get; internal set; } + /// + /// The current render delta time. + /// public static float RenderDelta { get; internal set; } + /// + /// If true, it uses , otherwise . + /// Default: false + /// public bool UseRender; + /// + /// Scaling of the delta time. + /// Default: 1 + /// public float Scale; + /// + /// The calculated delta time. + /// public float DeltaTime => (UseRender ? RenderDelta : UpdateDelta) * Scale; + /// + /// Creates a delta time assistant. + /// + /// The start scale for the delta time. Default: 1 + /// If true, it uses , otherwise . Default: false public Deltatime(float scale = 1, bool useRender = false) { UseRender = useRender; diff --git a/SMCode/SM.Base/Utility/Randomize.cs b/SMCode/SM.Base/Utility/Randomize.cs index 61ae0f5..a110088 100644 --- a/SMCode/SM.Base/Utility/Randomize.cs +++ b/SMCode/SM.Base/Utility/Randomize.cs @@ -2,20 +2,51 @@ namespace SM.Utility { + /// + /// A global helper class for randomization. + /// public class Randomize { + /// + /// The randomizer. + /// public static Random Randomizer = new Random(); + /// + /// Sets the seed for the randomizer. + /// + /// The specified seed. public static void SetSeed(int seed) { Randomizer = new Random(seed); } - public static bool GetBool(float tolerance) { return Randomizer.NextDouble() > tolerance; } + /// + /// Generates a double and checks if its under the tolerance. + /// + public static bool GetBool(float tolerance) { return Randomizer.NextDouble() < tolerance; } + /// + /// Generates a integer. + /// public static int GetInt() { return Randomizer.Next(); } + /// + /// Generates a integer with a maximum. + /// public static int GetInt(int max) { return Randomizer.Next(max); } + /// + /// Generates a integer with a minimum and maximum + /// public static int GetInt(int min, int max) { return Randomizer.Next(min, max); } + /// + /// Generates a float between 0 and 1. + /// public static float GetFloat() { return (float)Randomizer.NextDouble(); } + /// + /// Generates a float between 0 and the specified maximum. + /// public static float GetFloat(float max) { return (float)Randomizer.NextDouble() * max; } + /// + /// Generates a float between the specified minimum and the specified maximum. + /// public static float GetFloat(float min, float max) { return (float)Randomizer.NextDouble() * max + min; } } diff --git a/SMCode/SM.Base/Window/Contexts/DrawContext.cs b/SMCode/SM.Base/Window/Contexts/DrawContext.cs index a8adb02..a25c60e 100644 --- a/SMCode/SM.Base/Window/Contexts/DrawContext.cs +++ b/SMCode/SM.Base/Window/Contexts/DrawContext.cs @@ -5,18 +5,42 @@ using SM.OGL.Mesh; namespace SM.Base.Contexts { + /// + /// Contains important information for drawing. + /// public struct DrawContext { + /// + /// This says if it was forced to use the viewport camera. + /// public bool ForceViewport; - public bool Instancing; + /// + /// The current world matrix. + /// public Matrix4 World; + /// + /// The current view matrix. + /// public Matrix4 View; + /// + /// The drawing instances. + /// If there is only one, it's index 0 + /// public Instance[] Instances; + /// + /// The mesh. + /// public GenericMesh Mesh; + /// + /// The material. + /// public Material Material; + /// + /// The current world scale. + /// public Vector2 WorldScale; } } \ No newline at end of file diff --git a/SMCode/SM.Base/Window/Contexts/UpdateContext.cs b/SMCode/SM.Base/Window/Contexts/UpdateContext.cs index c1cd5a8..b20189a 100644 --- a/SMCode/SM.Base/Window/Contexts/UpdateContext.cs +++ b/SMCode/SM.Base/Window/Contexts/UpdateContext.cs @@ -1,7 +1,26 @@ -namespace SM.Base.Contexts +using OpenTK.Input; +using SM.Utility; + +namespace SM.Base.Contexts { + /// + /// The update context. + /// public struct UpdateContext { - public double Deltatime; + /// + /// The delta time. + /// + public float Deltatime => Defaults.DefaultDeltatime.DeltaTime; + + /// + /// The current keyboard state. + /// + public KeyboardState KeyboardState; + + /// + /// The current mouse state. + /// + public MouseState MouseState; } } \ No newline at end of file diff --git a/SMCode/SM.Base/Window/GenericWindow.cs b/SMCode/SM.Base/Window/GenericWindow.cs index 87e930c..1b92b4f 100644 --- a/SMCode/SM.Base/Window/GenericWindow.cs +++ b/SMCode/SM.Base/Window/GenericWindow.cs @@ -7,25 +7,36 @@ using OpenTK.Input; using SM.Base.Contexts; using SM.Base.Objects.Static; using SM.Base.Scene; +using SM.Base.Time; using SM.OGL; using SM.OGL.Shaders; using SM.Utility; namespace SM.Base { + /// + /// The base window. + /// public abstract class GenericWindow : GameWindow { private bool _loading = false; - public bool ForceViewportCamera { get; set; } = false; + /// + /// This tells you the current world scale. + /// protected Vector2 _worldScale = Vector2.Zero; + /// + /// This tells you the current aspect ratio of this window. + /// public float Aspect { get; private set; } = 0f; + /// protected GenericWindow() : base(1280, 720, GraphicsMode.Default, "Generic OGL Title", GameWindowFlags.Default, DisplayDevice.Default, 0, 0, GraphicsContextFlags.Default, null, true) { } + /// protected override void OnLoad(EventArgs e) { GLSystem.INIT_SYSTEM(); @@ -42,6 +53,8 @@ namespace SM.Base base.OnLoad(e); _loading = true; } + + /// protected override void OnResize(EventArgs e) { base.OnResize(e); @@ -58,25 +71,47 @@ namespace SM.Base } } + /// + /// This is triggered after all the window-loading has been done. + /// protected virtual void OnLoaded() { } + /// + /// Sets the world scale. + /// protected virtual void SetWorldScale() { } + /// protected override void OnUpdateFrame(FrameEventArgs e) { base.OnUpdateFrame(e); + Deltatime.UpdateDelta = (float)e.Time; + UpdateContext context = new UpdateContext() + { + KeyboardState = Keyboard.GetState(), + MouseState = Mouse.GetState() + }; + + Stopwatch.PerformTicks(context); } + /// + /// Grabs the cursor and make sure it doesn't leave the window. + /// + /// If true, it makes the cursor invisible. public void GrabCursor(bool makeItInvisible = true) { CursorGrabbed = true; CursorVisible = !makeItInvisible; } + /// + /// Ungrabs the cursor. + /// public void UngrabCursor() { CursorGrabbed = false; @@ -84,20 +119,38 @@ namespace SM.Base } } + /// + /// The base window. + /// + /// The scene type + /// The base item type + /// The camera type public abstract class GenericWindow : GenericWindow where TScene : GenericScene, new() where TItem : IShowItem where TCamera : GenericCamera, new() { + /// + /// The viewport camera. + /// public TCamera ViewportCamera { get; } + /// + /// This forces the render to use the viewport camera. + /// + public bool ForceViewportCamera { get; set; } = false; + /// + /// The current scene. + /// public TScene CurrentScene { get; private set; } + /// protected GenericWindow() { ViewportCamera = new TCamera(); } + /// protected override void OnRenderFrame(FrameEventArgs e) { DrawContext drawContext = new DrawContext() @@ -119,6 +172,7 @@ namespace SM.Base SwapBuffers(); } + /// protected override void OnResize(EventArgs e) { base.OnResize(e); @@ -126,6 +180,10 @@ namespace SM.Base ViewportCamera.RecalculateWorld(_worldScale, Aspect); } + /// + /// Sets the scene. + /// + /// public virtual void SetScene(TScene scene) { CurrentScene = scene; diff --git a/SMCode/SM.OGL/GLObject.cs b/SMCode/SM.OGL/GLObject.cs index ff4f57b..3de92f7 100644 --- a/SMCode/SM.OGL/GLObject.cs +++ b/SMCode/SM.OGL/GLObject.cs @@ -47,6 +47,22 @@ namespace SM.OGL } + /// + /// Is triggered, when something want to dispose this object. + /// + protected virtual void Dispose() {} + + /// + /// Re-compiles the object. + /// + public void Recompile() + { + if (!WasCompiled) return; + + Dispose(); + Compile(); + } + /// /// Names the object for debugging. /// diff --git a/SMCode/SM.OGL/Texture/TextureBase.cs b/SMCode/SM.OGL/Texture/TextureBase.cs index 634fa78..d2ffb23 100644 --- a/SMCode/SM.OGL/Texture/TextureBase.cs +++ b/SMCode/SM.OGL/Texture/TextureBase.cs @@ -15,11 +15,23 @@ namespace SM.OGL.Texture /// /// The texture filter. + /// Default: /// - public abstract TextureMinFilter Filter { get; set; } + public virtual TextureMinFilter Filter { get; set; } = TextureMinFilter.Linear; + /// /// The wrap mode. + /// Default: /// - public abstract TextureWrapMode WrapMode { get; set; } + public virtual TextureWrapMode WrapMode { get; set; } = TextureWrapMode.Repeat; + + /// + /// The Width of the texture + /// + public int Width { get; protected set; } + /// + /// The height of the texture + /// + public int Height { get; protected set; } } } \ No newline at end of file diff --git a/SMCode/SM2D/Drawing/DrawText.cs b/SMCode/SM2D/Drawing/DrawText.cs index 9558f78..e18ff07 100644 --- a/SMCode/SM2D/Drawing/DrawText.cs +++ b/SMCode/SM2D/Drawing/DrawText.cs @@ -11,13 +11,13 @@ namespace SM2D.Drawing public DrawText(Font font, string text) : base(font) { _text = text; - Transform.Size = new Vector2(1); + Transform.Size = new CVector2(1); } public override void Draw(DrawContext context) { base.Draw(context); - context.Instances = _modelMatrixs; + context.Instances = _instances; ApplyContext(ref context); context.View = Transform.GetMatrix() * context.View; diff --git a/SMCode/SM2D/Drawing/DrawTexture.cs b/SMCode/SM2D/Drawing/DrawTexture.cs index 53412ec..ad7b550 100644 --- a/SMCode/SM2D/Drawing/DrawTexture.cs +++ b/SMCode/SM2D/Drawing/DrawTexture.cs @@ -7,7 +7,6 @@ using SM.Base.Textures; using SM.Base.Types; using SM2D.Scene; using SM2D.Types; -using Vector2 = SM.Base.Types.Vector2; namespace SM2D.Drawing { @@ -37,7 +36,7 @@ namespace SM2D.Drawing public override void Draw(DrawContext context) { - Transform.Size = new Vector2(Texture.Map.Width * MasterScale * Scale, Texture.Map.Height * MasterScale * Scale); + Transform.Size = new CVector2(Texture.Map.Width * MasterScale * Scale, Texture.Map.Height * MasterScale * Scale); base.Draw(context); } } diff --git a/SMCode/SM2D/Scene/Camera.cs b/SMCode/SM2D/Scene/Camera.cs index ffd8de3..c5a4a12 100644 --- a/SMCode/SM2D/Scene/Camera.cs +++ b/SMCode/SM2D/Scene/Camera.cs @@ -1,6 +1,6 @@ using OpenTK; using SM.Base.Scene; -using Vector2 = SM.Base.Types.Vector2; +using SM.Base.Types; namespace SM2D.Scene { @@ -8,7 +8,7 @@ namespace SM2D.Scene { public override bool Orthographic { get; } = true; - public Vector2 Position = new Vector2(0); + public CVector2 Position = new CVector2(0); protected override Matrix4 ViewCalculation() { diff --git a/SMCode/SM2D/Scene/ItemCollection.cs b/SMCode/SM2D/Scene/ItemCollection.cs index bacad5e..986f5d4 100644 --- a/SMCode/SM2D/Scene/ItemCollection.cs +++ b/SMCode/SM2D/Scene/ItemCollection.cs @@ -9,7 +9,7 @@ namespace SM2D.Scene { public ItemCollection() { - Transform.Size = new Vector2(1); + Transform.Size = new CVector2(1); } public override void Draw(DrawContext context) diff --git a/SMCode/SM2D/Shader/Default2DShader.cs b/SMCode/SM2D/Shader/Default2DShader.cs index 9ac6abc..1932adf 100644 --- a/SMCode/SM2D/Shader/Default2DShader.cs +++ b/SMCode/SM2D/Shader/Default2DShader.cs @@ -11,8 +11,8 @@ namespace SM2D.Shader protected override bool AutoCompile { get; } = true; public Default2DShader() : base(new ShaderFileCollection( - AssemblyUtility.ReadAssemblyFile("Shader.ShaderFiles.default.vert"), - AssemblyUtility.ReadAssemblyFile("Shader.ShaderFiles.default.frag"))) + AssemblyUtility.ReadAssemblyFile("SM2D.Shader.ShaderFiles.default.vert"), + AssemblyUtility.ReadAssemblyFile("SM2D.Shader.ShaderFiles.default.frag"))) { } diff --git a/SMCode/SM2D/Types/Transformation.cs b/SMCode/SM2D/Types/Transformation.cs index 980c53a..6e27787 100644 --- a/SMCode/SM2D/Types/Transformation.cs +++ b/SMCode/SM2D/Types/Transformation.cs @@ -1,13 +1,13 @@ using OpenTK; using SM.Base.Scene; -using Vector2 = SM.Base.Types.Vector2; +using SM.Base.Types; namespace SM2D.Types { public class Transformation : GenericTransformation { - public Vector2 Position = new Vector2(0); - public Vector2 Size = new Vector2(50); + public CVector2 Position = new CVector2(0); + public CVector2 Size = new CVector2(50); public float Rotation; public override Matrix4 GetMatrix() diff --git a/SM_TEST/Program.cs b/SM_TEST/Program.cs index a898f54..ae04fc0 100644 --- a/SM_TEST/Program.cs +++ b/SM_TEST/Program.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; using System.Drawing; +using System.Runtime.InteropServices; using OpenTK; using OpenTK.Graphics; +using SM.Base.Time; using SM2D; using SM2D.Drawing; using SM2D.Object; @@ -27,7 +29,7 @@ namespace SM_TEST }; window = new GLWindow2D {Scaling = new Vector2(0, 1000)}; - window.GrabCursor(); + //window.GrabCursor(); window.SetScene(scene = new Scene()); window.Load += WindowOnLoad; window.RenderFrame += WindowOnUpdateFrame; @@ -41,9 +43,13 @@ namespace SM_TEST private static void WindowOnLoad(object sender, EventArgs e) { + Interval timer = new Interval(5); + timer.EndAction += (timer1, context) => Console.WriteLine("Interval..."); + timer.Start(); + col = new ItemCollection() { - Transform = { Position = new SM.Base.Types.Vector2(0, -400) }, + Transform = { Position = new SM.Base.Types.CVector2(0, -400) }, ZIndex = 1 }; @@ -53,14 +59,14 @@ namespace SM_TEST }); col.Objects.Add(new DrawColor(Color4.Black) { - Transform = { Rotation = 45, Position = new SM.Base.Types.Vector2(0, 25) }, + Transform = { Rotation = 45, Position = new SM.Base.Types.CVector2(0, 25) }, ZIndex = 2 }); scene.Objects.Add(col); scene.Objects.Add(new DrawText(font, "Testing...") { - Transform = { Position = new SM.Base.Types.Vector2(0, -400)}, + Transform = { Position = new SM.Base.Types.CVector2(0, -400)}, Color = Color4.Black }); @@ -77,7 +83,7 @@ namespace SM_TEST new PolygonVertex(new Vector2(0, .25f), new Color4(10,10,10,255)) }), Color4.LawnGreen) { - Transform = {Position = new SM.Base.Types.Vector2(50,0)} + Transform = {Position = new SM.Base.Types.CVector2(50,0)} }); scene.Objects.Add(new DrawPolygon(new Polygon(new[] { @@ -91,7 +97,7 @@ namespace SM_TEST new PolygonVertex(new Vector2(0, .25f), new Color4(10,10,10,255)) }), new Bitmap("soldier_logo.png")) { - Transform = {Position = new SM.Base.Types.Vector2(-50,0)} + Transform = {Position = new SM.Base.Types.CVector2(-50,0)} }); scene.Background.Color = Color4.Beige;