Jump to content

  • Log In with Google      Sign In   
  • Create Account


SDL + OpenGL V-Sync


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
16 replies to this topic

#1 Tolito   Members   -  Reputation: 256

Like
0Likes
Like

Posted 05 February 2013 - 03:23 PM

When I was using SDL alone for the graphics, SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1); did the job, but now that I am using SDL as a container for OpenGL, with OpenGL being used for graphics, it does not work and the problem is back. I have been looking for a solution for a little over an hour now but I have had no luck. Is there a simple, cross-platform solution that I am missing here? I would like to be able to do it using what is already available in SDL.h and SDL_opengl.h. I have tried including glx.h, using the -glx compiler flag, and using glXSwapIntervalEXT(1);, but I received an undefined reference to that and want to avoid using extensions anyway (so I don't have to worry about different ones for different platforms and all of that). Thank you! :)



Sponsor:

#2 proanim   Members   -  Reputation: 440

Like
-1Likes
Like

Posted 05 February 2013 - 08:39 PM

The easiest way to do this is to add Sleep(17); in your main loop. This will cap your framerate to 60 and you will have kind of v-sync. For me also works SDL_GL_SetSwapInterval((int)vsync); this will cap your framerate to refresh rate of the screen and you will have v-sync.



#3 santa01   Members   -  Reputation: 307

Like
2Likes
Like

Posted 06 February 2013 - 06:17 AM

glXSwapIntervalEXT is not crossplatform, for win you should call wglSwapIntervalEXT. Also, if I am not mistaken, for glXSwapIntervalEXT you should link against libX11 as -lX11

 

Also you can look at this post: http://forums.libsdl.org/viewtopic.php?p=24220&sid=a85c51359b9040234fece5114e1d5787#24220



#4 Tolito   Members   -  Reputation: 256

Like
0Likes
Like

Posted 06 February 2013 - 08:30 AM

The easiest way to do this is to add Sleep(17); in your main loop. This will cap your framerate to 60 and you will have kind of v-sync. For me also works SDL_GL_SetSwapInterval((int)vsync); this will cap your framerate to refresh rate of the screen and you will have v-sync.

 

I have been already been capping my framerate. I get an undefined reference to SDL_GL_SetSwapInterval when I try calling that. Thank you for your response!

 

 

 

glXSwapIntervalEXT is not crossplatform, for win you should call wglSwapIntervalEXT. Also, if I am not mistaken, for glXSwapIntervalEXT you should link against libX11 as -lX11

 

Also you can look at this post: http://forums.libsdl.org/viewtopic.php?p=24220&sid=a85c51359b9040234fece5114e1d5787#24220

 

The fact that they are not cross-platform is one of the reasons I want to avoid using them. Thanks for the linker settings and the link! The post you linked to appears to be using SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1); for versions of SDL that are not at least 1.3.0. I have used that and still have no luck. Do you suggest using SDL 1.3.0? Thanks for responding!



#5 proanim   Members   -  Reputation: 440

Like
1Likes
Like

Posted 06 February 2013 - 09:31 AM

 I get an undefined reference to SDL_GL_SetSwapInterval when I try calling that.

 

Sorry, forgot to mention this requires SDL 2 to work.

Undefined reference is indication that you are not linking correct libraries.

 

I see that you want to make this work on linux - aside from SDL 2 see this http://www.bitsphere.co.za/gameDev/openGL/vsync.php



#6 Tolito   Members   -  Reputation: 256

Like
0Likes
Like

Posted 06 February 2013 - 09:50 AM

Dang. I would like as much system compatibility as possible, and from what I have read, SDL 1.2 is supported the most, so I will need to use it. Thank you for the link! The information on the page seems to be helpful. Looks like I will have to settle with using extensions. Thanks again! :)


Edited by Tolito, 06 February 2013 - 09:50 AM.


#7 Tolito   Members   -  Reputation: 256

Like
0Likes
Like

Posted 06 February 2013 - 10:41 AM

Update: I implemented the code on the page you mentioned and it prints that there is no V-Sync to the console window. When I leave swapInterval(1);, there is a loop of it saying that there is no V-Sync. When I comment it out, the rendering window actually renders, but there is no V-Sync.

 

dpy = XOpenDisplay(NULL);
screen_num = DefaultScreen(dpy);

The above code is how I am initializing the variables needed by the code you linked to. Hopefully what is going wrong is I should not be using "NULL" where I am, but I could not find anything else to put there. Thanks again!



#8 santa01   Members   -  Reputation: 307

Like
1Likes
Like

Posted 06 February 2013 - 11:49 AM

Do you suggest using SDL 1.3.0?

 

It will be a good start smile.png As proanim@ mentioned its now 2.0 and should be cloned/compiled from mercurial repo.



#9 Tolito   Members   -  Reputation: 256

Like
0Likes
Like

Posted 06 February 2013 - 05:34 PM

Hopefully 2.0 will be considered the latest stable release here soon. What am I supposed to do about people on systems that only support SDL 1.2? Is SDL 2.0 less cross-platform? So many questions! Thanks for your responses, everyone!



#10 proanim   Members   -  Reputation: 440

Like
1Likes
Like

Posted 06 February 2013 - 06:11 PM

