2015-03-30 28 views
0

我真的堅持功能:glfwSetCursorPosCallback到另一個類

我的主窗口和主遊戲循環我做的:

// poll for input 
glfwPollEvents(); 

this->controls->handleInput(window, world->getPlayer()); 
glfwSetCursorPosCallback(window, controls->handleMouse); 

我想要做的是有一個班級負責爲控件,並讓這個類也處理鼠標。

我總是得到:

'Controls::handleMouse': function call missing argument list; use '&Controls::handleMouse' to create a pointer to member 

現在,當我試試這個,我得到:

'GLFWcursorposfun glfwSetCursorPosCallback(GLFWwindow *,GLFWcursorposfun)' : cannot convert argument 2 from 'void (__thiscall Controls::*)(GLFWwindow *,double,double)' to 'GLFWcursorposfun' 

不知道我在做什麼錯在這裏,作爲GLFWcursorposfun僅僅是一個帶有GLFWwindow的typedef和兩次雙打。

由於函數是在另一個類我試圖爲它創建一個原型,如:

class Controls { 
    void handleInput(GLFWwindow *window, object *gameObject); 
    void handleMouse(GLFWwindow *window, double mouseXPos, double mouseYPos); 
}; 

,但無濟於事。

編輯:當然,我可以將它設置爲& Controls :: handleMouse如果我使該函數成爲靜態,但我寧願能夠創建多個控件對象與他們操作的不同攝像頭和gameObjects。

另外,如何獲取正確的camera/gameObject數據呢?

+0

你可以看到我的回答這個問題,這在短期使'glfwSetWindowUserPointer'的用途:HTTP://計算器。 com/questions/7676971 /指向某一功能的成員-glass-member-glfw-setkeycallback/28660673#28660673 – N0vember 2015-04-02 01:32:58

回答

1

您不能將類的成員函數作爲函數傳遞。 glfwSetCursorPosCallback它期待一個函數並拋出錯誤,因爲它得到一個成員函數。

換句話說,您希望提供一個全局函數並將其傳遞給glfwSetCursorPosCallback

如果您確實希望控件對象獲取光標位置回調,您可以將控件實例存儲在全局變量中,並將回調傳遞給該實例。事情是這樣的:

static Controls* g_controls; 

void mousePosWrapper(double x, double y) 
{ 
    if (g_controls) 
    { 
     g_controls->handleMouse(x, y); 
    } 
} 

然後當你調用glfwSetCursorPosCallback你可以通過mousePosWrapper功能:

glfwSetCursorPosCallback(window, mousePosWrapper); 
+0

當然你是對的。這是相當不靈活的,所以我不使用回調函數,並決定最好實際並主動地獲取光標位置。 – Sorona 2015-03-31 18:52:36

1

我的解決辦法:不要使用回調製定者。相反,我做到以下幾點:

glfwPollEvents(); 

this->controls->handleInput(window, mainObj); 
this->controls->handleMouse(window, mainObj); 

並在handleMouse我做的:

GLdouble xPos, yPos; 
glfwGetCursorPos(window, &xPos, &yPos);