The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL and OpenGL Issues
Patricia Curtis
Guest

Hi Guys,             I have some issues with opengl and sdl, the first issue is switching from windowed to full screen and vice versa , get some all black textures, and the second more pressing issue, is i am getting tearing on my large sprite movements , i did have the move stuff in a timer and the draw stuff in the main loop, even if i tell it all to move after my flip it does not make much odds.


at the moment I'm doing this at the end of my main loop 


               DrawEverything();
       this_tick = SDL_GetTicks();
if ( this_tick < next_tick ) 
{
  SDL_Delay(next_tick-this_tick);
}
next_tick = this_tick + (1000/FRAMES_PER_SEC);
SDL_GL_SwapBuffers();
MoveEverything(0, NULL);


is this correct as i have ported my old 2D SDL code?, i draw everything to gScreen which is the output from SDL_SetVideoMode(... but i am obviously not getting double buffering even on SDL_FULLSCREEN with 


SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );


so how could i make my own double buffering ie Draw to one screen, copy it to the other and flip or swap and flip... any help much appreciated


Trish
SDL and OpenGL Issues
Simon Roby
Guest

1) Last time I checked, on most platforms the GL context is completely
lost when switching between windowed and full screen. To be sure you
won't lose everything you have to recreate everything after switching
(textures, etc.).

2) To reduce tearing you have to enable vertical blank syncing. Double
buffering is good towards reducing tearing, but not enough by itself
because the actual "physical" redraws can still happen while a frame
is in the middle of being thrown to the screen, which is what causes
tearing. The attribute you need is SDL_GL_SWAP_CONTROL. Keep in mind
your platform may not support it, though. Most video drivers (AMD and
nVidia, on Windows or Linux) also allow you to force vsync on all
OpenGL viewports, so you can try that too.
--
- SR

On Mon, Sep 14, 2009 at 16:01, Patricia Curtis
wrote:
Quote:
Hi Guys,
             I have some issues with opengl and sdl, the first issue is
switching from windowed to full screen and vice versa , get some all black
textures, and the second more pressing issue, is i am getting tearing on my
large sprite movements , i did have the move stuff in a timer and the draw
stuff in the main loop, even if i tell it all to move after my flip it does
not make much odds.
at the moment I'm doing this at the end of my main loop
               DrawEverything();
       this_tick = SDL_GetTicks();
if ( this_tick < next_tick )
{
  SDL_Delay(next_tick-this_tick);
}
next_tick = this_tick + (1000/FRAMES_PER_SEC);
SDL_GL_SwapBuffers();
MoveEverything(0, NULL);
is this correct as i have ported my old 2D SDL code?, i draw everything to
gScreen which is the output from SDL_SetVideoMode(... but i am obviously not
getting double buffering even on SDL_FULLSCREEN with
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
so how could i make my own double buffering ie Draw to one screen, copy it
to the other and flip or swap and flip... any help much appreciated
Trish
_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
SDL and OpenGL Issues
Bruno Adami
Guest

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 Mad) 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 Smile
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

Quote:
Date: Tue, 15 Sep 2009 17:47:19 +0100
Subject: Re: [SDL] SDL and OpenGL Issues
From:
To:

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:
Quote:
I recently switched to opengl too Smile I found that SDL (or some other magic
thing) automatically enables vsync (which is very good Very Happy, 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 pode utilizar o Messenger de qualquer tipo de celular? Saiba mais.
SDL and OpenGL Issues
Simon Roby
Guest

Well you should never rely on vsync for managing game logic time anyway.
A properly written game logic loop will work fine regardless of the
presence of vsync or not. At worst vsync will create some minor
overhead due to the extraneous sleeps.
--
- SR

On Tue, Sep 15, 2009 at 15:35, Bruno Adami wrote:
Quote:
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 Mad) 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 Smile
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

Quote:
Date: Tue, 15 Sep 2009 17:47:19 +0100
Subject: Re: [SDL] SDL and OpenGL Issues
From:
To:

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:
Quote:
I recently switched to opengl too Smile I found that SDL (or some other
magic
thing) automatically enables vsync (which is very good Very Happy, 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 pode utilizar o Messenger de qualquer tipo de celular? Saiba
mais.
_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
SDL and OpenGL Issues
kiaran
Guest

Hello,

When an SDL window is resized or switched between fullscreen/windowed modes
it will destroy and recreate the OpenGL context. You will need to reload
your textures into video memory which is why you are seeing a black screen.

Not sure about the tearing problem. Maybe something to do with vsync being
enabled/disabled? Just a guess though.

Good luck,
Kiaran



Patricia Curtis-2 wrote:
Quote:

Hi Guys, I have some issues with opengl and sdl, the first
issue
is switching from windowed to full screen and vice versa , get some all
black textures, and the second more pressing issue, is i am getting
tearing
on my large sprite movements , i did have the move stuff in a timer and
the
draw stuff in the main loop, even if i tell it all to move after my flip
it
does not make much odds.

at the moment I'm doing this at the end of my main loop

DrawEverything();
this_tick = SDL_GetTicks();
if ( this_tick < next_tick )
{
SDL_Delay(next_tick-this_tick);
}
next_tick = this_tick + (1000/FRAMES_PER_SEC);
SDL_GL_SwapBuffers();
MoveEverything(0, NULL);

is this correct as i have ported my old 2D SDL code?, i draw everything to
gScreen which is the output from SDL_SetVideoMode(... but i am obviously
not
getting double buffering even on SDL_FULLSCREEN with

SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

so how could i make my own double buffering ie Draw to one screen, copy it
to the other and flip or swap and flip... any help much appreciated

Trish

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org



--
View this message in context: http://www.nabble.com/SDL-and-OpenGL-Issues-tp25442541p25445086.html
Sent from the SDL mailing list archive at Nabble.com.

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
SDL and OpenGL Issues
Patricia Curtis
Guest

Thanks for your help guys, its all sorted now, the black textures were
an issue with my texture creator, and i have adopted a combination of
your responses for the vblank issue

SDL_GL_SwapBuffers();
StartTicks = EndTicks;
EndTicks = SDL_GetTicks();
DeltaTick = ((EndTicks - StartTicks) / 15.0);
RunTransition(); // this code use the DeltaTick as an multiplier for
the movements of the front end eg
// ScrollPos++; is now
ScrollPos+=(1*DeltaTick);

// now because there were too many changes needed in the animation
code (game 90% complete)
// i go through my animation loop a number of times depending how
slow the last frame was

while(AnimationTimes>1)
{
AnimationLoop(0, NULL);
AnimationTimes--;
}
AnimationTimes+=DeltaTick;

obviously i set the tick counts up before my main loop

StartTicks = SDL_GetTicks();
EndTicks = SDL_GetTicks();
DeltaTick = (double)((EndTicks - StartTicks) / 15.0);

also if i do a load of an image during my main loop i reset the
counters after the load so there are no large jumps in the frame rate

StartTicks = SDL_GetTicks();

Thanks a million
_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org