2 * Copyright 2010 Mario Zechner (contact@badlogicgames.com), Nathan Sweet (admin@esotericsoftware.com)
\r
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
\r
5 * License. You may obtain a copy of the License at
\r
7 * http://www.apache.org/licenses/LICENSE-2.0
\r
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
\r
10 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
\r
11 * governing permissions and limitations under the License.
\r
14 package com.badlogic.gdx.tests;
\r
16 import com.badlogic.gdx.Gdx;
\r
17 import com.badlogic.gdx.InputAdapter;
\r
18 import com.badlogic.gdx.InputProcessor;
\r
19 import com.badlogic.gdx.Files.FileType;
\r
20 import com.badlogic.gdx.graphics.BitmapFont;
\r
21 import com.badlogic.gdx.graphics.BitmapFontCache;
\r
22 import com.badlogic.gdx.graphics.Color;
\r
23 import com.badlogic.gdx.graphics.GL10;
\r
24 import com.badlogic.gdx.graphics.Sprite;
\r
25 import com.badlogic.gdx.graphics.SpriteBatch;
\r
26 import com.badlogic.gdx.graphics.BitmapFont.HAlignment;
\r
27 import com.badlogic.gdx.graphics.Texture.TextureFilter;
\r
28 import com.badlogic.gdx.graphics.Texture.TextureWrap;
\r
29 import com.badlogic.gdx.math.Matrix4;
\r
30 import com.badlogic.gdx.tests.utils.GdxTest;
\r
32 public class BitmapFontTest extends GdxTest {
\r
33 private SpriteBatch spriteBatch;
\r
34 private BitmapFont font;
\r
35 private Sprite logoSprite;
\r
36 private Color red = new Color(1, 0, 0, 1);
\r
37 private BitmapFontCache cache1, cache2, cache3, cache4, cache5;
\r
39 InputProcessor inputProcessor;
\r
41 @Override public void create () {
\r
42 Gdx.input.setInputProcessor(new InputAdapter() {
\r
43 public boolean touchDown (int x, int y, int pointer) {
\r
44 renderMode = (renderMode + 1) % 2;
\r
49 spriteBatch = new SpriteBatch();
\r
51 logoSprite = new Sprite(Gdx.graphics.newTexture(Gdx.files.getFileHandle("data/badlogic.jpg", FileType.Internal),
\r
52 TextureFilter.Linear, TextureFilter.Linear, TextureWrap.ClampToEdge, TextureWrap.ClampToEdge));
\r
53 logoSprite.setColor(1, 1, 1, 0.5f);
\r
55 font = new BitmapFont(Gdx.files.getFileHandle("data/verdana39.fnt", FileType.Internal), Gdx.files.getFileHandle(
\r
56 "data/verdana39.png", FileType.Internal), false);
\r
58 inputProcessor = new InputAdapter() {
\r
59 public boolean touchDown (int x, int y, int pointer) {
\r
60 renderMode = (renderMode + 1) % 2;
\r
65 Gdx.input.setInputProcessor(inputProcessor);
\r
67 cache1 = new BitmapFontCache(font);
\r
68 cache2 = new BitmapFontCache(font);
\r
69 cache3 = new BitmapFontCache(font);
\r
70 cache4 = new BitmapFontCache(font);
\r
71 cache5 = new BitmapFontCache(font);
\r
73 cache1.setText("(cached)", 10, 66);
\r
75 String text = "Sphinx of black quartz,\njudge my vow.";
\r
76 cache2.setColor(Color.RED);
\r
77 cache2.setMultiLineText(text, 5, 300);
\r
79 text = "How quickly\ndaft jumping zebras vex.";
\r
80 cache3.setColor(Color.BLUE);
\r
81 cache3.setMultiLineText(text, 5, 200, 470, BitmapFont.HAlignment.CENTER);
\r
83 text = "Kerning: LYA moo";
\r
84 cache4.setText(text, 210, 66, 0, text.length() - 3);
\r
86 text = "Forsaking monastic tradition, twelve jovial friars gave\nup their vocation for a questionable existence on the flying trapeze.";
\r
87 cache5.setColor(red);
\r
88 cache5.setWrappedText(text, 0, 300, 480, HAlignment.CENTER);
\r
91 @Override public void render () {
\r
92 red.a = (red.a + Gdx.graphics.getDeltaTime() * 0.1f) % 1;
\r
94 GL10 gl = Gdx.graphics.getGL10();
\r
95 gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
\r
96 spriteBatch.begin();
\r
97 logoSprite.draw(spriteBatch);
\r
98 switch (renderMode) {
\r
109 private void renderNormal () {
\r
110 String text = "Forsaking monastic tradition, twelve jovial friars gave\nup their vocation for a questionable existence on the flying trapeze.";
\r
111 font.setColor(red);
\r
112 font.drawWrapped(spriteBatch, text, 0, 300, 480, HAlignment.CENTER);
\r
114 font.setColor(Color.WHITE);
\r
115 font.draw(spriteBatch, "(normal)", 10, 66);
\r
117 if (red.a > 0.6f) return;
\r
119 text = "Sphinx of black quartz,\njudge my vow.";
\r
120 font.setColor(Color.RED);
\r
121 font.drawMultiLine(spriteBatch, text, 5, 300);
\r
123 text = "How quickly\ndaft jumping zebras vex.";
\r
124 font.setColor(Color.BLUE);
\r
125 font.drawMultiLine(spriteBatch, text, 5, 200, 470, BitmapFont.HAlignment.RIGHT);
\r
127 text = "Kerning: LYA moo";
\r
128 font.setColor(Color.WHITE);
\r
129 font.draw(spriteBatch, text, 210, 66, 0, text.length() - 3);
\r
132 private void renderCached () {
\r
133 cache5.setColor(red);
\r
134 cache5.draw(spriteBatch);
\r
136 cache1.draw(spriteBatch);
\r
138 if (red.a > 0.6f) return;
\r
140 cache2.draw(spriteBatch);
\r
141 cache3.draw(spriteBatch);
\r
142 cache4.draw(spriteBatch);
\r
145 public boolean needsGL20 () {
\r