Advertisement
Guest User

Untitled

a guest
Mar 16th, 2017
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.43 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.  
  34.             GL11.glBindTexture(GL11.GL_TEXTURE_2D, texID);
  35.             GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_ALPHA, BITMAP_W, BITMAP_H, 0, GL11.GL_ALPHA, GL11.GL_UNSIGNED_BYTE, bitmap);
  36.             GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
  37.             GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
  38.  
  39.             org.lwjgl.stb.STBImageWrite.stbi_write_png("Resources/test.png", BITMAP_W, BITMAP_H, 1, bitmap, 4);
  40.  
  41.         } catch (Exception e) {
  42.             e.printStackTrace();
  43.         }
  44.     }
  45.  
  46.     public int getFontHeight() {
  47.         return size;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement