Skip to content

Commit

Permalink
Merge pull request #52 from Over-Run/struct
Browse files Browse the repository at this point in the history
Update Marshal
  • Loading branch information
squid233 committed May 18, 2024
2 parents 74c53b6 + 4dd0e48 commit 7c03902
Show file tree
Hide file tree
Showing 35 changed files with 1,546 additions and 1,405 deletions.
6 changes: 3 additions & 3 deletions buildSrc/src/main/kotlin/myproject.java-conventions.gradle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ enum class Artifact(
"Single-file public domain libraries for fonts, images, ogg vorbis files and more.",
":stb", "Stb", NativeBinding.STB
),
VULKAN("overrungl-vulkan", "OverrunGL - Vulkan bindings",
"A new generation graphics and compute API that provides high-efficiency, cross-platform access to modern GPUs used in a wide variety of devices from PCs and consoles to mobile phones and embedded platforms.",
":vulkan", "Vulkan", null),
// VULKAN("overrungl-vulkan", "OverrunGL - Vulkan bindings",
// "A new generation graphics and compute API that provides high-efficiency, cross-platform access to modern GPUs used in a wide variety of devices from PCs and consoles to mobile phones and embedded platforms.",
// ":vulkan", "Vulkan", null),
;

fun nativeFileName(platform: NativePlatform): String? {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ jdkEnablePreview=true
#jdkEarlyAccessDoc=jdk22
kotlinTargetJdkVersion=21

overrunMarshalVersion=0.1.0-alpha.24-jdk22
overrunMarshalVersion=0.1.0-alpha.26-jdk22
overrunPlatformVersion=1.0.0
6 changes: 3 additions & 3 deletions modules/overrungl.glfw/src/main/java/overrungl/glfw/GLFW.java
Original file line number Diff line number Diff line change
Expand Up @@ -1953,7 +1953,7 @@ default MemorySegment setMonitorCallback(@Nullable GLFWMonitorFun callback) {
return null;
}
final int count = pCount.get(JAVA_INT, 0);
return new GLFWVidMode(pModes.reinterpret(GLFWVidMode.LAYOUT.scale(0L, count)), count);
return GLFWVidMode.OF.of(pModes.reinterpret(GLFWVidMode.OF.layout().scale(0L, count)), count);
}
}

