Skip to content

Choosing a backend

A backend runs JOID on a graphics and window library. Your UIs never mention it: switching backends changes your startup code and nothing else.

You will learn

  • Which backend fits your application, and what it needs from the machine
  • How to start a backend and run its frames
  • What register() does, and which settings come before it
  • How to run code once JOID is ready
  • How to style the window UI bridge: the window background and the tooltips

Pick the backend that fits your application

Section titled “Pick the backend that fits your application”

JOID has three backends. They draw the same pixels for the same UI, so the choice depends on your application, never on your UIs.

LWJGL 3VulkanLWJGL 2
ClassLwjgl3BackendVulkanBackendLwjgl2Backend
Window and inputGLFW (LWJGL 3.3.4)GLFW (LWJGL 3.3.4)Display, Mouse and Keyboard (LWJGL 2.9.1)
Inputattached by the backendattached by the backendread by Lwjgl2InputForwarder in your loop
Show a frameglfwSwapBuffersendFrame() of the Vulkan hostDisplay.update()

Start with LWJGL 3: it is the reference backend, the one whose pixels the other two are compared against. Choose Vulkan when your application already renders with Vulkan. Choose LWJGL 2 when your application already runs on LWJGL 2.

RequirementLWJGL 3VulkanLWJGL 2
Java8 or later8 or later8 or later
Graphicsan OpenGL 2.1 or later context, core or compatibility profile, with GLSL 1.20 and framebuffer objectsa Vulkan 1.3 device able to present to the windowan OpenGL 2.1 context with GLSL 1.20 and framebuffer objects
Buffers24-bit depth, 8-bit stencilcreated by the Vulkan host24-bit depth, 8-bit stencil
macOSstart the JVM with -XstartOnFirstThreadstart the JVM with -XstartOnFirstThreadnothing to add

The stencil buffer clips the masks of your UIs: without it, masked content draws outside its mask. Framebuffer objects come with OpenGL 3.0, or with the GL_ARB_framebuffer_object or GL_EXT_framebuffer_object extension of an older context. On a machine below the minimum, register() throws JOID needs OpenGL 2.1 with GLSL 1.20, this context offers … or JOID needs framebuffer objects (…), this context offers …; with Vulkan, creating the host throws No Vulkan 1.3 device able to present to the window was found.

Installation lists the jars and the libraries to declare for each backend.

JOID never creates a window or a loop: your application owns both. Create the window and its graphics context, then hand the window to the backend:

GLFW.glfwInit();
GlContextRequest.CORE_33.apply();
final long window = GLFW.glfwCreateWindow(1280, 720, "My application", 0L, 0L);
GLFW.glfwMakeContextCurrent(window);
GL.createCapabilities();

Full file: RunningApp.java

GlContextRequest sets the GLFW hints of the context before the window exists: CORE_33 asks for an OpenGL 3.3 core profile, with the 24-bit depth and 8-bit stencil buffers that JOID needs. COMPATIBILITY asks for the default context of the driver instead, for an application that also draws with the fixed pipeline of OpenGL, and CORE_32_FORWARD_COMPATIBLE for a driver that offers only a 3.2 forward-compatible core profile. GL.createCapabilities() belongs to LWJGL: it loads the OpenGL functions of the current context, and JOID calls none before it.

final Lwjgl3Backend backend = Lwjgl3Backend.create(window).register();

Full file: RunningApp.java

create(window) builds the backend; register() connects it to JOID. Call register() once the graphics context exists and is current, on the thread of your loop: the render bridge creates GPU objects at once, and that thread becomes the render thread.

Then open a UI and call frame() once per frame of your loop:

JOID.open(new RunningStartUI());
while (!GLFW.glfwWindowShouldClose(window)) {
GLFW.glfwPollEvents();
backend.frame();
GLFW.glfwSwapBuffers(window);
}
BridgeHandler.UI.getBridge(WindowUIBridge.class).closeAll();
GLFW.glfwDestroyWindow(window);
GLFW.glfwTerminate();

Full file: RunningApp.java

Backend.frame draws one frame of every open UI. It follows the size of the window by itself, so you need no resize callback. Embedding JOID shows how to draw other things in the same loop.

A dark grey Settings button on a dark card, in the middle of the window

