Java tutorial
/* * Copyright: * This file was created by BinaryTENSHi and distributed * as part of NoPassing. * * NoPassing lies under a license which can be * found in the LICENSE file in the root directory * File created @ [26.12.2013, 19:04:40 CH timezone] */ package com.binarytenshi.nopassing.core; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.math.Vector2; import com.binarytenshi.nopassing.lib.MathHelper; public abstract class CameraHandler { public static final float SCALE = 512; public static final int VIRTUAL_WIDTH = 1280; public static final int VIRTUAL_HEIGHT = 720; public static final float ASPECT_RATIO = (float) VIRTUAL_WIDTH / (float) VIRTUAL_HEIGHT; private static OrthographicCamera camera; private static Rectangle viewport; public static void initialize() { float w = Gdx.graphics.getWidth(); float h = Gdx.graphics.getHeight(); camera = new OrthographicCamera(); camera.setToOrtho(false, SCALE, SCALE * (h / w)); camera.zoom = 5f; move(MapHandler.getMapSize().x / 2, MapHandler.getMapSize().y / 2, 0); resize((int) w, (int) h); } public static void move(float x, float y, float z) { camera.position.add(x, y, z); } public static void resize(int width, int height) { float aspectRatio = (float) width / (float) height; float scale; Vector2 crop = new Vector2(0f, 0f); if (aspectRatio > ASPECT_RATIO) { scale = (float) height / (float) VIRTUAL_HEIGHT; crop.x = (width - VIRTUAL_WIDTH * scale) / 2f; } else if (aspectRatio < ASPECT_RATIO) { scale = (float) width / (float) VIRTUAL_WIDTH; crop.y = (height - VIRTUAL_HEIGHT * scale) / 2f; } else { scale = (float) width / (float) VIRTUAL_WIDTH; } float w = VIRTUAL_WIDTH * scale; float h = VIRTUAL_HEIGHT * scale; viewport = new Rectangle(crop.x, crop.y, w, h); } public static void zoom(int amount) { float newZoom = camera.zoom + amount * 0.4f; camera.zoom = MathHelper.clamp(newZoom, 1f, 10f); } public static Rectangle getViewport() { return viewport; } public static OrthographicCamera getCamera() { return camera; } }