Thats one alternative!
In my code I put a rest(1) (on windows I need to use timeBeginPeriod to set the timer precision to 1 ms :x) after the SDL_GL_SwapBuffers, so if there is no vsync support it wont take all CPU, it will consume more CPU than a PC that supports vsync (but not all CPU), but the game I am working on is full screen only, so it doesnt matter
Then I show a warning message saying that vsync could not be enabled.
So the game will run on different speeds from pc to pc? No! I dont use the fps to update my logic, I just use it to calc the time it takes every frame and make the lpf calculation (logics per frame), than the logic loop of the game will run ‘lpf’ times!
Something like this:
Game loop begin
- Start counting time / reset counting time
- Draw things
- SDL_GL_SwapBuffers and SDL_Delay(1)
- Get counted time and make calculation of lpf
- Update the logic ‘lpf’ times
Game loop end
The calculation would be something like this:
double lpfAux = 0; // Out of the Game loop scope
…
int elapsed = timer.elapsedTime(); // Do something to get the elapsed time up ther
lpfAux = (elapsed/LPS_WAIT); // LPS_WAIT = 1000/Logic per Second, its a double!!!
lpf = (int)lpfAux; // Convert it to int and put on lpf
lpfAux -= lpf; // Lets subtract from lpfAux, its to accurate the lpf calculation, elapsed/LPS_WAIT wont give us a integer number
…
for (int x = 0;x < lpf;x++)
// Logic loop
…
You can choice the best alternative that fits in your game! You only need to update the logic of your game uniformly not depending on a fix FPS value! Of course only if you intend to use vsync which I think its a great advantage. (it can have some small errors, like on one second it update 1002 times, so on the other second it will update 998 times, there are other techniques, like delta time)
Bruno> Date: Tue, 15 Sep 2009 17:47:19 +0100
Subject: Re: [SDL] SDL and OpenGL Issues
From: patricia.curtis at unboxedgames.com
To: @Bruno_Adami
Thanks for that Bruno, yeah its sorted the tearing , so i suppose i
will have to have the delay if the SDL_GL_SWAP_CONTROL fails and
manage my vblanking myself?
By large sprite movements i mean say two fullscreen sprites like
opening doors, (i do casual games for distribution though big fish and
others)
I have found out its my texture creator code that is making the black
textures, as i believe that I have to reload them all when i change
screen mode, and i think its not deleting all the old ones correctly.
but again thanks
Trish
2009/9/14 Bruno Adami <@Bruno_Adami>:
I recently switched to opengl too I found that SDL (or some other magic
thing) automatically enables vsync (which is very good :D, at least it does
that on Windows, I will check on Linux later), so maybe if you remove the
delay check it will stop tearing. Some old GPUs dont support vsync, others
vary on vsync (the most common is 60Hz I suppose), so you will to do some
time calculation to update the logic of the game (if you intend to use
vsync).
You can enable/disable vsync using:
SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL,1); // 0 disable - 1 enable
And to check if its enabled:
SDL_GL_GetAttribute(SDL_GL_SWAP_CONTROL,&test); // test is an int
And Set/Get attribute will return -1 if some error occurs, if the GPU doesnt
support vsync for example.
What do you mean by large sprite movements?
When you switch from windowed to full screen all textures get black? What if
you switch back?
Bruno
Navegue com seguran?a com o Novo Internet Explorer 8. Baixe agora, ? gratis!
Voc? sabia que o Hotmail mudou? Clique e descubra as novidades.
http://www.microsoft.com/brasil/windows/windowslive/products/hotmail.aspx