Multithready between AWT and JOGL
Posted by millerni456 on Sep 21, 2011; 9:35pm
URL: https://forum.jogamp.org/Multithready-between-AWT-and-JOGL-tp3356928.html
Hello, I've got an incredibly difficult error and I'm not sure what is causing it.
I'm working on a pick selection component for a game, and I use a MouseListener to 'schedule a pick-selection' using boolean values. After a pick-selection is scheduled, the GLAutoDrawable.display() method will check to see if it is scheduled, and if so, it will perform a selection.
Here's the problem, when the MouseListener alters the boolean value, it cannot be read from the GLAutoDrawable.display() method. And vice-versa.
So far I have tried to make it a volatile variable, and that does NOT work. The only way I can get it to work is if I make it a static variable, which I do not want to do.
My guess is that the way I have my class set up does not mesh the two threads together in a good manner. So I'll try to define my programs layout.
MainClass: (implements GLEventListener)
-on the init() method, it will create "GLButton" objects. During this call, it gives a reference to the GLCanvas for each button in order to add extra MouseListeners later.
-on the display() method it will call a render method of a "GLButton" object
GLButton:
-when constructed it will store the GLCanvas variable sent to it inside a class-scope variable. Also during the construction, a new MouseListener is added to the GLCanvas. The implementation of the MouseListener is inside the GLButton object.
-when render(GL2 gl2) method is called, it will draw the button and check if a pick-selection has occurred. If so, it will perform a selection base on the Mouse's location.
-when the mousePressed() method is called, a pick-selection is scheduled and the mouse's location is stored.
Here is some code to see what I was trying to explain:
public void mousePressed(MouseEvent e)
{
mouseLocation = e.getPoint();
schedulePickSelection();
}
//this method schedules a pick-selection
private synchronized void schedulePickSelection()//I probably don't even need it synchronized, just for debugging.
{
pickSchedule= true; //this is the class-scope variable to determine if a pick-selection is scheduled.
}
Later in the render method I have an if-statement to check if a pick-selection is scheduled. Remember that the render method() is called by the GLAutoDrawable thread.
if(isPickSelectionScheduled())
{//isPickSelectionSchedule() is returns true if a selection is still scheduled.
//reset the scedule (no longer scheduled)
pickSchedule = false;
//make a selection
select(c, gl2);
}
private synchronized boolean isPickSelectionScheduled()//again unsure about the synchronized.
{
return pickSchedule; //returns the class-scope variable mentions earlier.
}
And if there is any other information I should say in order to debug, let me know. One thing I should point out is that
I made a simple class to test this concept (without my whole game-structure) and everything worked fine. I didn't even have to declare volatile variables or synchronized methods. Just a MouseListener and the display() method.
If you would like more code just let me know.
Thanks for the help. Greatly appreciated. - Nick