Sign in to follow this  
Dark Engineer

How to use glfwSetKeyCallback?

Recommended Posts

I have this function

bool Camera::onKeyboard( int Key )
{
	bool Ret = false;

	switch (Key)
	{
	case GLFW_KEY_UP:
		{
			m_pos += (m_target * STEP_SCALE );
			Ret = true;
		} break;
	case GLFW_KEY_DOWN:
		{
			m_pos -= (m_target * STEP_SCALE );
			Ret = true;
		} break;
	case GLFW_KEY_LEFT:
		{
			glm::vec3 left = glm::cross(m_target, m_up );
			glm::normalize(left);
			left *= STEP_SCALE;
			m_pos += left;
			Ret = true;
		} break;
	case GLFW_KEY_RIGHT:
		{
			glm::vec3 right = glm::cross(m_up, m_target);
			glm::normalize(right);
			right *= STEP_SCALE;
			m_pos += right;
			Ret = true;
		} break;
	}
	return Ret;
}

Adn when i try to make function and run it

void GLFWCALL Camera::keyWrapper(int Key)
{
	onKeyboard(Key);
}
glfwSetKeyCallback(pGameCamera->keyWrapper);

it doesn't work. Can someone help me how to do it correct?

Share this post


Link to post
Share on other sites

First, to register the callback you need to make the function either a static member function, or a non-member function. Let's make it a static member:

class Camera {
    ...
    bool onKeyboard(int);
 
    static bool keyWrapper(int);
};

The static member needs to wrap the static member or non-member function call to a member function call with some instance of the camera class. Let's assume you have a global pointer to your camera. The wrapper function calls the keyboard handler on the global instance of the camera:

Camera *cameraInstance = nullptr;
 
bool Camera::keyWrapper(int key)
{
    return cameraInstance->onKeyboard(key);
}

Then set the camera pointer to your camera object and register the wrapper function.

Camera myCamera = ...;
ameraInstance = &myCamera;
 
glfwSetKeyCallback(Camera::keyWrapper);

Since GLFW doesn't handle member functions, you need to wrap the member function call in a static or non-member function call, and handle the object from the outside.

Share this post


Link to post
Share on other sites

This

		glfwSetKeyCallback(Camera::keyWrapper);

 

don't work

error C2664: 'glfwSetKeyCallback' : cannot convert parameter 1 from 'bool (__cdecl *)(int)' to 'GLFWkeyfun'
1>          None of the functions with this name in scope match the target type

Share this post


Link to post
Share on other sites

I assumed your code had the correct parameters for the callback, but apparently it takes two parameters; the key and the state of the key. Just change the wrapper callback to

static bool keyWrapper(int key, int action);

and adjust the rest of the code accordingly.

Share this post


Link to post
Share on other sites

It doesn't show errors but if i want to put this cameraInstance to the Camera class and my class is used by one another class then shows

1>main.obj : error LNK2005: "class Camera * cameraInstance" (?cameraInstance@@3PAVCamera@@A) already defined in Camera.obj
1>Pipeline.obj : error LNK2005: "class Camera * cameraInstance" (?cameraInstance@@3PAVCamera@@A) already defined in Camera.obj
1> fatal error LNK1169: one or more multiply defined symbols found
Edited by Dark Engineer

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this