Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. package fr.themode.engine.graphic;
  2.  
  3. import static org.lwjgl.opengl.GL11.*;
  4. import static org.lwjgl.stb.STBTruetype.*;
  5. import static org.lwjgl.system.MemoryStack.stackPush;
  6.  
  7. import java.io.IOException;
  8. import java.nio.ByteBuffer;
  9. import java.nio.FloatBuffer;
  10.  
  11. import org.lwjgl.BufferUtils;
  12. import org.lwjgl.stb.STBTTAlignedQuad;
  13. import org.lwjgl.stb.STBTTBakedChar;
  14. import org.lwjgl.system.MemoryStack;
  15.  
  16. import fr.themode.engine.util.IOUtil;
  17.  
  18. public class Font {
  19.  
  20. private String path;
  21. private int size;
  22. private int texID;
  23.  
  24. private STBTTBakedChar.Buffer cdata;
  25.  
  26. public Font(String path, int size) {
  27. this.path = path;
  28. this.size = size;
  29. }
  30.  
  31. public void draw(String text, float x, float y, FontStyle style, Color color) {
  32. if (!isLoaded())
  33. cdata = load();
  34.  
  35. switch (style) {
  36. case LEFT:
  37. x -= getWidth(text);
  38. break;
  39. case CENTERED:
  40. x -= getWidth(text) / 2;
  41. break;
  42. case RIGHT:
  43. break;
  44. }
  45.  
  46. try (MemoryStack stack = stackPush()) {
  47. FloatBuffer xx = stack.floats(0.0f);
  48. FloatBuffer yy = stack.floats(0.0f);
  49. STBTTAlignedQuad q = STBTTAlignedQuad.mallocStack(stack);
  50. glPushMatrix();
  51. glBindTexture(GL_TEXTURE_2D, texID);
  52.  
  53. glTranslatef(x, y, 0f);
  54. glColor4f((float) color.r / 255, (float) color.g / 255, (float) color.b / 255, color.a);
  55. glBegin(GL_QUADS);
  56. for (int i = 0; i < text.length(); i++) {
  57. char c = text.charAt(i);
  58. if (c == '\n') {
  59. yy.put(0, yy.get(0) + size);
  60. xx.put(0, 0.0f);
  61. continue;
  62. } else if (c < 32 || 128 <= c)
  63. continue;
  64. stbtt_GetBakedQuad(cdata, 512, 512, c - 32, xx, yy, q, true);
  65. glTexCoord2f(q.s0(), q.t0());
  66. glVertex2f(q.x0(), q.y0());
  67.  
  68. glTexCoord2f(q.s1(), q.t0());
  69. glVertex2f(q.x1(), q.y0());
  70.  
  71. glTexCoord2f(q.s1(), q.t1());
  72. glVertex2f(q.x1(), q.y1());
  73.  
  74. glTexCoord2f(q.s0(), q.t1());
  75. glVertex2f(q.x0(), q.y1());
  76. }
  77. glEnd();
  78.  
  79. glColor4f(1, 1, 1, 1);
  80.  
  81. glPopMatrix();
  82. }
  83. }
  84.  
  85. private STBTTBakedChar.Buffer load() {
  86. texID = glGenTextures();
  87. STBTTBakedChar.Buffer cdata = STBTTBakedChar.malloc(96);
  88.  
  89. try {
  90. ByteBuffer ttf = IOUtil.ioResourceToByteBuffer(path, 160 * 1024);
  91.  
  92. ByteBuffer bitmap = BufferUtils.createByteBuffer(512 * 512);
  93. stbtt_BakeFontBitmap(ttf, size, bitmap, 512, 512, 32, cdata);
  94.  
  95. glBindTexture(GL_TEXTURE_2D, texID);
  96. glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, 512, 512, 0, GL_ALPHA, GL_UNSIGNED_BYTE, bitmap);
  97. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  98. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  99. } catch (IOException e) {
  100. throw new RuntimeException(e);
  101. }
  102.  
  103. return cdata;
  104. }
  105.  
  106. public float getWidth(String text) {
  107. if (text == null || text.equals(""))
  108. return 0;
  109. float length;
  110.  
  111. try (MemoryStack stack = stackPush()) {
  112. FloatBuffer xx = stack.floats(0.0f);
  113. FloatBuffer yy = stack.floats(0.0f);
  114. STBTTAlignedQuad q = STBTTAlignedQuad.mallocStack(stack);
  115. for (int i = 0; i < text.length(); i++) {
  116. char c = text.charAt(i);
  117. if (c == '\n') {
  118. yy.put(0, yy.get(0) + size);
  119. xx.put(0, 0.0f);
  120. continue;
  121. } else if (c < 32 || 128 <= c)
  122. continue;
  123. stbtt_GetBakedQuad(cdata, 512, 512, c - 32, xx, yy, q, true);
  124. }
  125. length = q.x1();
  126. }
  127.  
  128. return length;
  129. }
  130.  
  131. private boolean isLoaded() {
  132. return texID > 0;
  133. }
  134.  
  135. public enum FontStyle {
  136. RIGHT, LEFT, CENTERED;
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement