Skip to content

Set up the project

In this lesson you create the Starfall project and open a window that shows the title of the game.

The word Starfall in large bold light grey letters at the center of a dark background

The result of this lesson: one UI with one text node.

Start from the program of the Quick Start, with the LWJGL 3 dev jar. It opens a window with a counter button: this lesson replaces that UI with the menu of the game.

Every file of the game goes in the package com.example.starfall, under src/main/java/com/example/starfall/. Set the main class of the application block of build.gradle:

application {
mainClass = 'com.example.starfall.Main'
if (System.getProperty('os.name').toLowerCase().contains('mac')) {
applicationDefaultJvmArgs = ['-XstartOnFirstThread']
}
}

MenuUI is the main menu. For now it is empty: a UI draws nothing by itself.

MenuUI.java
package com.example.starfall;
import dev.joid.lib.ui.core.UI;
public class MenuUI extends UI {
@Override
public void init() {}
}

Main is the program of the Quick Start, with the title of the game and the menu as the UI that JOID.open opens.

Main.java
package com.example.starfall;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL;
import dev.joid.backend.lwjgl3.GlContextRequest;
import dev.joid.backend.lwjgl3.Lwjgl3Backend;
import dev.joid.lib.JOID;
import dev.joid.lib.bridge.BridgeHandler;
import dev.joid.lib.bridge.ui.WindowUIBridge;
public final class Main {
private Main() {}
public static void main(final String[] arguments) {
GLFWErrorCallback.createPrint(System.err).set();
if (!GLFW.glfwInit()) {
throw new IllegalStateException("Unable to initialize GLFW");
}
GlContextRequest.CORE_33.apply();
final long window = GLFW.glfwCreateWindow(1280, 720, "Starfall", 0L, 0L);
if (window == 0L) {
throw new IllegalStateException("Unable to create the window");
}
GLFW.glfwMakeContextCurrent(window);
GL.createCapabilities();
final Lwjgl3Backend backend = Lwjgl3Backend.create(window).register();
JOID.open(new MenuUI());
while (!GLFW.glfwWindowShouldClose(window)) {
GLFW.glfwPollEvents();
backend.frame();
GLFW.glfwSwapBuffers(window);
}
BridgeHandler.UI.getBridge(WindowUIBridge.class).closeAll();
GLFW.glfwDestroyWindow(window);
GLFW.glfwTerminate();
}
}

Run it with gradle run. You should see a black window: the menu is open, but it has no node yet. Press Escape: the menu closes, and the window stays black, because no UI is open anymore.

JOID ships no font for your application. Put the regular and the bold faces of your font in src/main/resources/fonts/Montserrat/, as Montserrat-Regular.ttf and Montserrat-Bold.ttf. With another family, keep the folder and change the names in Theme.

Theme holds what every screen of the game shares: the font and the colors of its palette.

Theme.java
package com.example.starfall;
import dev.joid.lib.color.Color;
import dev.joid.lib.font.impl.msdf.MsdfFont;
import dev.joid.lib.font.impl.msdf.MsdfFontLoader;
public final class Theme {
public static final MsdfFont FONT = MsdfFontLoader.load(
"fonts/Montserrat/Montserrat-Regular.ttf",
"fonts/Montserrat/Montserrat-Bold.ttf"
).join();
public static final Color TEXT = new Color(231, 233, 238).lock();
public static final Color ELEMENT = new Color(38, 43, 53).lock();
private Theme() {}
}

The menu now shows the title at the center of the canvas:

MenuUI.java
package com.example.starfall;
import dev.joid.lib.draw.text.Text;
import dev.joid.lib.font.FontWeight;
import dev.joid.lib.font.TextInfo;
import dev.joid.lib.ui.core.UI;
import dev.joid.lib.ui.node.impl.text.TextNode;
import dev.joid.lib.utils.align.Align;
public class MenuUI extends UI {
@Override
public void init() {
final TextInfo title = TextInfo.create(Theme.FONT, 120F).weight(FontWeight.BOLD).color(Theme.TEXT);
TextNode
.create(960, 540)
.text(Text.create("Starfall", title))
.anchor(Align.CENTER)
.attach(this);
}
}
  • init() creates the nodes of the UI; attach(this) adds the text to it.
  • TextInfo describes the style of a text: the font, the size, the weight and the color.
  • (960, 540) is the center of the 1920 × 1080 design canvas, and Align.CENTER, given to anchor(...), puts the center of the text on that point.

Run it. You should see the title in bold light grey at the center of the window. Resize the window: the title stays centered and scales with it. If the run stops with an ExceptionInInitializerError in Theme, check the folder and the names of the font files.

The menu has a title and nothing else: the next lesson adds the buttons.

Show the version of JOID in small letters under the title, with the constant JOID.VERSION.

Solution

Add a second style, then a second text 100 design pixels lower:

final TextInfo small = TextInfo.create(Theme.FONT, 28F).color(Theme.TEXT);

Full file: ChallengeUI.java

TextNode
.create(960, 640)
.text(Text.create("JOID " + JOID.VERSION, small))
.anchor(Align.CENTER)
.attach(this);

Full file: ChallengeUI.java

Its own anchor centers it on the same column as the title.

  • A UI is a class that creates its nodes in init(), opened by JOID.open(...).
  • Your program owns the window and the loop; the backend draws a frame when you call frame().
  • JOID ships no font: you load your own font and describe the style of a text with a TextInfo.
  • Positions are design pixels on a 1920 × 1080 canvas, scaled to the window.

Report a problem with this page