Java 22 has been released, and the FFM API is finalized. In this article, I will introduce a library that uses this API called OverrunGL.
Introduction
OverrunGL is a Java library that allows accessing C libraries.
See the links below for more information.
Download
You can import OverrunGL with Maven coordinates. There is a generator for it.
Function Invocation
OverrunGL defines functions with interfaces and generates implementation at runtime.
Compare with LWJGL 3:
|
|
You can see that LWJGL 3 uses static methods, but OverrunGL uses instance methods.
For several libraries,
instances might be helpful in multithreading context by avoiding ThreadLocal
.
For example:
|
|
or ScopedValue
:
|
|
Modular Loading
The OpenGL module has supported modular loading. You can load only the functions you need. See this example:
|
|
This code only loads GL10C
and GL20C
.
It will not load other classes that GL
extends,
so it will improve the bootstrap performance.
An OpenGL state manager which does only invoke GL function when the state is actually changed.
|
|
Memory Management
OverrunGL uses MemorySegment
instead of NIO Buffers.
It is provided by the FFM API.
Allocation
Use Arena
, which does only allow allocating but not destroying memory manually.
The memory is only accessible within the scope of the Arena
.
|
|
Memory Stack
Memory stack (implementation from
LWJGL 3)
is a kind of Arena
. It allocates a memory segment once then reuses it.
Use MemoryStack::stackPush
to push a frame:
|
|
The push and pop operations must be symmetric.
Zero-length Segments
Native functions might return a pointer. If the layout of the returned pointer is not specified, the FFM API will convert it to a zero-length memory segment.
You can resize it with MemorySegment::reinterpret
.
This requires native access to the module of the caller.
|
|
Null Pointer
NULL
in C is modeled as MemorySegment.NULL
, which is equivalent to MemorySegment.ofAddress(0L)
.
Future Updates
OverrunGL has added GLFW, OpenGL, stb and Native File Dialog. We plan to add Vulkan, OpenAL, FreeType, Zstandard and Assimp and use Java 25 in the future.