Back to project page bbg-gdx-project-setup.
The source code is released under:
MIT License
If you think the Android project bbg-gdx-project-setup listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.bluebananagames.gametemplate.screen; // w w w . j a v a 2 s.co m import com.badlogic.gdx.Gdx; import com.badlogic.gdx.InputMultiplexer; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.scenes.scene2d.Stage; import com.bluebananagames.gametemplate.Assets; import com.bluebananagames.gametemplate.GameTemplateGame; public abstract class AbstractScreen implements com.badlogic.gdx.Screen { protected final GameTemplateGame game; protected final OrthographicCamera camera; protected final SpriteBatch batch; protected final Stage stage; protected final InputMultiplexer inputMultiplexer; private Rectangle viewport; protected Assets assets = new Assets(); public static final float CANVAS_WIDTH = 1920f; public static final float CANVAS_HEIGHT = 1080f; private static final float ASPECT_RATIO = CANVAS_WIDTH / CANVAS_HEIGHT; public AbstractScreen(GameTemplateGame game) { this.game = game; this.camera = new OrthographicCamera(CANVAS_WIDTH, CANVAS_HEIGHT); this.camera.position.set(CANVAS_WIDTH / 2f, CANVAS_HEIGHT / 2f, 0f); this.batch = new SpriteBatch(); this.stage = new Stage(CANVAS_WIDTH, CANVAS_HEIGHT, true); this.inputMultiplexer = new InputMultiplexer(stage); Gdx.input.setInputProcessor(inputMultiplexer); } @Override public void render(float delta) { // update and draw the stage actors stage.act(delta); camera.update(); if (viewport != null) { Gdx.gl.glViewport((int) viewport.x, (int) viewport.y, (int) viewport.width, (int) viewport.height); } // the following code clears the screen with the given RGB color (black) Gdx.gl.glClearColor(0f, 0f, 0f, 1f); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); } @Override public void dispose() { // dispose the collaborators stage.dispose(); batch.dispose(); assets.dispose(); } @Override public void resize(int width, int height) { // calculate new viewport float aspectRatio = (float) width / (float) height; float scale = 1f; Vector2 crop = new Vector2(0f, 0f); if (aspectRatio > ASPECT_RATIO) { scale = (float) height / (float) CANVAS_HEIGHT; crop.x = (width - CANVAS_WIDTH * scale) / 2f; } else if (aspectRatio < ASPECT_RATIO) { scale = (float) width / (float) CANVAS_WIDTH; crop.y = (height - CANVAS_HEIGHT * scale) / 2f; } else { scale = (float) width / (float) CANVAS_WIDTH; } float w = (float) CANVAS_WIDTH * scale; float h = (float) CANVAS_HEIGHT * scale; viewport = new Rectangle(crop.x, crop.y, w, h); camera.position.set(CANVAS_WIDTH / 2f, CANVAS_HEIGHT / 2f, 0f); // TODO: touch area is not right if using other aspect ratio then 1.6. // May not be an issue if depending on controller input alone. // stage.getCamera().position.set(CANVAS_WIDTH / 2f, CANVAS_HEIGHT / 2f, // 0f); // stage.getCamera().update(); } public Assets getAssets() { return assets; } }