1
0
mirror of synced 2024-12-11 07:16:05 +01:00

Fix CPU usage with animation player

This commit is contained in:
KillzXGaming 2019-09-28 20:52:34 -04:00
parent bf408b4e5d
commit 180b1e7e7c

View File

@ -5,7 +5,6 @@ using System.Data;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading;
using System.Windows.Forms; using System.Windows.Forms;
using System.IO; using System.IO;
using System.Diagnostics; using System.Diagnostics;
@ -76,8 +75,6 @@ namespace Toolbox.Library
public float Frame = 0; public float Frame = 0;
// Frame rate control // Frame rate control
private Thread renderThread;
private bool renderThreadIsUpdating = false;
public bool isOpen = true; public bool isOpen = true;
public float FrameCount public float FrameCount
@ -169,6 +166,8 @@ namespace Toolbox.Library
currentFrameUpDown.ForeColor = FormThemes.BaseTheme.FormForeColor; currentFrameUpDown.ForeColor = FormThemes.BaseTheme.FormForeColor;
currentFrameUpDown.BackColor = FormThemes.BaseTheme.FormBackColor; currentFrameUpDown.BackColor = FormThemes.BaseTheme.FormBackColor;
SetupTimer();
this.LostFocus += new System.EventHandler(AnimationPanel_LostFocus); this.LostFocus += new System.EventHandler(AnimationPanel_LostFocus);
} }
@ -177,6 +176,7 @@ namespace Toolbox.Library
AnimationPlayerState = PlayerState.Playing; AnimationPlayerState = PlayerState.Playing;
UpdateAnimationUI(); UpdateAnimationUI();
animationTrackBar.Play(); animationTrackBar.Play();
animationTimer.Start();
} }
private void Pause() private void Pause()
@ -184,6 +184,7 @@ namespace Toolbox.Library
AnimationPlayerState = PlayerState.Stop; AnimationPlayerState = PlayerState.Stop;
UpdateAnimationUI(); UpdateAnimationUI();
animationTrackBar.Stop(); animationTrackBar.Stop();
animationTimer.Stop();
} }
private void Stop() private void Stop()
@ -191,6 +192,7 @@ namespace Toolbox.Library
currentFrameUpDown.Value = 0; currentFrameUpDown.Value = 0;
AnimationPlayerState = PlayerState.Stop; AnimationPlayerState = PlayerState.Stop;
UpdateAnimationUI(); UpdateAnimationUI();
animationTimer.Stop();
} }
private void UpdateAnimationUI() private void UpdateAnimationUI()
@ -217,6 +219,16 @@ namespace Toolbox.Library
} }
} }
private Timer animationTimer;
private void SetupTimer()
{
animationTimer = new Timer
{
Interval = 100 / 60
};
animationTimer.Tick += new EventHandler(animationTimer_Tick);
}
private void AdvanceNextFrame() private void AdvanceNextFrame()
{ {
if (animationTrackBar.CurrentFrame == animationTrackBar.FrameCount - 1) if (animationTrackBar.CurrentFrame == animationTrackBar.FrameCount - 1)
@ -302,10 +314,15 @@ namespace Toolbox.Library
UpdateViewport(); UpdateViewport();
SetAnimationsToFrame(animationTrackBar.CurrentFrame); SetAnimationsToFrame(animationTrackBar.CurrentFrame);
if (!renderThreadIsUpdating || !IsPlaying) if (!IsPlaying)
UpdateViewport(); UpdateViewport();
} }
private void animationTimer_Tick(object sender, EventArgs e)
{
UpdateAnimationFrame();
}
private void SetAnimationsToFrame(float frameNum) private void SetAnimationsToFrame(float frameNum)
{ {
if (Viewport == null || currentAnimations.Count == 0) if (Viewport == null || currentAnimations.Count == 0)
@ -356,45 +373,6 @@ namespace Toolbox.Library
{ {
if (Viewport != null) if (Viewport != null)
Viewport.VSync = Runtime.enableVSync; Viewport.VSync = Runtime.enableVSync;
renderThread = new Thread(new ThreadStart(RenderAndAnimationLoop));
renderThread.Start();
}
private void RenderAndAnimationLoop()
{
if (IsDisposed)
return;
// TODO: We don't really need two timers.
Stopwatch animationStopwatch = Stopwatch.StartNew();
// Wait for UI to load before triggering paint events.
// int waitTimeMs = 500;
// Thread.Sleep(waitTimeMs);
// UpdateViewport();
int frameUpdateInterval = 5;
int animationUpdateInterval = 16;
while (isOpen)
{
// Always refresh the viewport when animations are playing.
if (renderThreadIsUpdating || IsPlaying)
{
if (animationStopwatch.ElapsedMilliseconds > animationUpdateInterval)
{
UpdateAnimationFrame();
animationStopwatch.Restart();
}
}
else
{
// Avoid wasting the CPU if we don't need to render anything.
Thread.Sleep(1);
}
}
} }
private void AnimationPanel_Enter(object sender, EventArgs e) private void AnimationPanel_Enter(object sender, EventArgs e)
@ -403,24 +381,18 @@ namespace Toolbox.Library
private void AnimationPanel_LostFocus(object sender, EventArgs e) private void AnimationPanel_LostFocus(object sender, EventArgs e)
{ {
renderThreadIsUpdating = false;
} }
private void AnimationPanel_Click(object sender, EventArgs e) private void AnimationPanel_Click(object sender, EventArgs e)
{ {
renderThreadIsUpdating = true;
} }
private void AnimationPanel_Leave(object sender, EventArgs e) private void AnimationPanel_Leave(object sender, EventArgs e)
{ {
renderThreadIsUpdating = false;
} }
public void ClosePanel() public void ClosePanel()
{ {
ResetModels(); ResetModels();
renderThread.Abort();
renderThreadIsUpdating = false;
currentAnimations.Clear(); currentAnimations.Clear();
isOpen = false; isOpen = false;
Dispose(); Dispose();