My understanding is that SDL 2.0 was supposed to be SDL 1.3, but they renamed it to 2.0 for some reason. Older versions used OpenGL to some extent and now SDL 2.0 is made to support newer OpenGL 3 and up context. So it is actually major change from the previous, but then again SDL 1.2 supported pc that are now very very old (single core cpu's and such). Is it less cross-platform? It isn't supposed to be.



#11 Tolito   Members   -  Reputation: 256

Like
0Likes
Like

Posted 06 February 2013 - 07:46 PM

Aww. So SDL 2.0 does not support older computers? Another thing is that with a Windows release, DLLs for whatever version of SDL can be packaged, but with Linux distributions, they have SDL 1.2 integrated already but no SDL 2.0 and distributing SDL 2.0 with whatever you release isn't as simple. I'm trying to determine whether it is worth switching or not.



#12 Tolito   Members   -  Reputation: 256

Like
0Likes
Like

Posted 07 February 2013 - 02:38 PM

Update:

SDL 2.0 is now installed. V-Sync still does not work, even when trying SDL_GL_SetSwapInterval(1);. sad.png Suggestions?


Edited by Tolito, 07 February 2013 - 02:41 PM.


#13 proanim   Members   -  Reputation: 440

Like
1Likes
Like

Posted 07 February 2013 - 08:32 PM

It must work, you are probably doing something wrong. For me it works 100%, with this function, I create window, context and setup everything I need to get started.

 

SDL_Window *MainWin;
SDL_GLContext MainContext;

int InitWindow(const char* title, int width, int height, bool vsync, int OGL_Major, 
	int OGL_Minor, int StencilSize, int DepthSize, int AA)
{
	// init SDL subsystems
	if(SDL_Init(SDL_INIT_VIDEO) == -1)
        return 1;

	// setup OpenGL version
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, OGL_Major);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, OGL_Minor);
	SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

	// setup stencil buffer
	SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, StencilSize);

	// use double buffering
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

	// setup depth buffer
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, DepthSize);

	// setup anti aliasing
	SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
	SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, AA);

	// create main window
	MainWin = SDL_CreateWindow(title, 
		SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 
		width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);

	MainContext = SDL_GL_CreateContext(MainWin); // attach OpenGL context to window

	// init GLEW
	if (glewInit() != GLEW_OK)
	{
		fprintf(stderr, "Failed to initialize GLEW\n");
		return -1;
	}

	SDL_GL_SetSwapInterval((int)vsync); // vsync

	return 0;
}

 

 

Note: I use GLEW to load OpenGL extensions, I am not sure if that also works for linux.


Edited by proanim, 07 February 2013 - 08:33 PM.


#14 Sik_the_hedgehog   Crossbones+   -  Reputation: 1549

Like
1Likes
Like

Posted 08 February 2013 - 05:36 AM

My understanding is that SDL 2.0 was supposed to be SDL 1.3, but they renamed it to 2.0 for some reason. Older versions used OpenGL to some extent and now SDL 2.0 is made to support newer OpenGL 3 and up context. So it is actually major change from the previous, but then again SDL 1.2 supported pc that are now very very old (single core cpu's and such). Is it less cross-platform? It isn't supposed to be.

 

Actually it was always supposed to be 2.0, a better question would be why they had stuck with 1.3 for so long (1.9 would have made more sense). Also to note that while it does support OpenGL 3 and above it still works with older computers that don't support it (mine only supports up to 2.1, so I should know).


Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

#15 Tolito   Members   -  Reputation: 256

Like
0Likes
Like

Posted 08 February 2013 - 09:43 AM

Proanim: Thanks! I cannot notice a lack of V-Sync now, but after taking a lot of screenshots, I finally was able to capture a frame without it. Do you think this was just a bug in the software that I used to take the screenshot? Here is my code to limit the frame-rate. Could it be a problem? I am not using GLEW, by the way.

ticks = SDL_GetTicks();
if ( (ticks-lastticks) < (1000/20) )  SDL_Delay((1000/20)-(ticks-lastticks));
lastticks = ticks;

 

Sik: Thank you for that information. It makes me more comfortable using SDL 2.0!


Edited by Tolito, 08 February 2013 - 09:44 AM.


#16 proanim   Members   -  Reputation: 440

Like
1Likes
Like

Posted 08 February 2013 - 11:26 AM

When I tried your code with or without SDL_GL_SetSwapInterval(1); and check with fraps it rounds down to 39 frames (this is 40 - fraps sometimes calculates -1 for some reason). So your code works correctly and does perform v-sync.

 

 I finally was able to capture a frame without it.

 

If you can't see any screen tearing while testing or when recording video of your program than it is not an issue. If it is only one frame (in every 100 for example) it is most likely un-noticable by the naked eye.

 

Do you think this was just a bug in the software that I used to take the screenshot?

 

This is a possibility, too.



#17 Tolito   Members   -  Reputation: 256

Like
0Likes
Like

Posted 09 February 2013 - 08:13 AM

Thank you very much for all of your help! Your posts have been helpful, useful, and educational! biggrin.png Thanking you and giving you reputation is the only way I can think of to thank you, so thanks again! :D


Edited by Tolito, 09 February 2013 - 08:14 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS