Created
July 17, 2021 02:46
-
-
Save Frontear/6e500c6bd04fb7a550d5f2171fe783b5 to your computer and use it in GitHub Desktop.
An attempt to draw text using GLFW
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example; | |
public final class Glyph { | |
private final int x, y, width, height; | |
public Glyph(final int x, final int y, final int width, final int height) { | |
this.x = x; | |
this.y = y; | |
this.width = width; | |
this.height = height; | |
} | |
public int getX() { | |
return x; | |
} | |
public int getY() { | |
return y; | |
} | |
public int getWidth() { | |
return width; | |
} | |
public int getHeight() { | |
return height; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.example; | |
import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks; | |
import static org.lwjgl.glfw.GLFW.*; | |
import static org.lwjgl.system.MemoryUtil.NULL; | |
import java.awt.*; | |
import java.awt.image.BufferedImage; | |
import java.io.*; | |
import javax.imageio.ImageIO; | |
import org.lwjgl.Version; | |
import org.lwjgl.glfw.*; | |
import org.lwjgl.opengl.GL; | |
public class Main { | |
private static final GLFWErrorCallback errorCallback = GLFWErrorCallback.createPrint(System.err); | |
private static final GLFWKeyCallback keyCallback = GLFWKeyCallback.create((w, k, s, a, m) -> { | |
if (k == GLFW_KEY_ESCAPE && a == GLFW_PRESS) { | |
glfwSetWindowShouldClose(w, true); | |
} | |
}); | |
public static void main(String[] args) throws IOException { | |
createFontBitmap(); | |
if (true) | |
return; | |
glfwSetErrorCallback(errorCallback); | |
if (!glfwInit()) { | |
throw new IllegalStateException("!glfwInit()"); | |
} | |
var window = glfwCreateWindow(640, 480, "FontTest", NULL, NULL); | |
if (window == NULL) { | |
glfwTerminate(); | |
throw new IllegalStateException("window == NULL"); | |
} | |
glfwSetKeyCallback(window, keyCallback); | |
glfwMakeContextCurrent(window); | |
GL.createCapabilities(); | |
while (!glfwWindowShouldClose(window)) { | |
glfwSwapBuffers(window); | |
glfwPollEvents(); | |
} | |
glfwDestroyWindow(window); | |
glfwTerminate(); | |
glfwFreeCallbacks(window); | |
} | |
private static void createFontBitmap() throws IOException { | |
var alphabet = "abcdefghijklmnopqrstuvwxyz"; | |
final var image = new BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB); | |
final var graphics = image.createGraphics(); | |
{ | |
graphics.setColor(Color.WHITE); | |
graphics.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 48)); | |
var metrics = graphics.getFontMetrics(); | |
for (int i = 0, x = image.getMinX(), y = metrics.getAscent() + image.getMinY(); i < alphabet.length(); ++i) { | |
var alpha = alphabet.charAt(i); | |
{ | |
var width = metrics.charWidth(alpha); | |
var height = metrics.getHeight(); | |
if (x + width > image.getWidth()) { | |
y += height; | |
x = 0; | |
} | |
graphics.drawString(String.valueOf(alpha), x, y); | |
x += width; | |
} | |
} | |
} | |
graphics.dispose(); | |
ImageIO.write(image, "PNG", new File(System.getProperty("java.io.tmpdir"), "image.png")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This makes use of lwjgl 3.2.3, if necessary, the dependencies for gradle are listed below: