FontRenderer.java 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package me.hammerle.snuviengine.api;
  2. import java.nio.ByteBuffer;
  3. import me.hammerle.snuviengine.util.Rectangle;
  4. import me.hammerle.snuviengine.util.Color;
  5. import org.lwjgl.BufferUtils;
  6. import static org.lwjgl.opengl.GL11.*;
  7. import static org.lwjgl.opengl.GL15.*;
  8. import static org.lwjgl.opengl.GL20.*;
  9. import static org.lwjgl.opengl.GL30.*;
  10. public class FontRenderer
  11. {
  12. public static final char COLOR_CHAR = '&';
  13. private static final float[] COLORS = new float[128];
  14. private static final float[] DARK_COLORS = new float[128];
  15. private static final int FONT_SIZE = 8;
  16. private static final int LINE_STEP = 1;
  17. private static final float SHADOW_STEP = 1.0f;
  18. private final static int MAX_LENGTH = 256;
  19. private final static int BUFFER_BYTE_LENGTH = 1024 * 1024;
  20. private final static int OBJECT_LENGTH = 120 * MAX_LENGTH * 2;
  21. static
  22. {
  23. COLORS['0'] = Float.intBitsToFloat(Color.get(0, 0, 0));
  24. COLORS['1'] = Float.intBitsToFloat(Color.get(0, 0, 170));
  25. COLORS['2'] = Float.intBitsToFloat(Color.get(0, 170, 0));
  26. COLORS['3'] = Float.intBitsToFloat(Color.get(0, 170, 170));
  27. COLORS['4'] = Float.intBitsToFloat(Color.get(170, 0, 0));
  28. COLORS['5'] = Float.intBitsToFloat(Color.get(170, 0, 170));
  29. COLORS['6'] = Float.intBitsToFloat(Color.get(255, 170, 0));
  30. COLORS['7'] = Float.intBitsToFloat(Color.get(170, 170, 170));
  31. COLORS['8'] = Float.intBitsToFloat(Color.get(85, 85, 85));
  32. COLORS['9'] = Float.intBitsToFloat(Color.get(85, 85, 255));
  33. COLORS['a'] = Float.intBitsToFloat(Color.get(85, 255, 85));
  34. COLORS['b'] = Float.intBitsToFloat(Color.get(85, 255, 255));
  35. COLORS['c'] = Float.intBitsToFloat(Color.get(255, 85, 85));
  36. COLORS['d'] = Float.intBitsToFloat(Color.get(255, 85, 255));
  37. COLORS['e'] = Float.intBitsToFloat(Color.get(255, 255, 85));
  38. COLORS['f'] = Float.intBitsToFloat(Color.get(255, 255, 255));
  39. float factor = 0.5f;
  40. DARK_COLORS['0'] = Float.intBitsToFloat(Color.darken(Color.get(0, 0, 0), factor));
  41. DARK_COLORS['1'] = Float.intBitsToFloat(Color.darken(Color.get(0, 0, 170), factor));
  42. DARK_COLORS['2'] = Float.intBitsToFloat(Color.darken(Color.get(0, 170, 0), factor));
  43. DARK_COLORS['3'] = Float.intBitsToFloat(Color.darken(Color.get(0, 170, 170), factor));
  44. DARK_COLORS['4'] = Float.intBitsToFloat(Color.darken(Color.get(170, 0, 0), factor));
  45. DARK_COLORS['5'] = Float.intBitsToFloat(Color.darken(Color.get(170, 0, 170), factor));
  46. DARK_COLORS['6'] = Float.intBitsToFloat(Color.darken(Color.get(255, 170, 0), factor));
  47. DARK_COLORS['7'] = Float.intBitsToFloat(Color.darken(Color.get(170, 170, 170), factor));
  48. DARK_COLORS['8'] = Float.intBitsToFloat(Color.darken(Color.get(85, 85, 85), factor));
  49. DARK_COLORS['9'] = Float.intBitsToFloat(Color.darken(Color.get(85, 85, 255), factor));
  50. DARK_COLORS['a'] = Float.intBitsToFloat(Color.darken(Color.get(85, 255, 85), factor));
  51. DARK_COLORS['b'] = Float.intBitsToFloat(Color.darken(Color.get(85, 255, 255), factor));
  52. DARK_COLORS['c'] = Float.intBitsToFloat(Color.darken(Color.get(255, 85, 85), factor));
  53. DARK_COLORS['d'] = Float.intBitsToFloat(Color.darken(Color.get(255, 85, 255), factor));
  54. DARK_COLORS['e'] = Float.intBitsToFloat(Color.darken(Color.get(255, 255, 85), factor));
  55. DARK_COLORS['f'] = Float.intBitsToFloat(Color.darken(Color.get(255, 255, 255), factor));
  56. }
  57. private final Texture[] FONT_TEXTURE = new Texture[]
  58. {
  59. new Texture("font8x8.png", true),
  60. new Texture("font16x16.png", true),
  61. new Texture("font24x24.png", true)
  62. };
  63. private final int vao;
  64. private final int vbo;
  65. private float color = 0.0f;
  66. private int scale = 1;
  67. private ByteBuffer buffer = BufferUtils.createByteBuffer(OBJECT_LENGTH);
  68. private int offset = BUFFER_BYTE_LENGTH - OBJECT_LENGTH;
  69. protected FontRenderer()
  70. {
  71. vao = glGenVertexArrays();
  72. vbo = glGenBuffers();
  73. GLHelper.glBindVertexArray(vao);
  74. GLHelper.glBindBuffer(vbo);
  75. glEnableVertexAttribArray(0);
  76. glEnableVertexAttribArray(1);
  77. glEnableVertexAttribArray(2);
  78. glVertexAttribPointer(0, 2, GL_FLOAT, false, 20, 0);
  79. glVertexAttribPointer(1, 2, GL_FLOAT, false, 20, 8);
  80. glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, true, 20, 16);
  81. }
  82. protected void setScale(int scale)
  83. {
  84. this.scale = scale;
  85. }
  86. private void addRectangle(float minX, float minY, char c)
  87. {
  88. minY = Math.round(minY * scale) / scale;
  89. minX = Math.round(minX * scale) / scale;
  90. float tMinX = (c & 0xF) / 16.0f;
  91. float tMinY = (c >> 4) / 16.0f;
  92. float tMaxX = tMinX + 0.0625f;
  93. float tMaxY = tMinY + 0.0625f;
  94. float maxX = minX + FONT_SIZE;
  95. float maxY = minY + FONT_SIZE;
  96. buffer.putFloat(minX);
  97. buffer.putFloat(maxY);
  98. buffer.putFloat(tMinX);
  99. buffer.putFloat(tMaxY);
  100. buffer.putFloat(color);
  101. buffer.putFloat(minX);
  102. buffer.putFloat(minY);
  103. buffer.putFloat(tMinX);
  104. buffer.putFloat(tMinY);
  105. buffer.putFloat(color);
  106. buffer.putFloat(maxX);
  107. buffer.putFloat(maxY);
  108. buffer.putFloat(tMaxX);
  109. buffer.putFloat(tMaxY);
  110. buffer.putFloat(color);
  111. buffer.putFloat(maxX);
  112. buffer.putFloat(maxY);
  113. buffer.putFloat(tMaxX);
  114. buffer.putFloat(tMaxY);
  115. buffer.putFloat(color);
  116. buffer.putFloat(minX);
  117. buffer.putFloat(minY);
  118. buffer.putFloat(tMinX);
  119. buffer.putFloat(tMinY);
  120. buffer.putFloat(color);
  121. buffer.putFloat(maxX);
  122. buffer.putFloat(minY);
  123. buffer.putFloat(tMaxX);
  124. buffer.putFloat(tMinY);
  125. buffer.putFloat(color);
  126. }
  127. public float drawString(float x, float y, boolean shadow, String s)
  128. {
  129. GLHelper.glBindBuffer(vbo);
  130. offset += OBJECT_LENGTH;
  131. if(offset + OBJECT_LENGTH >= BUFFER_BYTE_LENGTH)
  132. {
  133. offset = 0;
  134. glBufferData(GL_ARRAY_BUFFER, BUFFER_BYTE_LENGTH, GL_STREAM_DRAW);
  135. }
  136. buffer = glMapBufferRange(GL_ARRAY_BUFFER, offset, OBJECT_LENGTH, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT, buffer);
  137. if(buffer == null)
  138. {
  139. return y;
  140. }
  141. if(shadow)
  142. {
  143. addString(x + SHADOW_STEP, y + SHADOW_STEP, DARK_COLORS, s);
  144. }
  145. y = addString(x, y, COLORS, s);
  146. buffer.flip();
  147. glUnmapBuffer(GL_ARRAY_BUFFER);
  148. GLHelper.glBindVertexArray(vao);
  149. FONT_TEXTURE[Math.min(scale - 1, FONT_TEXTURE.length - 1)].bind();
  150. glDrawArrays(GL_TRIANGLES, offset / 20, buffer.limit() / 20);
  151. buffer.limit(buffer.capacity());
  152. return y;
  153. }
  154. public float drawString(float x, float y, String s)
  155. {
  156. return drawString(x, y, true, s);
  157. }
  158. private float addString(float x, float y, float[] colors, String s)
  159. {
  160. int l = Math.min(s.length(), MAX_LENGTH);
  161. float oldX = x;
  162. color = colors['f'];
  163. for(int pos = 0; pos < l; pos++)
  164. {
  165. char c = s.charAt(pos);
  166. if(c == COLOR_CHAR)
  167. {
  168. pos++;
  169. if(pos < l)
  170. {
  171. int index = s.charAt(pos);
  172. if(index >= 0 && index <= colors.length)
  173. {
  174. color = colors[s.charAt(pos)];
  175. }
  176. }
  177. continue;
  178. }
  179. else if(c == '\n')
  180. {
  181. y += FONT_SIZE + LINE_STEP;
  182. x = oldX;
  183. continue;
  184. }
  185. addRectangle(x, y, c);
  186. x += FONT_SIZE;
  187. }
  188. return y + FONT_SIZE + LINE_STEP;
  189. }
  190. public Rectangle getSize(String s)
  191. {
  192. int longestLine = 0;
  193. int length = 0;
  194. int counter = 1;
  195. for(int i = 0; i < s.length(); i++)
  196. {
  197. switch(s.charAt(i))
  198. {
  199. case '\n':
  200. counter++;
  201. if(length > longestLine)
  202. {
  203. longestLine = length;
  204. }
  205. length = 0;
  206. break;
  207. case COLOR_CHAR:
  208. i++;
  209. break;
  210. default:
  211. length++;
  212. }
  213. }
  214. if(length > longestLine)
  215. {
  216. longestLine = length;
  217. }
  218. return new Rectangle(FONT_SIZE * longestLine, (FONT_SIZE + LINE_STEP) * counter);
  219. }
  220. public Rectangle getSize(int w, int h)
  221. {
  222. return new Rectangle(FONT_SIZE * w, (FONT_SIZE + LINE_STEP) * h);
  223. }
  224. public float getHeight()
  225. {
  226. return FONT_SIZE + LINE_STEP;
  227. }
  228. public float getWidth()
  229. {
  230. return FONT_SIZE;
  231. }
  232. }