The program above, with RunningStartUI open: the window shows the UI from the first frame.

closeAll() of the window UI bridge, after the loop, closes every UI that the bridge holds, without asking their beforeClose listeners: their onClose listeners and the onDetach listeners of their nodes run, their stores and remembered fields reach the disk. Call it on each UI bridge of your own as well, once the loop is over.

When the JVM exits normally with UIs still open, a shutdown hook of JOID calls closeAll() on every UI bridge, then saves the stores; a process that is killed or crashes saves nothing. The hook waits 200 ms at most for the render thread, then closes the UIs from its own thread: call closeAll() yourself, on the render thread, before the program ends.

The program stays the same; only the window, the backend and the way you show a frame change.

Vulkan. Raise the stack size of LWJGL before anything else, create the window without an OpenGL context, and give the backend a WindowVulkanHost, which owns the device and the swapchain of the window:

VulkanApp.java
package com.example.running;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.system.Configuration;
import dev.joid.backend.vulkan.VulkanBackend;
import dev.joid.backend.vulkan.host.WindowVulkanHost;
import dev.joid.lib.JOID;
import dev.joid.lib.bridge.BridgeHandler;
import dev.joid.lib.bridge.ui.WindowUIBridge;
public final class VulkanApp {
private VulkanApp() {}
public static void main(final String[] args) {
Configuration.STACK_SIZE.set(1024);
GLFW.glfwInit();
GLFW.glfwWindowHint(GLFW.GLFW_CLIENT_API, GLFW.GLFW_NO_API);
final long window = GLFW.glfwCreateWindow(1280, 720, "My application", 0L, 0L);
final WindowVulkanHost host = WindowVulkanHost.create(window);
final VulkanBackend backend = VulkanBackend.create(window, host).register();
host.beginFrame();
JOID.open(new RunningStartUI());
while (!GLFW.glfwWindowShouldClose(window)) {
GLFW.glfwPollEvents();
backend.frame();
host.endFrame();
if (!GLFW.glfwWindowShouldClose(window)) {
host.beginFrame();
}
}
BridgeHandler.UI.getBridge(WindowUIBridge.class).closeAll();
backend.destroy();
host.destroy();
GLFW.glfwDestroyWindow(window);
GLFW.glfwTerminate();
}
}
  • Each frame of the loop draws between beginFrame() and endFrame() of the host, so the first frame begins right after register().
  • endFrame() shows the image on the window, and beginFrame() starts the next one at once, unless the window is closing.
  • After the loop, no frame is open: closeAll() closes the UIs, then destroy() releases the backend, then the host.

LWJGL 2. Call Lwjgl2Natives.install before the Display exists: it extracts the LWJGL 2 natives into the cache folder, unless org.lwjgl.librarypath or java.library.path already provides them. Register the backend once the Display exists:

Lwjgl2App.java
package com.example.running;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.PixelFormat;
import dev.joid.backend.lwjgl2.Lwjgl2Backend;
import dev.joid.backend.lwjgl2.Lwjgl2Natives;
import dev.joid.base.lwjgl2.input.Lwjgl2InputForwarder;
import dev.joid.lib.JOID;
import dev.joid.lib.bridge.BridgeHandler;
import dev.joid.lib.bridge.ui.WindowUIBridge;
public final class Lwjgl2App {
private Lwjgl2App() {}
public static void main(final String[] args) throws LWJGLException {
Lwjgl2Natives.install();
Display.setDisplayMode(new DisplayMode(1280, 720));
Display.setTitle("My application");
Display.create(new PixelFormat().withDepthBits(24).withStencilBits(8));
final Lwjgl2Backend backend = Lwjgl2Backend.create().register();
JOID.open(new RunningStartUI());
while (!Display.isCloseRequested()) {
Lwjgl2InputForwarder.poll();
backend.frame();
Display.update();
}
BridgeHandler.UI.getBridge(WindowUIBridge.class).closeAll();
Display.destroy();
}
}

LWJGL 2 has no input callbacks, so Lwjgl2InputForwarder.poll reads the input at the start of each frame.

