LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: elias4444 on July 29, 2009, 19:44:40

Title: Does LWJGL-Applet require AWTGLCanvas?
Post by: elias4444 on July 29, 2009, 19:44:40
Just wondering...

I can get ALMOST everything working just fine in my engine by using Display.setParent(Canvas) - notice, I use a Canvas object, and not AWTGLCanvas. However, I don't get any keyboard input.

In order to get input and whatnot, do I need to use AWTGLCanvas? I only ask because I'm getting some weird errors if I switch over, but if I have to, I'll push ahead with it.

Thanks.
Title: Re: Does LWJGL-Applet require AWTGLCanvas?
Post by: kappa on July 29, 2009, 19:54:42
nope you don't need AWTGLCanvas, when using Display.setParent() Input works just as it would on a native display.

Be sure the Canvas/Display is getting focus.
Title: Re: Does LWJGL-Applet require AWTGLCanvas?
Post by: elias4444 on July 29, 2009, 20:07:11
Hmmm... what else can I do to make sure the applet is getting focus? I've tried clicking on it numerous times.
Title: Re: Does LWJGL-Applet require AWTGLCanvas?
Post by: kappa on July 29, 2009, 20:55:11
hmm, clicking should really give you focus.

which browser and os are you using?
Title: Re: Does LWJGL-Applet require AWTGLCanvas?
Post by: elias4444 on July 29, 2009, 21:41:40
I've tried it on both Firefox 3.5 and Safari 4 on Mac OS X.
Title: Re: Does LWJGL-Applet require AWTGLCanvas?
Post by: kappa on July 29, 2009, 21:57:57
could possibly be a bug on mac, does minecraft work for you?

keyboard input that is?

http://www.minecraft.net
Title: Re: Does LWJGL-Applet require AWTGLCanvas?
Post by: elias4444 on July 29, 2009, 22:08:39
Hmmm... looking like a bug. I had mouse control, but no keyboard with Minecraft. It got a little tricky to get out of the game since I couldn't press ESC to release the mouse.  :P
Title: Re: Does LWJGL-Applet require AWTGLCanvas?
Post by: elias4444 on July 30, 2009, 16:36:59
So, is this bug fixable?
Title: Re: Does LWJGL-Applet require AWTGLCanvas?
Post by: kappa on July 30, 2009, 18:05:42
its really odd that keyboard input should be broken on mac's, but to honest not much testing has gone into lwjgl applets + mac since the applet setParent stuff is relatively new and not tested too much on mac due to mac's being rare.

I've done some digging to see if other mac users suffer from the same problem and came across this thread on the minecraft forums

http://minecraftforum.net/viewtopic.php?id=960

apparently according to that thread the new update of Java has fixed the problem.

Any more info you can give regarding the problem? what type of mac do you have? and which version of Java do you use?

remember keyboard input will only get captured if the applet is getting focus, so could be that the applet is not getting focus for some reason, you could do some testing to see if clicking is actually give the Display focus, check this with Display.isActive() and parentCanvas.hasFocus();

thx
Title: Re: Does LWJGL-Applet require AWTGLCanvas?
Post by: elias4444 on July 30, 2009, 19:19:06
Well, I'm on a Mac Pro, and I've setup the latest Java version (Java 6 64-bit) to load for Applets first from the Java Preferences. I also tried loading Minecraft on my Macbook Pro, but only get a black applet screen (in both Firefox and Safari).
Title: Re: Does LWJGL-Applet require AWTGLCanvas?
Post by: elias4444 on July 30, 2009, 22:11:55
I wrote a quick and dirty applet to test which version of Java the browser is actually using. Turns out, both machines are still using Java 5 (even though Java 6 has been bumped to the top of the preference order). Not sure what else can be done about that though.
Title: Re: Does LWJGL-Applet require AWTGLCanvas?
Post by: elias4444 on July 30, 2009, 22:26:10
Looks like this guy knows the answer:

http://www.nobel-joergensen.com/roller/java/entry/java_6_applets_is_coming (http://www.nobel-joergensen.com/roller/java/entry/java_6_applets_is_coming)

Guess this means that Mac users once again have to wait (either that, or we need to get LWJGL applets working for Java 1.5 on the Mac).


Title: Re: Does LWJGL-Applet require AWTGLCanvas?
Post by: elias4444 on July 30, 2009, 23:05:35
... Ok ...

I've been poking around through the AppletLoader code, and I noticed that the switchApplet() method starts the lwjglApplet in the current thread. Now, I'm no expert, so I'm probably just misunderstanding the code, but I've noticed with my own attempts that when I try to launch a Display from within a swing or AWT object, the keyboard never works (sound familiar?). In order to allow LWJGL to the Keyboard, I have to launch the openGL rendering class in a different thread. I'm not sure if this is a Mac only issue, but it sure sounds close to what we're seeing here.
Title: Re: Does LWJGL-Applet require AWTGLCanvas?
Post by: kappa on July 30, 2009, 23:15:39
Its unlikely to be a lwjgl appletloader issue, since once its completes its job it passes over control to the applet it has just downloaded and disables itself. Its mostly likely to do with how Display.setParent() works, so your looking at the MacOSXDisplay class and other MacOSX* classes in the org.lwjgl.opengl.* package.

Quote from: javalwjgl on July 30, 2009, 18:05:42
remember keyboard input will only get captured if the applet is getting focus, so could be that the applet is not getting focus for some reason, you could do some testing to see if clicking is actually give the Display focus, check this with Display.isActive() and parentCanvas.hasFocus();

