SceneViewport - Add OnUpdate virtual function

This commit is contained in:
miku-666
2024-03-13 17:50:35 +01:00
parent 0a01a4c8da
commit a41b0c4782
2 changed files with 34 additions and 10 deletions

View File

@@ -40,7 +40,7 @@ namespace PckStudio.Rendering
internal class SceneViewport : GLControl
{
/// <summary>
/// Refresh rate at which the frame is updated. Default is 50(Hz)
/// Refresh rate at which the frame is updated. Default is 60(Hz)
/// </summary>
public int RefreshRate
{
@@ -53,15 +53,30 @@ namespace PckStudio.Rendering
}
protected PerspectiveCamera Camera { get; }
protected EventHandler OnTimerTick { get; set; }
private int refreshRate = 60;
protected class TimestepEventArgs : EventArgs
{
public readonly float Delta;
public TimestepEventArgs(double seconds)
{
Delta = (float)seconds;
}
}
protected virtual void OnUpdate(object sender, TimestepEventArgs e)
{
Debug.WriteLine(e.Delta);
}
private int refreshRate = 120;
private Timer timer;
private Stopwatch stopwatch;
private bool isInitialized;
private ShaderProgram colorShader;
private VertexArray VAO;
private IndexBuffer IBO;
private bool isInitialized;
protected void Init()
{
@@ -98,10 +113,15 @@ namespace PckStudio.Rendering
public SceneViewport() : base()
{
timer = new Timer();
stopwatch = new Stopwatch();
RefreshRate = refreshRate;
timer.Tick += TimerTick;
timer.Start();
timer.Enabled = !DesignMode;
if (!DesignMode)
{
timer.Start();
stopwatch.Start();
}
Camera = new PerspectiveCamera(60f, new Vector3(0f, 0f, 0f));
VSync = true;
isInitialized = false;
@@ -112,6 +132,7 @@ namespace PckStudio.Rendering
if (disposing)
{
timer.Stop();
stopwatch.Stop();
timer.Dispose();
}
MakeCurrent();
@@ -161,8 +182,11 @@ namespace PckStudio.Rendering
private void TimerTick(object sender, EventArgs e)
{
OnTimerTick?.Invoke(sender, e);
Invalidate();
stopwatch.Stop();
double deltaTime = stopwatch.Elapsed.TotalMilliseconds;
OnUpdate(sender, new TimestepEventArgs(deltaTime));
stopwatch.Restart();
Refresh();
}
protected override void OnSizeChanged(EventArgs e)

View File

@@ -284,7 +284,6 @@ namespace PckStudio.Rendering
_shaders = new ShaderLibrary();
ANIM ??= new SkinANIM(SkinAnimMask.RESOLUTION_64x64);
OnTimerTick = AnimationTick;
ModelData = new ObservableCollection<SkinBOX>();
ModelData.CollectionChanged += ModelData_CollectionChanged;
}
@@ -1275,8 +1274,9 @@ namespace PckStudio.Rendering
return model;
}
private void AnimationTick(object sender, EventArgs e)
protected override void OnUpdate(object sender, TimestepEventArgs e)
{
base.OnUpdate(sender, e);
animationCurrentRotationAngle += animationRotationSpeed;
if (animationCurrentRotationAngle >= animationMaxAngleInDegrees || animationCurrentRotationAngle <= -animationMaxAngleInDegrees)
animationRotationSpeed = -animationRotationSpeed;