Skip to content

Instantly share code, notes, and snippets.

@Frontear
Created July 17, 2021 02:46
Show Gist options
  • Save Frontear/6e500c6bd04fb7a550d5f2171fe783b5 to your computer and use it in GitHub Desktop.
Save Frontear/6e500c6bd04fb7a550d5f2171fe783b5 to your computer and use it in GitHub Desktop.
An attempt to draw text using GLFW
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;
}
}
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"));
}
}
@Frontear
Copy link
Author

This makes use of lwjgl 3.2.3, if necessary, the dependencies for gradle are listed below:

dependencies {
    implementation platform("org.lwjgl:lwjgl-bom:$lwjglVersion")

    implementation "org.lwjgl:lwjgl"
    implementation "org.lwjgl:lwjgl-assimp"
    implementation "org.lwjgl:lwjgl-bgfx"
    implementation "org.lwjgl:lwjgl-glfw"
    implementation "org.lwjgl:lwjgl-nanovg"
    implementation "org.lwjgl:lwjgl-nuklear"
    implementation "org.lwjgl:lwjgl-openal"
    implementation "org.lwjgl:lwjgl-opengl"
    implementation "org.lwjgl:lwjgl-par"
    implementation "org.lwjgl:lwjgl-stb"
    implementation "org.lwjgl:lwjgl-vulkan"
    runtimeOnly "org.lwjgl:lwjgl::$lwjglNatives"
    runtimeOnly "org.lwjgl:lwjgl-assimp::$lwjglNatives"
    runtimeOnly "org.lwjgl:lwjgl-bgfx::$lwjglNatives"
    runtimeOnly "org.lwjgl:lwjgl-glfw::$lwjglNatives"
    runtimeOnly "org.lwjgl:lwjgl-nanovg::$lwjglNatives"
    runtimeOnly "org.lwjgl:lwjgl-nuklear::$lwjglNatives"
    runtimeOnly "org.lwjgl:lwjgl-openal::$lwjglNatives"
    runtimeOnly "org.lwjgl:lwjgl-opengl::$lwjglNatives"
    runtimeOnly "org.lwjgl:lwjgl-par::$lwjglNatives"
    runtimeOnly "org.lwjgl:lwjgl-stb::$lwjglNatives"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment