From 357bb12eef425ae91a1055f60f788a450f965fb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Zurcher?= Date: Wed, 27 Jun 2018 10:13:39 +0200 Subject: Assets supports TrueType fonts --- README.md | 2 +- assets/data/veteran-typewriter.ttf | Bin 0 -> 52548 bytes build.gradle | 8 ++++++++ core/src/ch/asynk/zproject/Assets.java | 36 +++++++++++++++++++++++++++++++++ core/src/ch/asynk/zproject/Hud.java | 14 ++++++++++++- 5 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 assets/data/veteran-typewriter.ttf diff --git a/README.md b/README.md index 65e305b..d3267fa 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,5 @@ # TODO - - Fonts - UI widgets - SquareMap - HexMap @@ -13,3 +12,4 @@ - BoardCamera - Assets - LoadingScreen + - Fonts diff --git a/assets/data/veteran-typewriter.ttf b/assets/data/veteran-typewriter.ttf new file mode 100644 index 0000000..202e264 Binary files /dev/null and b/assets/data/veteran-typewriter.ttf differ diff --git a/build.gradle b/build.gradle index e95a7a7..4e567c0 100644 --- a/build.gradle +++ b/build.gradle @@ -42,6 +42,7 @@ project(":desktop") { implementation project(":core") implementation "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion" implementation "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop" + implementation "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop" } } @@ -58,6 +59,12 @@ project(":android") { natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86" natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64" + implementation "com.badlogicgames.gdx:gdx-freetype:$gdxVersion" + natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi" + natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a" + natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a" + natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86" + natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64" } } @@ -66,6 +73,7 @@ project(":core") { dependencies { implementation "com.badlogicgames.gdx:gdx:$gdxVersion" + implementation "com.badlogicgames.gdx:gdx-freetype:$gdxVersion" } } diff --git a/core/src/ch/asynk/zproject/Assets.java b/core/src/ch/asynk/zproject/Assets.java index 8584810..1e66e23 100644 --- a/core/src/ch/asynk/zproject/Assets.java +++ b/core/src/ch/asynk/zproject/Assets.java @@ -1,15 +1,42 @@ package ch.asynk.zproject; import com.badlogic.gdx.assets.AssetManager; +import com.badlogic.gdx.assets.loaders.FileHandleResolver; +import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver; import com.badlogic.gdx.utils.Disposable; import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.TextureAtlas; +import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator; +import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGeneratorLoader; +import com.badlogic.gdx.graphics.g2d.freetype.FreetypeFontLoader; +import com.badlogic.gdx.graphics.g2d.freetype.FreetypeFontLoader.FreeTypeFontLoaderParameter; public class Assets extends AssetManager implements Disposable { public static final String CORNER = "data/corner.png"; public static final String MAP_00 = "data/map_00.png"; public static final String LOADING = "data/loading.atlas"; + public static final String FONT = "data/veteran-typewriter.ttf"; + public static final String FONT_20 = "size20.ttf"; + public static final String FONT_25 = "size25.ttf"; + + private final FreeTypeFontLoaderParameter params20; + private final FreeTypeFontLoaderParameter params25; + + public Assets() + { + super(); + final FileHandleResolver resolver = new InternalFileHandleResolver(); + setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver)); + setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(resolver)); + params20 = new FreeTypeFontLoaderParameter(); + params20.fontFileName = FONT; + params20.fontParameters.size = 20; + params25 = new FreeTypeFontLoaderParameter(); + params25.fontFileName = FONT; + params25.fontParameters.size = 25; + } @Override public void dispose() { @@ -28,6 +55,11 @@ public class Assets extends AssetManager implements Disposable return get(assetName, TextureAtlas.class); } + public BitmapFont getFont(String assetName) + { + return get(assetName, BitmapFont.class); + } + public void loadLoading() { load(LOADING, TextureAtlas.class); @@ -42,11 +74,15 @@ public class Assets extends AssetManager implements Disposable { load(MAP_00, Texture.class); load(CORNER, Texture.class); + load(FONT_20, BitmapFont.class, params20); + load(FONT_25, BitmapFont.class, params25); } public void unloadGame() { unload(MAP_00); unload(CORNER); + unload(FONT_20); + unload(FONT_25); } } diff --git a/core/src/ch/asynk/zproject/Hud.java b/core/src/ch/asynk/zproject/Hud.java index 38728c0..4614cd4 100644 --- a/core/src/ch/asynk/zproject/Hud.java +++ b/core/src/ch/asynk/zproject/Hud.java @@ -1,7 +1,9 @@ package ch.asynk.zproject; -import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.graphics.g2d.BitmapFont; +import com.badlogic.gdx.graphics.g2d.GlyphLayout; import com.badlogic.gdx.graphics.g2d.Sprite; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.math.Rectangle; import com.badlogic.gdx.utils.Disposable; @@ -9,10 +11,16 @@ import com.badlogic.gdx.utils.Disposable; public class Hud implements Disposable { private final Sprite hud; + private final GlyphLayout glyphLayout; + private final BitmapFont font20; + private final BitmapFont font25; public Hud(final Assets assets) { this.hud = new Sprite(assets.getTexture(assets.CORNER)); + this.glyphLayout = new GlyphLayout(); + font20 = assets.getFont(assets.FONT_20); + font25 = assets.getFont(assets.FONT_25); } @Override public void dispose() @@ -36,5 +44,9 @@ public class Hud implements Disposable hud.setPosition(right, top); hud.setRotation(270); hud.draw(batch); + glyphLayout.setText(font20, "Hello"); + font20.draw(batch, glyphLayout, 60, 30); + glyphLayout.setText(font25, "worlD"); + font25.draw(batch, glyphLayout, 120, 32); } } -- cgit v1.1-2-g2b99