1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.tests; 18 19 import com.badlogic.gdx.Gdx; 20 import com.badlogic.gdx.graphics.GL20; 21 import com.badlogic.gdx.graphics.OrthographicCamera; 22 import com.badlogic.gdx.graphics.Texture; 23 import com.badlogic.gdx.graphics.g2d.BitmapFont; 24 import com.badlogic.gdx.graphics.g2d.SpriteBatch; 25 import com.badlogic.gdx.graphics.g2d.TextureRegion; 26 import com.badlogic.gdx.math.Matrix4; 27 import com.badlogic.gdx.math.Vector3; 28 import com.badlogic.gdx.tests.utils.GdxTest; 29 import com.badlogic.gdx.tests.utils.OrthoCamController; 30 31 public class ParallaxTest extends GdxTest { 32 class ParallaxCamera extends OrthographicCamera { 33 Matrix4 parallaxView = new Matrix4(); 34 Matrix4 parallaxCombined = new Matrix4(); 35 Vector3 tmp = new Vector3(); 36 Vector3 tmp2 = new Vector3(); 37 38 public ParallaxCamera (float viewportWidth, float viewportHeight) { 39 super(viewportWidth, viewportHeight); 40 } 41 42 public Matrix4 calculateParallaxMatrix (float parallaxX, float parallaxY) { 43 update(); 44 tmp.set(position); 45 tmp.x *= parallaxX; 46 tmp.y *= parallaxY; 47 48 parallaxView.setToLookAt(tmp, tmp2.set(tmp).add(direction), up); 49 parallaxCombined.set(projection); 50 Matrix4.mul(parallaxCombined.val, parallaxView.val); 51 return parallaxCombined; 52 } 53 } 54 55 TextureRegion[] layers; 56 ParallaxCamera camera; 57 OrthoCamController controller; 58 SpriteBatch batch; 59 BitmapFont font; 60 61 @Override 62 public void create () { 63 Texture texture = new Texture(Gdx.files.internal("data/layers.png")); 64 layers = new TextureRegion[3]; 65 layers[0] = new TextureRegion(texture, 0, 0, 542, 363); 66 layers[1] = new TextureRegion(texture, 0, 363, 1024, 149); 67 layers[2] = new TextureRegion(texture, 547, 0, 224, 51); 68 69 camera = new ParallaxCamera(480, 320); 70 controller = new OrthoCamController(camera); 71 Gdx.input.setInputProcessor(controller); 72 batch = new SpriteBatch(); 73 font = new BitmapFont(Gdx.files.internal("data/arial-15.fnt"), false); 74 } 75 76 @Override 77 public void dispose () { 78 layers[0].getTexture().dispose(); 79 batch.dispose(); 80 font.dispose(); 81 } 82 83 @Override 84 public void render () { 85 Gdx.gl.glClearColor(242 / 255.0f, 210 / 255.0f, 111 / 255.0f, 1); 86 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 87 88 // keep camera in foreground layer bounds 89 boolean updateCamera = false; 90 if (camera.position.x < -1024 + camera.viewportWidth / 2) { 91 camera.position.x = -1024 + (int)(camera.viewportWidth / 2); 92 updateCamera = true; 93 } 94 95 if (camera.position.x > 1024 - camera.viewportWidth / 2) { 96 camera.position.x = 1024 - (int)(camera.viewportWidth / 2); 97 updateCamera = true; 98 } 99 100 if (camera.position.y < 0) { 101 camera.position.y = 0; 102 updateCamera = true; 103 } 104 // arbitrary height of scene 105 if (camera.position.y > 400 - camera.viewportHeight / 2) { 106 camera.position.y = 400 - (int)(camera.viewportHeight / 2); 107 updateCamera = true; 108 } 109 110 // background layer, no parallax, centered around origin 111 batch.setProjectionMatrix(camera.calculateParallaxMatrix(0, 0)); 112 batch.disableBlending(); 113 batch.begin(); 114 batch.draw(layers[0], -(int)(layers[0].getRegionWidth() / 2), -(int)(layers[0].getRegionHeight() / 2)); 115 batch.end(); 116 batch.enableBlending(); 117 118 // midground layer, 0.5 parallax (move at half speed on x, full speed on y) 119 // layer is 1024x320 120 batch.setProjectionMatrix(camera.calculateParallaxMatrix(0.5f, 1)); 121 batch.begin(); 122 batch.draw(layers[1], -512, -160); 123 batch.end(); 124 125 // foreground layer, 1.0 parallax (move at full speed) 126 // layer is 2048x320 127 batch.setProjectionMatrix(camera.calculateParallaxMatrix(1f, 1)); 128 batch.begin(); 129 for (int i = 0; i < 9; i++) { 130 batch.draw(layers[2], i * layers[2].getRegionWidth() - 1024, -160); 131 } 132 batch.end(); 133 134 // draw fps 135 batch.getProjectionMatrix().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 136 batch.begin(); 137 font.draw(batch, "fps: " + Gdx.graphics.getFramesPerSecond(), 0, 30); 138 batch.end(); 139 } 140 } 141