www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - glfw callback trouble

reply Hannes <Hannes_member pathlink.com> writes:
I'm having problems with the glfwSetKeyCallback function. It is defined as:
void  glfwSetKeyCallback( GLFWkeyfun cbfun );

GLFWkeyfun is defined as:
typedef void (* GLFWkeyfun)(int, int);

My callback function looks like this:
void keyCallback(int key, int state)

But when I try to set the callback function:
glfwSetKeyCallback(&keyCallback);

I get the following errors:

main.d(99): function glfw.glfwSetKeyCallback (GLFWkeyfun) does not match argumen
t types (void(*)(int state, int key))
main.d(99): cannot implicitly convert expression (#keyCallback) of type void(*)(
int state, int key) to GLFWkeyfun



If I explicitly cast to GLFWkeyfun, the parameters I get are nonsense. I played
a little with it and got it working this way:

void keyCallback(int state, int key, int junk) {...}
glfwSetKeyCallback(cast(GLFWkeyfun)&keyCallback);

So basically I had to switch the parameters and add a third, useless parameter.
I only found this "solution" by accident. Now I'm certain there is a correct way
to do this, this feels like an awful hack.
Oct 18 2005
parent reply Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
Hannes wrote:
 I'm having problems with the glfwSetKeyCallback function. It is defined as:
 void  glfwSetKeyCallback( GLFWkeyfun cbfun );
 
 GLFWkeyfun is defined as:
 typedef void (* GLFWkeyfun)(int, int);
 
 My callback function looks like this:
 void keyCallback(int key, int state)
 
try: extern(Windows) void keyCallback(int key, int state);
 But when I try to set the callback function:
 glfwSetKeyCallback(&keyCallback);
 
 I get the following errors:
 
 main.d(99): function glfw.glfwSetKeyCallback (GLFWkeyfun) does not match
argumen
 t types (void(*)(int state, int key))
 main.d(99): cannot implicitly convert expression (#keyCallback) of type
void(*)(
 int state, int key) to GLFWkeyfun
 
 
I really hate these errors but it is usually a calling convention problem.
 
 If I explicitly cast to GLFWkeyfun, the parameters I get are nonsense. I played
 a little with it and got it working this way:
 
 void keyCallback(int state, int key, int junk) {...}
 glfwSetKeyCallback(cast(GLFWkeyfun)&keyCallback);
 
 So basically I had to switch the parameters and add a third, useless parameter.
 I only found this "solution" by accident. Now I'm certain there is a correct
way
 to do this, this feels like an awful hack.
 
 
Oct 18 2005
parent reply Hannes <Hannes_member pathlink.com> writes:
 My callback function looks like this:
try: extern(Windows) void keyCallback(int key, int state)
This works. Thanks a lot! I wonder, where could I have found this information by myself?
Oct 19 2005
parent reply David Medlock <noone nowhere.com> writes:
Hannes wrote:
My callback function looks like this:
try: extern(Windows) void keyCallback(int key, int state)
This works. Thanks a lot! I wonder, where could I have found this information by myself?
The latest GLFW 2.5 has examples using D. GLFW is a great library. The Lua support in it was started by yours truly. -David
Oct 19 2005
parent Ivan Senji <ivan.senji_REMOVE_ _THIS__gmail.com> writes:
David Medlock wrote:
 Hannes wrote:
 
 My callback function looks like this:
try: extern(Windows) void keyCallback(int key, int state)
This works. Thanks a lot! I wonder, where could I have found this information by myself?
The latest GLFW 2.5 has examples using D.
This is the reason i am using it. :)
 GLFW is a great library. The 
 Lua support in it was started by yours truly.
 
It really is great but the only thing i miss is not being able to get to the HWND of the window.
 -David
Oct 19 2005