How to tell if a context has been created?

Started by
5 comments, last by MarkS_ 10 years, 9 months ago

I'm working on an OpenGL-based library that does not do context creation. I need a way for the library to test that an OpenGL context has been created, so that it can exit gracefully if not.

How do I do this?

Advertisement

If you use GLEW, you can type:




//Initialize GLEW
if(glewInit() != GLEW_OK)
{
    //On failure
}

If this fails, then normally, the context has yet to be created.

Another way, is to call


glGetError()

If the context has yet to be created, then it will always return GL_INVALID_OPERATION (1282).

Note, however that multiple errors issued by OpenGL calls are saved by OpenGL, so the spec says that glGetError() should always be called in a loop, until it returns GL_NO_ERROR.

Since other OpenGL calls may have triggered an invalid operation, to be sure, you could call glGetError() a large number of times in a row (1000 maybe?), and if the last return value is still GL_INVALID_OPERATION, then you know the context is not created.

Hope it helps.

Since context creation is platform-specific, you can use a platform-specific call to find out if one exists; e.g on Windows wglGetCurrentContext will do the job.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Hmmm.. I'm not doing context creation mainly because I don't want a load of preprocessor code guards. What I am doing can be done without any platform-specific code. I tested __SKYe's tip on using glGetError() and that works, so I may have to use that. Not very elegant though.

Wasn't checking if "glGetString(GL_VERSION)" returns null the way to do that? (null meaning there is no context...)

Regarding glGetError: Each type of error has an associated bit that is set on error, and cleared and returned on the call to glGetError. That means it is enough to call it enough times to have GL_INVALID_OPERATION return twice without an intermediate GL_NO_ERROR.

But I also agree that is is not an elegant solution. The best choice is, like it or not, to use the platform API you're running on to actually ask for the context directly. Is it really that bad to wrap 2-3 lines of code in an #if per platform, and how many platforms with distinct OpenGL layers are you really targeting?

edit: And if you insist on using OpenGL to determine wither itself is initialized, you should be aware of why that is a bad idea and that you should stick to the API that is actually responsible for initializing OpenGL. From the OpenGL specification:


Issuing GL commands when the program is not connected to a context results in undefined behavior.

Thus, using OpenGL to determine its state is undefined. It is then up to you to decide if relying on undefined behavior is better than relying on platform dependent, but defined, behavior.

Is it really that bad to wrap 2-3 lines of code in an #if per platform, and how many platforms with distinct OpenGL layers are you really targeting?

dry.png Hush! I want to be lazy!


Thus, using OpenGL to determine its state is undefined. It is then up to you to decide if relying on undefined behavior is better than relying on platform dependent, but defined, behavior.

Well, I guess I'm going to have to break down and do it correctly then.sad.png

This topic is closed to new replies.

Advertisement