Added FixedUpdates
This commit is contained in:
parent
777c2f6256
commit
21eaaa4900
8 changed files with 124 additions and 24 deletions
|
|
@ -66,6 +66,7 @@
|
|||
<Compile Include="Drawing\ShaderArguments.cs" />
|
||||
<Compile Include="Drawing\TextureTransformation.cs" />
|
||||
<Compile Include="PostEffects\PostProcessFinals.cs" />
|
||||
<Compile Include="Scene\IFixedScriptable.cs" />
|
||||
<Compile Include="Shaders\MaterialShader.cs" />
|
||||
<Compile Include="Drawing\Particles\ParticleContext.cs" />
|
||||
<Compile Include="Drawing\Particles\ParticleMovement.cs" />
|
||||
|
|
@ -76,6 +77,7 @@
|
|||
<Compile Include="Utility\Ray.cs" />
|
||||
<Compile Include="Utility\Util.cs" />
|
||||
<Compile Include="Window\Contexts\DrawContext.cs" />
|
||||
<Compile Include="Window\Contexts\FixedUpdateContext.cs" />
|
||||
<Compile Include="Window\Contexts\UpdateContext.cs" />
|
||||
<Compile Include="Window\GLWindow.cs" />
|
||||
<Compile Include="Log.cs" />
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ using System.Collections.ObjectModel;
|
|||
using OpenTK;
|
||||
using SM.Base;
|
||||
using SM.Base.Drawing;
|
||||
using SM.Base.Window.Contexts;
|
||||
using SM.Base.Windows;
|
||||
|
||||
#endregion
|
||||
|
|
@ -14,9 +15,10 @@ namespace SM.Base.Scene
|
|||
/// <summary>
|
||||
/// Contains a list of show items.
|
||||
/// </summary>
|
||||
public abstract class GenericItemCollection : List<IShowItem>, IShowItem, IShowCollection, IScriptable
|
||||
public abstract class GenericItemCollection : List<IShowItem>, IShowItem, IShowCollection, IScriptable, IFixedScriptable
|
||||
{
|
||||
private List<IScriptable> _scriptableObjects = new List<IScriptable>();
|
||||
private List<IFixedScriptable> _fixedScriptables = new List<IFixedScriptable>();
|
||||
|
||||
/// <summary>
|
||||
/// Currently active script objects.
|
||||
|
|
@ -50,6 +52,16 @@ namespace SM.Base.Scene
|
|||
}
|
||||
}
|
||||
|
||||
public virtual void FixedUpdate(FixedUpdateContext context)
|
||||
{
|
||||
if (!Active || !UpdateActive) return;
|
||||
|
||||
for (int i = 0; i < _fixedScriptables.Count; i++)
|
||||
{
|
||||
_fixedScriptables[i].FixedUpdate(context);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IShowCollection.Draw" />
|
||||
public virtual void Draw(DrawContext context)
|
||||
{
|
||||
|
|
@ -83,6 +95,8 @@ namespace SM.Base.Scene
|
|||
|
||||
if (item is IScriptable scriptable)
|
||||
AddScript(scriptable);
|
||||
|
||||
if (item is IFixedScriptable fixedScriptable) _fixedScriptables.Add(fixedScriptable);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -103,6 +117,7 @@ namespace SM.Base.Scene
|
|||
public void AddScript(IScriptable item)
|
||||
{
|
||||
_scriptableObjects.Add(item);
|
||||
if (item is IFixedScriptable fs) _fixedScriptables.Add(fs);
|
||||
}
|
||||
|
||||
public new void Remove(params IShowItem[] items)
|
||||
|
|
@ -113,6 +128,9 @@ namespace SM.Base.Scene
|
|||
|
||||
if (item is IScriptable scriptable)
|
||||
RemoveScript(scriptable);
|
||||
|
||||
if (item is IFixedScriptable fixedScriptable)
|
||||
_fixedScriptables.Remove(fixedScriptable);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -134,6 +152,7 @@ namespace SM.Base.Scene
|
|||
public void RemoveScript(IScriptable item)
|
||||
{
|
||||
_scriptableObjects.Remove(item);
|
||||
if (item is IFixedScriptable fs) _fixedScriptables.Remove(fs);
|
||||
}
|
||||
|
||||
public ICollection<IShowItem> GetAllItems(bool includeCollections = false)
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ using System.Dynamic;
|
|||
using System.Windows.Controls;
|
||||
using SM.Base;
|
||||
using SM.Base.Drawing;
|
||||
using SM.Base.Window.Contexts;
|
||||
using SM.Base.Windows;
|
||||
using SM.Utility;
|
||||
|
||||
|
|
@ -105,6 +106,12 @@ namespace SM.Base.Scene
|
|||
_hud?.Update(context);
|
||||
}
|
||||
|
||||
public virtual void FixedUpdate(FixedUpdateContext context)
|
||||
{
|
||||
_objectCollection?.FixedUpdate(context);
|
||||
_hud?.FixedUpdate(context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Draws this scene.
|
||||
/// </summary>
|
||||
|
|
|
|||
15
SMCode/SM.Base/Scene/IFixedScriptable.cs
Normal file
15
SMCode/SM.Base/Scene/IFixedScriptable.cs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
using SM.Base.Window.Contexts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SM.Base.Scene
|
||||
{
|
||||
public interface IFixedScriptable
|
||||
{
|
||||
void FixedUpdate(FixedUpdateContext context);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -15,6 +15,8 @@ namespace SM.Base.Drawing
|
|||
/// </summary>
|
||||
public abstract class MaterialShader : GenericShader
|
||||
{
|
||||
static bool _canLineWidth = true;
|
||||
|
||||
/// <inheritdoc />
|
||||
protected MaterialShader(string combinedData) : base(combinedData)
|
||||
{}
|
||||
|
|
@ -39,10 +41,20 @@ namespace SM.Base.Drawing
|
|||
|
||||
context.Mesh.Activate();
|
||||
|
||||
if (_canLineWidth)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (context.Mesh is ILineMesh lineMesh)
|
||||
GL.LineWidth(context.Material.ShaderArguments.Get("LineWidth", lineMesh.LineWidth));
|
||||
else if (context.Material.ShaderArguments.ContainsKey("LineWidth"))
|
||||
GL.LineWidth((float)context.Material.ShaderArguments["LineWidth"]);
|
||||
}
|
||||
catch
|
||||
{
|
||||
_canLineWidth = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (context.Material.Blending)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -17,6 +17,24 @@
|
|||
/// </summary>
|
||||
public bool UseRender;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The current update delta time.
|
||||
/// </summary>
|
||||
public static float UpdateDelta { get; internal set; }
|
||||
|
||||
public static float FixedUpdateDelta { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The current render delta time.
|
||||
/// </summary>
|
||||
public static float RenderDelta { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// The calculated delta time.
|
||||
/// </summary>
|
||||
public float DeltaTime => (UseRender ? RenderDelta : UpdateDelta) * Scale;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a delta time assistant.
|
||||
/// </summary>
|
||||
|
|
@ -30,20 +48,5 @@
|
|||
UseRender = useRender;
|
||||
Scale = scale;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The current update delta time.
|
||||
/// </summary>
|
||||
public static float UpdateDelta { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// The current render delta time.
|
||||
/// </summary>
|
||||
public static float RenderDelta { get; internal set; }
|
||||
|
||||
/// <summary>
|
||||
/// The calculated delta time.
|
||||
/// </summary>
|
||||
public float DeltaTime => (UseRender ? RenderDelta : UpdateDelta) * Scale;
|
||||
}
|
||||
}
|
||||
13
SMCode/SM.Base/Window/Contexts/FixedUpdateContext.cs
Normal file
13
SMCode/SM.Base/Window/Contexts/FixedUpdateContext.cs
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SM.Base.Window.Contexts
|
||||
{
|
||||
public struct FixedUpdateContext
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,7 @@
|
|||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using System.Windows;
|
||||
using System.Windows.Forms;
|
||||
using OpenTK;
|
||||
|
|
@ -6,7 +9,9 @@ using OpenTK.Graphics;
|
|||
using OpenTK.Input;
|
||||
using SM.Base.Controls;
|
||||
using SM.Base.Scene;
|
||||
using SM.Base.Window.Contexts;
|
||||
using SM.OGL;
|
||||
using SM.Utility;
|
||||
using Mouse = SM.Base.Controls.Mouse;
|
||||
|
||||
namespace SM.Base.Windows
|
||||
|
|
@ -14,6 +19,7 @@ namespace SM.Base.Windows
|
|||
public class GLWindow : GameWindow, IGenericWindow
|
||||
{
|
||||
private Vector2 _flagWindowSize;
|
||||
private Thread _fixedUpdateThread;
|
||||
|
||||
public bool Loading { get; private set; } = true;
|
||||
public float AspectRatio { get; set; }
|
||||
|
|
@ -98,6 +104,12 @@ namespace SM.Base.Windows
|
|||
Mouse.MouseMoveEvent(e, this);
|
||||
}
|
||||
|
||||
protected override void OnClosing(CancelEventArgs e)
|
||||
{
|
||||
base.OnClosing(e);
|
||||
_fixedUpdateThread.Abort();
|
||||
}
|
||||
|
||||
public void Update(UpdateContext context)
|
||||
{
|
||||
|
||||
|
|
@ -163,12 +175,29 @@ namespace SM.Base.Windows
|
|||
default:
|
||||
throw new ArgumentOutOfRangeException(nameof(newFlag), newFlag, null);
|
||||
}
|
||||
}
|
||||
|
||||
if (newFlag == WindowFlags.BorderlessWindow)
|
||||
public void RunFixedUpdate(float updatesPerSecond)
|
||||
{
|
||||
WindowBorder = WindowBorder.Hidden;
|
||||
X = Screen.PrimaryScreen.Bounds.X;
|
||||
Y = Screen.PrimaryScreen.Bounds.Y;
|
||||
Deltatime.FixedUpdateDelta = 1 / (float)updatesPerSecond;
|
||||
|
||||
_fixedUpdateThread = new Thread(ExecuteFixedUpdate);
|
||||
_fixedUpdateThread.Start();
|
||||
}
|
||||
|
||||
private void ExecuteFixedUpdate()
|
||||
{
|
||||
Stopwatch deltaStop = new Stopwatch();
|
||||
while (Thread.CurrentThread.ThreadState != System.Threading.ThreadState.AbortRequested)
|
||||
{
|
||||
deltaStop.Restart();
|
||||
|
||||
FixedUpdateContext context = new FixedUpdateContext();
|
||||
|
||||
CurrentScene?.FixedUpdate(context);
|
||||
|
||||
long delta = deltaStop.ElapsedMilliseconds;
|
||||
Thread.Sleep(Math.Max((int)(Deltatime.FixedUpdateDelta * 1000) - (int)delta, 0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue