Sort methods in GLFW and OpenGL window classes

This commit is contained in:
JeremyStar™ 2024-07-23 17:48:10 +02:00
parent ec3ecc0113
commit 2899ba2e8a
Signed by: JeremyStarTM
GPG key ID: E366BAEF67E4704D
2 changed files with 119 additions and 100 deletions

View file

@ -57,6 +57,7 @@ public abstract class GlfwWindow extends Window {
*/
private long identifierLong;
// ------------------------------------------------ [ Window initialization ] ------------------------------------------------ //
/**
* Creates a new window.
*
@ -100,104 +101,6 @@ public abstract class GlfwWindow extends Window {
*/
protected abstract void initializeGlfwWindow() throws Throwable;
/**
* Updates the window state.
*
* @since v1-alpha2
*/
@Override
public void updateState() {
// Ensure running on the main thread
if (!Miscellaneous.onMainThread())
throw new NotOnMainThreadException();
// Own context
ownContext();
// Set swap interval based on isDisallowTearing setting
glfwSwapInterval(Miscellaneous.getIntegerizedBoolean(GraphicsSubsystemConfiguration.getInstance().isDisallowTearing()));
// Integer arrays, used for 'size' and 'position'
int[] width = new int[0];
int[] height = new int[0];
// Set window size
glfwGetWindowSize(identifierLong, width, height);
super.setSize(new Vec2i(width[0], height[0]));
// Set window position
glfwGetWindowPos(identifierLong, width, height);
super.setPosition(new Vec2i(width[0], height[0]));
// Set window mode
if (Miscellaneous.getBooleanizedInteger(glfwGetWindowAttrib(identifierLong, GLFW_ICONIFIED)))
super.setWindowMode(WindowMode.MINIMIZED);
else if (Miscellaneous.getBooleanizedInteger(glfwGetWindowAttrib(identifierLong, GLFW_MAXIMIZED)))
super.setWindowMode(WindowMode.MAXIMIZED);
else if (Miscellaneous.getBooleanizedInteger(glfwGetWindowAttrib(identifierLong, GLFW_VISIBLE)))
super.setWindowMode(WindowMode.WINDOWED);
else if (Miscellaneous.getBooleanizedInteger(glfwGetWindowAttrib(identifierLong, GLFW_VISIBLE)))
super.setWindowMode(WindowMode.HIDDEN);
// Set booleans
super.setResizable(Miscellaneous.getBooleanizedInteger(glfwGetWindowAttrib(identifierLong, GLFW_RESIZABLE)));
super.setOnTop(Miscellaneous.getBooleanizedInteger(glfwGetWindowAttrib(identifierLong, GLFW_FLOATING)));
super.setTransparent(Miscellaneous.getBooleanizedInteger(glfwGetWindowAttrib(identifierLong, GLFW_TRANSPARENT_FRAMEBUFFER)));
// Make Graphics API update it's state
updateGlfwState();
}
/**
* Updates the state of the GLFW window.
*
* @throws Throwable throwable
* @since v1-alpha2
*/
protected abstract void updateGlfwState();
/** {@inheritDoc} */
@Override
public void render() throws NotOnMainThreadException {
// Ensure running on the main thread
if (!Miscellaneous.onMainThread())
throw new NotOnMainThreadException();
glfwSwapBuffers(identifierLong);
glfwPollEvents();
}
/** {@inheritDoc} */
@Override
public boolean isClosureRequested() {
return glfwWindowShouldClose(identifierLong);
}
/** {@inheritDoc} */
@Override
public boolean isFocused() {
return Tristate.toBoolean(Miscellaneous.getTristatedInteger(glfwGetWindowAttrib(identifierLong, GLFW_FOCUSED)));
}
/** {@inheritDoc} */
public void focus() {
glfwFocusWindow(identifierLong);
}
/** {@inheritDoc} */
public void requestAttention() {
glfwRequestWindowAttention(identifierLong);
}
/**
* Updates the OpenGL context.
*
* @since v1-alpha2
*/
public void ownContext() {
glfwMakeContextCurrent(identifierLong);
}
/**
* (Re-)Creates the GLFW window.
*
@ -236,12 +139,16 @@ public abstract class GlfwWindow extends Window {
identifierLong = identifier;
setIdentifier(String.valueOf(identifier));
// Own context
ownContext();
// Set swap interval based on isDisallowTearing setting
glfwSwapInterval(Miscellaneous.getIntegerizedBoolean(GraphicsSubsystemConfiguration.getInstance().isDisallowTearing()));
// Update the window state
setTitle(getTitle());
setSize(getSize());
setMinimumSize(getMinimumSize());
setMaximumSize(getMaximumSize());
setPosition(getPosition());
setWindowMode(getWindowMode());
}
@ -252,6 +159,116 @@ public abstract class GlfwWindow extends Window {
*/
public abstract void setWindowHints();
// ------------------------------------------------ [ State updates ] ------------------------------------------------ //
/**
* Updates the window state.
*
* @since v1-alpha2
*/
@Override
public void updateState() {
// Ensure running on the main thread
if (!Miscellaneous.onMainThread())
throw new NotOnMainThreadException();
// Own context
ownContext();
// Set swap interval based on isDisallowTearing setting
glfwSwapInterval(Miscellaneous.getIntegerizedBoolean(GraphicsSubsystemConfiguration.getInstance().isDisallowTearing()));
/*
// Initialize IntBuffers, used for 'size' and 'position'
IntBuffer width = MemoryUtil.memAllocInt(1);
IntBuffer height = MemoryUtil.memAllocInt(1);
// Update window size
glfwGetFramebufferSize(identifierLong, width, height);
super.setSize(new Vec2i(width.get(0), height.get(0)));
// Clear IntBuffers
width.clear();
height.clear();
// Update window position
glfwGetWindowPos(identifierLong, width, height);
super.setPosition(new Vec2i(width.get(0), height.get(0)));
*/
// Update window mode
if (Miscellaneous.getBooleanizedInteger(glfwGetWindowAttrib(identifierLong, GLFW_ICONIFIED)))
super.setWindowMode(WindowMode.MINIMIZED);
else if (Miscellaneous.getBooleanizedInteger(glfwGetWindowAttrib(identifierLong, GLFW_MAXIMIZED)))
super.setWindowMode(WindowMode.MAXIMIZED);
else if (Miscellaneous.getBooleanizedInteger(glfwGetWindowAttrib(identifierLong, GLFW_VISIBLE)))
super.setWindowMode(WindowMode.WINDOWED);
else if (Miscellaneous.getBooleanizedInteger(glfwGetWindowAttrib(identifierLong, GLFW_VISIBLE)))
super.setWindowMode(WindowMode.HIDDEN);
// Update booleans
super.setResizable(Miscellaneous.getBooleanizedInteger(glfwGetWindowAttrib(identifierLong, GLFW_RESIZABLE)));
super.setOnTop(Miscellaneous.getBooleanizedInteger(glfwGetWindowAttrib(identifierLong, GLFW_FLOATING)));
super.setTransparent(Miscellaneous.getBooleanizedInteger(glfwGetWindowAttrib(identifierLong, GLFW_TRANSPARENT_FRAMEBUFFER)));
// Make Graphics API update it's state
updateGlfwState();
}
/**
* Updates the state of the GLFW window.
*
* @throws Throwable throwable
* @since v1-alpha2
*/
protected abstract void updateGlfwState();
// ------------------------------------------------ [ Rendering ] ------------------------------------------------ //
/** {@inheritDoc} */
@Override
public void render() throws NotOnMainThreadException {
// Ensure running on the main thread
if (!Miscellaneous.onMainThread())
throw new NotOnMainThreadException();
glfwSwapBuffers(identifierLong);
glfwPollEvents();
}
// ------------------------------------------------ [ GLFW handling ] ------------------------------------------------ //
/**
* Updates the OpenGL context.
*
* @since v1-alpha2
*/
public void ownContext() {
glfwMakeContextCurrent(identifierLong);
}
// ------------------------------------------------ [ Information/Action methods ] ------------------------------------------------ //
/** {@inheritDoc} */
@Override
public boolean isClosureRequested() {
return glfwWindowShouldClose(identifierLong);
}
/** {@inheritDoc} */
@Override
public boolean isFocused() {
return Tristate.toBoolean(Miscellaneous.getTristatedInteger(glfwGetWindowAttrib(identifierLong, GLFW_FOCUSED)));
}
/** {@inheritDoc} */
public void focus() {
glfwFocusWindow(identifierLong);
}
/** {@inheritDoc} */
public void requestAttention() {
glfwRequestWindowAttention(identifierLong);
}
// ------------------------------------------------ [ Setter overrides ] ------------------------------------------------ //
/** {@inheritDoc} */
@Override
public void setName(@NotNull String name) {

View file

@ -38,6 +38,7 @@ import static org.lwjgl.glfw.GLFW.*;
*/
@SuppressWarnings({ "unused" })
public class OpenGlWindow extends GlfwWindow {
// ------------------------------------------------ [ Window initialization ] ------------------------------------------------ //
/**
* Creates a new window.
*
@ -83,6 +84,7 @@ public class OpenGlWindow extends GlfwWindow {
glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
}
// ------------------------------------------------ [ Information/Action methods ] ------------------------------------------------ //
/** {@inheritDoc} */
@NotNull
@Override