Archived
0

fix: Bug regenerate texture in FontEngine class

This commit is contained in:
2015-09-21 12:08:27 +03:00
parent 65d8a40a51
commit acc146d06a
2 changed files with 29 additions and 7 deletions

View File

@@ -27,7 +27,19 @@ public class Texture {
} }
public void setImage(BufferedImage image) { public void setImage(BufferedImage image) {
boolean recalc_onePixel = false;
if (getWidth() != image.getWidth() || getHeight() != image.getHeight()) {
recalc_onePixel = true;
}
this.image = image; this.image = image;
bind();
prepare_image(image);
if (recalc_onePixel) {
prepare_onePixel();
}
} }
public void bind() { public void bind() {
@@ -53,7 +65,6 @@ public class Texture {
public void updateTexture() { public void updateTexture() {
bind(); bind();
prepare_image(image); prepare_image(image);
prepare_onePixel();
} }
public int getWidth() { public int getWidth() {

View File

@@ -20,7 +20,7 @@ import ru.dmitriymx.lwjgl.tools.Texture;
public class FontEngine { public class FontEngine {
private Font font; private Font font;
private Texture glyphTexture; private Texture glyphTexture;
private int texture_width = 128, texture_height = 128; private int texture_width, texture_height;
private Graphics2D g2d; private Graphics2D g2d;
private boolean anti_aliasing = false; private boolean anti_aliasing = false;
private Map<Character, CharData> charMap = new HashMap<>(); private Map<Character, CharData> charMap = new HashMap<>();
@@ -85,6 +85,10 @@ public class FontEngine {
glyphTexture.bind(); glyphTexture.bind();
} }
public Texture getGlyphTexture() {
return glyphTexture;
}
public CharData getCharData(char chr) { public CharData getCharData(char chr) {
return charMap.get(chr); return charMap.get(chr);
} }
@@ -137,15 +141,20 @@ public class FontEngine {
FontMetrics fontMetrics = g2d.getFontMetrics(); FontMetrics fontMetrics = g2d.getFontMetrics();
int widthChar = fontMetrics.charWidth(chr) - visualBounds.x; int widthChar = fontMetrics.charWidth(chr) - visualBounds.x;
if (texture_offset_char_y >= texture_height || (texture_offset_char_y + visualBounds.height) > texture_height) {
recreateGlyphTexture();
}
if (texture_offset_char_x >= texture_width || (texture_offset_char_x + visualBounds.width) > texture_width) { if (texture_offset_char_x >= texture_width || (texture_offset_char_x + visualBounds.width) > texture_width) {
texture_offset_char_y = max_height + 1; texture_offset_char_y = max_height + 1;
texture_offset_char_x = 0; texture_offset_char_x = 0;
} }
if (texture_offset_char_y >= texture_height || (texture_offset_char_y + visualBounds.height) > texture_height) {
recreateGlyphTexture();
if (texture_offset_char_x >= texture_width || (texture_offset_char_x + visualBounds.width) > texture_width) {
texture_offset_char_y = max_height + 1;
texture_offset_char_x = 0;
}
}
g2d.drawGlyphVector(glyphVector, texture_offset_char_x - visualBounds.x, texture_offset_char_y - visualBounds.y); g2d.drawGlyphVector(glyphVector, texture_offset_char_x - visualBounds.x, texture_offset_char_y - visualBounds.y);
return new CharData(texture_offset_char_x, texture_offset_char_y, visualBounds.width, visualBounds.height, ~visualBounds.x + 1, return new CharData(texture_offset_char_x, texture_offset_char_y, visualBounds.width, visualBounds.height, ~visualBounds.x + 1,
~visualBounds.y + 1, widthChar); ~visualBounds.y + 1, widthChar);
@@ -165,13 +174,15 @@ public class FontEngine {
texture_offset_char_y = 0; texture_offset_char_y = 0;
max_height = 0; max_height = 0;
// установка новой текстуры
glyphTexture.setImage(img);
// отрисовка символов по новой // отрисовка символов по новой
for (char chr : charMap.keySet()) { for (char chr : charMap.keySet()) {
putChar(chr); putChar(chr);
} }
// обновление текстуры // обновление текстуры
glyphTexture.setImage(img);
glyphTexture.updateTexture(); glyphTexture.updateTexture();
} }
} }