Skip to content

Commit

Permalink
a bit more work on GLFW
Browse files Browse the repository at this point in the history
Rethink:
- AttributeList needs a "update" feature
-- maybe have two AttributeLists:
-- one being created by the Creator be StateAttributeList
-- one ModifyAttributeList, gotten from StateAttributeList#modify() and then apply()-ed later, triggering updates
  • Loading branch information
Firestar99 committed Jan 20, 2018
1 parent 64a7a12 commit 8fcc91c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 51 deletions.
30 changes: 16 additions & 14 deletions src/space/engine/render/window/WindowFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,26 @@ public class WindowFormat {
public static final AttributeListCreator ATTRIBUTE_LIST_CREATOR = new AttributeListCreator();

//main window settings
public static final IKey<Integer> WINDOW_WIDTH = ATTRIBUTE_LIST_CREATOR.generateKey(800); //done
public static final IKey<Integer> WINDOW_HEIGHT = ATTRIBUTE_LIST_CREATOR.generateKey(600); //done
public static final IKey<WindowMode> WINDOW_MODE = ATTRIBUTE_LIST_CREATOR.generateKey(WINDOWED); //done
public static final IKey<Integer> WINDOW_POS_X = ATTRIBUTE_LIST_CREATOR.generateKey();
public static final IKey<Integer> WINDOW_POS_Y = ATTRIBUTE_LIST_CREATOR.generateKey();
public static final IKey<Integer> WINDOW_WIDTH = ATTRIBUTE_LIST_CREATOR.generateKey(800);
public static final IKey<Integer> WINDOW_HEIGHT = ATTRIBUTE_LIST_CREATOR.generateKey(600);
public static final IKey<WindowMode> WINDOW_MODE = ATTRIBUTE_LIST_CREATOR.generateKey(WINDOWED);

//additional window settings
public static final IKey<Boolean> VISIBLE = ATTRIBUTE_LIST_CREATOR.generateKey(TRUE); //done
public static final IKey<String> MONITOR = ATTRIBUTE_LIST_CREATOR.generateKey(); //done
public static final IKey<String> TITLE = ATTRIBUTE_LIST_CREATOR.generateKey("Untitled Window"); //done
public static final IKey<Boolean> RESIZEABLE = ATTRIBUTE_LIST_CREATOR.generateKey(FALSE); //done
public static final IKey<Boolean> DOUBLEBUFFER = ATTRIBUTE_LIST_CREATOR.generateKey(TRUE); //done
public static final IKey<Boolean> VISIBLE = ATTRIBUTE_LIST_CREATOR.generateKey(TRUE);
public static final IKey<String> MONITOR = ATTRIBUTE_LIST_CREATOR.generateKey();
public static final IKey<String> TITLE = ATTRIBUTE_LIST_CREATOR.generateKey("Untitled Window");
public static final IKey<Boolean> RESIZEABLE = ATTRIBUTE_LIST_CREATOR.generateKey(FALSE);
public static final IKey<Boolean> DOUBLEBUFFER = ATTRIBUTE_LIST_CREATOR.generateKey(TRUE);

//gl window settings
public static final IKey<GLApiType> GL_API_TYPE = ATTRIBUTE_LIST_CREATOR.generateKey(GL); //done
public static final IKey<GLProfile> GL_PROFILE = ATTRIBUTE_LIST_CREATOR.generateKey(PROFILE_ANY); //done
public static final IKey<Integer> GL_VERSION_MAJOR = ATTRIBUTE_LIST_CREATOR.generateKey(2); //done
public static final IKey<Integer> GL_VERSION_MINOR = ATTRIBUTE_LIST_CREATOR.generateKey(1); //done
public static final IKey<Boolean> GL_FORWARD_COMPATIBLE = ATTRIBUTE_LIST_CREATOR.generateKey(TRUE); //done
public static final IKey<IWindow> GL_CONTEXT_SHARE = ATTRIBUTE_LIST_CREATOR.generateKey(); //done
public static final IKey<GLApiType> GL_API_TYPE = ATTRIBUTE_LIST_CREATOR.generateKey(GL);
public static final IKey<GLProfile> GL_PROFILE = ATTRIBUTE_LIST_CREATOR.generateKey(PROFILE_ANY);
public static final IKey<Integer> GL_VERSION_MAJOR = ATTRIBUTE_LIST_CREATOR.generateKey(2);
public static final IKey<Integer> GL_VERSION_MINOR = ATTRIBUTE_LIST_CREATOR.generateKey(1);
public static final IKey<Boolean> GL_FORWARD_COMPATIBLE = ATTRIBUTE_LIST_CREATOR.generateKey(TRUE);
public static final IKey<IWindow> GL_CONTEXT_SHARE = ATTRIBUTE_LIST_CREATOR.generateKey();

//fbo
public static final IKey<Integer> FBO_R = ATTRIBUTE_LIST_CREATOR.generateKey(8);
Expand Down
90 changes: 53 additions & 37 deletions src/space/engine/render/window/glfw/GLFWWindowFramework.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,39 +59,56 @@ public class GLFWWindow implements IWindow {

public GLFWWindow(IAttributeList format) {
synchronized (GLFW_SYNC) {
glfwWindowHint(GLFW_VISIBLE, curr.pull(format, VISIBLE) ? GLFW_TRUE : GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, curr.pull(format, RESIZEABLE) ? GLFW_TRUE : GLFW_FALSE);
glfwWindowHint(GLFW_DOUBLEBUFFER, curr.pull(format, DOUBLEBUFFER) ? GLFW_TRUE : GLFW_FALSE);
glfwWindowHint(GLFW_VISIBLE, format.push(curr, VISIBLE) ? GLFW_TRUE : GLFW_FALSE);
glfwWindowHint(GLFW_RESIZABLE, format.push(curr, RESIZEABLE) ? GLFW_TRUE : GLFW_FALSE);
glfwWindowHint(GLFW_DOUBLEBUFFER, format.push(curr, DOUBLEBUFFER) ? GLFW_TRUE : GLFW_FALSE);

//GLApi
glfwWindowHint(GLFW_CLIENT_API, covertGLApiTypeToGLFWApi(curr.pull(format, GL_API_TYPE)));
glfwWindowHint(GLFW_OPENGL_PROFILE, covertGLProfileToGLFWProfile(curr.pull(format, GL_PROFILE)));
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, curr.pull(format, GL_VERSION_MAJOR));
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, curr.pull(format, GL_VERSION_MINOR));
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, curr.pull(format, GL_FORWARD_COMPATIBLE) ? GLFW_TRUE : GLFW_FALSE);
glfwWindowHint(GLFW_CLIENT_API, covertGLApiTypeToGLFWApi(format.push(curr, GL_API_TYPE)));
glfwWindowHint(GLFW_OPENGL_PROFILE, covertGLProfileToGLFWProfile(format.push(curr, GL_PROFILE)));
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, format.push(curr, GL_VERSION_MAJOR));
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, format.push(curr, GL_VERSION_MINOR));
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, format.push(curr, GL_FORWARD_COMPATIBLE) ? GLFW_TRUE : GLFW_FALSE);

