I’m playing with C++, OpenGL and SDL doing a game that should be able to
run in Windows and Mac OS X.
I have a problem that only happens on Windows.
Let me describe the scenario.
First I create an OpenGL renderer and get the current context:
https://github.com/felipowsky/GrifoEngine/blob/master/sources/Window.cpp#L103-L130
this->renderer = SDL_CreateRenderer(this->window, rendererIndex,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
this->context = SDL_GL_GetCurrentContext();
In my scene I add a timer to create some asteroids:
https://github.com/felipowsky/GrifoEngine/blob/master/examples/Spaceship/main.cpp#L182
this->timer.Start(INTERVAL_CREATE_ASTEROID,
SpaceshipScene::CreateAsteroidTimer, this);
Internally this timer uses SDL_AddTimer and keeps the current window and
context for itself:
https://github.com/felipowsky/GrifoEngine/blob/master/sources/Timer.cpp#L38-L47
this->window = SDL_GL_GetCurrentWindow();
this->context = SDL_GL_GetCurrentContext();
this->callback = callback;
this->param = param;
this->timerId = SDL_AddTimer(interval, Timer::ExecuteCallback, this);
When the callback executes I use MakeCurrent to make sure that OpenGL will
run in the right context (the window and context from the main thread):
https://github.com/felipowsky/GrifoEngine/blob/master/sources/Timer.cpp#L19-L33
bool hasContext = timer->window != NULL && timer->context != NULL;
if (hasContext) { SDL_GL_MakeCurrent(timer->window, timer->context);}
Uint32 result = timer->callback(interval, timer->param);
if (hasContext) { SDL_GL_MakeCurrent(timer->window, NULL);}
On Mac OS X this code is working perfectly, but on Windows it isn’t drawing
the asteroids on the window neither throwing any error.
I’ve tried to create a context for every callback execution, it works, but
the game runs slowly and doesn’t seem the right thing to do.
What am I doing wrong?
Thanks in advance.