Search code examples
javawindowresolutionslick2d

With Slick2D, how to change the resolution during gameplay?


I am developing a tile-based strategy game using Java and the Slick2D API.

So far so good, but I've come to a standstill on my options menu. I have plans for the user to be able to change the resolution during gameplay (it is pretty common, after all).

I can already change to fullscreen and back to windowed, this was pretty simple...

//"fullScreenOption" is a checkbox-like button.
if (fullScreenOption.isMouseOver(mouseX, mouseY)) {
   if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
      fullScreenOption.state = !fullScreenOption.state;
      container.setFullscreen(fullScreenOption.state);
   }
}

But the container class (Implemented by Slick, not me), contrary to my previous beliefs, does not seem to have any display-mode/resolution-change functions! And that's pretty much the situation...

I know it's possible, but i don't know what is the class responsible (if it exists)!

The AppGameContainer class, used on the very start of the game's initialization, is the only place with any functions for changing the display-mode that I've found so far, but it's only used at the very start, and as of Slick's tutorials, is implemented as local.

//This is my implementation of it...
  public static void main(String[] args) throws SlickException {
    AppGameContainer app = new AppGameContainer(new Main());
//    app.setTargetFrameRate(60);
    app.setVSync(true);
    app.setDisplayMode(800, 600, false);
    app.start();
  }

I can define it as a static global on the Main class, in order to use it inside the update() method of the options screen, but it's probably a (very) bad way to do it...


Solution

  • There is an easy way to do this. I found this on another site. You can cast the GameContainer object (the one you get from the update method) into an AppGameContainer method. Then you can access the setDisplayMode:

    AppGameContainer gc = (AppGameContainer) container;
    gc.setDisplayMode(800, 600, false);
    

    Original post here.