Thread overview | ||||||
---|---|---|---|---|---|---|
|
May 24, 2014 derelict glfw won't set callbacks | ||||
---|---|---|---|---|
| ||||
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)); enforce (glfwInit (), "glfwInit failed"); window = glfwCreateWindow (screen_dims.x, screen_dims.y, "sup", null, null); glfwMakeContextCurrent (window); glfwSwapInterval (0); If I forget about the callbacks, everything functions normally (I can render, etc). Here is error_callback: static extern (C) void error_callback (int, const (char)* error) nothrow { import std.c.stdio: fprintf, stderr; fprintf (stderr, "error glfw: %s\n", error); } It is a static member function but pulling it out to the module scope changes nothing. It doesn't work when I do it from the main thread or a secondary thread. The load-set_error_callback-glfwInit sequence is recommended everywhere I look up D's GLFW bindings, so apparently this works for other people and I must be missing something. And in the event that this can't be solved, I suppose the thing to do is to either poll for input events manually or try to get that part working in C then call it from D? |
May 24, 2014 Re: derelict glfw won't set callbacks | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vlad Levenfeld | On Saturday, 24 May 2014 at 13:31:46 UTC, Vlad Levenfeld 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));
> enforce (glfwInit (), "glfwInit failed");
> window = glfwCreateWindow (screen_dims.x, screen_dims.y, "sup", null, null);
> glfwMakeContextCurrent (window);
> glfwSwapInterval (0);
>
> If I forget about the callbacks, everything functions normally (I can render, etc).
> Here is error_callback:
>
> static extern (C) void error_callback (int, const (char)* error) nothrow {
> import std.c.stdio: fprintf, stderr;
> fprintf (stderr, "error glfw: %s\n", error);
> }
>
> It is a static member function but pulling it out to the module scope changes nothing.
> It doesn't work when I do it from the main thread or a secondary thread.
>
> The load-set_error_callback-glfwInit sequence is recommended everywhere I look up D's GLFW bindings, so apparently this works for other people and I must be missing something.
>
> And in the event that this can't be solved, I suppose the thing to do is to either poll for input events manually or try to get that part working in C then call it from D?
glfwSetErrorCallback returns the previous callback, so it makes sense null is returned on the first call.
|
May 24, 2014 Re: derelict glfw won't set callbacks | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vlad Levenfeld | 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; } ----- |
May 24, 2014 Re: derelict glfw won't set callbacks | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | Haha, I should have read more carefully. Must have looked at that line in the doc a dozed times and never picked up on that detail. Your example worked for me - I wasn't calling "glfwPollEvents." Thanks! |
Copyright © 1999-2021 by the D Language Foundation