Class ScalableGameScreen

All Implemented Interfaces:
com.badlogic.gdx.InputProcessor, com.badlogic.gdx.Screen, com.badlogic.gdx.utils.Disposable, HUD, WorldSize

public abstract class ScalableGameScreen extends GameScreenWithHUD
An abstract base class for game screens that automatically handle scaling using a FitViewport.

This class extends GameScreenWithHUD and provides a camera and viewport setup that ensures your game world maintains a consistent aspect ratio across different screen sizes.

Subclasses only need to implement their own rendering and game logic; scaling, projection matrices, and input coordinate transformations are handled automatically.

Usage example:

 public class MyLevelScreen extends ScalableGameScreen {
     public MyLevelScreen() {
         super(800, 600); // World size in world units
     }

     @Override
     public void render(float delta) {
         super.render(delta); // Applies viewport and sets projection matrices
         // Your rendering logic here
     }
 }
 
See Also:
  • Constructor Details

    • ScalableGameScreen

      public ScalableGameScreen(int worldWidth, int worldHeight)
      Creates a new scalable game screen with a FitViewport configured to the given world size.
      Parameters:
      worldWidth - the width of the virtual game world, in world units
      worldHeight - the height of the virtual game world, in world units
  • Method Details

    • render

      public void render(float delta)
      Renders the screen for the current frame.

      This method applies the viewport and updates the projection matrices for both the SpriteBatch and ShapeRenderer provided by GameApp.

      Subclasses should call super.render(delta) at the start of their render() method to ensure proper scaling and projection setup.

      Specified by:
      render in interface com.badlogic.gdx.Screen
      Specified by:
      render in class GameScreen
      Parameters:
      delta - the time in seconds since the last frame was rendered
    • resize

      public void resize(int width, int height)
      Handles window resizing by updating the viewport.

      The FitViewport ensures that the aspect ratio of the game world remains consistent regardless of screen dimensions.

      Specified by:
      resize in interface com.badlogic.gdx.Screen
      Overrides:
      resize in class GameScreenWithHUD
      Parameters:
      width - the new width of the window in pixels
      height - the new height of the window in pixels
    • getWorldWidth

      public float getWorldWidth()
      Returns the width of the virtual game world in world units.
      Returns:
      the world width
    • getWorldHeight

      public float getWorldHeight()
      Returns the height of the virtual game world in world units.
      Returns:
      the world height
    • getMouseWorldPosition

      public com.badlogic.gdx.math.Vector2 getMouseWorldPosition()
      Converts the current mouse or touch position from screen coordinates into world coordinates.
      Returns:
      a Vector2 representing the mouse position in world space
    • getMouseX

      public float getMouseX()
      Returns the current mouse X position in world coordinates.
      Returns:
      the mouse X position in world space
    • getMouseY

      public float getMouseY()
      Returns the current mouse Y position in world coordinates.
      Returns:
      the mouse Y position in world space
    • getViewport

      public com.badlogic.gdx.utils.viewport.FitViewport getViewport()
      Returns the FitViewport used by this screen.

      This can be used to manually access or modify camera properties. It is recommended only for advanced use cases.

      Returns:
      the active FitViewport
    • getWorldCamera

      public com.badlogic.gdx.graphics.Camera getWorldCamera()
      Description copied from interface: WorldSize
      Returns the combined matrix of the world camera.

      This is used by the HUD to switch back to world rendering.

      Returns:
      the world camera's combined matrix