Search code examples
javalibgdx

Switching to fullscreen crashes application


Introduction

I’m attempting to switch my application from windowed to fullscreen. Earlier this would have been as easy as making use of LibGDX’s setDisplayMode(Display), as well as the same library’s getDesktopDisplayMode(). Unfortunately, some things have changed.

Research

As of LibGDX 1.8.0—inside the Graphics—what was previously known as setDisplayMode(DisplayMode) has been renamed to setFullScreenMode(DisplayMode), as well as what was previously known as getDesktopDisplayMode() has been renamed to getDisplayMode().

By then, one might think, that using the renamed methods in the same way as one previously would to switch from windowed to fullscreen would work just as fine:

// Tabulator key triggers change from windowed to fullscreen
// The application works fine until that of pressing the tabulator key
if(Gdx.input.isKeyJustPressed(Keys.TAB))
    Gdx.graphics.setFullscreenMode(Gdx.graphics.getDisplayMode());

This doesn’t work fine by any means. As a matter of fact, the application hangs followed by crashing upon pressing the tabulator key.

It’s worth to notice that the setFullscreenMode(Display)according to the documentation—returns a boolean depending on whether or not the operation was successful. In this case, if you were to put it inside a print statement, you can clearly tell it never returns: even should you let the application be for a while.

Moreover

I took a good look around the web—in vain—searching for possible causes of as to why this might be happening. Looking at the release notes of 1.8.0, it truly seems as though all that needs be done is switch from using the previous methods contained by the library to the new ones.

Question

Does anyone have any insight on as to why the application crashes upon pressing the tabulator key—granted the code following it as shown above is present? Perhas someone could at the very least point me in the right direction?

There’s a fair chance what I’ve concluded is missing out on some important facts.


Solution

  • Solution

    It turns out I’ve managed to be slightly silly!

    Background

    Whenever I create applications—especially games—even though I use libraries such as LibGDX, I like to go ahead and create myself a separate thread for handling the game logic. This yields me more control over the application as a whole—whether it be tick extremely frequently whilst rendering more seldom, or the other way around.

    By the way of my logic, the provided code from the question...

    if(Gdx.input.isKeyJustPressed(Keys.TAB))
        Gdx.graphics.setFullscreenMode(Gdx.graphics.getDisplayMode());
    

    ... I decided to put inside the tick method. Since that’s inside a separate thread, LibGDX probably felt a little left out.

    Moreover

    From my understanding, LibGDX handles all its logic inside the ApplicationAdapter’s render method. That’s where it evaluates all of the user’s actions and makes sure they don’t collide with other things taking place to its own various different components.

    For the aforementioned reasons—when I decided to put the “fullscreen logic” inside a separate thread, LibGDX crashed. Oh well, at least it works now—That is, by putting the aforementioned code inside the LibGDX’s ApplicationHandler’s render method!