How to limit 60 FPS in a loop?

You can talk about almost anything that you want to on this board.

Moderator: Moderators

Post Reply
User avatar
Zepper
Formerly Fx3
Posts: 3221
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

How to limit 60 FPS in a loop?

Post by Zepper » Fri Sep 18, 2020 5:32 pm

Ages ago, I asked about 60hz framerate with Allegro's timer. Some machines do, but mine don't (for some unknown reason, it locks at 50hz). So, I ended up using Allegro's vsync() until today.

I've found here another way of creating a 60hz refresh rate. It worked, but I noted a problem when the game scrolls left/right - it's simply not smooth.
Any improvements?

Code: Select all

int bt=0; //a trick for 0.5 adjustment
   clock_t mid_v = clock()+13-bt;
   
   do {
      SwitchToThread();
   } while( clock() < mid_v );
   
   blit(video_bitmap,screen,0,0,scr_x,scr_y,vid->length,video->height);
   bt ^= 1;
Why +13?
I don't know. It was the value that made 60hz possible. The "bt" is an adjustment.
Last edited by Zepper on Fri Sep 18, 2020 6:24 pm, edited 1 time in total.

lidnariq
Posts: 9796
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: Emulation refresh rate of 60hz?

Post by lidnariq » Fri Sep 18, 2020 5:50 pm

What are you trying to solve by using an arbitrary 60Hz that isn't locked to the screen, instead of using whatever the screen is locked to?

tepples
Posts: 22093
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Emulation refresh rate of 60hz?

Post by tepples » Fri Sep 18, 2020 5:56 pm

I guess ability to keep the emulated NES running at the expected speed even on a monitor whose refresh rate is 50 Hz or 75 Hz or whatever else.

User avatar
Zepper
Formerly Fx3
Posts: 3221
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Re: Emulation refresh rate of 60hz?

Post by Zepper » Fri Sep 18, 2020 5:58 pm

lidnariq wrote:
Fri Sep 18, 2020 5:50 pm
What are you trying to solve by using an arbitrary 60Hz that isn't locked to the screen, instead of using whatever the screen is locked to?
What do you mean??
Well, once a game frame is rendered by the PPU (the 256x240 image), it calls a function to control the refresh rate. Usually, vsync() works fine.
Yes, my monitor uses 60hz too.

lidnariq
Posts: 9796
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: Emulation refresh rate of 60hz?

Post by lidnariq » Fri Sep 18, 2020 6:05 pm

If you have a situation where the emulated system is 60Hz, and your display is 60Hz, using any other solution other than vsync will cause tearing. Thus the question: why? what problem does this solve? what improvement does this cause?

User avatar
Quietust
Posts: 1624
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Emulation refresh rate of 60hz?

Post by Quietust » Fri Sep 18, 2020 6:17 pm

If you want to emulate the NTSC NES at exactly 60Hz and have accurate timing, then you'll need to slow the CPU down to 1.78683MHz.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.

User avatar
Zepper
Formerly Fx3
Posts: 3221
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Re: Emulation refresh rate of 60hz?

Post by Zepper » Fri Sep 18, 2020 6:22 pm

Quietust wrote:
Fri Sep 18, 2020 6:17 pm
If you want to emulate the NTSC NES at exactly 60Hz and have accurate timing, then you'll need to slow the CPU down to 1.78683MHz.
Let me rephrase my question.
I want to know how to limit 60 FPS in a loop.
lidnariq wrote:
Fri Sep 18, 2020 6:05 pm
If you have a situation where the emulated system is 60Hz, and your display is 60Hz, using any other solution other than vsync will cause tearing. Thus the question: why? what problem does this solve? what improvement does this cause?
True, but all the programs I know bring the option to turn on/off vsync.

User avatar
Dwedit
Posts: 4360
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: How to limit 60 FPS in a loop?

Post by Dwedit » Fri Sep 18, 2020 9:52 pm

Happy case: 60FPS refresh rate, 60FPS gameplay speed.
You use Present (from Direct3D/DXGI) to update the screen. It enqueues a screen update, and waits for vsync (with no unnecessary CPU usage) if there is already an update pending.

Less happy case: Something that's not 60FPS as the refresh rate.
Use high resolution timers, and either wait for them in a spinning polling loop, or use waitable timers and WaitForSingleObjectEx.
You want your target time to be relative to the last target time.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!

none
Posts: 48
Joined: Thu Sep 03, 2020 12:56 am

Re: How to limit 60 FPS in a loop?

Post by none » Wed Sep 23, 2020 3:53 pm

The problem with your wait loop is that you take the target clock just before the loop, while in reallity the timepoint you want to check against is just after the loop finishes... this is why you need to add random values to make it work.

For example, for 60fps, you want to wait exactly 16.666 milliseconds every frame. But you wait longer, because the time needed for rendering the stuff and everything else you do will pass additionally to the time the wait loop waits.

But, as others have said, better not use the wait loop. Maybe you can try to just skip every eleventh frame (display 10 frames, skip one)? This will probably look better than turning vsync off. Or you could try blending frames.

Post Reply