I'm writing cross-platform (iOS, Android, OSX, Windows) library code that draws its output using OpenGL. I expect my users will often try to call my library's rendering functions without a valid OpenGL context (especially when they are first getting started), so how can I cleanly handle the error?
So far I have tried calling some OpenGL function and then calling glGetError()
to see if it failed (and printing my user a helpful error message), but glGetError()
segfaults if there is no context:
// Test for OpenGL Context
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This line segfaults
if (glGetError() != GL_NO_ERROR)
printf("You tried to call a rendering function but no OpenGL context was found!");
Is there a way to check for a valid context within OpenGL without getting a segfault?