Skip to content

Commit

Permalink
[GLFW] Rewrite GLFW structures, GLFWNative, GLFWVulkan
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Jan 30, 2024
1 parent b668b71 commit acbd0bc
Show file tree
Hide file tree
Showing 15 changed files with 479 additions and 949 deletions.
4 changes: 1 addition & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,5 @@ jdkEnablePreview=true
jdkEarlyAccessDoc=jdk22
kotlinTargetJdkVersion=21

overrunMarshalVersion=0.1.0-alpha.9-jdk22
overrunMarshalVersion=0.1.0-alpha.10-jdk22
overrunPlatformVersion=1.0.0

cLibraryVersion=0.1.0.0
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public final class RuntimeHelper {
public static final boolean CHECKS = Configurations.CHECKS.get();
private static final StackWalker STACK_WALKER = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
private static final Map<String, MemoryLayout> CANONICAL_LAYOUTS = LINKER.canonicalLayouts();
/**
* Some canonical layouts
*/
public static final MemoryLayout LONG = CANONICAL_LAYOUTS.get("long"),
SIZE_T = CANONICAL_LAYOUTS.get("size_t"),
WCHAR_T = CANONICAL_LAYOUTS.get("wchar_t");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.jetbrains.annotations.Nullable;
import overrungl.Configurations;
import overrungl.OverrunGL;
import overrungl.internal.Exceptions;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand Down Expand Up @@ -99,7 +98,7 @@ private static void trackAbort(long address, Allocation allocationOld, Allocatio
trackAbortPrint(allocationOld, "Old", addressHex);
trackAbortPrint(allocationNew, "New", addressHex);

throw Exceptions.ISE."The memory address specified is already being tracked: 0x\{addressHex}";
throw new IllegalStateException(STR."The memory address specified is already being tracked: 0x\{addressHex}");
}

private static void trackAbortPrint(Allocation allocation, String name, String address) {
Expand Down Expand Up @@ -138,7 +137,7 @@ static long untrack(long address) {
private static void untrackAbort(long address) {
String addressHex = Long.toHexString(address).toUpperCase();

throw Exceptions.ISE."The memory address specified is not being tracked: 0x\{addressHex}";
throw new IllegalStateException(STR."The memory address specified is not being tracked: 0x\{addressHex}");
}

private record Allocation(long address, long size, long threadId, @Nullable Object[] stacktrace) {
Expand Down
26 changes: 11 additions & 15 deletions modules/overrungl.glfw/src/main/java/overrungl/glfw/GLFW.java
Original file line number Diff line number Diff line change
Expand Up @@ -1757,15 +1757,15 @@ public static MemorySegment ngetVideoModes(MemorySegment monitor, MemorySegment
* <a href="https://www.glfw.org/docs/latest/intro_guide.html#error_handling">error</a> occurred.
* @see #ngetVideoModes(MemorySegment, MemorySegment) ngetVideoModes
*/
public static @Nullable GLFWVidMode.Buffer getVideoModes(MemorySegment monitor) {
public static @Nullable GLFWVidMode getVideoModes(MemorySegment monitor) {
var stack = MemoryStack.stackGet();
long stackPointer = stack.getPointer();
try {
var pCount = stack.callocInt();
var pModes = ngetVideoModes(monitor, pCount);
return RuntimeHelper.isNullptr(pModes) ?
null :
new GLFWVidMode.Buffer(pModes, pCount.get(JAVA_INT, 0));
new GLFWVidMode(pModes, pCount.get(JAVA_INT, 0));
} finally {
stack.setPointer(stackPointer);
}
Expand Down Expand Up @@ -1931,7 +1931,7 @@ public static void nsetGammaRamp(MemorySegment monitor, MemorySegment ramp) {
* @see #nsetGammaRamp(MemorySegment, MemorySegment) nsetGammaRamp
*/
public static void setGammaRamp(MemorySegment monitor, GLFWGammaRamp ramp) {
nsetGammaRamp(monitor, ramp.address());
nsetGammaRamp(monitor, ramp.segment());
}

/**
Expand Down Expand Up @@ -2395,11 +2395,11 @@ public static void nsetWindowIcon(MemorySegment window, int count, MemorySegment
* count is zero.
* @see #nsetWindowIcon(MemorySegment, int, MemorySegment) nsetWindowIcon
*/
public static void setWindowIcon(MemorySegment window, int count, GLFWImage.Buffer images) {
public static void setWindowIcon(MemorySegment window, int count, GLFWImage images) {
if (images == null || count == 0) {
nsetWindowIcon(window, 0, MemorySegment.NULL);
} else {
nsetWindowIcon(window, count, images.address());
nsetWindowIcon(window, count, images.segment());
}
}

Expand All @@ -2411,12 +2411,8 @@ public static void setWindowIcon(MemorySegment window, int count, GLFWImage.Buff
* revert to the default window icon.
* @see #nsetWindowIcon(MemorySegment, int, MemorySegment) nsetWindowIcon
*/
public static void setWindowIcon(MemorySegment window, @Nullable GLFWImage.Buffer images) {
if (images == null) {
nsetWindowIcon(window, 0, MemorySegment.NULL);
} else {
setWindowIcon(window, (int) Math.min(images.elementCount(), Integer.MAX_VALUE), images);
}
public static void setWindowIcon(MemorySegment window, @Nullable GLFWImage images) {
setWindowIcon(window, images == null ? 0 : Math.toIntExact(images.elementCount()), images);
}

/**
Expand Down Expand Up @@ -4401,7 +4397,7 @@ public static MemorySegment ncreateCursor(MemorySegment image, int xhot, int yho
* @see #ncreateCursor(MemorySegment, int, int) ncreateCursor
*/
public static MemorySegment createCursor(GLFWImage image, int xhot, int yhot) {
return ncreateCursor(image.address(), xhot, yhot);
return ncreateCursor(image.segment(), xhot, yhot);
}

/**
Expand Down Expand Up @@ -5384,7 +5380,7 @@ public static boolean ngetGamepadState(int jid, MemorySegment state) {
* @see #ngetGamepadState(int, MemorySegment) ngetGamepadState
*/
public static boolean getGamepadState(int jid, GLFWGamepadState state) {
return ngetGamepadState(jid, state.address());
return ngetGamepadState(jid, state.segment());
}

/**
Expand Down Expand Up @@ -5767,7 +5763,7 @@ public static boolean extensionSupported(String extension) {
* without a current context will cause a {@link #NO_CURRENT_CONTEXT} error.
* <p>
* This function does not apply to Vulkan. If you are rendering with Vulkan,
* see {@link GLFWVulkan#nglfwGetInstanceProcAddress(MemorySegment, MemorySegment) glfwGetInstanceProcAddress},
* see {@link GLFWVulkan#ngetInstanceProcAddress(MemorySegment, MemorySegment) glfwGetInstanceProcAddress},
* {@code vkGetInstanceProcAddr} and {@code vkGetDeviceProcAddr} instead.
*
* @param procName The ASCII encoded name of the function.
Expand Down Expand Up @@ -5823,7 +5819,7 @@ public static MemorySegment getProcAddress(String procName) {
* surface creation or even instance creation is possible. Call
* {@link #ngetRequiredInstanceExtensions getRequiredInstanceExtensions} to check whether the
* extensions necessary for Vulkan surface creation are available and
* {@link GLFWVulkan#glfwGetPhysicalDevicePresentationSupport glfwGetPhysicalDevicePresentationSupport}
* {@link GLFWVulkan#getPhysicalDevicePresentationSupport glfwGetPhysicalDevicePresentationSupport}
* to check whether a queue family of a physical device supports image presentation.
*
* @return {@code true} if Vulkan is minimally available, or {@code false}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2022-2023 Overrun Organization
* Copyright (c) 2022-2024 Overrun Organization
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -16,81 +16,78 @@

package overrungl.glfw;

import overrungl.Struct;
import overrun.marshal.struct.Struct;

import java.lang.foreign.*;
import java.lang.foreign.MemoryLayout.PathElement;
import java.lang.invoke.VarHandle;

/**
* This describes the input state of a gamepad.
* <h2>Layout</h2>
* <pre><code>
* struct GLFWgamepadstate {
* unsigned char {@link #buttons() buttons}[15];
* float {@link #axes() axes}[6];
* unsigned char {@link #buttons}[15];
* float {@link #axes}[6];
* }</code></pre>
*
* @author squid233
* @since 0.1.0
*/
public final class GLFWGamepadState extends Struct {
/**
* The struct member layout.
*/
public static final SequenceLayout
BUTTONS_LAYOUT = MemoryLayout.sequenceLayout(15, ValueLayout.JAVA_BYTE).withName("buttons"),
AXES_LAYOUT = MemoryLayout.sequenceLayout(6, ValueLayout.JAVA_FLOAT).withName("axes");
/**
* The struct layout.
*/
public static final StructLayout LAYOUT = MemoryLayout.structLayout(
BUTTONS_LAYOUT,
MemoryLayout.paddingLayout(8), // padding needed. will FFM API adds padding automatically in the future?
AXES_LAYOUT
MemoryLayout.sequenceLayout(15, ValueLayout.JAVA_BYTE).withName("buttons"),
MemoryLayout.paddingLayout(1L),
MemoryLayout.sequenceLayout(6, ValueLayout.JAVA_FLOAT).withName("axes")
);
private static final VarHandle
pButtons = LAYOUT.varHandle(PathElement.groupElement("buttons"), PathElement.sequenceElement()),
pAxes = LAYOUT.varHandle(PathElement.groupElement("axes"), PathElement.sequenceElement());
/**
* 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 final StructHandleSizedByteArray buttons = StructHandleSizedByteArray.of(this, "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 final StructHandleSizedFloatArray axes = StructHandleSizedFloatArray.of(this, "axes");

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

/**
* {@return the elements size of this struct in bytes}
* Allocates a struct with the given layout.
*
* @param allocator the allocator
* @param elementCount the element count
*/
public static long sizeof() {
return LAYOUT.byteSize();
public GLFWGamepadState(SegmentAllocator allocator, long elementCount) {
super(allocator, elementCount, LAYOUT);
}

/**
* Creates a {@code GLFWgamepadstate} instance with the given allocator.
* Creates a struct with the given layout.
*
* @param allocator the allocator
* @return the instance
* @param segment the segment
*/
public static GLFWGamepadState create(SegmentAllocator allocator) {
return new GLFWGamepadState(allocator.allocate(LAYOUT));
public GLFWGamepadState(MemorySegment segment) {
super(segment, LAYOUT);
}

/**
* Gets the button state array.
* Allocates a struct with the given layout.
*
* @return The states of each <a href="https://www.glfw.org/docs/latest/group__gamepad__buttons.html">gamepad button</a>,
* {@code PRESS} or {@code RELEASE}.
* @param allocator the allocator
*/
public byte[] buttons() {
byte[] arr = new byte[15];
for (int i = 0; i < arr.length; i++) {
arr[i] = (byte) pButtons.get(segment(), (long) i);
}
return arr;
public GLFWGamepadState(SegmentAllocator allocator) {
super(allocator, LAYOUT);
}

/**
Expand All @@ -100,21 +97,7 @@ public byte[] buttons() {
* @return the state, {@code PRESS} or {@code RELEASE}
*/
public boolean button(int index) {
return (byte) pButtons.get(segment(), (long) index) == GLFW.PRESS;
}

/**
* Gets the axe state array.
*
* @return 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 float[] axes() {
float[] arr = new float[6];
for (int i = 0; i < arr.length; i++) {
arr[i] = (float) pAxes.get(segment(), (long) i);
}
return arr;
return buttons.get(index) == GLFW.PRESS;
}

/**
Expand All @@ -124,6 +107,6 @@ public float[] axes() {
* @return the state, in the range -1.0 to 1.0 inclusive
*/
public float axe(int index) {
return (float) pAxes.get(segment(), (long) index);
return axes.get(index);
}
}
Loading

0 comments on commit acbd0bc

Please sign in to comment.