Search in sources :

Example 1 with Callback

use of org.lwjgl.system.Callback in project lwjgl3-demos by LWJGL.

the class SwtAndGlfwDemo method main.

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();
    errorCallback.free();
}
Also used : GLFWKeyCallback(org.lwjgl.glfw.GLFWKeyCallback) GLFWErrorCallback(org.lwjgl.glfw.GLFWErrorCallback) Listener(org.eclipse.swt.widgets.Listener) FillLayout(org.eclipse.swt.layout.FillLayout) GLCanvas(org.eclipse.swt.opengl.GLCanvas) Shell(org.eclipse.swt.widgets.Shell) GLCapabilities(org.lwjgl.opengl.GLCapabilities) GLFWErrorCallback(org.lwjgl.glfw.GLFWErrorCallback) Callback(org.lwjgl.system.Callback) GLFWKeyCallback(org.lwjgl.glfw.GLFWKeyCallback) GLData(org.eclipse.swt.opengl.GLData) Event(org.eclipse.swt.widgets.Event) Display(org.eclipse.swt.widgets.Display)

Aggregations

FillLayout (org.eclipse.swt.layout.FillLayout)1 GLCanvas (org.eclipse.swt.opengl.GLCanvas)1 GLData (org.eclipse.swt.opengl.GLData)1 Display (org.eclipse.swt.widgets.Display)1 Event (org.eclipse.swt.widgets.Event)1 Listener (org.eclipse.swt.widgets.Listener)1 Shell (org.eclipse.swt.widgets.Shell)1 GLFWErrorCallback (org.lwjgl.glfw.GLFWErrorCallback)1 GLFWKeyCallback (org.lwjgl.glfw.GLFWKeyCallback)1 GLCapabilities (org.lwjgl.opengl.GLCapabilities)1 Callback (org.lwjgl.system.Callback)1