diff --git a/src/java/ru/dmitriymx/lwjgl/tools/Texture.java b/src/java/ru/dmitriymx/lwjgl/tools/Texture.java index fc3f1ea..eaa922a 100644 --- a/src/java/ru/dmitriymx/lwjgl/tools/Texture.java +++ b/src/java/ru/dmitriymx/lwjgl/tools/Texture.java @@ -27,7 +27,19 @@ public class Texture { } public void setImage(BufferedImage image) { + boolean recalc_onePixel = false; + + if (getWidth() != image.getWidth() || getHeight() != image.getHeight()) { + recalc_onePixel = true; + } + this.image = image; + bind(); + prepare_image(image); + + if (recalc_onePixel) { + prepare_onePixel(); + } } public void bind() { @@ -53,7 +65,6 @@ public class Texture { public void updateTexture() { bind(); prepare_image(image); - prepare_onePixel(); } public int getWidth() { diff --git a/src/java/ru/dmitriymx/lwjgl/tools/fontengine/FontEngine.java b/src/java/ru/dmitriymx/lwjgl/tools/fontengine/FontEngine.java index 0882a1e..e1f0075 100644 --- a/src/java/ru/dmitriymx/lwjgl/tools/fontengine/FontEngine.java +++ b/src/java/ru/dmitriymx/lwjgl/tools/fontengine/FontEngine.java @@ -20,7 +20,7 @@ import ru.dmitriymx.lwjgl.tools.Texture; public class FontEngine { private Font font; private Texture glyphTexture; - private int texture_width = 128, texture_height = 128; + private int texture_width, texture_height; private Graphics2D g2d; private boolean anti_aliasing = false; private Map charMap = new HashMap<>(); @@ -85,6 +85,10 @@ public class FontEngine { glyphTexture.bind(); } + public Texture getGlyphTexture() { + return glyphTexture; + } + public CharData getCharData(char chr) { return charMap.get(chr); } @@ -137,15 +141,20 @@ public class FontEngine { FontMetrics fontMetrics = g2d.getFontMetrics(); 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) { texture_offset_char_y = max_height + 1; 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); return new CharData(texture_offset_char_x, texture_offset_char_y, visualBounds.width, visualBounds.height, ~visualBounds.x + 1, ~visualBounds.y + 1, widthChar); @@ -165,13 +174,15 @@ public class FontEngine { texture_offset_char_y = 0; max_height = 0; + // установка новой текстуры + glyphTexture.setImage(img); + // отрисовка символов по новой for (char chr : charMap.keySet()) { putChar(chr); } // обновление текстуры - glyphTexture.setImage(img); glyphTexture.updateTexture(); } }