Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


How to tell if a context has been created?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 MarkS   Prime Members   -  Reputation: 884

Like
0Likes
Like

Posted 08 July 2013 - 10:06 PM

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?



Sponsor:

#2 __SKYe   Members   -  Reputation: 1095

Like
3Likes
Like

Posted 08 July 2013 - 10:16 PM

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.


Edited by __SKYe, 08 July 2013 - 10:24 PM.


#3 mhagain   Crossbones+   -  Reputation: 8271

Like
1Likes
Like

Posted 09 July 2013 - 01:40 AM

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.


It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#4 MarkS   Prime Members   -  Reputation: 884

Like
0Likes
Like

Posted 09 July 2013 - 03:14 AM

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.



#5 japro   Members   -  Reputation: 887

Like
0Likes
Like

Posted 09 July 2013 - 03:25 AM

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



#6 Brother Bob   Moderators   -  Reputation: 8570

Like
2Likes
Like

Posted 09 July 2013 - 03:27 AM

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.


Edited by Brother Bob, 09 July 2013 - 03:34 AM.


#7 MarkS   Prime Members   -  Reputation: 884

Like
0Likes
Like

Posted 10 July 2013 - 03:11 PM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS