Created
May 10, 2014 15:02
-
-
Save badlogic/31d6c1fd76a51a8566c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.badlogic.gdx.tests; | |
import com.badlogic.gdx.Files.FileType; | |
import com.badlogic.gdx.Gdx; | |
import com.badlogic.gdx.files.FileHandle; | |
import com.badlogic.gdx.graphics.Color; | |
import com.badlogic.gdx.graphics.GL20; | |
import com.badlogic.gdx.graphics.Pixmap.Format; | |
import com.badlogic.gdx.graphics.Texture; | |
import com.badlogic.gdx.graphics.Texture.TextureFilter; | |
import com.badlogic.gdx.graphics.g2d.BitmapFont; | |
import com.badlogic.gdx.graphics.g2d.BitmapFont.Glyph; | |
import com.badlogic.gdx.graphics.g2d.PixmapPacker; | |
import com.badlogic.gdx.graphics.g2d.SpriteBatch; | |
import com.badlogic.gdx.graphics.g2d.TextureAtlas; | |
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion; | |
import com.badlogic.gdx.graphics.g2d.TextureRegion; | |
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; | |
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeBitmapFontData; | |
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter; | |
import com.badlogic.gdx.scenes.scene2d.Stage; | |
import com.badlogic.gdx.scenes.scene2d.ui.Label; | |
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle; | |
import com.badlogic.gdx.tests.utils.GdxTest; | |
import com.badlogic.gdx.utils.Array; | |
public class FontPackingTest extends GdxTest { | |
private final static int SIZE_1 = 30; | |
private final static int SIZE_2 = 20; | |
private final static int COUNT = 2; | |
private final static String CHARS = "ABC"; | |
private PixmapPacker mPacker; | |
private TextureAtlas mAtlas; | |
private BitmapFont[] mFonts = new BitmapFont[COUNT]; | |
private Stage mStage; | |
private Label[] mLabels = new Label[COUNT]; | |
private SpriteBatch mBatch; | |
@Override | |
public void create() { | |
mBatch = new SpriteBatch(); | |
mPacker = new PixmapPacker(512, 512, Format.RGBA8888, 0, false); | |
mFonts[0] = loadFont("data/MTLc3m.ttf", CHARS, SIZE_1); | |
mFonts[1] = loadFont("data/MTLc3m.ttf", CHARS, SIZE_2); | |
mStage = new Stage(); | |
mLabels[0] = new Label(CHARS, new LabelStyle(mFonts[0], Color.WHITE)); | |
mLabels[1] = new Label(CHARS, new LabelStyle(mFonts[1], Color.CYAN)); | |
for (Label label : mLabels) { | |
mStage.addActor(label); | |
} | |
} | |
private BitmapFont loadFont(String name, String characters, int size) { | |
if (mAtlas == null) { | |
mAtlas = mPacker.generateTextureAtlas(TextureFilter.Nearest, | |
TextureFilter.Nearest, false); | |
} | |
FreeTypeFontGenerator generator = null; | |
FileHandle fontHandle = Gdx.files | |
.getFileHandle(name, FileType.Internal); | |
if (!fontHandle.exists()) { | |
throw new IllegalStateException("No font available?"); | |
} | |
generator = new FreeTypeFontGenerator(fontHandle); | |
FreeTypeFontParameter params = new FreeTypeFontParameter(); | |
params.size = size; | |
params.characters = characters; | |
params.packer = mPacker; | |
FreeTypeBitmapFontData data = generator.generateData(params); | |
mPacker.updateTextureAtlas(mAtlas, TextureFilter.Nearest, TextureFilter.Nearest, false); | |
Array<TextureRegion> fontRegions = new Array<TextureRegion>(); | |
// Remove ".ttf" at the end... | |
final String fontKey = String.format("%s_%d_", | |
name.substring(0, name.length() - 4), size); | |
for (AtlasRegion region : mAtlas.getRegions()) { | |
if (region.name.startsWith(fontKey)) { | |
fontRegions.add(region); | |
} | |
} | |
generator.dispose(); | |
Texture texture = mAtlas.getTextures().first(); | |
return new BitmapFont(data, | |
new TextureRegion(texture, 0, 0, texture.getWidth(), texture.getHeight()), | |
true); | |
} | |
@Override | |
public void dispose() { | |
mAtlas.dispose(); | |
} | |
@Override | |
public void render() { | |
Gdx.gl.glClearColor(0, 0, 0, 1); | |
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); | |
// mStage.act(); | |
// mStage.draw(); | |
mBatch.begin(); | |
mFonts[0].setColor(Color.RED); | |
mFonts[0].draw(mBatch, CHARS, 0, 50); | |
mFonts[1].setColor(Color.BLUE); | |
mFonts[1].draw(mBatch, CHARS, 0, 100); | |
int x = 0; | |
for(TextureRegion region: mAtlas.getRegions()) { | |
mBatch.draw(region, x, 150); | |
x += region.getRegionWidth() + 4; | |
} | |
mBatch.end(); | |
} | |
@Override | |
public void resize(int width, int height) { | |
mStage.getViewport().update(width, height); | |
float x = 0.0f; | |
float y = 10.0f; | |
for (Label label : mLabels) { | |
label.setPosition(x, y); | |
y += 30.0f; | |
} | |
} | |
@Override | |
public void pause() { | |
} | |
@Override | |
public void resume() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment