我目前正在做一个2D游戏引擎使用C#,GDI和已经建立了简单的框架帽。游戏只能渲染60fps。C#GDI游戏循环(FPS计数器)
据我知道有一个与代码没有问题,但是,我只想呈现60fps的更清洁的方式并没有更多的。
这里是我的代码,任何帮助将是巨大的
public void Run()
{
window.Show();
window.Focus();
Initialize();
isRunning = true;
canRender = true;
timer = new Stopwatch();
timer.Start();
// the amount of milliseconds needed to pass before rendering next frame
double frameCapCounter = 16.666666;
while (isRunning)
{
Application.DoEvents();
if (window.Focused)
{
if (timer.ElapsedMilliseconds >= frameCapCounter)
{
canRender = true;
frames += 1; // update amount of frames
frameCapCounter += 16.666666; // increment counter
}
else
{
canRender = false;
}
// this is used to check if a second has passed, and if so
// we set the fps variable to the amount of frames rendered
// and reset all variables.
if (timer.ElapsedMilliseconds >= 1000)
{
fps = frames;
frames = 0;
frameCapCounter = 0;
timer.Restart();
}
Update();
LateUpdate();
if (canRender)
Render();
else
{
Thread.Sleep(1);
}
}
}
}
从SlimDX的人(https://slimdx.org/docs/html/Managed_Message_Loop.htm)用来代替'Application.DoEvents()'此:https://blogs.msdn.microsoft.com/tmiller/ 2005/05/05 /我的,最后的后上渲染循环 - 但愿/ –