/
SwtAndGlfwDemo.java
122 lines (113 loc) · 4.44 KB
/
SwtAndGlfwDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright LWJGL. All rights reserved.
* License terms: https://www.lwjgl.org/license
*/
package org.lwjgl.demo.opengl.swt;
import static org.lwjgl.opengl.GL.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.opengl.GLCanvas;
import org.eclipse.swt.opengl.GLData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.glfw.GLFWKeyCallback;
import org.lwjgl.opengl.GLCapabilities;
import org.lwjgl.opengl.GLUtil;
import org.lwjgl.system.Callback;
/**
* Shows how to use SWT and GLFW windows side-by-side.
*
* @author Kai Burjack
*/
public class SwtAndGlfwDemo {
public static void main(String[] args) {
// Create SWT window
Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("SWT window");
shell.setLayout(new FillLayout());
shell.addListener(SWT.Traverse, new Listener() {
public void handleEvent(Event event) {
switch (event.detail) {
case SWT.TRAVERSE_ESCAPE:
shell.close();
event.detail = SWT.TRAVERSE_NONE;
event.doit = false;
break;
default:
break;
}
}
});
GLData data = new GLData();
data.doubleBuffer = true;
GLCanvas swtCanvas = new GLCanvas(shell, SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE, data);
int dw = shell.getSize().x - shell.getClientArea().width;
int dh = shell.getSize().y - shell.getClientArea().height;
shell.setSize(800 + dw, 600 + dh);
swtCanvas.setCurrent();
GLCapabilities swtCapabilities = createCapabilities();
Callback swtDebugProc = GLUtil.setupDebugMessageCallback();
shell.open();
// Create GLFW window
GLFWErrorCallback errorCallback;
glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
if (!glfwInit())
throw new IllegalStateException("Unable to initialize GLFW");
glfwDefaultWindowHints();
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);
long glfwWindow = glfwCreateWindow(800, 600, "GLFW window", NULL, NULL);
GLFWKeyCallback keyCallback;
glfwSetKeyCallback(glfwWindow, keyCallback = new GLFWKeyCallback() {
public void invoke(long window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
glfwSetWindowShouldClose(window, true);
}
});
glfwMakeContextCurrent(glfwWindow);
GLCapabilities glfwCapabilities = createCapabilities();
Callback glfwDebugProc = GLUtil.setupDebugMessageCallback();
glfwShowWindow(glfwWindow);
while (!shell.isDisposed() && !glfwWindowShouldClose(glfwWindow)) {
// Process window messages (for both SWT _and_ GLFW)
display.readAndDispatch();
// Render to SWT window
if (!swtCanvas.isDisposed()) {
swtCanvas.setCurrent();
setCapabilities(swtCapabilities);
glClearColor(0.2f, 0.4f, 0.6f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
swtCanvas.swapBuffers();
}
// Render to GLFW window
if (glfwGetWindowAttrib(glfwWindow, GLFW_VISIBLE) == GLFW_TRUE) {
glfwMakeContextCurrent(glfwWindow);
setCapabilities(glfwCapabilities);
glClearColor(0.2f, 0.3f, 0.4f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(glfwWindow);
}
}
// Dispose of SWT
if (swtDebugProc != null)
swtDebugProc.free();
if (!shell.isDisposed())
shell.dispose();
display.dispose();
// Dispose of GLFW
if (glfwDebugProc != null)
glfwDebugProc.free();
keyCallback.free();
glfwDestroyWindow(glfwWindow);
glfwTerminate();
glfwSetErrorCallback(null).free();
}
}