I am working on a very complicated graphics rendering, using OpenGL, and it is very time consuming, in the sense that it creates a delay in the event handling, of my application to an extend that, the OS will think my application is freezing ! Therefore, I was thinking of running two threads, one for the OpenGL rendering and the other for event handling. But, I dont think this is possible for my current framework that I'm using!
Basically, I'm using SDL 2.0 to set up an OpenGL context, and then using vertex and fragment shaders to do the rendering.
Code:
// Initialize OpenGL context
// Set up VAO, VBO, compile vertex/frag. shaders, link, use program etc.
//
bool run = true;
SDL_Event e;
while(run)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
render(); // Complicated rendering :(
SDL_GL_SwapWindow(window);
while(SDL_PollEvent(&e))
{
switch(e.type){
case SDL_QUIT:
run = false;
break;
}
}
}
As you might realize if render
is slow, then event handling will lag. This might crash my application !
The SDL library wants everything in the same thread I guess. If I move the event handling part to another thread, then OpenGL won't render anything! Similarly, if I move the OpenGL based render
function to another thread, still it won't render anything!!! In fact event handling is a must for SDL in particular and to the OS in general.
What is the practical remedy to this issue?