Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

11991 Posts in 1587 Topics- by 3509 Members - Latest Member: lioneltenel

26. May 2012, 08:42:19 pm
Xith3D CommunityGeneral CategorySupport (Moderator: Marvin Fröhlich)registerXXX in RenderLoop
Pages: [1]
Print
Author Topic: registerXXX in RenderLoop  (Read 1998 times)
starscream
Enjoying the stay
*
Offline Offline

Posts: 25


View Profile
« on: 13. December 2006, 10:35:58 pm »

Hi,

make my first steps through the code and found something at the registerXX-methods, that makes me pensively:

    public KeyboardDevice registerKeyboard(KeyboardDevice keyboard)
    {
        keyboard.registerListener( inputListener );
        keyboardDevices.add( keyboard );
       
        return( keyboard );
    }
   
[JavaDoc]

    public KeyboardDevice registerKeyboard(Canvas3D canvas)
    {
        KeyboardDevice keyboard = null;
       
        switch ( canvas.get3DPeer().getType() )
        {
            case JOGL:
                keyboard = new AWTKeyboard( canvas.get3DPeer().getComponent() );
                break;
               
            case LWJGL:
                keyboard = new LWJGLKeyboard( LWJGLKeyboard.InputMode.BUFFERED );
                break;
        }
       
        if (keyboard != null)
        {
            keyboard.registerListener( inputListener );
            keyboardDevices.add( keyboard );
        }
       
        return( keyboard );
    }
   


I think, the methods should be implemented at this way:

    public KeyboardDevice registerKeyboard(KeyboardDevice keyboard)
    {
        if (keyboard != null)
        {
            keyboard.registerListener( inputListener );
            keyboardDevices.add( keyboard );
        }
       
        return( keyboard );
    }
   
    /**
     * Registers a KeyboardDevice on this object.
     * It takes the value from canvas.getOGLLayer() and for
     * Jogl, AWT-Devices are created, for LWJGL LWJGL-Devices.
     *
     * @param canvas the Canvas3D to take the OGLLayer from
     * @return the registered device itself
     */
    public KeyboardDevice registerKeyboard(Canvas3D canvas)
    {
        KeyboardDevice keyboard = null;
       
        switch ( canvas.get3DPeer().getType() )
        {
            case JOGL:
                keyboard = new AWTKeyboard( canvas.get3DPeer().getComponent() );
                break;
               
            case LWJGL:
                keyboard = new LWJGLKeyboard( LWJGLKeyboard.InputMode.BUFFERED );
                break;
        }
       
        return registerKeyboard( keyboard );
    }
   

Maybe i am just to fussy...
Logged
Marvin Fröhlich
Xith Lord
Administrator
Guru
*****
Offline Offline

Posts: 4381


May the 4th, be with you...


View Profile
« Reply #1 on: 13. December 2006, 10:46:25 pm »

Why that? The registerListener( KeyboardDevice ) method won't be called with a null argument. If it did, it is a logical error in the user's code. Please do correct me, but I don't see, why it is better this way.

Marvin
Logged
'n ddrylliog
Guru
*****
Offline Offline

Posts: 1188



View Profile WWW Email
« Reply #2 on: 14. December 2006, 06:05:36 pm »

@starscream : if a null keyboard is passed through registerListener(), it should throw a NullPointerException, which is a very good thing indeed.
Logged
Marvin Fröhlich
Xith Lord
Administrator
Guru
*****
Offline Offline

Posts: 4381


May the 4th, be with you...


View Profile
« Reply #3 on: 14. December 2006, 06:59:16 pm »

@starscream : if a null keyboard is passed through registerListener(), it should throw a NullPointerException, which is a very good thing indeed.

It will.

Code:
keyboard.registerListener( inputListener );
This line does it.

Marvin
Logged
starscream
Enjoying the stay
*
Offline Offline

Posts: 25


View Profile
« Reply #4 on: 14. December 2006, 09:08:55 pm »