Expand Down Expand Up @@ -1988,10 +1988,10 @@ default MemorySegment setMonitorCallback(@Nullable GLFWMonitorFun callback) {
*/
@Nullable
@Skip
default GLFWVidMode.Value getVideoMode(MemorySegment monitor) {
default GLFWVidMode getVideoMode(MemorySegment monitor) {
var pMode = ngetVideoMode(monitor);
if (Unmarshal.isNullPointer(pMode)) return null;
return new GLFWVidMode(pMode.reinterpret(GLFWVidMode.LAYOUT.byteSize())).value();
return GLFWVidMode.OF.of(pMode.reinterpret(GLFWVidMode.OF.layout().byteSize()));
}

/**
Expand Down
137 changes: 98 additions & 39 deletions modules/overrungl.glfw/src/main/java/overrungl/glfw/GLFWAllocator.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@

package overrungl.glfw;

import overrun.marshal.LayoutBuilder;
import overrun.marshal.Marshal;
import overrun.marshal.struct.Struct;
import overrun.marshal.struct.StructHandle;
import overrun.marshal.struct.StructAllocator;

import java.lang.foreign.*;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.invoke.MethodHandles;

/**
* Custom heap memory allocator.
Expand All @@ -31,72 +35,127 @@
* @see GLFW#initAllocator
* @since 0.1.0
*/
public final class GLFWAllocator extends Struct {
public interface GLFWAllocator extends Struct<GLFWAllocator> {
/**
* The layout of this struct.
* The allocator
*/
public static final StructLayout LAYOUT = MemoryLayout.structLayout(
ValueLayout.ADDRESS.withName("allocate"),
ValueLayout.ADDRESS.withName("reallocate"),
ValueLayout.ADDRESS.withName("deallocate"),
ValueLayout.ADDRESS.withName("user")
StructAllocator<GLFWAllocator> OF = new StructAllocator<>(
MethodHandles.lookup(),
LayoutBuilder.struct()
.cAddress("allocate")
.cAddress("reallocate")
.cAddress("deallocate")
.cAddress("user")
.build()
);

/**
* The memory allocation function. See {@link GLFWAllocateFun} for details about
* allocation function.
* {@return the memory allocation function}
* See {@link GLFWAllocateFun} for details about allocation function.
*/
public static final StructHandle.Upcall<GLFWAllocateFun> allocate = StructHandle.ofUpcall(LAYOUT, "allocate", GLFWAllocateFun::wrap);
MemorySegment allocate();

/**
* The memory reallocation function. See {@link GLFWReallocateFun} for details about
* reallocation function.
* Sets {@link #allocate()}.
*
* @param val the value
* @return this
*/
public static final StructHandle.Upcall<GLFWReallocateFun> reallocate = StructHandle.ofUpcall(LAYOUT, "reallocate", GLFWReallocateFun::wrap);
GLFWAllocator allocate(MemorySegment val);

/**
* {@return the memory reallocation function}
* See {@link GLFWReallocateFun} for details about reallocation function.
*/
MemorySegment reallocate();

/**
* The memory deallocation function. See {@link GLFWDeallocateFun} for details about
* deallocation function.
* Sets {@link #reallocate()}.
*
* @param val the value
* @return this
*/
public static final StructHandle.Upcall<GLFWDeallocateFun> deallocate = StructHandle.ofUpcall(LAYOUT, "deallocate", GLFWDeallocateFun::wrap);
GLFWAllocator reallocate(MemorySegment val);

/**
* The user pointer for this custom allocator. This value will be passed to the
* allocator functions.
* {@return the memory deallocation function}
* See {@link GLFWDeallocateFun} for details about deallocation function.
*/
public static final StructHandle.Address user = StructHandle.ofAddress(LAYOUT, "user");
MemorySegment deallocate();

/**
* Creates a struct with the given layout.
* Sets {@link #deallocate()}.
*
* @param segment the segment
* @param elementCount the element count
* @param val the value
* @return this
*/
public GLFWAllocator(MemorySegment segment, long elementCount) {
super(segment, elementCount, LAYOUT);
GLFWAllocator deallocate(MemorySegment val);

/**
* {@return the user pointer for this custom allocator}
* This value will be passed to the allocator functions.
*/
MemorySegment user();

/**
* Sets {@link #user()}.
*
* @param val the value
* @return this
*/
GLFWAllocator user(MemorySegment val);

/**
* {@return {@link #allocate()}}
*/
default GLFWAllocateFun javaAllocate() {
return GLFWAllocateFun.wrap(allocate());
}

/**
* Allocates a struct with the given layout.
* Sets {@link #allocate()}.
*
* @param allocator the allocator
* @param elementCount the element count
* @param arena the arena
* @param val the value
* @return this
*/
public GLFWAllocator(SegmentAllocator allocator, long elementCount) {
super(allocator, elementCount, LAYOUT);
default GLFWAllocator javaAllocate(Arena arena, GLFWAllocateFun val) {
return allocate(Marshal.marshal(arena, val));
}

/**
* Creates a struct with the given layout.
* {@return {@link #reallocate()}}
*/
default GLFWReallocateFun javaReallocate() {
return GLFWReallocateFun.wrap(reallocate());
}

/**
* Sets {@link #reallocate()}.
*
* @param segment the segment
* @param arena the arena
* @param val the value
* @return this
*/
default GLFWAllocator javaReallocate(Arena arena, GLFWReallocateFun val) {
return reallocate(Marshal.marshal(arena, val));
}

/**
* {@return {@link #deallocate()}}
*/
public GLFWAllocator(MemorySegment segment) {
super(segment, LAYOUT);
default GLFWDeallocateFun javaDeallocate() {
return GLFWDeallocateFun.wrap(deallocate());
}

/**
* Allocates a struct with the given layout.
* Sets {@link #deallocate()}.
*
* @param allocator the allocator
* @param arena the arena
* @param val the value
* @return this
*/
public GLFWAllocator(SegmentAllocator allocator) {
super(allocator, LAYOUT);
default GLFWAllocator javaDeallocate(Arena arena, GLFWDeallocateFun val) {
return deallocate(Marshal.marshal(arena, val));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package overrungl.glfw;

import overrun.marshal.LayoutBuilder;
import overrun.marshal.struct.Struct;
import overrun.marshal.struct.StructAllocator;

import java.lang.foreign.*;
import java.lang.invoke.MethodHandles;

/**
* This describes the input state of a gamepad.
Expand All @@ -32,72 +35,44 @@
* @author squid233
* @since 0.1.0
*/
public final class GLFWGamepadState extends Struct {
public interface GLFWGamepadState extends Struct<GLFWGamepadState> {
/**
* The struct layout.
* The allocator
*/
public static final StructLayout LAYOUT = MemoryLayout.structLayout(
MemoryLayout.sequenceLayout(15, ValueLayout.JAVA_BYTE).withName("buttons"),
MemoryLayout.paddingLayout(1L),
MemoryLayout.sequenceLayout(6, ValueLayout.JAVA_FLOAT).withName("axes")
StructAllocator<GLFWGamepadState> OF = new StructAllocator<>(
MethodHandles.lookup(),
LayoutBuilder.struct()
.cArray("buttons", 15, ValueLayout.JAVA_BYTE)
.cArray("axes", 6, ValueLayout.JAVA_FLOAT)
.build()
);

/**
* The states of each <a href="https://www.glfw.org/docs/latest/group__gamepad__buttons.html">gamepad button</a>,
* {@link GLFW#PRESS} or {@link GLFW#RELEASE}.
*/
public static final StructHandleSizedByteArray buttons = StructHandleSizedByteArray.of(LAYOUT, "buttons");
/**
* The states of each <a href="https://www.glfw.org/docs/latest/group__gamepad__axes.html">gamepad axis</a>,
* in the range -1.0 to 1.0 inclusive.
*/
public static final StructHandleSizedFloatArray axes = StructHandleSizedFloatArray.of(LAYOUT, "axes");

/**
* Creates a struct with the given layout.
*
* @param segment the segment
* @param elementCount the element count
*/
public GLFWGamepadState(MemorySegment segment, long elementCount) {
super(segment, elementCount, LAYOUT);
}

/**
* Allocates a struct with the given layout.
*
* @param allocator the allocator
* @param elementCount the element count
*/
public GLFWGamepadState(SegmentAllocator allocator, long elementCount) {
super(allocator, elementCount, LAYOUT);
}

/**
* Creates a struct with the given layout.
*
* @param segment the segment
* @param index the index
* @return the state
*/
public GLFWGamepadState(MemorySegment segment) {
super(segment, LAYOUT);
}
byte buttons(long index);

/**
* Allocates a struct with the given layout.
* The states of each <a href="https://www.glfw.org/docs/latest/group__gamepad__axes.html">gamepad axis</a>,
* in the range -1.0 to 1.0 inclusive.
*
* @param allocator the allocator
* @param index the index
* @return the state
*/
public GLFWGamepadState(SegmentAllocator allocator) {
super(allocator, LAYOUT);
}
float axes(long index);

/**
* Gets the button state at the given index.
*
* @param index the index
* @return the state, {@code PRESS} or {@code RELEASE}
*/
public boolean button(int index) {
return buttons.get(this, index) == GLFW.PRESS;
default boolean button(int index) {
return buttons(index) == GLFW.PRESS;
}

/**
Expand All @@ -106,7 +81,7 @@ public boolean button(int index) {
* @param index the index
* @return the state, in the range -1.0 to 1.0 inclusive
*/
public float axe(int index) {
return axes.get(this, index);
default float axe(int index) {
return axes(index);
}
}
Loading

0 comments on commit 7c03902

Please sign in to comment.