Callback functions in GLFW

本文详细介绍了GLFW 3.2中的多种回调函数,包括错误处理、显示器管理、窗口操作及输入事件等,为读者提供了丰富的示例代码,有助于更好地理解和使用这些函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Callback functions in GLFW


These are almost all the callback functions I read in the documentation of GLFW 3.2, excluding the callback functions about the Joystick input.
The subtitle indicates which guide the callback functions come from.


refer to the documentation, if you do not know how to correctly use these callback functions. About some Joystic input, I haven’t read it this time.


Introduction to the API

glfwSetErrorCallback(error_callback);
void error_callback(int error, const char* description)
{
    puts(description);
}

Monitor guide

glfwSetMonitorCallback(monitor_callback);
void monitor_callback(GLFWmonitor* monitor, int event)
{
    if (event == GLFW_CONNECTED)
    {
        // The monitor was connected
    }
    else if (event == GLFW_DISCONNECTED)
    {
        // The monitor was disconnected
    }
}

Window guide

glfwSetWindowCloseCallback(window, window_close_callback);
void window_close_callback(GLFWwindow* window)
{
    if (!time_to_close)
        glfwSetWindowShouldClose(window, GLFW_FALSE);
}

glfwSetWindowSizeCallback(window, window_size_callback);
void window_size_callback(GLFWwindow* window, int width, int height)
{
}

glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    glViewport(0, 0, width, height);
}

glfwSetWindowPosCallback(window, window_pos_callback);
void window_pos_callback(GLFWwindow* window, int xpos, int ypos)
{
}

glfwSetWindowIconifyCallback(window, window_iconify_callback);
void window_iconify_callback(GLFWwindow* window, int iconified)
{
    if (iconified)
    {
        // The window was iconified
    }
    else
    {
        // The window was restored
    }
}

glfwSetWindowFocusCallback(window, window_focus_callback);
void window_focus_callback(GLFWwindow* window, int focused)
{
    if (focused)
    {
        // The window gained input focus
    }
    else
    {
        // The window lost input focus
    }
}

glfwSetWindowRefreshCallback(m_handle, window_refresh_callback);
void window_refresh_callback(GLFWwindow* window)
{
    draw_editor_ui(window);
    glfwSwapBuffers(window);
}

Input guide

glfwSetKeyCallback(window, key_callback);
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (key == GLFW_KEY_E && action == GLFW_PRESS)
        activate_airship();
}

glfwSetCharCallback(window, character_callback);
void character_callback(GLFWwindow* window, unsigned int codepoint)
{
}

glfwSetCharModsCallback(window, charmods_callback);
void charmods_callback(GLFWwindow* window, unsigned int codepoint, int mods)
{
}

glfwSetCursorPosCallback(window, cursor_pos_callback);
static void cursor_position_callback(GLFWwindow* window, double xpos, double ypos)
{
}

glfwSetCursorEnterCallback(window, cursor_enter_callback);
void cursor_enter_callback(GLFWwindow* window, int entered)
{
    if (entered)
    {
        // The cursor entered the client area of the window
    }
    else
    {
        // The cursor left the client area of the window
    }
}

glfwSetMouseButtonCallback(window, mouse_button_callback);
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
    if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS)
        popup_menu();
}

glfwSetScrollCallback(window, scroll_callback);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset)
{
}

glfwSetDropCallback(window, drop_callback);
void drop_callback(GLFWwindow* window, int count, const char** paths)
{
    int i;
    for (i = 0;  i < count;  i++)
        handle_dropped_file(paths[i]);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值