//FBO
glfwWindowHint(GLFW_RED_BITS, format.push(curr, FBO_R));
glfwWindowHint(GLFW_GREEN_BITS, format.push(curr, FBO_G));
glfwWindowHint(GLFW_BLUE_BITS, format.push(curr, FBO_B));
glfwWindowHint(GLFW_ALPHA_BITS, format.push(curr, FBO_A));
glfwWindowHint(GLFW_DEPTH_BITS, format.push(curr, FBO_DEPTH));
glfwWindowHint(GLFW_STENCIL_BITS, format.push(curr, FBO_STENCIL));

//windowMode
long monitorPointer;
WindowMode windowMode = curr.pull(format, WINDOW_MODE);
WindowMode windowMode = format.push(curr, WINDOW_MODE);
if (windowMode == FULLSCREEN) {
glfwWindowHint(GLFW_DECORATED, GLFW_TRUE);
monitorPointer = getMonitorPointer(curr.pull(format, MONITOR));
monitorPointer = getMonitorPointer(format.push(curr, MONITOR));
} else {
glfwWindowHint(GLFW_DECORATED, windowMode == BORDERLESS ? GLFW_FALSE : GLFW_TRUE);
curr.reset(MONITOR);
monitorPointer = 0;
}

//create
windowPointer = glfwCreateWindow(curr.pull(format, WINDOW_WIDTH), curr.pull(format, WINDOW_HEIGHT), curr.pull(format, TITLE), monitorPointer, getWindowSharePointer(curr.get(GL_CONTEXT_SHARE)));
windowPointer = glfwCreateWindow(format.push(curr, WINDOW_WIDTH), format.push(curr, WINDOW_HEIGHT), format.push(curr, TITLE), monitorPointer, getWindowSharePointer(curr.get(GL_CONTEXT_SHARE)));
}
}

