• 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.InputAdapter;
21 import com.badlogic.gdx.graphics.Color;
22 import com.badlogic.gdx.graphics.GL20;
23 import com.badlogic.gdx.graphics.Texture;
24 import com.badlogic.gdx.graphics.g2d.BitmapFont;
25 import com.badlogic.gdx.graphics.g2d.BitmapFontCache;
26 import com.badlogic.gdx.graphics.g2d.Sprite;
27 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
28 import com.badlogic.gdx.math.Matrix4;
29 import com.badlogic.gdx.tests.utils.GdxTest;
30 import com.badlogic.gdx.utils.Align;
31 
32 public class BitmapFontFlipTest extends GdxTest {
33 	private SpriteBatch spriteBatch;
34 	private Texture texture;
35 	private BitmapFont font;
36 	private Sprite logoSprite;
37 	private Color red = new Color(1, 0, 0, 0);
38 	private BitmapFontCache cache1, cache2, cache3, cache4, cache5;
39 	private BitmapFontCache cacheScaled1, cacheScaled2, cacheScaled3, cacheScaled4, cacheScaled5;
40 	int renderMode;
41 
42 	@Override
create()43 	public void create () {
44 		Gdx.input.setInputProcessor(new InputAdapter() {
45 			public boolean touchDown (int x, int y, int pointer, int newParam) {
46 				renderMode = (renderMode + 1) % 4;
47 				return false;
48 			}
49 		});
50 
51 		spriteBatch = new SpriteBatch();
52 		spriteBatch.setProjectionMatrix(new Matrix4().setToOrtho(0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), 0, 0, 1));
53 
54 		texture = new Texture(Gdx.files.internal("data/badlogic.jpg"));
55 		logoSprite = new Sprite(texture);
56 		logoSprite.flip(false, true);
57 		logoSprite.setPosition(0, 320 - 256);
58 		logoSprite.setColor(1, 1, 1, 0.5f);
59 
60 		font = new BitmapFont(Gdx.files.internal("data/verdana39.fnt"), Gdx.files.internal("data/verdana39.png"), true);
61 
62 		cache1 = font.newFontCache();
63 		cache2 = font.newFontCache();
64 		cache3 = font.newFontCache();
65 		cache4 = font.newFontCache();
66 		cache5 = font.newFontCache();
67 		createCaches("cached", cache1, cache2, cache3, cache4, cache5);
68 
69 		font.getData().setScale(1.33f);
70 		cacheScaled1 = font.newFontCache();
71 		cacheScaled2 = font.newFontCache();
72 		cacheScaled3 = font.newFontCache();
73 		cacheScaled4 = font.newFontCache();
74 		cacheScaled5 = font.newFontCache();
75 		createCaches("cache scaled", cacheScaled1, cacheScaled2, cacheScaled3, cacheScaled4, cacheScaled5);
76 	}
77 
createCaches(String type, BitmapFontCache cache1, BitmapFontCache cache2, BitmapFontCache cache3, BitmapFontCache cache4, BitmapFontCache cache5)78 	private void createCaches (String type, BitmapFontCache cache1, BitmapFontCache cache2, BitmapFontCache cache3,
79 		BitmapFontCache cache4, BitmapFontCache cache5) {
80 		cache1.setText("(" + type + ")", 10, 320 - 66);
81 
82 		String text = "Sphinx of black quartz,\njudge my vow.";
83 		cache2.setColor(Color.RED);
84 		cache2.setText(text, 5, 320 - 300);
85 
86 		text = "How quickly\ndaft jumping zebras vex.";
87 		cache3.setColor(Color.BLUE);
88 		cache3.setText(text, 5, 320 - 200, 470, Align.center, false);
89 
90 		text = "Kerning: LYA moo";
91 		cache4.setText(text, 210, 320 - 66, 0, text.length() - 3, 0, Align.left, false);
92 
93 		text = "Forsaking monastic tradition, twelve jovial friars gave\nup their vocation for a questionable existence on the flying trapeze.";
94 		cache5.setColor(red);
95 		cache5.setText(text, 0, 320 - 300, 480, Align.center, false);
96 	}
97 
98 	@Override
render()99 	public void render () {
100 		red.a = (red.a + Gdx.graphics.getDeltaTime() * 0.1f) % 1;
101 
102 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
103 		spriteBatch.begin();
104 		logoSprite.draw(spriteBatch);
105 		switch (renderMode) {
106 		case 0:
107 			font.getData().setScale(1);
108 			renderNormal("normal");
109 			break;
110 		case 1:
111 			font.getData().setScale(1);
112 			renderCached();
113 			break;
114 		case 2:
115 			font.getData().setScale(red.a + 0.5f);
116 			renderNormal("normal scaled");
117 			break;
118 		case 3:
119 			font.getData().setScale(1);
120 			renderCachedScaled();
121 			break;
122 		}
123 		spriteBatch.end();
124 	}
125 
renderNormal(String type)126 	private void renderNormal (String type) {
127 		String text = "Forsaking monastic tradition, twelve jovial friars gave\nup their vocation for a questionable existence on the flying trapeze.";
128 		font.setColor(red);
129 		font.draw(spriteBatch, text, 0, 320 - 300, 480, Align.center, false);
130 
131 		font.setColor(Color.WHITE);
132 		font.draw(spriteBatch, "(" + type + ")", 10, 320 - 66);
133 
134 		if (red.a > 0.6f) return;
135 
136 		text = "Sphinx of black quartz,\njudge my vow.";
137 		font.setColor(Color.RED);
138 		font.draw(spriteBatch, text, 5, 320 - 300);
139 
140 		text = "How quickly\ndaft jumping zebras vex.";
141 		font.setColor(Color.BLUE);
142 		font.draw(spriteBatch, text, 5, 320 - 200, 470, Align.right, false);
143 
144 		text = "Kerning: LYA moo";
145 		font.setColor(Color.WHITE);
146 		font.draw(spriteBatch, text, 210, 320 - 66, 0, text.length() - 3, 0, Align.left, false);
147 	}
148 
renderCached()149 	private void renderCached () {
150 		cache5.setColors(red);
151 		cache5.draw(spriteBatch);
152 
153 		cache1.draw(spriteBatch);
154 
155 		if (red.a > 0.6f) return;
156 
157 		cache2.draw(spriteBatch);
158 		cache3.draw(spriteBatch);
159 		cache4.draw(spriteBatch);
160 	}
161 
renderCachedScaled()162 	private void renderCachedScaled () {
163 		cacheScaled5.setColors(red);
164 		cacheScaled5.draw(spriteBatch);
165 
166 		cacheScaled1.draw(spriteBatch);
167 
168 		if (red.a > 0.6f) return;
169 
170 		cacheScaled2.draw(spriteBatch);
171 		cacheScaled3.draw(spriteBatch);
172 		cacheScaled4.draw(spriteBatch);
173 	}
174 
175 	@Override
dispose()176 	public void dispose () {
177 		spriteBatch.dispose();
178 		font.dispose();
179 		texture.dispose();
180 	}
181 }
182