Hello Guest

Using OpenGL commands in static initialization block

  • 5 Replies
  • 6947 Views
Using OpenGL commands in static initialization block
« 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.

Code: [Select]

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?

*

Offline WiESi

  • **
  • 70
Using OpenGL commands in static initialization block
« Reply #1 on: January 28, 2006, 08:50:59 »
You can only do this if a display was created before.

WiESi

Using OpenGL commands in static initialization block
« Reply #2 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.

Using OpenGL commands in static initialization block
« Reply #3 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

Using OpenGL commands in static initialization block
« Reply #4 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.

Using OpenGL commands in static initialization block
« Reply #5 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.