On 5/24/14, Vlad Levenfeld via Digitalmars-d-learn
<digitalmars-d-learn@puremagic.com> wrote:
> Any attempt to set callbacks in GLFW returns a null and the
> callback doesn't work.
>
> The first enforcement fails in this example:
>
> DerelictGLFW3.load ();
> enforce (glfwSetErrorCallback (&error_callback));

It's ok if this fails because this is how it's documented:

*  @return The previously set callback, or `NULL` if no callback was set or an
*  error occurred.

Below is a full snippet of using DerelictGLFW and a simple escape key binding:

-----
import derelict.glfw3.glfw3;

import std.conv;
import std.exception;
import std.stdio;

shared static this()
{
    DerelictGLFW3.load();
}

extern(C) void error_callback(int error, const(char)* description) nothrow
{
    printf("%s %s", error, description);
}

int main()
{
    /* Initialize the library */
    enforce(glfwInit());

    glfwSetErrorCallback(&error_callback);

    GLFWwindow* window;

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", null, null);
    scope(exit) glfwTerminate();
    enforce(window !is null);

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    extern(C) void onKeyEvent(GLFWwindow* window, int key, int
scancode, int state, int modifier) nothrow
    {
        if (key == GLFW_KEY_ESCAPE && state == GLFW_PRESS)
            glfwSetWindowShouldClose(window, true);
    }

    glfwSetKeyCallback(window, &onKeyEvent);

    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */

        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    return 0;
}
-----

Reply via email to