FontRenderer.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. private final static float ERROR = 0.0f;
  13. private final static Texture[] FONT_TEXTURE = new Texture[]
  14. {
  15. new Texture("font8x8.png", true),
  16. new Texture("font16x16.png", true),
  17. new Texture("font24x24.png", true)
  18. };
  19. public static final char COLOR_CHAR = '&';
  20. private static final float[] COLORS = new float[128];
  21. private static final float[] DARK_COLORS = new float[128];
  22. private static final int FONT_SIZE = 8;
  23. private static final int LINE_STEP = 1;
  24. private static final float SHADOW_STEP = 1f;
  25. static
  26. {
  27. COLORS['0'] = Float.intBitsToFloat(Color.get(0, 0, 0));
  28. COLORS['1'] = Float.intBitsToFloat(Color.get(0, 0, 170));
  29. COLORS['2'] = Float.intBitsToFloat(Color.get(0, 170, 0));
  30. COLORS['3'] = Float.intBitsToFloat(Color.get(0, 170, 170));
  31. COLORS['4'] = Float.intBitsToFloat(Color.get(170, 0, 0));
  32. COLORS['5'] = Float.intBitsToFloat(Color.get(170, 0, 170));
  33. COLORS['6'] = Float.intBitsToFloat(Color.get(255, 170, 0));
  34. COLORS['7'] = Float.intBitsToFloat(Color.get(170, 170, 170));
  35. COLORS['8'] = Float.intBitsToFloat(Color.get(85, 85, 85));
  36. COLORS['9'] = Float.intBitsToFloat(Color.get(85, 85, 255));
  37. COLORS['a'] = Float.intBitsToFloat(Color.get(85, 255, 85));
  38. COLORS['b'] = Float.intBitsToFloat(Color.get(85, 255, 255));
  39. COLORS['c'] = Float.intBitsToFloat(Color.get(255, 85, 85));
  40. COLORS['d'] = Float.intBitsToFloat(Color.get(255, 85, 255));
  41. COLORS['e'] = Float.intBitsToFloat(Color.get(255, 255, 85));
  42. COLORS['f'] = Float.intBitsToFloat(Color.get(255, 255, 255));
  43. float factor = 0.5f;
  44. DARK_COLORS['0'] = Float.intBitsToFloat(Color.darken(Color.get(0, 0, 0), factor));
  45. DARK_COLORS['1'] = Float.intBitsToFloat(Color.darken(Color.get(0, 0, 170), factor));
  46. DARK_COLORS['2'] = Float.intBitsToFloat(Color.darken(Color.get(0, 170, 0), factor));
  47. DARK_COLORS['3'] = Float.intBitsToFloat(Color.darken(Color.get(0, 170, 170), factor));
  48. DARK_COLORS['4'] = Float.intBitsToFloat(Color.darken(Color.get(170, 0, 0), factor));
  49. DARK_COLORS['5'] = Float.intBitsToFloat(Color.darken(Color.get(170, 0, 170), factor));
  50. DARK_COLORS['6'] = Float.intBitsToFloat(Color.darken(Color.get(255, 170, 0), factor));
  51. DARK_COLORS['7'] = Float.intBitsToFloat(Color.darken(Color.get(170, 170, 170), factor));
  52. DARK_COLORS['8'] = Float.intBitsToFloat(Color.darken(Color.get(85, 85, 85), factor));
  53. DARK_COLORS['9'] = Float.intBitsToFloat(Color.darken(Color.get(85, 85, 255), factor));
  54. DARK_COLORS['a'] = Float.intBitsToFloat(Color.darken(Color.get(85, 255, 85), factor));
  55. DARK_COLORS['b'] = Float.intBitsToFloat(Color.darken(Color.get(85, 255, 255), factor));
  56. DARK_COLORS['c'] = Float.intBitsToFloat(Color.darken(Color.get(255, 85, 85), factor));
  57. DARK_COLORS['d'] = Float.intBitsToFloat(Color.darken(Color.get(255, 85, 255), factor));
  58. DARK_COLORS['e'] = Float.intBitsToFloat(Color.darken(Color.get(255, 255, 85), factor));
  59. DARK_COLORS['f'] = Float.intBitsToFloat(Color.darken(Color.get(255, 255, 255), factor));
  60. }
  61. private int vao;
  62. private int vbo;
  63. private float color;
  64. private ByteBuffer buffer = BufferUtils.createByteBuffer(OBJECT_LENGTH);
  65. private final static int MAX_LENGTH = 256;
  66. private final static int BUFFER_BYTE_LENGTH = 1024 * 1024;
  67. private final static int OBJECT_LENGTH = 120 * MAX_LENGTH * 2;
  68. private int offset = BUFFER_BYTE_LENGTH - OBJECT_LENGTH;
  69. protected FontRenderer()
  70. {
  71. Shader.addTask(() ->
  72. {
  73. vao = glGenVertexArrays();
  74. vbo = glGenBuffers();
  75. GLHelper.glBindVertexArray(vao);
  76. GLHelper.glBindBuffer(vbo);
  77. glEnableVertexAttribArray(0);
  78. glEnableVertexAttribArray(1);
  79. glEnableVertexAttribArray(2);
  80. glVertexAttribPointer(0, 2, GL_FLOAT, false, 20, 0);
  81. glVertexAttribPointer(1, 2, GL_FLOAT, false, 20, 8);
  82. glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, true, 20, 16);
  83. });
  84. }
  85. private void addRectangle(float minX, float minY, char c)
  86. {
  87. float scale = Shader.getViewScale();
  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(Shader.getViewScale() - 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 length = 0;
  193. int counter = 1;
  194. for(int i = 0; i < s.length(); i++)
  195. {
  196. switch(s.charAt(i))
  197. {
  198. case '\n':
  199. counter++;
  200. break;
  201. case COLOR_CHAR:
  202. i++;
  203. break;
  204. default:
  205. length++;
  206. }
  207. }
  208. return new Rectangle(FONT_SIZE * length, (FONT_SIZE + LINE_STEP) * counter);
  209. }
  210. public Rectangle getSize(int w, int h)
  211. {
  212. return new Rectangle(FONT_SIZE * w, (FONT_SIZE + LINE_STEP) * h);
  213. }
  214. public float getHeight()
  215. {
  216. return FONT_SIZE + LINE_STEP;
  217. }
  218. public float getWidth()
  219. {
  220. return FONT_SIZE;
  221. }
  222. }