-
Notifications
You must be signed in to change notification settings - Fork 3
Getting Started
squid233 edited this page Oct 22, 2022
·
30 revisions
It's currently using JDK 19.
Now we'll create a window with red background color using GLFW and OpenGL.
import org.overrun.glib.gl.GL;
import org.overrun.glib.gl.GLCaps;
import org.overrun.glib.glfw.Callbacks;
import org.overrun.glib.glfw.GLFW;
import org.overrun.glib.glfw.GLFWErrorCallback;
import org.overrun.glib.glfw.GLFWVidMode;
import java.lang.foreign.MemoryAddress;
import java.lang.foreign.MemorySession;
import static org.overrun.glib.gl.GLConstC.*;
/**
* Hello world
*
* @author You
*/
public final class HelloWorld {
/* The window handle */
private MemoryAddress window;
public void run() {
init();
loop();
// Free the window callbacks and destroy the window
Callbacks.free(window);
GLFW.destroyWindow(window);
// Terminate GLFW and remove the error callback
GLFW.terminate();
GLFW.setErrorCallback(null);
}
private void init() {
// Set up an error callback. The default implementation
// will print the error message in System.err.
GLFWErrorCallback.createPrint().set();
// Initialize GLFW. Most GLFW functions will not work before doing this.
if (!GLFW.init()) {
throw new IllegalStateException("Unable to initialize GLFW");
}
// Configure GLFW
// optional, the current window hints are already the default
GLFW.defaultWindowHints();
// the window will stay hidden after creation
GLFW.windowHint(GLFW.VISIBLE, false);
// the window will be resizable
GLFW.windowHint(GLFW.RESIZABLE, true);
// Create the window
window = GLFW.createWindow(300, 300, "Hello World!", MemoryAddress.NULL, MemoryAddress.NULL);
if (window == MemoryAddress.NULL)
throw new RuntimeException("Failed to create the GLFW window");
// Set up a key callback. It will be called every time a key is pressed, repeated or released.
GLFW.setKeyCallback(window, (handle, key, scancode, action, mods) -> {
if (key == GLFW.KEY_ESCAPE && action == GLFW.RELEASE) {
// We will detect this in the rendering loop
GLFW.setWindowShouldClose(window, true);
}
});
GLFW.setFramebufferSizeCallback(window, (handle, width, height) ->
// Resize the viewport
GL.viewport(0, 0, width, height));
// Get the resolution of the primary monitor
var vidMode = GLFW.getVideoMode(GLFW.getPrimaryMonitor());
if (vidMode != null) {
// Get the window size passed to glfwCreateWindow
var size = GLFW.getWindowSize(window, pWidth, pHeight);
// Center the window
GLFW.setWindowPos(
window,
(vidMode.width() - size.x()) / 2,
(vidMode.height() - size.y()) / 2
);
}
// Make the OpenGL context current
GLFW.makeContextCurrent(window);
// Enable v-sync
GLFW.swapInterval(1);
// Make the window visible
GLFW.showWindow(window);
}
private void loop() {
// Load OpenGL capabilities with GLFW.
// The default loading function uses a compatibility profile,
// which can access deprecated and removed functions.
if (GLCaps.load(GLFW::getProcAddress) == 0)
throw new IllegalStateException("Failed to load OpenGL");
// Set the clear color
GL.clearColor(1.0f, 0.0f, 0.0f, 1.0f);
// Run the rendering loop until the user has attempted to close
// the window or has pressed the ESCAPE key.
while (!GLFW.windowShouldClose(window)) {
// clear the framebuffer
GL.clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// swap the color buffers
GLFW.swapBuffers(window);
// Poll for window events. The key callback above will only be
// invoked during this call.
GLFW.pollEvents();
}
}
public static void main(String[] args) {
new HelloWorld().run();
}
}
Copyright (c) 2022-2025 Overrun Organization. In case of any problems with this wiki, please search or create an issue.