Class ScalableGameScreen
- All Implemented Interfaces:
com.badlogic.gdx.InputProcessor,com.badlogic.gdx.Screen,com.badlogic.gdx.utils.Disposable,HUD,WorldSize
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:
-
GameScreenWithHUDFitViewportGameApp
-
Constructor Summary
ConstructorsConstructorDescriptionScalableGameScreen(int worldWidth, int worldHeight) Creates a new scalable game screen with aFitViewportconfigured to the given world size. -
Method Summary
Modifier and TypeMethodDescriptioncom.badlogic.gdx.math.Vector2Converts the current mouse or touch position from screen coordinates into world coordinates.floatReturns the current mouse X position in world coordinates.floatReturns the current mouse Y position in world coordinates.com.badlogic.gdx.utils.viewport.FitViewportReturns theFitViewportused by this screen.com.badlogic.gdx.graphics.CameraReturns the combined matrix of the world camera.floatReturns the height of the virtual game world in world units.floatReturns the width of the virtual game world in world units.voidrender(float delta) Renders the screen for the current frame.voidresize(int width, int height) Handles window resizing by updating the viewport.Methods inherited from class nl.saxion.gameapp.screens.GameScreenWithHUD
enableHUD, getHUDCamera, getHUDHeight, getHUDViewport, getHUDWidth, getMouseHUDPosition, getMouseHUDX, getMouseHUDY, getUIStage, hudToWorld, hudXtoWorldX, hudYtoWorldY, isHUDEnabled, onUIInteraction, renderUI, switchToHudRendering, switchToWorldRendering, worldToHUD, worldXtoHUDX, worldYtoHUDYMethods inherited from class nl.saxion.gameapp.screens.GameScreen
dispose, keyDown, keyTyped, keyUp, mouseMoved, pause, resume, scrolled, touchCancelled, touchDown, touchDragged, touchUpMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.badlogic.gdx.Screen
hide, show
-
Constructor Details
-
ScalableGameScreen
public ScalableGameScreen(int worldWidth, int worldHeight) Creates a new scalable game screen with aFitViewportconfigured to the given world size.- Parameters:
worldWidth- the width of the virtual game world, in world unitsworldHeight- 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
SpriteBatchandShapeRendererprovided byGameApp.Subclasses should call
super.render(delta)at the start of theirrender()method to ensure proper scaling and projection setup.- Specified by:
renderin interfacecom.badlogic.gdx.Screen- Specified by:
renderin classGameScreen- 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
FitViewportensures that the aspect ratio of the game world remains consistent regardless of screen dimensions.- Specified by:
resizein interfacecom.badlogic.gdx.Screen- Overrides:
resizein classGameScreenWithHUD- Parameters:
width- the new width of the window in pixelsheight- 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
Vector2representing 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 theFitViewportused 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:WorldSizeReturns 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
-