• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Input;
21 import com.badlogic.gdx.InputAdapter;
22 import com.badlogic.gdx.graphics.GL20;
23 import com.badlogic.gdx.graphics.OrthographicCamera;
24 import com.badlogic.gdx.graphics.Pixmap;
25 import com.badlogic.gdx.graphics.Pixmap.Format;
26 import com.badlogic.gdx.graphics.Texture;
27 import com.badlogic.gdx.graphics.g2d.BitmapFont;
28 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
29 import com.badlogic.gdx.graphics.glutils.HdpiUtils;
30 import com.badlogic.gdx.tests.utils.GdxTest;
31 import com.badlogic.gdx.utils.Array;
32 import com.badlogic.gdx.utils.Scaling;
33 import com.badlogic.gdx.utils.viewport.ExtendViewport;
34 import com.badlogic.gdx.utils.viewport.ScalingViewport;
35 import com.badlogic.gdx.utils.viewport.ScreenViewport;
36 import com.badlogic.gdx.utils.viewport.Viewport;
37 
38 /** Cycles viewports while rendering with SpriteBatch, also shows how to draw in the black bars. */
39 public class ViewportTest2 extends GdxTest {
40 	Array<Viewport> viewports;
41 	Viewport viewport;
42 	Array<String> names;
43 	String name;
44 
45 	private SpriteBatch batch;
46 	private Texture texture;
47 	private BitmapFont font;
48 	private OrthographicCamera camera;
49 
create()50 	public void create () {
51 		font = new BitmapFont();
52 		font.setColor(0, 0, 0, 1);
53 
54 		Pixmap pixmap = new Pixmap(16, 16, Format.RGBA8888);
55 		pixmap.setColor(1, 1, 1, 1);
56 		pixmap.fill();
57 		texture = new Texture(pixmap);
58 
59 		batch = new SpriteBatch();
60 
61 		camera = new OrthographicCamera();
62 		camera.position.set(100, 100, 0);
63 		camera.update();
64 
65 		viewports = ViewportTest1.getViewports(camera);
66 		viewport = viewports.first();
67 
68 		names = ViewportTest1.getViewportNames();
69 		name = names.first();
70 
71 		Gdx.input.setInputProcessor(new InputAdapter() {
72 			public boolean keyDown (int keycode) {
73 				if (keycode == Input.Keys.SPACE) {
74 					int index = (viewports.indexOf(viewport, true) + 1) % viewports.size;
75 					name = names.get(index);
76 					viewport = viewports.get(index);
77 					resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
78 				}
79 				return false;
80 			}
81 		});
82 	}
83 
render()84 	public void render () {
85 		batch.setProjectionMatrix(camera.projection);
86 		batch.setTransformMatrix(camera.view);
87 
88 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
89 		batch.begin();
90 		// draw a white background so we are able to see the black bars
91 		batch.setColor(1, 1, 1, 1);
92 		batch.draw(texture, -4096, -4096, 4096, 4096, 8192, 8192, 1, 1, 0, 0, 0, 16, 16, false, false);
93 
94 		batch.setColor(1, 0, 0, 1);
95 		batch.draw(texture, 150, 100, 16, 16, 32, 32, 1, 1, 45, 0, 0, 16, 16, false, false);
96 
97 		font.draw(batch, viewport.getClass().getSimpleName(), 150, 100);
98 		batch.end();
99 
100 		if (viewport instanceof ScalingViewport) {
101 			// This shows how to set the viewport to the whole screen and draw within the black bars.
102 			ScalingViewport scalingViewport = (ScalingViewport)viewport;
103 			int screenWidth = Gdx.graphics.getWidth();
104 			int screenHeight = Gdx.graphics.getHeight();
105 			HdpiUtils.glViewport(0, 0, screenWidth, screenHeight);
106 			batch.getProjectionMatrix().idt().setToOrtho2D(0, 0, screenWidth, screenHeight);
107 			batch.getTransformMatrix().idt();
108 			batch.begin();
109 			float leftGutterWidth = scalingViewport.getLeftGutterWidth();
110 			if (leftGutterWidth > 0) {
111 				batch.draw(texture, 0, 0, leftGutterWidth, screenHeight);
112 				batch.draw(texture, scalingViewport.getRightGutterX(), 0, scalingViewport.getRightGutterWidth(), screenHeight);
113 			}
114 			float bottomGutterHeight = scalingViewport.getBottomGutterHeight();
115 			if (bottomGutterHeight > 0) {
116 				batch.draw(texture, 0, 0, screenWidth, bottomGutterHeight);
117 				batch.draw(texture, 0, scalingViewport.getTopGutterY(), screenWidth, scalingViewport.getTopGutterHeight());
118 			}
119 			batch.end();
120 			viewport.update(screenWidth, screenHeight, true); // Restore viewport.
121 		}
122 	}
123 
resize(int width, int height)124 	public void resize (int width, int height) {
125 		System.out.println(name);
126 		viewport.update(width, height);
127 	}
128 
dispose()129 	public void dispose () {
130 		texture.dispose();
131 		batch.dispose();
132 	}
133 }
134