Sure, but two methods doing the same with different results?
And duplicated code?
I thought that it would be better, if there could only be one result (and a NullpointerException is a good hint to show trouble in the user-code) and only one point in the code, where the inputListener ist registered to the keyboard and this keyboard is added to the KeyboardDevices.
The code could look like this:

    public KeyboardDevice registerKeyboard(KeyboardDevice keyboard)
    {
        keyboard.registerListener( inputListener );
        keyboardDevices.add( keyboard );
        return( keyboard );
    }
   
    /**
     * Registers a KeyboardDevice on this object.
     * It takes the value from canvas.getOGLLayer() and for
     * Jogl, AWT-Devices are created, for LWJGL LWJGL-Devices.
     *
     * @param canvas the Canvas3D to take the OGLLayer from
     * @return the registered device itself
     */
    public KeyboardDevice registerKeyboard(Canvas3D canvas)
    {
        KeyboardDevice keyboard = null;
       
        switch ( canvas.get3DPeer().getType() )
        {
            case JOGL:
                keyboard = new AWTKeyboard( canvas.get3DPeer().getComponent() );
                break;
               
            case LWJGL:
                keyboard = new LWJGLKeyboard( LWJGLKeyboard.InputMode.BUFFERED );
                break;
        }
       
        return registerKeyboard( keyboard );
    }



But as i told: maybe i am just to fussy ;-)
Logged
Marvin Fröhlich
Xith Lord
Administrator
Guru
*****
Offline Offline

Posts: 4381


May the 4th, be with you...


View Profile
« Reply #5 on: 14. December 2006, 09:42:49 pm »

The code could look like this:
Code:
    public KeyboardDevice registerKeyboard(KeyboardDevice keyboard)
    {
        keyboard.registerListener( inputListener );
        keyboardDevices.add( keyboard );
        return( keyboard );
    }
   
    /**
     * Registers a KeyboardDevice on this object.
     * It takes the value from canvas.getOGLLayer() and for
     * Jogl, AWT-Devices are created, for LWJGL LWJGL-Devices.
     *
     * @param canvas the Canvas3D to take the OGLLayer from
     * @return the registered device itself
     */
    public KeyboardDevice registerKeyboard(Canvas3D canvas)
    {
        KeyboardDevice keyboard = null;
       
        switch ( canvas.get3DPeer().getType() )
        {
            case JOGL:
                keyboard = new AWTKeyboard( canvas.get3DPeer().getComponent() );
                break;
               
            case LWJGL:
                keyboard = new LWJGLKeyboard( LWJGLKeyboard.InputMode.BUFFERED );
                break;
        }
       
        return registerKeyboard( keyboard );
    }

Yes. You're right. I'll change it. Thanks.

Marvin


EDIT: Done.
« Last Edit: 14. December 2006, 09:45:30 pm by Marvin Fröhlich » Logged
starscream
Enjoying the stay
*
Offline Offline

Posts: 25


View Profile
« Reply #6 on: 15. December 2006, 09:37:40 pm »

i will take a look at my first xith-change  Wink
Maybe i can help you later - if i understand the whole thing...
Logged
starscream
Enjoying the stay
*
Offline Offline

Posts: 25


View Profile
« Reply #7 on: 15. December 2006, 10:40:33 pm »