@Override
public void update(IAttributeList format) {

if (format.anyDifference(curr, VISIBLE))
if (format.push(curr, VISIBLE)) {
glfwShowWindow(windowPointer);
} else {
glfwHideWindow(windowPointer);
}
if (format.anyDifference(curr, RESIZEABLE))
glfwSetWindowAttrib(windowPointer, GLFW_RESIZABLE, format.push(curr, RESIZEABLE) ? GLFW_TRUE : GLFW_FALSE);
if (format.anyDifference(curr, DOUBLEBUFFER))
glfwSetWindowAttrib(windowPointer, GLFW_DOUBLEBUFFER, format.push(curr, DOUBLEBUFFER) ? GLFW_TRUE : GLFW_FALSE);

// glfwSetWindowMon
}

@Override
Expand Down Expand Up @@ -141,36 +158,35 @@ protected static int covertGLProfileToGLFWProfile(GLProfile type) {
}

protected static long getMonitorPointer(String monitorName) {
if (monitorName != null && !monitorName.isEmpty()) {
BufferAllocatorStack allocStack = Side.getSide().get(Side.BUFFER_STACK_ALLOC);
try {
allocStack.push();
Buffer sizeBuffer = allocStack.malloc(8);
long dest = nglfwGetMonitors(sizeBuffer.address());
long size = sizeBuffer.getLong(0);
Buffer list = allocStack.alloc(dest, size);

for (long i = 0; i < size; i += 8) {
long monitorPointer = list.getLong(i);
if (monitorName.equals(glfwGetMonitorName(monitorPointer)))
return monitorPointer;
}

throw new IllegalArgumentException("Monitor named '" + monitorName + "' not found!");
} finally {
allocStack.pop();
if (monitorName == null || monitorName.isEmpty())
return glfwGetPrimaryMonitor();

BufferAllocatorStack allocStack = Side.getSide().get(Side.BUFFER_STACK_ALLOC);
try {
allocStack.push();
Buffer sizeBuffer = allocStack.malloc(8);
long dest = nglfwGetMonitors(sizeBuffer.address());
long size = sizeBuffer.getLong(0);
Buffer list = allocStack.alloc(dest, size);

for (long i = 0; i < size; i += 8) {
long monitorPointer = list.getLong(i);
if (monitorName.equals(glfwGetMonitorName(monitorPointer)))
return monitorPointer;
}
throw new IllegalArgumentException("Monitor named '" + monitorName + "' not found!");
} finally {
allocStack.pop();
}
return glfwGetPrimaryMonitor();
}

protected static long getWindowSharePointer(IWindow windowShare) {
if (windowShare != null) {
if (!(windowShare instanceof GLFWWindow))
throw new IllegalArgumentException("GL_CONTEXT_SHARE was not of type GLFWWindow, instead was " + windowShare.getClass().getName());
return ((GLFWWindow) windowShare).windowPointer;
}
return 0;
if (windowShare == null)
return 0;

if (!(windowShare instanceof GLFWWindow))
throw new IllegalArgumentException("GL_CONTEXT_SHARE was not of type GLFWWindow, instead was " + windowShare.getClass().getName());
return ((GLFWWindow) windowShare).windowPointer;
}

//free
Expand Down

0 comments on commit 8fcc91c

Please sign in to comment.