any luck with testing the above quoted parts with your lwjgl applet?
Title: Re: Does LWJGL-Applet require AWTGLCanvas?
Post by: elias4444 on July 30, 2009, 23:52:21
Quoteany luck with testing the above quoted parts with your lwjgl applet?
None.  :(

I check for both methods. In my appletviewer (launched from Eclipse), I can gain focus by clicking on it, no problem.
In both Safari and Firefox however, I can't get focus by any means known to my pitiful brain.  ::)

On the Windows side, in Firefox, by clicking in the applet the Display becomes active, but the Canvas.hasFocus() still return as false.

Check it out:

http://www.tommytwisters.com/games/rollemdice/test/applet.html (http://www.tommytwisters.com/games/rollemdice/test/applet.html)

Press the spacebar to role the dice (so long as you have focus, and you're not on a Mac).
Title: Re: Does LWJGL-Applet require AWTGLCanvas?
Post by: kappa on July 31, 2009, 00:20:50
ok great, so at least we know now that the keyboard fails due to focus not being available. So the bug is that the Display is not getting focus when it is clicked.

We had the same/similar issue with Linux and Keyboard input with lwjgl applets a while back but since we can test it easily (i use Linux) we were able to fix it (see last patch to LinuxDisplay).

Display.setParent() essentially just slaps a native Display on top of a canvas, so when you click the Display it knows that it has focus and grabs the keyboard. So as you have observed Canvas.hasFocus() is under the Display and doesn't get focus, if the Canvas does get focus it transfers it too the Display hence loosing focus itself (since only one thing can get focus at a time, either the Canvas or the Display).

Now I'm guessing that this is a similar bug to the one on linux, focus was being lost as soon as Display was clicked.

Next we need to know if Display ever gets focus, if you can output a

System.out.println("DISPLAY HAS FOCUS : " + Display.isActive() + " Canvas has Focus " + canvas.hasFocus());

every frame, then click the applet and check console if the Display ever returns true? (even if its only for 1 frame)

this'll hopefully narrow down the things that might be going wrong.

p.s. your applets works very nicely here on linux including keyboard input.
Title: Re: Does LWJGL-Applet require AWTGLCanvas?
Post by: elias4444 on July 31, 2009, 00:37:07
Well, I tried it. Here's the output from my applet:

==========================================
Attempting to Initialize Graphics Mode
==========================================
fullscreen: Applet Mode
resolution: 800 x 600
colordepth: 32bpp
AAmode: x4
==========================================

Compiling: com/tommyengine/shaders/examples/shadowspotppl.vert ... 0 errors detected
Compiling: com/tommyengine/shaders/examples/shadowspotppl.frag ... 0 errors detected
* Epsilon: 1
* ShadowMap: 0
* shadowsActive: 6
* baseTexture: 8
* Accessibility: 2
* baseTextureActive: 7
* isSpotLight: 5
* normalMap: 4
* normalMapActive: 2
* vTangent: 1
==========================================
Texture System Enabled...
Max Texture Size: 4096
==========================================

Throwing Dice
DISPLAY HAS FOCUS: false      CANVAS HAS FOCUS: false
Shadow Map FBO successfully created
Object Compiled. Faces: 108 Frames: 1
DISPLAY HAS FOCUS: false      CANVAS HAS FOCUS: false
DISPLAY HAS FOCUS: false      CANVAS HAS FOCUS: false
DISPLAY HAS FOCUS: false      CANVAS HAS FOCUS: false
DISPLAY HAS FOCUS: false      CANVAS HAS FOCUS: false
DISPLAY HAS FOCUS: false      CANVAS HAS FOCUS: false
DISPLAY HAS FOCUS: false      CANVAS HAS FOCUS: false
etc...


Doesn't look like the Display or Canvas ever has focus.  ???
Title: Re: Does LWJGL-Applet require AWTGLCanvas?
Post by: kappa on July 31, 2009, 00:44:50
hmm, looks like it's a slightly different problem then, does clicking make a difference? (i.e. give focus for 1 frame)
Title: Re: Does LWJGL-Applet require AWTGLCanvas?
Post by: elias4444 on July 31, 2009, 01:00:51
HA!!!! FOUND IT!!!!!!

The Applet object has to exist in my looping object! Currently, I have the Applet create a "scene" object, which runs the loop and updates the display (which is linked to the Canvas via a "screen" object). For some reason, as soon as I passed the Applet object into the scene object (by simply making it a variable in the scenes constructor), I could click for focus!!!

I'm not sure I understand the nuances of that, but it works!  ;D
Found that totally by chance too!

Now if I can just get it to shutdown properly :P

Thanks for the help. Sorry if I stirred up trouble again.
Title: Re: Does LWJGL-Applet require AWTGLCanvas?
Post by: elias4444 on July 31, 2009, 01:39:42
BTW, I still have the problem with the Minecraft.net applet... so you may want to fix that.  ;)
Title: Re: Does LWJGL-Applet require AWTGLCanvas?
Post by: kappa on July 31, 2009, 01:48:16
k, i'll point this thread out to Markus next time I see him online :)

btw do keyboard controls for slick2d applets work for you? example like this game (http://gamejolt.com/online/games/platformer/castle-of-dread/324/)
Title: Re: Does LWJGL-Applet require AWTGLCanvas?
Post by: elias4444 on July 31, 2009, 01:52:23
They do actually! Guess we should have tried those before!  :P