SDL + OpenGL V-Sync

Started by
15 comments, last by Tolito 11 years, 2 months ago

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.

Advertisement

Update:

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

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.

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.

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!

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.

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

This topic is closed to new replies.

Advertisement