Advertisement
Guest User

Untitled

a guest
Mar 16th, 2017
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. package test;
  2.  
  3. import java.nio.ByteBuffer;
  4.  
  5. import org.lwjgl.BufferUtils;
  6. import org.lwjgl.opengl.GL11;
  7. import org.lwjgl.stb.STBTTBakedChar;
  8.  
  9. import net.mantagames.lwjgl3engine.util.IOUtil;
  10.  
  11. public class TrueTypeFont {
  12.     private static final int BITMAP_W = 512;
  13.     private static final int BITMAP_H = 512;
  14.  
  15.     private int size;
  16.  
  17.     public TrueTypeFont(int size) {
  18.         this.size = size;
  19.  
  20.         int texID = GL11.glGenTextures();
  21.         STBTTBakedChar.Buffer cdata = STBTTBakedChar.malloc(96);
  22.  
  23.         try {
  24.             ByteBuffer ttf = IOUtil.ioResourceToByteBuffer("Resources/temp/Arial.ttf", 160 * 1024);
  25.             if ( ttf == null ) {
  26.                 System.err.println("Font not found");
  27.                 return;
  28.             }
  29.  
  30.             ByteBuffer bitmap = BufferUtils.createByteBuffer(BITMAP_W * BITMAP_H);
  31.             org.lwjgl.stb.STBTruetype.stbtt_BakeFontBitmap(ttf, getFontHeight(), bitmap, BITMAP_W, BITMAP_H, 32, cdata);
  32.  
  33.             GL11.glBindTexture(GL11.GL_TEXTURE_2D, texID);
  34.             GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, BITMAP_W, BITMAP_H, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, bitmap);
  35.             GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
  36.             GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
  37.  
  38.             org.lwjgl.stb.STBImageWrite.stbi_write_png("test.png", BITMAP_W, BITMAP_H, 4, bitmap, 4);
  39.         } catch (Exception e) {
  40.             throw new RuntimeException(e);
  41.         }
  42.     }
  43.  
  44.     public int getFontHeight() {
  45.         return size;
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement