Skip to main content
gamedev.net
🔒 Locked

Vsync question

Started by shane1985 22 replies 1.7k views
Original Post
shane1985
shane1985
My question is this: With my current project, when I have vsync off, everything pretty much works as it should. The problem is, when I turn vsync back on, the calculations are done less frequently, and therefore are less accurate for animation. Does the game loop wait for the vsync time to end until it continues, even though it is already done drawing? Here is sort of a psuedo-code version of my main loop: while(1) { if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { TranslateMessage(&msg);// Translate The Message DispatchMessage(&msg);// Dispatch The Message } else // If There Are No Messages { timer = Update_Timer(); Update(timer); // do calculations Render(); // render the scene } }
Agony
Agony
Depends on what library you use to render, but I know that with DirectX, the Present() call at the end of each frame will block until the vsync time comes, so yes, in your case, it would indeed slow down the rate of updating stuff. I'm guessing that it's like this with most libraries. (Besides, if it doesn't block, then after Render(), you'll get back to your loop, handle any messages, update your world, and then call Render() again, all before the previous Render() call has even gone through, since the vsync time still hasn't elapsed. In this case, you'll be good if Render() just skips everything and returns, but if it blocks, waiting for the previous Render() to finish, then you'll have the same problem as above.)
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
Stru
Stru
You could have animation and rendering in separate threads. It can be tricky but you dont have to worry about waiting for a vsync. You just have to worry about animation finishing before you start redering.
shane1985
shane1985
How would I go about using different threads for calculations and rendering? Also, how do you go about making sure the animation is done before rendering?

Thanks!
Raduprv
Raduprv
You can use semaphores and/or mutexes to syncronise between threads, so the rendering thread will have to wait for the animation thread to do it's job.
Setting a timer callback gives you a new thread, by default.
_the_phantom_
_the_phantom_
just out of intrest, why would you only update the game if there WASNT an event? Surely you should be updating the game regardless?
Raduprv
Raduprv
Quote:
Original post by _the_phantom_
just out of intrest, why would you only update the game if there WASNT an event? Surely you should be updating the game regardless?


AI? Sound? Sprites such as projectiles?
shane1985
shane1985
Alright, I set up a timer with SetTimer(), and every time the timer expires, I call the update function. The timer I set is to 10 milliseconds, or to update 100 times per second. When I was doing everything every frame, I was getting around 150 FPS. Now that I set the timer it only updates at around 60 FPS. Could the vsync still be slowing my program down? Does it still wait for rendering even though the update should be running on a different thread?

Thanks!

-Shane
Raduprv
Raduprv
First, a 10 MS timer is a bad idea, a guaranteed granularity is around 20-30 MS I think (well, it depends on the OS, library, etc.)
Second, you need to make your timmer and drawing independent of eachother. Set a mutex on the drawing to prevent the timer from doing bad stuff while drawing.
shane1985
shane1985
Well what is the point if you can't go under 33 - 50 times per second. That makes it kinda useless. Are there better timers out there?

Thanks!
-Shane
Raduprv
Raduprv
Oh, sorry, I was wrong. The granularity is smaller, around 10 MS.
From the SDL docs:
Quote:

The granularity of the timer is platform-dependent, but you should count on it being at least 10 ms as this is the most common number. This means that if you request a 16 ms timer, your callback will run approximately 20 ms later on an unloaded system.
shane1985
shane1985
Well, ideally, to make the calculations as accurate as possible, I don't want to be waiting 1/60 of a second to calculate the new positions for everything.
shane1985
shane1985
The deal is, when I enable vsync, or use a timer that isn't fast enough, it performs the movement calculations too infrequently. This results in inaccurate collision detection and physics.

_the_phantom_
_the_phantom_
Quote:
Original post by Raduprv
Quote:
Original post by _the_phantom_
just out of intrest, why would you only update the game if there WASNT an event? Surely you should be updating the game regardless?


AI? Sound? Sprites such as projectiles?


how are they related to the windows message pump?
Raduprv
Raduprv
Quote:
Original post by _the_phantom_
Quote:
Original post by Raduprv
Quote:
Original post by _the_phantom_
just out of intrest, why would you only update the game if there WASNT an event? Surely you should be updating the game regardless?


AI? Sound? Sprites such as projectiles?


how are they related to the windows message pump?


You didn't say "update the window" but "update the game".
Regardless, the window update is independent to whether or not there is any event.
_the_phantom_
_the_phantom_
*points at the start of the thread*
Correct, I didnt say update the window coz that wasnt what i was talking about [wink]
The code at the start ONLY updates the game states when a windows message pump event doesnt happen, so even moving the mouse would stall the game for whatever reason.
Thus my wondering why this was how it was done [smile]
_the_phantom_
_the_phantom_
heh, no worries, i'd had come back to it earier but i'd forgotten about this thread [smile]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.