Hello everyone,
I was going around and making sure my project would work under all configurations (32/64 bit, Debug/Release) in Visual Studio C++ 2012. Mostly, things seemed to work alright after updating some lib files. The sole exception seems to be 32-bit Release mode.
In that particular configuration, SDL sometimes fails to create an OpenGL context, citing that the handle is invalid. It doesn't even have the decency to fail consistently.
Here's the code in question:
std::condition_variable var;
std::mutex windowMutex;
std::unique_lock<std::mutex> lock(windowMutex);
bool windowFailed = false;
windowThread = std::thread([this, &var, &windowFailed] {
SDL_Init(SDL_INIT_TIMER | SDL_INIT_EVENTS | SDL_INIT_GAMECONTROLLER
| SDL_INIT_JOYSTICK |SDL_INIT_HAPTIC);
window = SDL_CreateWindow("Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
1280, 720, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
if (window == NULL) {
windowFailed = true;
var.notify_one();
return;
}
var.notify_one();
//this runs an infinite loop of waiting for input
inputBuffer.update();
});
var.wait(lock, [this, &windowFailed] {
return window != nullptr || windowFailed;
});
glcontext = SDL_GL_CreateContext(window);
if (!glcontext) {
std::cout<<"OpenGl context creation failed! "<<SDL_GetError()<<std::endl;
}
//... other code not consequential to the topic
The more astute among you may have realized that I am initializing SDL and creating a window in a separate thread. I would prefer to handle input in a separate thread from the logic and rendering, especially since SDL_PollEvents uses far more CPU than SDL_WaitEvents. Input in SDL is required to be handled in the thread which creates the window, which is why it is set up as you see it.
I will note that not using a thread for this purpose seems to fix the issue. I would prefer to avoid this solution if possible, for the reasons stated above.
So, is there a better solution than putting the input handling code back on the main thread, or will I have to buck up and do it?