register() runs these steps in this order:

  1. It throws IllegalStateException when the backend is already registered: a backend registers once.
  2. It checks that the backend was built for the same major version of JOID, and throws The backend was built for JOID ….x, but JOID … is loaded: update the backend when it was not.
  3. It applies the data and cache folders set with dataDir(...) and cacheDir(...).
  4. It registers the audio bridge of the backend, unless you called audio(false), then its window and render bridges.
  5. It registers a thread bridge tied to the calling thread, which becomes the render thread: work sent from other threads runs there at the start of the next frame.
  6. It adds the window UI bridge, unless you registered a UI bridge of your own before (Embedding JOID).
  7. It attaches the input to the window (LWJGL 3 and Vulkan).
  8. It loads JOID: it prints a banner with the version, the dev mode and the two folders ([JOID] … loaded in dev mode, data in …, cache in …), cleans the cache folder on a background thread, and installs the shutdown hook that closes the open UIs and saves the stores.
  9. It runs the code given to JOID.onReady.

The settings of a backend come between create(...) and register(), all optional:

SettingEffect
Backend.audiofalse registers no audio bridge, for tests and machines without an audio device. A video with a sound track still plays its picture.
Backend.dataDir, Backend.cacheDirMove the folders where JOID writes, see Where JOID stores files.
final Lwjgl3Backend backend = Lwjgl3Backend.create(window).audio(false).register();

Code that runs before the backend is registered cannot open a UI: no bridge is there to receive it. A library, a plugin or a module that does not control the startup order gives its setup to JOID.onReady instead:

public static void install() {
JOID.onReady(() -> JOID.open(new RunningStartUI()));
}

Full file: RunningLibrary.java

Before register(), JOID keeps that code and runs it at the end of register(), on the render thread. Once the backend is registered, the code runs at once on the render thread, or at the start of the next frame when you call onReady from another thread. JOID.inst().isReady() tells you which case you are in.

When you register no UI bridge of your own, the backend registers a WindowUIBridge. Every frame, it clears the window with its background color, draws the open UIs and draws the tooltip of the hovered node.

Get it from the UI registry once the backend is registered, and set what you need:

BridgeHandler.UI
.getBridge(WindowUIBridge.class)
.background(RunningApp.STAGE)
.hoverInfo(TextInfo.create(ExampleFont.REGULAR, 20F).color(RunningApp.TEXT));

Full file: RunningApp.java

SettingWhat it changes
WindowUIBridge.backgroundThe color that clears the window before every frame, behind every UI.
WindowUIBridge.hoverInfoThe font, size and color of the tooltip text.
WindowUIBridge.hoverColorThe fill of the tooltip box.
WindowUIBridge.hoverBorderColorThe border of the tooltip box, one design pixel wide.

Once hoverInfo is set, every node that gives a hover(...) text shows this tooltip, with no code in the UI.

getBridge(WindowUIBridge.class) returns the highest bridge of that class or of a subclass, and null when you registered a UI bridge of another kind before the backend. Hover, tooltips and cursors shows how a node declares its tooltip.

To run JOID on another library or engine, implement its bridges: Writing a backend walks through it.

  • A backend runs JOID on LWJGL 3, Vulkan or LWJGL 2; your UIs are the same on all three.
  • Start with LWJGL 3; pick Vulkan or LWJGL 2 to match the library your application already uses.
  • Create the window and its context, then create(...), the optional settings, and register() once, on the thread of your loop.
  • Call frame() once per frame of your loop; it follows the window size by itself.
  • After the loop, closeAll() closes the open UIs, so that their onClose listeners run and their stores reach the disk.
  • JOID.onReady runs setup code once the backend is registered, whatever the startup order.
  • The window UI bridge clears the window with background, and draws tooltips once you set hoverInfo.

A window in your palette. Give the window background the color #171A21 and tooltips with text #E7E9EE on a box #262B35.

Solution
BridgeHandler.UI
.getBridge(WindowUIBridge.class)
.background(Color.decode("#171A21"))
.hoverColor(Color.decode("#262B35"))
.hoverInfo(TextInfo.create(font, 20F).color(Color.decode("#E7E9EE")));

Open from a library. Move the JOID.open(...) call of the program above into a library class that may run before the backend starts.

Solution

Wrap it in JOID.onReady(() -> JOID.open(new RunningStartUI()));. The code runs at the end of register(), or at once if the backend is already registered.

Report a problem with this page