From bf0609c492c3a0c38123efbbae51539c21230c88 Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Fri, 18 Sep 2015 14:19:16 +0300 Subject: [PATCH] create SimpleText --- .../lwjgl/tools/fontengine/SimpleText.java | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/java/ru/dmitriymx/lwjgl/tools/fontengine/SimpleText.java diff --git a/src/java/ru/dmitriymx/lwjgl/tools/fontengine/SimpleText.java b/src/java/ru/dmitriymx/lwjgl/tools/fontengine/SimpleText.java new file mode 100644 index 0000000..441ec61 --- /dev/null +++ b/src/java/ru/dmitriymx/lwjgl/tools/fontengine/SimpleText.java @@ -0,0 +1,66 @@ +package ru.dmitriymx.lwjgl.tools.fontengine; + +import org.lwjgl.util.Renderable; + +import ru.dmitriymx.lwjgl.tools.Tessellator; + +import static org.lwjgl.opengl.GL11.*; + +public class SimpleText implements Renderable { + private Tessellator tess = Tessellator.getInstance(); + private FontEngine engine; + private char[] chars; + private float[] color4f = new float[]{1f, 1f, 1f, 1f}; + + public SimpleText(String string, FontEngine engine) { + this.engine = engine; + } + + public void setString(String string) { + this.chars = string.toCharArray(); + engine.checkChars(chars); + } + + public String getString() { + return String.valueOf(chars); + } + + public void setColor(float red, float green, float blue, float alpha) { + this.color4f[0] = red; + this.color4f[1] = green; + this.color4f[2] = blue; + this.color4f[3] = alpha; + } + + public float[] getColor() { + return color4f; + } + + @Override + public String toString() { + return getClass().getCanonicalName() + "[" + getString() + "]"; + } + + @Override + public void render() { + engine.bindTexture(); + float current_x = 0f; + + tess.startDrawingUseVA(GL_QUADS); + + for (char ch : chars) { + FontEngine.CharData charData = engine.getCharData(ch); + current_x -= charData.visual_bounds_x; + + tess.setColor(color4f[0], color4f[1], color4f[2], color4f[3]).setTexture(charData.texture_offset_x1, charData.texture_offset_y1).addVertex(current_x, charData.visual_bounds_y, 0); + tess.setColor(color4f[0], color4f[1], color4f[2], color4f[3]).setTexture(charData.texture_offset_x2, charData.texture_offset_y1).addVertex(current_x + charData.visual_bounds_width, charData.visual_bounds_y, 0); + tess.setColor(color4f[0], color4f[1], color4f[2], color4f[3]).setTexture(charData.texture_offset_x2, charData.texture_offset_y2).addVertex(current_x + charData.visual_bounds_width, charData.visual_bounds_y - charData.visual_bounds_height, 0); + tess.setColor(color4f[0], color4f[1], color4f[2], color4f[3]).setTexture(charData.texture_offset_x1, charData.texture_offset_y2).addVertex(current_x, charData.visual_bounds_y - charData.visual_bounds_height, 0); + + current_x += charData.width_char; + } + + tess.drawVA(); + } + +}