|
@@ -1,6 +1,7 @@
|
|
|
package me.hammerle.snuviengine.api;
|
|
|
|
|
|
import java.nio.FloatBuffer;
|
|
|
+import me.hammerle.snuviengine.game.Rectangle;
|
|
|
import me.hammerle.snuviengine.util.Color;
|
|
|
import org.lwjgl.BufferUtils;
|
|
|
import static org.lwjgl.opengl.GL11.*;
|
|
@@ -14,6 +15,10 @@ public class FontRenderer
|
|
|
private static final float[] COLORS = new float[128];
|
|
|
private static final float[] DARK_COLORS = new float[128];
|
|
|
|
|
|
+ private static final int FONT_SIZE = 8;
|
|
|
+ private static final int LINE_STEP = 1;
|
|
|
+ private static final float SHADOW_STEP = 1f;
|
|
|
+
|
|
|
static
|
|
|
{
|
|
|
COLORS['0'] = Float.intBitsToFloat(Color.get(0, 0, 0));
|
|
@@ -83,8 +88,15 @@ public class FontRenderer
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- private void addRectangle(float minX, float minY, float maxX, float maxY, float tMinX, float tMinY, float tMaxX, float tMaxY)
|
|
|
+ private void addRectangle(float minX, float minY, char c)
|
|
|
{
|
|
|
+ float tMinX = (c & 0xF) / 16.0f;
|
|
|
+ float tMinY = (c >> 4) / 16.0f;
|
|
|
+ float tMaxX = tMinX + 0.0625f;
|
|
|
+ float tMaxY = tMinY + 0.0625f;
|
|
|
+ float maxX = minX + FONT_SIZE;
|
|
|
+ float maxY = minY + FONT_SIZE;
|
|
|
+
|
|
|
buffer.put(minX);
|
|
|
buffer.put(maxY);
|
|
|
buffer.put(tMinX);
|
|
@@ -122,41 +134,32 @@ public class FontRenderer
|
|
|
buffer.put(color);
|
|
|
}
|
|
|
|
|
|
- public void drawString(float x, float y, boolean shadow, String s)
|
|
|
+ public float drawString(float x, float y, boolean shadow, String s)
|
|
|
{
|
|
|
- int l = Math.min(s.length(), MAX_LENGTH);
|
|
|
-
|
|
|
if(shadow)
|
|
|
{
|
|
|
- color = DARK_COLORS['f'];
|
|
|
- float oldX = x;
|
|
|
- float oldY = y;
|
|
|
-
|
|
|
- for(int pos = 0; pos < l; pos++)
|
|
|
- {
|
|
|
- char c = s.charAt(pos);
|
|
|
- if(c == '&')
|
|
|
- {
|
|
|
- pos++;
|
|
|
- int index = s.charAt(pos);
|
|
|
- if(index >= 0 && index <= COLORS.length)
|
|
|
- {
|
|
|
- color = DARK_COLORS[s.charAt(pos)];
|
|
|
- }
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- float tMinX = (c & 0xF) / 16.0f;
|
|
|
- float tMinY = 0.9375f - ((c >> 4) / 16.0f);
|
|
|
- addRectangle(x + 1, y - 1, x + 17, y + 15, tMinX, tMinY, tMinX + 0.0625f, tMinY + 0.0625f);
|
|
|
- x += 16;
|
|
|
- }
|
|
|
-
|
|
|
- x = oldX;
|
|
|
- y = oldY;
|
|
|
+ addString(x + SHADOW_STEP, y + SHADOW_STEP, DARK_COLORS, s);
|
|
|
}
|
|
|
+ y = addString(x, y, COLORS, s);
|
|
|
|
|
|
- color = COLORS['f'];
|
|
|
+ buffer.flip();
|
|
|
+
|
|
|
+ glBindVertexArray(vao);
|
|
|
+ glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
|
|
+ glBufferSubData(GL_ARRAY_BUFFER, 0, buffer);
|
|
|
+ glDrawArrays(GL_TRIANGLES, 0, buffer.limit() / 5);
|
|
|
+
|
|
|
+ buffer.limit(buffer.capacity());
|
|
|
+ return y;
|
|
|
+ }
|
|
|
+
|
|
|
+ private float addString(float x, float y, float[] colors, String s)
|
|
|
+ {
|
|
|
+ int l = Math.min(s.length(), MAX_LENGTH);
|
|
|
+
|
|
|
+ float oldX = x;
|
|
|
+ color = colors['f'];
|
|
|
+
|
|
|
for(int pos = 0; pos < l; pos++)
|
|
|
{
|
|
|
char c = s.charAt(pos);
|
|
@@ -164,29 +167,51 @@ public class FontRenderer
|
|
|
{
|
|
|
pos++;
|
|
|
int index = s.charAt(pos);
|
|
|
- if(index >= 0 && index <= COLORS.length)
|
|
|
+ if(index >= 0 && index <= colors.length)
|
|
|
{
|
|
|
- color = COLORS[s.charAt(pos)];
|
|
|
+ color = colors[s.charAt(pos)];
|
|
|
}
|
|
|
continue;
|
|
|
}
|
|
|
-
|
|
|
- float tMinX = (c & 0xF) / 16.0f;
|
|
|
- float tMinY = 0.9375f - ((c >> 4) / 16.0f);
|
|
|
- addRectangle(x, y, x + 16, y + 16, tMinX, tMinY, tMinX + 0.0625f, tMinY + 0.0625f);
|
|
|
- x += 16;
|
|
|
+ else if(c == '\n')
|
|
|
+ {
|
|
|
+ y += FONT_SIZE + LINE_STEP;
|
|
|
+ x = oldX;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ addRectangle(x, y, c);
|
|
|
+ x += FONT_SIZE;
|
|
|
}
|
|
|
-
|
|
|
- buffer.flip();
|
|
|
-
|
|
|
- glBindVertexArray(vao);
|
|
|
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
|
|
|
- glBufferSubData(GL_ARRAY_BUFFER, 0, buffer);
|
|
|
- glDrawArrays(GL_TRIANGLES, 0, buffer.limit() / 5);
|
|
|
-
|
|
|
- buffer.limit(buffer.capacity());
|
|
|
+ return y + FONT_SIZE + LINE_STEP;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Rectangle getSize(String s)
|
|
|
+ {
|
|
|
+ int length = 0;
|
|
|
+ int counter = 0;
|
|
|
+ for(int i = 0; i < s.length(); i++)
|
|
|
+ {
|
|
|
+ switch(s.charAt(i))
|
|
|
+ {
|
|
|
+ case '\n':
|
|
|
+ counter++;
|
|
|
+ break;
|
|
|
+ case '&':
|
|
|
+ i++;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ length++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return new Rectangle(FONT_SIZE * length, (FONT_SIZE + LINE_STEP) * counter);
|
|
|
}
|
|
|
|
|
|
+ public float getHeight()
|
|
|
+ {
|
|
|
+ return FONT_SIZE + LINE_STEP;
|
|
|
+ }
|
|
|
+
|
|
|
public void delete()
|
|
|
{
|
|
|
buffer = null;
|