I saw this episode from Extra Credits on FPS and they mention that you should normally lock the fps to multiples of 30 because it matches up with the monitor's refresh rates and reduces artifacts. My code is running at 60fps but I'm still getting a very subtle artifacts in the movement.
https://www.youtube.com/watch?v=zL5kOyHWI_E
It's in Windows Forms so perhaps there's really nothing to be done about it.
My render loop looks like this:
private void Render() {
Update();
if (RenderDelegate != null)
RenderDelegate(_renderFunctions);
var msPause = 1000 / (double)FpsLimit - _gameTime.DeltaTime;
if (msPause > 0)
System.Threading.Thread.Sleep((int)msPause);
_attachedSystem.NextFrame(); // Will call Invalidate();
}
Implemtation in the WinForms control
protected void FormOnPaint(object sender, PaintEventArgs paintEventArgs) {
_graphic = paintEventArgs.Graphics;
_render();
}
void IGameMessages.DrawImage(Image image, Rectangle rect) {
_graphic.DrawImage(image, rect);
}
void IGameMessages.NextFrame() {
Control.Invalidate();
}
I'm guessing that sleeping by a dynamic value is not the preferred way because it might be like 1% out of sync with the monitor refresh rate or something? The fps output swings between 58 to 63 fps (There probably something wrong with the DeltaTime calculations too)
WinForm is not ideal. The reason is that screen updates are not depending your app. There is no screen interupt in WinForms.
I kept a timer and moved an object from left to right, 800 pixels, the "sprite" alike object had a start counter integer. Depending on how much time had past a new position was taken.
if(!x.AtEnd) {
X.DeltaTime = X.Startime-currentTime;
int x = myStartTime - currentTime;
x.position = X.DeltaTime % 430; //time was in ms and 430 just a number
if (x.postion > x.end) {
x.position=x.end;
x.AtEnd=true;
}
}
It depended a bit on the number how fast it moved and how well it looked (which depends on computer speed as well, as slower computers take more time to execute on other threads as well). Using something like above I got a reasonable smooth moving object, not as smooth as in games, but it worked well and there was no blocking (Delaying) code in it (as I tried with thread delays so each movement took equal time, but that didn't got smooth either, the screen update is too random for that)