I got bored yesterday and had an idea. So, i decided to write myself a small text renderer. It's far from finished or perfect, but it works.
You need to initialize it with a font file that is alpha based. I got the idea from the coke and code one after i tried using that and it didn't work. But it should be pretty straight forward. I would also appreciate feedback!
You can download the font's i used, be aware one is from the coke and code tutorial but only the top font and the other is and 8-bit i made:
http://lonecoder.heliohost.org/download/1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| import static org.lwjgl.opengl.GL11.*;
import java.io.IOException; import org.newdawn.slick.opengl.Texture; import org.newdawn.slick.opengl.TextureLoader; import org.newdawn.slick.util.ResourceLoader;
import net.lonecoder.reachfortheskies.*;
public class TextRenderer { public static final int CENTER = 0; public static final int LEFT = 1; public static final int RIGHT = 2; private static int charWidth; private static int charHeight; private static float charWidthInTex; private static float charHeightInTex; private static int charsAcross; private static int charsDown; private static int charSpacing; private static Texture texture; public TextRenderer(String texFileName, int charWidth, int charHeight) { TextRenderer.charWidth = charWidth; TextRenderer.charHeight = charHeight; try { texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(texFileName)); } catch (IOException e) { e.printStackTrace(); } charsAcross = texture.getImageWidth() / charWidth; charsDown = texture.getImageHeight() / charHeight; System.out.println(texture.getImageWidth() + " " + texture.getWidth()); charWidthInTex = texture.getWidth() / charsAcross; charHeightInTex = texture.getHeight() / charsDown; charSpacing = charWidth - 5; } public static void drawString(String text ,double x, double y, double size, float red, float green, float blue, double orientation) { double charSize; double charSpace; if (size == 0) { charSize = charWidth; charSpace = charSpacing; } else { charSize = charSpacing * size; charSpace = charSize - 4; } if (orientation == CENTER) { x = (Game.WIDTH - (text.length() * charSpace))/2; } if (orientation == LEFT) { x = 0; } if (orientation == RIGHT) { x = (Game.WIDTH - (text.length() * charSpace + 10)); } glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); glEnable(GL_TEXTURE_2D); glPushMatrix(); texture.bind(); glBegin(GL_QUADS); for (int i = 0; i < text.length(); i++) { int c = (text.charAt(i)) - 32; float u = (c % charsAcross) * charWidthInTex; float v = (c / charsAcross) * charHeightInTex;
glColor3f(red, green, blue); glTexCoord2f(u, v); glVertex2d(x + (i * charSpace), y); glTexCoord2f(u + charWidthInTex, v); glVertex2d(x + (i * charSpace) + charSize, y); glTexCoord2f(u + charWidthInTex, v + charHeightInTex); glVertex2d(x + (i * charSpace) + charSize, y + charSize); glTexCoord2f(u, v + charHeightInTex); glVertex2d(x + (i * charSpace), y + charSize); } glEnd(); glPopMatrix(); glDisable(GL_TEXTURE_2D); glDisable(GL_BLEND); } } |
This is just how i use it:
1 2
| TextRenderer textRenderer; textRenderer = new TextRenderer("res/8-bit_font1.png", 32, 32); |
As you can access it statically, i make calls like this without creating any objects:
1
| TextRenderer.drawString("HELLO", 0, 0, 1, 1f, 1f, 1f, TextRenderer.LEFT); |