diff --git a/SMCode/SM.Base/Drawing/DrawingBasis.cs b/SMCode/SM.Base/Drawing/DrawingBasis.cs index 1f6b5c0..ea53bc8 100644 --- a/SMCode/SM.Base/Drawing/DrawingBasis.cs +++ b/SMCode/SM.Base/Drawing/DrawingBasis.cs @@ -2,11 +2,12 @@ using System.Collections.Generic; using SM.Base.Contexts; +using SM.Base.Scene; using SM.OGL.Mesh; #endregion -namespace SM.Base.Scene +namespace SM.Base.Drawing { /// /// Contains general basis systems for drawing objects. @@ -31,12 +32,7 @@ namespace SM.Base.Scene /// public ICollection Flags { get; set; } - - /// - public virtual void Update(UpdateContext context) - { - } - + /// public void Draw(DrawContext context) { @@ -76,5 +72,11 @@ namespace SM.Base.Scene /// The current transformation. /// public TTransformation Transform = new TTransformation(); + + /// + protected override void DrawContext(ref DrawContext context) + { + context.ModelMaster = Transform.GetMatrix(); + } } } \ No newline at end of file diff --git a/SMCode/SM.Base/Drawing/GenericTransformation.cs b/SMCode/SM.Base/Drawing/GenericTransformation.cs index 90f98d8..970b81d 100644 --- a/SMCode/SM.Base/Drawing/GenericTransformation.cs +++ b/SMCode/SM.Base/Drawing/GenericTransformation.cs @@ -4,7 +4,7 @@ using OpenTK; #endregion -namespace SM.Base.Scene +namespace SM.Base.Drawing { /// /// Contains methods for using transformations right. diff --git a/SMCode/SM.Base/Drawing/Instance.cs b/SMCode/SM.Base/Drawing/Instance.cs index 4522d14..2473fa8 100644 --- a/SMCode/SM.Base/Drawing/Instance.cs +++ b/SMCode/SM.Base/Drawing/Instance.cs @@ -4,26 +4,26 @@ using OpenTK; #endregion -namespace SM.Base.Scene +namespace SM.Base.Drawing { /// /// This represens a drawing instance. /// - public struct Instance + public class Instance { /// /// The model matrix. /// - public Matrix4 ModelMatrix; + public Matrix4 ModelMatrix = Matrix4.Identity; /// /// The texture offset. /// - public Vector2 TexturePosition; + public Vector2 TexturePosition = Vector2.Zero; /// /// The texture scale. /// - public Vector2 TextureScale; + public Vector2 TextureScale = Vector2.One; } } \ No newline at end of file diff --git a/SMCode/SM.Base/Drawing/Material.cs b/SMCode/SM.Base/Drawing/Material.cs index 2582626..a1bf6d1 100644 --- a/SMCode/SM.Base/Drawing/Material.cs +++ b/SMCode/SM.Base/Drawing/Material.cs @@ -5,7 +5,7 @@ using SM.OGL.Texture; #endregion -namespace SM.Base.Scene +namespace SM.Base.Drawing { /// /// Represents a material. diff --git a/SMCode/SM.Base/Drawing/MaterialShader.cs b/SMCode/SM.Base/Drawing/MaterialShader.cs index 13c7a31..758a69d 100644 --- a/SMCode/SM.Base/Drawing/MaterialShader.cs +++ b/SMCode/SM.Base/Drawing/MaterialShader.cs @@ -6,24 +6,29 @@ using SM.OGL.Shaders; #endregion -namespace SM.Base.Scene +namespace SM.Base.Drawing { /// /// A general class to work with material shaders properly. /// public abstract class MaterialShader : GenericShader { + /// + protected MaterialShader(string combinedData) : base(combinedData) + {} + /// protected MaterialShader(string vertex, string fragment) : base(vertex, fragment) { } + /// protected MaterialShader(ShaderFileCollection shaderFileFiles) : base(shaderFileFiles) { } /// - /// Draws the context. + /// Prepares the context for the drawing. /// /// The context public virtual void Draw(DrawContext context) @@ -39,6 +44,10 @@ namespace SM.Base.Scene GL.UseProgram(0); } + /// + /// Draws the context. + /// + /// protected virtual void DrawProcess(DrawContext context) { diff --git a/SMCode/SM.Base/Drawing/Particles/ParticleContext.cs b/SMCode/SM.Base/Drawing/Particles/ParticleContext.cs new file mode 100644 index 0000000..79ca40e --- /dev/null +++ b/SMCode/SM.Base/Drawing/Particles/ParticleContext.cs @@ -0,0 +1,19 @@ +using SM.Base.Time; + +namespace SM.Base.Drawing.Particles +{ + /// + /// A context, with that the particle system sends the information for the movement function. + /// + public struct ParticleContext + { + /// + /// The Timer of the particles + /// + public Timer Timer; + /// + /// The current speed of the particles. + /// + public float Speed; + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Drawing/Particles/ParticleDrawingBasis.cs b/SMCode/SM.Base/Drawing/Particles/ParticleDrawingBasis.cs new file mode 100644 index 0000000..281e5e7 --- /dev/null +++ b/SMCode/SM.Base/Drawing/Particles/ParticleDrawingBasis.cs @@ -0,0 +1,126 @@ +using System; +using System.Collections.Generic; +using OpenTK; +using SM.Base.Contexts; +using SM.Base.Scene; +using SM.Base.Time; +using SM.Base.Types; +using SM.OGL.Shaders; + +namespace SM.Base.Drawing.Particles +{ + /// + /// The (drawing) basis for particles + /// + public abstract class ParticleDrawingBasis : DrawingBasis, IScriptable + where TTransform : GenericTransformation, new() + where TDirection : struct + { + + /// + /// This contains all important information for each particle. + /// + protected ParticleStruct[] particleStructs; + /// + /// This contains the different instances for the particles. + /// + protected List instances; + + /// + /// The stopwatch of the particles. + /// + protected Timer timer; + + /// + /// The amount of particles + /// + public int Amount = 32; + /// + /// The maximum speed of the particles + /// + public float MaxSpeed = 1; + + /// + /// Get/Sets the state of pausing. + /// + public bool Paused + { + get => timer.Paused; + set => timer.Paused = value; + } + + /// + /// Controls the movement of each particles. + /// + public abstract Func MovementCalculation { get; set; } + + protected ParticleDrawingBasis(TimeSpan duration) + { + timer = new Timer(duration); + } + + /// + /// Triggers the particles. + /// + public void Trigger() + { + timer.Start(); + + CreateParticles(); + } + + /// + public void Update(UpdateContext context) + { + if (!timer.Running) return; + + ParticleContext particleContext = new ParticleContext() + { + Timer = timer, + }; + + for (int i = 0; i < Amount; i++) + { + particleContext.Speed = particleStructs[i].Speed; + instances[i].ModelMatrix = CreateMatrix(particleStructs[i], MovementCalculation(particleStructs[i].Direction, particleContext)); + } + } + + /// + protected override void DrawContext(ref DrawContext context) + { + if (!timer.Active) return; + + base.DrawContext(ref context); + + context.Instances = instances; + + context.Shader.Draw(context); + } + + /// + /// Creates the particles. + /// + protected virtual void CreateParticles() + { + particleStructs = new ParticleStruct[Amount]; + instances = new List(); + for (int i = 0; i < Amount; i++) + { + particleStructs[i] = CreateObject(i); + + instances.Add(new Instance()); + } + } + + /// + /// Creates a particle. + /// + protected abstract ParticleStruct CreateObject(int index); + + /// + /// Generates the desired matrix for drawing. + /// + protected abstract Matrix4 CreateMatrix(ParticleStruct Struct, TDirection relativePosition); + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Drawing/Particles/ParticleMovement.cs b/SMCode/SM.Base/Drawing/Particles/ParticleMovement.cs new file mode 100644 index 0000000..a989898 --- /dev/null +++ b/SMCode/SM.Base/Drawing/Particles/ParticleMovement.cs @@ -0,0 +1,19 @@ +using OpenTK; + +namespace SM.Base.Drawing.Particles +{ + /// + /// Contains methods for particle movements. + /// + public class ParticleMovement + { + /// + /// Default movement for 2D. + /// + public static Vector2 Default2D(Vector2 direction, ParticleContext context) => direction * (context.Timer.Elapsed * context.Speed); + /// + /// Default movement for 3D. + /// + public static Vector3 Default3D(Vector3 direction, ParticleContext context) => direction * (context.Timer.Elapsed * context.Speed); + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Drawing/Particles/ParticleStruct.cs b/SMCode/SM.Base/Drawing/Particles/ParticleStruct.cs new file mode 100644 index 0000000..52660e8 --- /dev/null +++ b/SMCode/SM.Base/Drawing/Particles/ParticleStruct.cs @@ -0,0 +1,25 @@ +using OpenTK; +using SM.Base.Types; + +namespace SM.Base.Drawing.Particles +{ + /// + /// A particle... + /// + public struct ParticleStruct + where TDirection : struct + { + /// + /// A direction, that the particle should travel. + /// + public TDirection Direction; + /// + /// A matrix to store rotation and scale. + /// + public Matrix4 Matrix; + /// + /// Speeeeeeeeeed + /// + public float Speed; + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Text/CharParameter.cs b/SMCode/SM.Base/Drawing/Text/CharParameter.cs similarity index 95% rename from SMCode/SM.Base/Text/CharParameter.cs rename to SMCode/SM.Base/Drawing/Text/CharParameter.cs index d5da9e9..1b9fc87 100644 --- a/SMCode/SM.Base/Text/CharParameter.cs +++ b/SMCode/SM.Base/Drawing/Text/CharParameter.cs @@ -4,7 +4,7 @@ using System; #endregion -namespace SM.Base.Text +namespace SM.Base.Drawing.Text { /// /// Contains information for a font character. diff --git a/SMCode/SM.Base/Text/Font.cs b/SMCode/SM.Base/Drawing/Text/Font.cs similarity index 99% rename from SMCode/SM.Base/Text/Font.cs rename to SMCode/SM.Base/Drawing/Text/Font.cs index 4be852c..183b612 100644 --- a/SMCode/SM.Base/Text/Font.cs +++ b/SMCode/SM.Base/Drawing/Text/Font.cs @@ -5,11 +5,10 @@ using System.Drawing; using System.Drawing.Text; using OpenTK.Graphics.OpenGL4; using SM.Base.Textures; -using SM.Data.Fonts; #endregion -namespace SM.Base.Text +namespace SM.Base.Drawing.Text { /// /// Represents a font. diff --git a/SMCode/SM.Base/Text/FontCharStorage.cs b/SMCode/SM.Base/Drawing/Text/FontCharStorage.cs similarity index 96% rename from SMCode/SM.Base/Text/FontCharStorage.cs rename to SMCode/SM.Base/Drawing/Text/FontCharStorage.cs index 4a4a59f..6c39c80 100644 --- a/SMCode/SM.Base/Text/FontCharStorage.cs +++ b/SMCode/SM.Base/Drawing/Text/FontCharStorage.cs @@ -1,4 +1,4 @@ -namespace SM.Data.Fonts +namespace SM.Base.Drawing.Text { /// /// Contains default char sets. diff --git a/SMCode/SM.Base/Text/TextDrawingBasis.cs b/SMCode/SM.Base/Drawing/Text/TextDrawingBasis.cs similarity index 98% rename from SMCode/SM.Base/Text/TextDrawingBasis.cs rename to SMCode/SM.Base/Drawing/Text/TextDrawingBasis.cs index fa32628..9c08ffe 100644 --- a/SMCode/SM.Base/Text/TextDrawingBasis.cs +++ b/SMCode/SM.Base/Drawing/Text/TextDrawingBasis.cs @@ -4,11 +4,10 @@ using System; using OpenTK; using OpenTK.Graphics; using SM.Base.Contexts; -using SM.Base.Scene; #endregion -namespace SM.Base.Text +namespace SM.Base.Drawing.Text { /// /// Defines a basis for text drawing. diff --git a/SMCode/SM.Base/PostProcess/PostProcessShader.cs b/SMCode/SM.Base/PostProcess/PostProcessShader.cs index ac22477..f143296 100644 --- a/SMCode/SM.Base/PostProcess/PostProcessShader.cs +++ b/SMCode/SM.Base/PostProcess/PostProcessShader.cs @@ -8,20 +8,33 @@ using SM.Utility; namespace SM.Base.PostProcess { + /// + /// Specific shader for post processing. + /// public class PostProcessShader : GenericShader { - private static ShaderFile _fragExtensions = new ShaderFile(AssemblyUtility.ReadAssemblyFile("SM.Base.PostProcess.DefaultFiles.extensions.frag")); - private static ShaderFile _normalVertex = new ShaderFile(AssemblyUtility.ReadAssemblyFile("SM.Base.PostProcess.DefaultFiles.vertexFile.vert")); - private static string _normalVertexWithExt = + private static readonly ShaderFile _fragExtensions = new ShaderFile(AssemblyUtility.ReadAssemblyFile("SM.Base.PostProcess.DefaultFiles.extensions.frag")); + private static readonly ShaderFile _normalVertex = new ShaderFile(AssemblyUtility.ReadAssemblyFile("SM.Base.PostProcess.DefaultFiles.vertexFile.vert")); + private static readonly string _normalVertexWithExt = AssemblyUtility.ReadAssemblyFile("SM.Base.PostProcess.DefaultFiles.vertexWithExt.vert"); + /// + /// Creates the shader with the default vertex shader and custom fragment. + /// public PostProcessShader(string fragment) : this(_normalVertex, - new ShaderFile(fragment)) { } + new ShaderFile(fragment)) + { } + /// + /// Creates the shader with an vertex extension and custom fragment. + /// + /// + /// public PostProcessShader(string vertexExt, string fragment) : this(new ShaderFile(_normalVertexWithExt) { GLSLExtensions = new List() { new ShaderFile(vertexExt) } - }, new ShaderFile(fragment)) { } + }, new ShaderFile(fragment)) + { } private PostProcessShader(ShaderFile vertex, ShaderFile fragment) : base( new ShaderFileCollection(vertex, fragment)) @@ -29,6 +42,10 @@ namespace SM.Base.PostProcess fragment.GLSLExtensions.Add(_fragExtensions); } + /// + /// Draws the shader without special uniforms. + /// + /// public void Draw(ColorAttachment color) { GL.UseProgram(this); @@ -44,6 +61,11 @@ namespace SM.Base.PostProcess GL.UseProgram(0); } + /// + /// Draws the shader with special uniforms. + /// + /// + /// public void Draw(ColorAttachment color, Action setUniformAction) { GL.UseProgram(this); diff --git a/SMCode/SM.Base/SM.Base.csproj b/SMCode/SM.Base/SM.Base.csproj index 3cfafc1..2fe8e21 100644 --- a/SMCode/SM.Base/SM.Base.csproj +++ b/SMCode/SM.Base/SM.Base.csproj @@ -50,10 +50,15 @@ + + + + + @@ -62,10 +67,10 @@ - - - - + + + + diff --git a/SMCode/SM.Base/SMRenderer.cs b/SMCode/SM.Base/SMRenderer.cs index 5585c00..d69a1c7 100644 --- a/SMCode/SM.Base/SMRenderer.cs +++ b/SMCode/SM.Base/SMRenderer.cs @@ -1,8 +1,9 @@ #region usings +using SM.Base.Drawing; +using SM.Base.Drawing.Text; using SM.Base.Objects.Static; using SM.Base.Scene; -using SM.Base.Text; using SM.OGL.Mesh; using SM.OGL.Shaders; using SM.Utility; @@ -36,6 +37,9 @@ namespace SM.Base /// public static Deltatime DefaultDeltatime = new Deltatime(); + /// + /// The default material shader. + /// public static MaterialShader DefaultMaterialShader; /// @@ -48,7 +52,9 @@ namespace SM.Base /// public static ulong CurrentFrame { get; internal set; } = 0; - public static GenericWindow CurrentWindow; - public static GenericScene CurrentScene; + /// + /// Represents the current active window. + /// + public static GenericWindow CurrentWindow { get; internal set; } } } \ No newline at end of file diff --git a/SMCode/SM.Base/Scene/GenericItemCollection.cs b/SMCode/SM.Base/Scene/GenericItemCollection.cs index d5a4ca9..3e70f59 100644 --- a/SMCode/SM.Base/Scene/GenericItemCollection.cs +++ b/SMCode/SM.Base/Scene/GenericItemCollection.cs @@ -1,7 +1,9 @@ #region usings using System.Collections.Generic; +using System.Collections.ObjectModel; using SM.Base.Contexts; +using SM.Base.Drawing; #endregion @@ -11,9 +13,15 @@ namespace SM.Base.Scene /// Contains a list of show items. /// /// The type of show items. - public abstract class GenericItemCollection : List, IShowItem, IShowCollection + public abstract class GenericItemCollection : List, IShowItem, IShowCollection, IScriptable where TItem : IShowItem { + private List _scriptableObjects = new List(); + + /// + /// Currently active script objects. + /// + public ReadOnlyCollection ScriptableObjects => new ReadOnlyCollection(_scriptableObjects); /// public List Objects => this; @@ -24,18 +32,20 @@ namespace SM.Base.Scene public string Name { get; set; } = "Unnamed Item Collection"; /// - public ICollection Flags { get; set; } = new[] {"collection"}; + public ICollection Flags { get; set; } = new List() {"collection"}; /// public virtual void Update(UpdateContext context) { - for (var i = 0; i < Objects.Count; i++) - this[i].Update(context); + for (var i = 0; i < _scriptableObjects.Count; i++) + _scriptableObjects[i].Update(context); } /// public virtual void Draw(DrawContext context) { + context.LastPassthough = this; + for (var i = 0; i < Objects.Count; i++) this[i].Draw(context); } @@ -51,26 +61,68 @@ namespace SM.Base.Scene } /// - /// Adds a item. + /// Adds a item to the draw and the script collection, when applicable. /// public new void Add(TItem item) + { + AddObject(item); + + if (item is IScriptable scriptable) + AddScript(scriptable); + + } + + /// + /// Adds the object to the collection. + /// + /// + public void AddObject(TItem item) { base.Add(item); item.Parent = this; item.OnAdded(this); } + /// + /// Adds the script to the collection. + /// + /// + public void AddScript(IScriptable item) + { + _scriptableObjects.Add(item); + } /// - /// Removes a item. + /// Removes a item from the draw and script collection, when applicable. /// /// public new void Remove(TItem item) + { + RemoveObject(item); + + if (item.GetType().IsAssignableFrom(typeof(IScriptable))) + RemoveScript((IScriptable)item); + } + + /// + /// Remove the object from the draw collection. + /// + /// + public void RemoveObject(TItem item) { base.Remove(item); item.Parent = null; item.OnRemoved(this); } + /// + /// Remove the object from the script collection. + /// + /// + public void RemoveScript(IScriptable item) + { + _scriptableObjects.Remove(item); + } + /// /// Returns a object with this name or the default, if not available. /// Not reclusive. @@ -111,6 +163,7 @@ namespace SM.Base.Scene for (var i = 0; i < Count; i++) { var obj = this[i]; + if (obj.Flags == null) continue; if (obj.Flags.Contains(flag)) list.Add(obj); } diff --git a/SMCode/SM.Base/Scene/GenericScene.cs b/SMCode/SM.Base/Scene/GenericScene.cs index 37168a1..d206b9d 100644 --- a/SMCode/SM.Base/Scene/GenericScene.cs +++ b/SMCode/SM.Base/Scene/GenericScene.cs @@ -13,6 +13,8 @@ namespace SM.Base.Scene /// public abstract class GenericScene { + + private IBackgroundItem _background; /// @@ -27,6 +29,9 @@ namespace SM.Base.Scene _background = value; } } + /// + /// If true, the scene was already initialized. + /// public bool IsInitialized { get; private set; } /// @@ -137,7 +142,6 @@ namespace SM.Base.Scene /// public override void Update(UpdateContext context) { - _Background?.Update(context); _objectCollection.Update(context); _hud.Update(context); } diff --git a/SMCode/SM.Base/Scene/IScriptable.cs b/SMCode/SM.Base/Scene/IScriptable.cs new file mode 100644 index 0000000..2772e3f --- /dev/null +++ b/SMCode/SM.Base/Scene/IScriptable.cs @@ -0,0 +1,15 @@ +using SM.Base.Contexts; + +namespace SM.Base.Scene +{ + /// + /// Defines a object as script. + /// + public interface IScriptable + { + /// + /// Updates the object. + /// + void Update(UpdateContext context); + } +} \ No newline at end of file diff --git a/SMCode/SM.Base/Scene/IShowItem.cs b/SMCode/SM.Base/Scene/IShowItem.cs index 5121b9f..b698073 100644 --- a/SMCode/SM.Base/Scene/IShowItem.cs +++ b/SMCode/SM.Base/Scene/IShowItem.cs @@ -26,13 +26,7 @@ namespace SM.Base.Scene /// Contains specific flags for the object. /// ICollection Flags { get; set; } - - /// - /// Tells the object to update own systems. - /// - /// The update context - void Update(UpdateContext context); - + /// /// Tells the object to draw its object. /// diff --git a/SMCode/SM.Base/Time/Interval.cs b/SMCode/SM.Base/Time/Interval.cs index dcbeb75..5f089a7 100644 --- a/SMCode/SM.Base/Time/Interval.cs +++ b/SMCode/SM.Base/Time/Interval.cs @@ -28,26 +28,7 @@ namespace SM.Base.Time 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(); + Reset(); } } } \ No newline at end of file diff --git a/SMCode/SM.Base/Time/Stopwatch.cs b/SMCode/SM.Base/Time/Stopwatch.cs index e064c2e..36d361c 100644 --- a/SMCode/SM.Base/Time/Stopwatch.cs +++ b/SMCode/SM.Base/Time/Stopwatch.cs @@ -1,5 +1,6 @@ #region usings +using System; using System.Collections.Generic; using SM.Base.Contexts; @@ -13,20 +14,47 @@ namespace SM.Base.Time public class Stopwatch { private static List _activeStopwatches = new List(); + private bool _paused = false; + + public bool Active { get; private set; } = false; + + public bool Paused + { + get => _paused; + set + { + if (value) + Pause(); + else + Resume(); + } + } + + public bool Running => Active && !Paused; /// /// Contains how much time already has passed. (in seconds) /// - public float Elapsed { get; private set; } + public float Elapsed { get; protected set; } + + /// + /// Contains the TimeSpan of how much time already passed. + /// + public TimeSpan ElapsedSpan { get; protected set; } /// /// Starts the stopwatch. /// public virtual void Start() { + if (Active) return; + _activeStopwatches.Add(this); + Active = true; } + + /// /// Performs a tick. /// @@ -34,6 +62,23 @@ namespace SM.Base.Time private protected virtual void Tick(UpdateContext context) { Elapsed += context.Deltatime; + ElapsedSpan = TimeSpan.FromSeconds(Elapsed); + } + + /// + /// Resumes the timer. + /// + protected virtual void Resume() + { + _paused = false; + } + + /// + /// Pauses the timer. + /// + protected virtual void Pause() + { + _paused = true; } /// @@ -41,6 +86,9 @@ namespace SM.Base.Time /// public virtual void Stop() { + if (!Active) return; + + Active = false; _activeStopwatches.Remove(this); } @@ -54,7 +102,11 @@ namespace SM.Base.Time internal static void PerformTicks(UpdateContext context) { - for (var i = 0; i < _activeStopwatches.Count; i++) _activeStopwatches[i].Tick(context); + for (var i = 0; i < _activeStopwatches.Count; i++) + { + if (_activeStopwatches[i].Paused) continue; + _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 index f25425b..2b973a8 100644 --- a/SMCode/SM.Base/Time/Timer.cs +++ b/SMCode/SM.Base/Time/Timer.cs @@ -65,7 +65,7 @@ namespace SM.Base.Time /// protected virtual void Stopping(UpdateContext context) { - EndAction?.Invoke(this, context); + TriggerEndAction(context); Stop(); } diff --git a/SMCode/SM.Base/Types/CVector2.cs b/SMCode/SM.Base/Types/CVector2.cs index 3fdf66b..a9128b2 100644 --- a/SMCode/SM.Base/Types/CVector2.cs +++ b/SMCode/SM.Base/Types/CVector2.cs @@ -73,6 +73,9 @@ namespace SM.Base.Types base.Add(vector); } + /// + /// Converts a to + /// public static implicit operator CVector2(Vector2 v) => new CVector2(v.X,v.Y); } } \ No newline at end of file diff --git a/SMCode/SM.Base/Utility/Randomize.cs b/SMCode/SM.Base/Utility/Randomize.cs index d8875cc..5597cc2 100644 --- a/SMCode/SM.Base/Utility/Randomize.cs +++ b/SMCode/SM.Base/Utility/Randomize.cs @@ -1,6 +1,9 @@ #region usings using System; +using System.Collections; +using System.Collections.Generic; +using System.Diagnostics; #endregion @@ -9,7 +12,7 @@ namespace SM.Utility /// /// A global helper class for randomization. /// - public class Randomize + public static class Randomize { /// /// The randomizer. @@ -78,7 +81,12 @@ namespace SM.Utility /// public static float GetFloat(float min, float max) { - return (float) Randomizer.NextDouble() * max + min; + return (float) Randomizer.NextDouble() * (max - min) + min; + } + + public static TSource GetRandomItem(this IList list) + { + return list[GetInt(0, list.Count - 1)]; } } } \ No newline at end of file diff --git a/SMCode/SM.Base/Window/Contexts/DrawContext.cs b/SMCode/SM.Base/Window/Contexts/DrawContext.cs index 332bb0b..1d31a22 100644 --- a/SMCode/SM.Base/Window/Contexts/DrawContext.cs +++ b/SMCode/SM.Base/Window/Contexts/DrawContext.cs @@ -1,6 +1,8 @@ #region usings +using System.Collections.Generic; using OpenTK; +using SM.Base.Drawing; using SM.Base.Scene; using SM.OGL.Mesh; @@ -28,6 +30,11 @@ namespace SM.Base.Contexts /// public Matrix4 View; + /// + /// The current WorldView matrix. + /// + public Matrix4 WorldView; + /// /// The master model matrix. /// @@ -37,7 +44,7 @@ namespace SM.Base.Contexts /// The drawing instances. /// If there is only one, it's index 0 /// - public Instance[] Instances; + public IList Instances; /// /// The mesh. @@ -59,6 +66,11 @@ namespace SM.Base.Contexts /// public Vector2 WorldScale; + /// + /// The last collection the context was passed though. + /// + public object LastPassthough; + /// /// Returns the appropriate shader. /// diff --git a/SMCode/SM.Base/Window/GenericWindow.cs b/SMCode/SM.Base/Window/GenericWindow.cs index 26f5f76..03a3fc8 100644 --- a/SMCode/SM.Base/Window/GenericWindow.cs +++ b/SMCode/SM.Base/Window/GenericWindow.cs @@ -8,6 +8,7 @@ using OpenTK.Graphics; using OpenTK.Graphics.OpenGL4; using OpenTK.Input; using SM.Base.Contexts; +using SM.Base.Drawing; using SM.Base.Objects.Static; using SM.Base.PostProcess; using SM.Base.Scene; @@ -38,6 +39,12 @@ namespace SM.Base /// public float Aspect { get; private set; } + /// + /// If false, the window will not react on updates and will not render something. + /// + /// Default: false + /// + /// public bool ReactWhileUnfocused = false; /// @@ -45,6 +52,9 @@ namespace SM.Base { } + /// + /// Creates a window... + /// protected GenericWindow(int width, int height, string title, GameWindowFlags flags, bool vSync = true) : base(width, height, GraphicsMode.Default, title, flags, DisplayDevice.Default, GLSettings.ForcedVersion.MajorVersion, GLSettings.ForcedVersion.MinorVersion, GraphicsContextFlags.Default) @@ -56,6 +66,8 @@ namespace SM.Base /// protected override void OnLoad(EventArgs e) { + SMRenderer.CurrentWindow = this; + GLSystem.INIT_SYSTEM(); GLSettings.ShaderPreProcessing = true; @@ -251,7 +263,8 @@ namespace SM.Base }, Mesh = Plate.Object, ForceViewport = ForceViewportCamera, - WorldScale = _worldScale + WorldScale = _worldScale, + LastPassthough = this }; base.OnRenderFrame(e); diff --git a/SMCode/SM.Base/Window/RenderPipeline.cs b/SMCode/SM.Base/Window/RenderPipeline.cs index 575c4ac..a7436a4 100644 --- a/SMCode/SM.Base/Window/RenderPipeline.cs +++ b/SMCode/SM.Base/Window/RenderPipeline.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Threading; using SM.Base.Contexts; +using SM.Base.Drawing; using SM.Base.Scene; using SM.OGL.Framebuffer; diff --git a/SMCode/SM.OGL/Shaders/GenericShader.cs b/SMCode/SM.OGL/Shaders/GenericShader.cs index b368685..afb8409 100644 --- a/SMCode/SM.OGL/Shaders/GenericShader.cs +++ b/SMCode/SM.OGL/Shaders/GenericShader.cs @@ -1,5 +1,7 @@ #region usings +using System; +using System.Linq; using OpenTK.Graphics.OpenGL4; using SM.OGL.Mesh; @@ -24,6 +26,46 @@ namespace SM.OGL.Shaders /// protected UniformCollection Uniforms; + protected GenericShader(string combinedData) + { + int firstPos = combinedData.IndexOf("//# region ", StringComparison.Ordinal); + string header = combinedData.Substring(0, firstPos); + + int regionAmount = combinedData.Split(new string[] {"//# region "}, StringSplitOptions.None).Length - 1; + int pos = firstPos + 10; + + string vertex = ""; + string geometry = ""; + string fragment = ""; + for (int i = 0; i < regionAmount; i++) + { + int posOfBreak = combinedData.IndexOf("\n", pos, StringComparison.Ordinal); + string name = combinedData.Substring(pos, posOfBreak - pos).Trim(); + + int nextPos = i == regionAmount - 1 ? combinedData.Length : combinedData.IndexOf("//# region ", posOfBreak, StringComparison.Ordinal); + + string data = header + combinedData.Substring(posOfBreak, nextPos - posOfBreak); + pos = nextPos + 10; + + switch (name) + { + case "vertex": + vertex = data.Replace("vmain()", "main()"); + break; + case "geometry": + geometry = data.Replace("gmain()", "main()"); + break; + case "fragment": + fragment = data.Replace("fmain()", "main()"); + break; + } + } + + Console.WriteLine(); + + ShaderFileFiles = new ShaderFileCollection(vertex,fragment, geometry); + } + protected GenericShader(string vertex, string fragment) : this(new ShaderFileCollection(vertex, fragment)){} /// @@ -47,8 +89,7 @@ namespace SM.OGL.Shaders Name(GetType().Name); ShaderFileFiles.Detach(this); - Uniforms = new UniformCollection(); - Uniforms.ParentShader = this; + Uniforms = new UniformCollection {ParentShader = this}; Uniforms.Import(this); GLDebugging.CheckGLErrors($"A error occured at shader creation for '{GetType()}': %code%"); diff --git a/SMCode/SM.OGL/Shaders/ShaderFileCollection.cs b/SMCode/SM.OGL/Shaders/ShaderFileCollection.cs index b15e2f6..aaba513 100644 --- a/SMCode/SM.OGL/Shaders/ShaderFileCollection.cs +++ b/SMCode/SM.OGL/Shaders/ShaderFileCollection.cs @@ -31,8 +31,8 @@ namespace SM.OGL.Shaders /// /// The vertex source file. /// The fragment source file. - public ShaderFileCollection(string vertex, string fragment) : this(new ShaderFile(vertex), - new ShaderFile(fragment)) + public ShaderFileCollection(string vertex, string fragment, string geometry = "") : this(new ShaderFile(vertex), + new ShaderFile(fragment), geometry != "" ? new ShaderFile(geometry) : null) { } diff --git a/SMCode/SM2D/Drawing/DrawBackground.cs b/SMCode/SM2D/Drawing/DrawBackground.cs index 33188ed..4522442 100644 --- a/SMCode/SM2D/Drawing/DrawBackground.cs +++ b/SMCode/SM2D/Drawing/DrawBackground.cs @@ -5,6 +5,7 @@ using System.Drawing; using OpenTK; using OpenTK.Graphics; using SM.Base.Contexts; +using SM.Base.Drawing; using SM.Base.Objects.Static; using SM.Base.Scene; using SM.Base.Textures; diff --git a/SMCode/SM2D/Drawing/DrawBackgroundShader.cs b/SMCode/SM2D/Drawing/DrawBackgroundShader.cs index 5bdf4f4..0480f1c 100644 --- a/SMCode/SM2D/Drawing/DrawBackgroundShader.cs +++ b/SMCode/SM2D/Drawing/DrawBackgroundShader.cs @@ -1,4 +1,5 @@ using SM.Base.Contexts; +using SM.Base.Drawing; using SM.Base.Scene; using SM2D.Scene; diff --git a/SMCode/SM2D/Drawing/DrawColor.cs b/SMCode/SM2D/Drawing/DrawColor.cs index 17ffa17..04826fa 100644 --- a/SMCode/SM2D/Drawing/DrawColor.cs +++ b/SMCode/SM2D/Drawing/DrawColor.cs @@ -2,6 +2,7 @@ using OpenTK.Graphics; using SM.Base.Contexts; +using SM.Base.Drawing; using SM.Base.Scene; using SM2D.Scene; using SM2D.Types; @@ -31,8 +32,6 @@ namespace SM2D.Drawing protected override void DrawContext(ref DrawContext context) { - context.Instances[0].ModelMatrix = Transform.GetMatrix(); - context.Shader.Draw(context); } } diff --git a/SMCode/SM2D/Drawing/DrawComplex.cs b/SMCode/SM2D/Drawing/DrawComplex.cs index c1a883e..b2df861 100644 --- a/SMCode/SM2D/Drawing/DrawComplex.cs +++ b/SMCode/SM2D/Drawing/DrawComplex.cs @@ -1,6 +1,7 @@ #region usings using SM.Base.Contexts; +using SM.Base.Drawing; using SM.Base.Scene; using SM.OGL.Mesh; using SM2D.Scene; @@ -28,7 +29,7 @@ namespace SM2D.Drawing protected override void DrawContext(ref DrawContext context) { - context.Instances[0].ModelMatrix = Transform.GetMatrix(); + base.DrawContext(ref context); context.Shader.Draw(context); } diff --git a/SMCode/SM2D/Drawing/DrawParticles.cs b/SMCode/SM2D/Drawing/DrawParticles.cs new file mode 100644 index 0000000..0eadc4e --- /dev/null +++ b/SMCode/SM2D/Drawing/DrawParticles.cs @@ -0,0 +1,34 @@ +using System; +using OpenTK; +using SM.Base.Drawing.Particles; +using SM.Utility; +using SM2D.Scene; +using SM2D.Types; + +namespace SM2D.Drawing +{ + public class DrawParticles : ParticleDrawingBasis, I2DShowItem + { + public int ZIndex { get; set; } + public override Func MovementCalculation { get; set; } = ParticleMovement.Default2D; + + public DrawParticles(TimeSpan duration) : base(duration) + { + } + + protected override ParticleStruct CreateObject(int index) + { + return new ParticleStruct() + { + Matrix = Matrix4.CreateScale(1), + Direction = new Vector2(Randomize.GetFloat(-1, 1), Randomize.GetFloat(-1, 1)), + Speed = Randomize.GetFloat(MaxSpeed) + }; + } + + protected override Matrix4 CreateMatrix(ParticleStruct Struct, Vector2 direction) + { + return Struct.Matrix * Matrix4.CreateTranslation(direction.X, direction.Y, 0); + } + } +} \ No newline at end of file diff --git a/SMCode/SM2D/Drawing/DrawShader.cs b/SMCode/SM2D/Drawing/DrawShader.cs index c95f8f6..d57d563 100644 --- a/SMCode/SM2D/Drawing/DrawShader.cs +++ b/SMCode/SM2D/Drawing/DrawShader.cs @@ -1,4 +1,5 @@ using SM.Base.Contexts; +using SM.Base.Drawing; using SM.Base.Scene; using SM2D.Scene; using SM2D.Types; @@ -16,8 +17,7 @@ namespace SM2D.Drawing protected override void DrawContext(ref DrawContext context) { - context.Instances[0].ModelMatrix = Transform.GetMatrix(); - + base.DrawContext(ref context); _material.CustomShader.Draw(context); } } diff --git a/SMCode/SM2D/Drawing/DrawText.cs b/SMCode/SM2D/Drawing/DrawText.cs index 38f78bc..e801c61 100644 --- a/SMCode/SM2D/Drawing/DrawText.cs +++ b/SMCode/SM2D/Drawing/DrawText.cs @@ -1,7 +1,7 @@ #region usings using SM.Base.Contexts; -using SM.Base.Text; +using SM.Base.Drawing.Text; using SM.Base.Types; using SM2D.Scene; using SM2D.Types; @@ -24,9 +24,7 @@ namespace SM2D.Drawing { base.DrawContext(ref context); context.Instances = _instances; - - context.View = Transform.GetMatrix() * context.View; - + context.Shader.Draw(context); } } diff --git a/SMCode/SM2D/SM2D.csproj b/SMCode/SM2D/SM2D.csproj index 73e4c50..a809619 100644 --- a/SMCode/SM2D/SM2D.csproj +++ b/SMCode/SM2D/SM2D.csproj @@ -44,6 +44,7 @@ + @@ -71,13 +72,10 @@ SM.OGL - - - - + \ No newline at end of file diff --git a/SMCode/SM2D/Scene/Camera.cs b/SMCode/SM2D/Scene/Camera.cs index e76c2a7..3900717 100644 --- a/SMCode/SM2D/Scene/Camera.cs +++ b/SMCode/SM2D/Scene/Camera.cs @@ -21,7 +21,7 @@ namespace SM2D.Scene public override void RecalculateWorld(Vector2 world, float aspect) { OrthographicWorld = - Matrix4.CreateOrthographicOffCenter(-world.X / 2, world.X / 2, world.Y / 2, -world.Y / 2, 0.1f, 4f); + Matrix4.CreateOrthographic(world.X, world.Y, 0.1f, 100f); } } } \ No newline at end of file diff --git a/SMCode/SM2D/Shader/Default2DShader.cs b/SMCode/SM2D/Shader/Default2DShader.cs index ae4d0da..d3692f7 100644 --- a/SMCode/SM2D/Shader/Default2DShader.cs +++ b/SMCode/SM2D/Shader/Default2DShader.cs @@ -2,6 +2,7 @@ using OpenTK.Graphics.OpenGL4; using SM.Base.Contexts; +using SM.Base.Drawing; using SM.Base.Scene; using SM.OGL.Shaders; using SM.Utility; @@ -13,12 +14,9 @@ namespace SM2D.Shader public class Default2DShader : MaterialShader { public static Default2DShader MaterialShader = new Default2DShader(); + - //protected override bool AutoCompile { get; } = true; - - private Default2DShader() : base(new ShaderFileCollection( - AssemblyUtility.ReadAssemblyFile("SM2D.Shader.ShaderFiles.default.vert"), - AssemblyUtility.ReadAssemblyFile("SM2D.Shader.ShaderFiles.default.frag"))) + private Default2DShader() : base(AssemblyUtility.ReadAssemblyFile("SM2D.Shader.ShaderFiles.default.glsl")) { Load(); } @@ -32,7 +30,7 @@ namespace SM2D.Shader Uniforms.GetArray("Instances").Set((i, uniforms) => { - if (i >= context.Instances.Length) return false; + if (i >= context.Instances.Count) return false; var instance = context.Instances[i]; uniforms["ModelMatrix"].SetMatrix4(instance.ModelMatrix); @@ -46,7 +44,7 @@ namespace SM2D.Shader Uniforms["Tint"].SetUniform4(context.Material.Tint); Uniforms["Texture"].SetTexture(context.Material.Texture, Uniforms["UseTexture"]); - DrawObject(context.Mesh, context.Instances.Length); + DrawObject(context.Mesh, context.Instances.Count); } } } \ No newline at end of file diff --git a/SMCode/SM2D/Shader/ShaderFiles/default.frag b/SMCode/SM2D/Shader/ShaderFiles/default.frag deleted file mode 100644 index fc8964f..0000000 --- a/SMCode/SM2D/Shader/ShaderFiles/default.frag +++ /dev/null @@ -1,15 +0,0 @@ -#version 330 - -in vec2 vTexture; -in vec4 vColor; - -uniform vec4 Tint; -uniform bool UseTexture; -uniform sampler2D Texture; - -layout(location = 0) out vec4 color; - -void main() { - color = vColor * Tint; - if (UseTexture) color *= texture(Texture, vTexture); -} \ No newline at end of file diff --git a/SMCode/SM2D/Shader/ShaderFiles/default.glsl b/SMCode/SM2D/Shader/ShaderFiles/default.glsl new file mode 100644 index 0000000..efab3a6 --- /dev/null +++ b/SMCode/SM2D/Shader/ShaderFiles/default.glsl @@ -0,0 +1,29 @@ +#version 330 + +//# region vertex + +//# import SM_base_vertex_basic +void ApplyTexModifier(); +void CheckVertexColor(); +void ApplyModelTransformation(); + +void vmain() { + ApplyTexModifier(); + CheckVertexColor(); + ApplyModelTransformation(); +} + +//# region fragment +in vec2 vTexture; +in vec4 vColor; + +uniform vec4 Tint; +uniform bool UseTexture; +uniform sampler2D Texture; + +layout(location = 0) out vec4 color; + +void fmain() { + color = vColor * Tint; + if (UseTexture) color *= texture(Texture, vTexture); +} \ No newline at end of file diff --git a/SMCode/SM2D/Shader/ShaderFiles/default.vert b/SMCode/SM2D/Shader/ShaderFiles/default.vert deleted file mode 100644 index 22bc148..0000000 --- a/SMCode/SM2D/Shader/ShaderFiles/default.vert +++ /dev/null @@ -1,12 +0,0 @@ -#version 330 - -//# import SM_base_vertex_basic -void ApplyTexModifier(); -void CheckVertexColor(); -void ApplyModelTransformation(); - -void main() { - ApplyTexModifier(); - CheckVertexColor(); - ApplyModelTransformation(); -} \ No newline at end of file diff --git a/SMCode/SM2D/Types/Transformation.cs b/SMCode/SM2D/Types/Transformation.cs index fbfd066..811e4f1 100644 --- a/SMCode/SM2D/Types/Transformation.cs +++ b/SMCode/SM2D/Types/Transformation.cs @@ -1,6 +1,7 @@ #region usings using OpenTK; +using SM.Base.Drawing; using SM.Base.Scene; using SM.Base.Types; using SM.Utility; diff --git a/SM_TEST/Program.cs b/SM_TEST/Program.cs index d732f17..4b02b87 100644 --- a/SM_TEST/Program.cs +++ b/SM_TEST/Program.cs @@ -5,13 +5,16 @@ using System.Runtime.InteropServices; using System.Security.Authentication.ExtendedProtection.Configuration; using OpenTK; using OpenTK.Graphics; +using OpenTK.Input; using SM.Base; +using SM.Base.Scene; using SM.Base.Time; +using SM.Utility; using SM2D; using SM2D.Drawing; using SM2D.Object; using SM2D.Scene; -using Font = SM.Base.Text.Font; +using Font = SM.Base.Drawing.Text.Font; using Vector2 = OpenTK.Vector2; namespace SM_TEST @@ -20,8 +23,6 @@ namespace SM_TEST { static Scene scene; private static Font font; - private static ItemCollection col; - private static DrawPolygon polyogn; private static GLWindow2D window; static void Main(string[] args) { @@ -40,75 +41,23 @@ namespace SM_TEST window.Run(); } + private static DrawParticles particles; private static void WindowOnUpdateFrame(object sender, FrameEventArgs e) { - Vector2 mousepos = window.Mouse.InWorld(); - //polyogn.Transform.Position.Set(mousepos); - polyogn.Transform.TurnTo(mousepos); + if (Keyboard.GetState()[Key.R]) + particles.Trigger(); + particles.Paused = Keyboard.GetState()[Key.P]; } private static void WindowOnLoad(object sender, EventArgs e) { - col = new ItemCollection() + particles = new DrawParticles(TimeSpan.FromSeconds(5)) { - Transform = { Position = new SM.Base.Types.CVector2(0, 400) }, - ZIndex = 1 + MaxSpeed = 10 }; + window.CurrentScene.Objects.Add(particles); - col.Add(new DrawTexture(new Bitmap("soldier_logo.png")) - { - ZIndex = 1 - }); - col.Add(new DrawColor(Color4.Black) - { - 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.CVector2(0, 400)}, - Color = Color4.Black - }); - - scene.Objects.Add(new DrawPolygon(Polygon.GenerateCircle(),Color4.Blue)); - scene.Objects.Add(polyogn = new DrawPolygon(new Polygon(new[] - { - new PolygonVertex(new Vector2(.25f, 0), Color4.White), - new PolygonVertex(new Vector2(.75f, 0), Color4.White), - new PolygonVertex(new Vector2(1, .25f), Color4.White), - new PolygonVertex(new Vector2(1, .75f), Color4.White), - new PolygonVertex(new Vector2(.75f, 1), Color4.White), - new PolygonVertex(new Vector2(.25f, 1), Color4.White), - new PolygonVertex(new Vector2(0, .75f), new Color4(10,10,10,255)), - new PolygonVertex(new Vector2(0, .25f), new Color4(10,10,10,255)) - }), Color4.LawnGreen) - { - Transform = {Position = new SM.Base.Types.CVector2(50,0)} - }); - scene.Objects.Add(new DrawPolygon(new Polygon(new[] - { - new PolygonVertex(new Vector2(.25f, 0), Color4.White), - new PolygonVertex(new Vector2(.75f, 0), Color4.White), - new PolygonVertex(new Vector2(1, .25f), Color4.White), - new PolygonVertex(new Vector2(1, .75f), Color4.White), - new PolygonVertex(new Vector2(.75f, 1), Color4.White), - new PolygonVertex(new Vector2(.25f, 1), Color4.White), - new PolygonVertex(new Vector2(0, .75f), new Color4(10,10,10,255)), - new PolygonVertex(new Vector2(0, .25f), new Color4(10,10,10,255)) - }), new Bitmap("soldier_logo.png")) - { - Transform = {Position = new SM.Base.Types.CVector2(-50,0)} - }); - - scene.Background.Color = Color4.Beige; - - /*scene.HUD.Add(new DrawText(font, "GIVE ME A HUD HUG!") - { - Color = Color4.Black, - Spacing = .75f - });*/ + //particles.Trigger(); } } } \ No newline at end of file