setDisplayMode

Started by theplayer, September 22, 2009, 08:43:15

Previous topic - Next topic

theplayer

Hi at all,

i have a simple question about setDisplayMode. Always when i try to switch the display mode of an already created display it kills my x server on linux. i think i can short see the new window with the resolution but i get no error because my netbeans dies with the x server.
if i try to destroy the display i get an

Thread(main,5,main) already has the context current.

exception

i'm using lwjgl 2.1

edit:
could it be possible, that i should call the method from the same thread where the display is created? i'm using a swing hack for lwjgl and events are invoked in a subthread. so the click is a new thread could this be the problem?

greets theplayer

broumbroum

Quote from: theplayer on September 22, 2009, 08:43:15
edit:
could it be possible, that i should call the method from the same thread where the display is created? i'm using a swing hack for lwjgl and events are invoked in a subthread. so the click is a new thread could this be the problem?

greets theplayer

Yes, this could be.

theplayer

Display.create and Display.destroy are in the same class in different methods. The mouse clicks are threaded events sended over SwingUtilities.invokeAndWait(). Is a method called from such a mouseEvent executed in the mouse event thread or in the original program thread? if it is executed in the event thread this would be the problem but hpw is it possible to execute it in the program thread?

Here is my code for changing display mode:
if(Display.isCreated()) {
try {
	Display.releaseContext();
	Display.destroy();
} catch (LWJGLException ex) {
	ex.printStackTrace();
}
}

dm=findDisplayMode(800,600);
Display.setDisplayMode(dm);


It crashes at Display.destroy with this exception:

java.lang.IllegalStateException: From thread Thread[AWT-EventQueue-0,6,main]: Thread[main,5,main] already has the context current
        at org.lwjgl.opengl.Context.checkAccess(Context.java:172)
        at org.lwjgl.opengl.Context.forceDestroy(Context.java:228)
        at org.lwjgl.opengl.Display.destroyContext(Display.java:946)
        at org.lwjgl.opengl.Display.destroy(Display.java:929)
        at Graphics.GraphicsManager.setDisplayMode(GraphicsManager.java:69)
        at Graphics.GraphicsManager.setResolution(GraphicsManager.java:194)


i hope you understand my problem and could help me. thanks for your help

greetz theplayer

theplayer

Hi I've primary found the error,

as broumbroum posted "this could be". i'm in the "AWT-EventQueue-0" thread. is there a way to invoke another thread or something like this?

theplayer

Kai

Quoteis there a way to invoke another thread or something like this?
Yes, there is.

You have to use the same mechanism that Swing uses via it's SwingUtilities.invokeLater/invokeAndWait methods.
They do it via a Queue that contains all "Runnable"s that should be executed on the AWT thread.
This Queue is being polled and processed by the AWT thread continuously.
So all you have to do is to make your own Queue that takes Runnables or whatever you like and let your own Thread process that queue in a loop.

Then, everytime you want a command being processed in your special Thread, you would simply add that command wrapped in a Runnable into that queue and wait for it to be processed.
If you want the calling Thread (the one that adds the Runnable into the Queue to be processed by another Thread) to wait until the Runnable has been processed, you can use Thread synchronisation methods, such as wait() and notify().

broumbroum

Quote from: theplayer on September 22, 2009, 11:52:26
Here is my code for changing display mode:
if(Display.isCreated()) {
try {
	Display.releaseContext();
	Display.destroy();
} catch (LWJGLException ex) {
	ex.printStackTrace();
}
}

dm=findDisplayMode(800,600);
Display.setDisplayMode(dm);

Then you should not "releaseContext" before destroy.

theplayer

hi Kain and broumbroum,

i've found a working solution. it was the wrong thread and an event queue like SwingUtilities is working correct.

thanks for your help. the lwjgl forum is great in helping programmers.

theplayer