Search code examples
opengldglfw

Derelict3: seg fault on glfwPollEvents


I'm trying to get a basic GLFW example running with Derelict3 on D2. I pulled this example from the D mailing list archives: http://forum.dlang.org/thread/[email protected]

Here's my code so far:

import std.stdio;

import derelict.opengl3.gl;
import derelict.glfw3.glfw3;

pragma(lib, "DerelictGL3");
pragma(lib, "DerelictGLFW3");
pragma(lib, "DerelictUtil");
pragma(lib, "dl");

const int width = 800;
const int height = 600;

void init() {
    glViewport(0,0,width,height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_POLYGON);
        glVertex2d(0,0);
        glVertex2d(0,height);
        glVertex2d(width,height);
        glVertex2d(height,0);
    glEnd();
}

extern (C) {
    void resizeWindow(GLFWwindow window, int w, int h) {
    }

    void refreshWindow(GLFWwindow window) {
        writeln("Refresh");
        display();
    }

    void mouseMove(GLFWwindow window, int x, int y) {
    }

    void mouseClick(GLFWwindow window, int button, int action) {
    }

    int windowClose(GLFWwindow window) {
        //running = false;
        return GL_TRUE;
    }

    void keyTrigger(GLFWwindow window, int key, int action) {
        if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
            //running = false;
        }
    }
}

void main() {
    DerelictGL.load();
    DerelictGLFW3.load();

    if (!glfwInit()) {
        writeln("glfwInit didn't work");
        return;
    }

    auto window = glfwCreateWindow(width,height,GLFW_WINDOWED,"Hello 
DerelictGLFW3",null);
    glfwMakeContextCurrent(window);

    init();

    // register callbacks
    /*
    glfwSetWindowRefreshCallback(window, &refreshWindow);
    glfwSetWindowSizeCallback(window, &resizeWindow);
    glfwSetCursorPosCallback(window, &mouseMove);
    glfwSetMouseButtonCallback(window, &mouseClick);
    glfwSetWindowCloseCallback(window, &windowClose);
    glfwSetKeyCallback(window, &keyTrigger);
    */

    bool opened = true;
    while(opened) {
        display();
        glfwSwapBuffers(window);

        writeln("Before");
        glfwPollEvents();
        writeln("After");
    }
    glfwTerminate();
}

This currently compiles and runs as expected, but as soon as I uncomment all of the callback registrations, I get a segfault just before glfwPollEvents().

I'm not sure what's going on here. I thought it could be a library conflict, but everything else seems to run well.

Building with:

dmd -I$HOME/sandbox/Derelict3/import -L-L$HOME/sandbox/Derelict3/lib -ofgame test.d

Platform: Linux x64 (Fedora 17)

$HOME/sandbox/Derelict3 is a git clone of https://github.com/aldacron/Derelict3

Also, is there's an example of rendering a simple shape using Derelict3 and GLFW?


Solution

  • Your code works fine for me using the latest Derelict3 (commit b133eda) and GLFW3 (26abe0a) from https://github.com/elmindreda/glfw.git

    I'd do these things (in this order):

    1. Ensure your Derelict shared libraries are up to date (run rdmd build.d in the Derelict3/build directory).
    2. Ensure your GLFW3 libs are up-to date (I assume you're building from source, so make sure to pull and rebuild if new commits come in). Don't forget to make install.
    3. If you still experience the problem, try using GDB to get a backtrace (I recommend compiling your test code with -g).

    Not really so simple example (uses programmable pipeline): https://gist.github.com/4090381