StringBuffer behavior in LWJGL

Posted by Michael Oberlin on Game Development See other posts from Game Development or by Michael Oberlin
Published on 2013-11-05T21:12:36Z Indexed on 2013/11/05 22:14 UTC
Read the original article Hit count: 231

Filed under:
|
|
|
|

Okay, I've been programming in Java for about ten years, but am entirely new to LWJGL. I have a specific problem whilst attempting to create a text console. I have built a class meant to abstract input polling to it, which (in theory) captures key presses from the Keyboard object and appends them to a StringBuilder/StringBuffer, then retrieves the completed string after receiving the ENTER key.

The problem is, after I trigger the String return (currently with ESCAPE), and attempt to print it to System.out, I consistently get a blank line. I can get an appropriate string length, and I can even sample a single character out of it and get complete accuracy, but it never prints the actual string.

I could swear that LWJGL slipped some kind of thread-safety trick in while I wasn't looking.

Here's my code:

static volatile StringBuffer command = new StringBuffer();

@Override
public void chain(InputPoller poller) {
    this.chain = poller;
}

@Override
public synchronized void poll() {
    //basic testing for modifier keys, to be used later on
    boolean shift = false, alt = false, control = false, superkey = false;

    if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT) || Keyboard.isKeyDown(Keyboard.KEY_RSHIFT))
        shift = true;
    if(Keyboard.isKeyDown(Keyboard.KEY_LMENU) || Keyboard.isKeyDown(Keyboard.KEY_RMENU))
        alt = true;
    if(Keyboard.isKeyDown(Keyboard.KEY_LCONTROL) || Keyboard.isKeyDown(Keyboard.KEY_RCONTROL))
        control = true;
    if(Keyboard.isKeyDown(Keyboard.KEY_LMETA) || Keyboard.isKeyDown(Keyboard.KEY_RMETA))
        superkey = true;

    while(Keyboard.next())
        if(Keyboard.getEventKeyState()) {
            command.append(Keyboard.getEventCharacter());
        }

        if (Framework.isConsoleEnabled() && Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
            System.out.println("Escape down");
            System.out.println(command.length() + " characters polled"); //works
            System.out.println(command.toString().length()); //works
            System.out.println(command.toString().charAt(4)); //works
            System.out.println(command.toString().toCharArray()); //blank line!
            System.out.println(command.toString()); //blank line!

            Framework.disableConsole();
        }

        //TODO: Add command construction and console management after that
    }
}

Maybe the answer's obvious and I'm just feeling tired, but I need to walk away from this for a while. If anyone sees the issue, please let me know. This machine is running the latest release of Java 7 on Ubuntu 12.04, Mate desktop environment.

Many thanks.

© Game Development or respective owner

Related posts about java

Related posts about lwjgl