LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: Evil-Devil on September 08, 2012, 21:51:03

Title: Friendly Gameloop to allow tab /focus switch (ALT+TAB)?
Post by: Evil-Devil on September 08, 2012, 21:51:03
Hi all,

its no problem to create a straight forward rendering, but still I haven't found a way to write a loop that does allow other activities beneath the ogl app.


while (this.running) {
if (Display.isCloseRequested()) {
this.running = false;
this.dispose();
}

if (Display.isActive() && Display.isVisible()) {
this.render();
Display.update();
}
}


My rendering loop is pretty easy, but even starting the application from within eclipse, setting the focus on eclipse and switch back to the ogl app, it does not respond anymore. I would appreciate any suggestions to avoid this as i like to have an app that does allow tab switching.
Title: Re: Friendly Gameloop to allow tab /focus switch (ALT+TAB)?
Post by: CodeBunny on September 09, 2012, 03:49:47
Here's mine (very abstracted, but fairly straightforward):

protected void run()
{
start(); // Initializes the game
while (!shouldExit()) // Checks both a control boolean and Display.isCloseRequested()
{
controller.tick(); // Updates the time
update(controller.delta()); // Updates the game based upon the delta time
render(); // Renders the game, also calls Display.Update()
controller.sleep(); // Handles sleeping the thread
}
end(); // Handles shutdown.
}


Display.isActive() seems to be bugged. When I added it to my game, it froze just like yours - upon losing focus, it will permanently return false, and also seems to block exit operations from proceeding. I've basically taken the option of not using it whatsoever.
Title: Re: Friendly Gameloop to allow tab /focus switch (ALT+TAB)?
Post by: Obsyd on September 09, 2012, 14:55:49
If your Display.udpade is not being called it will seem like your app is frozen. (because Dsiplay.update calls Display.processMessages())
Display.processMessages() still needs to be called once in a while.
Title: Re: Friendly Gameloop to allow tab /focus switch (ALT+TAB)?
Post by: delt0r on September 09, 2012, 16:59:22
I use Display.isActive and it works like a charm. I just sleep 100ms each loop so update is always getting called. I have noticed massive performance penalty with composing desktops. Turning it off works nicely.
Title: Re: Friendly Gameloop to allow tab /focus switch (ALT+TAB)?
Post by: CodeBunny on September 09, 2012, 20:41:06
Ah, that makes sense. Gotcha.
Title: Re: Friendly Gameloop to allow tab /focus switch (ALT+TAB)?
Post by: Evil-Devil on September 10, 2012, 07:25:04
Quote from: Obsyd on September 09, 2012, 14:55:49
If your Display.udpade is not being called it will seem like your app is frozen. (because Dsiplay.update calls Display.processMessages())
Display.processMessages() still needs to be called once in a while.

Ah ok, I'll give it a try :)
Title: Re: Friendly Gameloop to allow tab /focus switch (ALT+TAB)?
Post by: Evil-Devil on September 10, 2012, 20:55:03
Sry 4 double post...

It works, thanks alot =)


if (Display.isVisible() && Display.isActive()) {
this.render();
Display.update();
} else {
Display.processMessages();
}