OSDN Git Service

33371993c0e692c519a4e04d2f7d0828409bac0a
[mikumikustudio/libgdx-mikumikustudio.git] / tests / gdx-tests / src / com / badlogic / gdx / tests / BitmapFontTest.java
1 /*\r
2  * Copyright 2010 Mario Zechner (contact@badlogicgames.com), Nathan Sweet (admin@esotericsoftware.com)\r
3  * \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
6  * \r
7  * http://www.apache.org/licenses/LICENSE-2.0\r
8  * \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
12  */\r
13 \r
14 package com.badlogic.gdx.tests;\r
15 \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
31 \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
38         int renderMode;\r
39         InputProcessor inputProcessor;\r
40 \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
45                                 return false;\r
46                         }\r
47                 });\r
48 \r
49                 spriteBatch = new SpriteBatch();\r
50 \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
54 \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
57 \r
58                 inputProcessor = new InputAdapter() {\r
59                         public boolean touchDown (int x, int y, int pointer) {\r
60                                 renderMode = (renderMode + 1) % 2;\r
61                                 return false;\r
62                         }\r
63                 };\r
64 \r
65                 Gdx.input.setInputProcessor(inputProcessor);\r
66 \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
72 \r
73                 cache1.setText("(cached)", 10, 66);\r
74 \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
78 \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
82 \r
83                 text = "Kerning: LYA moo";\r
84                 cache4.setText(text, 210, 66, 0, text.length() - 3);\r
85 \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
89         }\r
90 \r
91         @Override public void render () {\r
92                 red.a = (red.a + Gdx.graphics.getDeltaTime() * 0.1f) % 1;\r
93 \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
99                 case 0:\r
100                         renderNormal();\r
101                         break;\r
102                 case 1:\r
103                         renderCached();\r
104                         break;\r
105                 }\r
106                 spriteBatch.end();\r
107         }\r
108 \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
113 \r
114                 font.setColor(Color.WHITE);\r
115                 font.draw(spriteBatch, "(normal)", 10, 66);\r
116 \r
117                 if (red.a > 0.6f) return;\r
118 \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
122 \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
126 \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
130         }\r
131 \r
132         private void renderCached () {\r
133                 cache5.setColor(red);\r
134                 cache5.draw(spriteBatch);\r
135 \r
136                 cache1.draw(spriteBatch);\r
137 \r
138                 if (red.a > 0.6f) return;\r
139 \r
140                 cache2.draw(spriteBatch);\r
141                 cache3.draw(spriteBatch);\r
142                 cache4.draw(spriteBatch);\r
143         }\r
144 \r
145         public boolean needsGL20 () {\r
146                 return false;\r
147         }\r
148 }\r