Skip to content

Instantly share code, notes, and snippets.

@faithandbrave
Created January 4, 2014 15:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faithandbrave/8256388 to your computer and use it in GitHub Desktop.
Save faithandbrave/8256388 to your computer and use it in GitHub Desktop.
class MyWindow { // AppじゃなくMyWindowなのがポイント
GLFWWindow* raw_window_;
public:
MyWindow()
{
raw_window_ = glfwCreateWindow(960, 640,
PREPRO_TO_STR(PRODUCT_NAME), nullptr, nullptr);
glfwSetWindowUserPointer(raw_window_, this);
glfwSetMouseButtonCallback(raw_window_, mouseButtonCallbackImpl);
}
private:
static void mouseButtonCallbackImpl(GLFWWindow* raw_window)
{
MyWindow* window =
reinterpret_cast<MyWindow*>(
glfwGetWindowUserPointer(raw_window)
);
// 静的メンバ関数から非静的メンバ関数を呼び出す
window->mouseButtonCallback();
}
void mouseButtonCallback() {}
};
@faithandbrave
Copy link
Author

GLFW 3.0で入った、User Pointerの用途。
ウィンドウのコールバック関数内で、GLFWWindow*のほかに、ユーザー定義のウィンドウクラスのインスタンスを使用するためにある。(C++のためのC API)

ウィンドウ固有のコールバック関数を登録するたびに、毎回thisを設定させるのがイヤだったのだろう。
GLFW 3.0でマルチウィンドウサポートが入ったので、そのためにウィンドウごとに値を持たせる仕組みが必要だったのだろう。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment