LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: Whackjack on January 27, 2006, 23:03:12

Title: Using OpenGL commands in static initialization block
Post by: Whackjack on January 27, 2006, 23:03:12
The subject is the question.  Can/should I do any OpenGL work in a static initialization block of a class, e.g.


static
{
   GL11.glGenLists(1);
   // do more gl stuff
}


My question, more directly, is, will the OpenGL libraries be fully loaded by the time my class has been loaded by the classloader and the static block runs?
Title: Using OpenGL commands in static initialization block
Post by: WiESi on January 28, 2006, 08:50:59
You can only do this if a display was created before.

WiESi
Title: Using OpenGL commands in static initialization block
Post by: Whackjack on January 30, 2006, 03:53:49
Okay, I guess that makes sense.  It can be presumed that nothing OpenGL related can be done until Display.create() is called, yes?
That would certainly preclude any OpenGL commands in a class static code block.
I do have a work around, in that case, but it's not as elegant as a static block.
Title: Using OpenGL commands in static initialization block
Post by: darkprophet on January 30, 2006, 14:55:46
The static block is only called when anything from that object is called.


I.e. "SomeObject.SomeField" or "SomeObject.someMethod" or even a constructor...

If you can be sure that a valid context* is available before doing anything with that class, your ok.

DP
Title: Using OpenGL commands in static initialization block
Post by: Whackjack on January 30, 2006, 17:21:56
So the question then becomes... How can I make sure that a valid context exists before the class loads?  And the answer will be, don't reference the class until Display.create() has been called.
That all seems way too JVM specific for me.  I think I'll stick to the alternative, a static init flag.
Title: Using OpenGL commands in static initialization block
Post by: Evil-Devil on January 30, 2006, 18:47:38
You might add an if conditional like

if (Display.isCreated())

and then call your static init stuff.
And to make sure that it really init a init variable might be usefull or a reference to the class that calls Display.create() so your static block may be able to call it too.