Checked out but found changes only at the registerKeyboard.
registerMouse seems to be the same:

    /**
     * Registers a MouseDevice on this object.
     *
     * @param mouse the MouseDevice to register
     * @return the registered device itself
     */
    public MouseDevice registerMouse(MouseDevice mouse)
    {
        mouse.registerListener( inputListener );
        mouseDevices.add( mouse );
       
        return( mouse );
    }
   
    /**
     * Registers a MouseDevice on this object.
     * It takes the value from canvas.getOGLLayer() and for
     * Jogl, AWT-Devices are created, for LWJGL LWJGL-Devices.
     *
     * @param canvas the Canvas3D to take the OGLLayer from
     */
    public MouseDevice registerMouse(Canvas3D canvas)
    {
        MouseDevice mouse = null;
       
        switch ( canvas.get3DPeer().getType() )
        {
            case JOGL:
                mouse = new AWTMouse( canvas.get3DPeer().getComponent() );
                break;
               
            case LWJGL:
                mouse = new LWJGLMouse( LWJGLMouse.InputMode.POLLED );
                break;
        }
       
        return( registerMouse( mouse ) );
    }
   
    /**
     * Registers a Keyboard- and a MouseDevice on this object.
     *
     * @param keyboard the KeyboardDevice to register
     * @param mouse the MouseDevice to register
     */
    public void registerKeyboardAndMouse(KeyboardDevice keyboard, MouseDevice mouse)
    {
        registerKeyboard( keyboard );
        registerMouse( mouse );
    }
   

And directly after that ther is a method i do not understand too:


    /**
     * Registers a Keyboard- and a MouseDevice on this object.
     * It takes the value from canvas.getOGLLayer() and for
     * Jogl, AWT-Devices are created, for LWJGL LWJGL-Devices.
     *
     * @param canvas the Canvas3D to take the OGLLayer from
     */
    public void registerKeyboardAndMouse(Canvas3D canvas)
    {
       if (!canvasRegisteredForInput.contains( canvas ))
        {
            registerKeyboard( canvas );
            registerMouse( canvas );
           
            canvasRegisteredForInput.add( canvas );
       }
    }


I do not understand the canvasRegisterForInput.
It is a private one which is just constructed in the Constructor and only used in this method. Do you realy need it anymore or is it a legacy one?
In my opinion the method should look like this:

    public void registerKeyboardAndMouse(Canvas3D canvas)
    {
            registerKeyboard( canvas );
            registerMouse( canvas );
    }

The reference to the canvasRegisterForInput should completely be removed.

And one more thing: thank you for Xith3D!
Logged
Marvin Fröhlich
Xith Lord
Administrator
Guru
*****
Offline Offline

Posts: 4381


May the 4th, be with you...


View Profile
« Reply #8 on: 15. December 2006, 11:26:15 pm »

Checked out but found changes only at the registerKeyboard.
registerMouse seems to be the same:

Stupid me! I forgot to change that for registerMouse. Thanks for the hint. It's changed now.

And directly after that ther is a method i do not understand too:

Code:
    /**
     * Registers a Keyboard- and a MouseDevice on this object.
     * It takes the value from canvas.getOGLLayer() and for
     * Jogl, AWT-Devices are created, for LWJGL LWJGL-Devices.
     *
     * @param canvas the Canvas3D to take the OGLLayer from
     */
    public void registerKeyboardAndMouse(Canvas3D canvas)
    {
    if (!canvasRegisteredForInput.contains( canvas ))
        {
            registerKeyboard( canvas );
            registerMouse( canvas );
           
            canvasRegisteredForInput.add( canvas );
    }
    }

I do not understand the canvasRegisterForInput.
It is a private one which is just constructed in the Constructor and only used in this method. Do you realy need it anymore or is it a legacy one?

Actually this is very new. It is to avoid to register a standard mouse and keyboard for a canvas twice. It doesn't make much sense to register a mouse or a keyboard twice to the same RenderLoop. This would always fire an input event twice (or more). So this check is a good one Wink. And yes, it's just private and for internal use.

And one more thing: thank you for Xith3D!

You're welcome Smiley.

Marvin
Logged
'n ddrylliog
Guru
*****
Offline Offline

Posts: 1188



View Profile WWW Email
« Reply #9 on: 16. December 2006, 12:45:33 pm »

And one more thing: thank you for Xith3D!
You're welcome, though kudos ought to go to Dave, Will, Yuri, JCD, Lilian, Croft, Kev, Arne, Flo, Bohdan,  and all others I forget here (forgive my Variable-ReliAbility-Memory (VRAM)  Grin ).

Oh, and Marvin and me Smiley (of course)
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic