/* Reshape window CC BY-SA Edouard.Thiel@univ-amu.fr - 24/12/2024 */ #include #include class MyApp { bool m_ok = false; GLFWwindow* m_window = nullptr; void displayGL() { std::cout << __func__ << std::endl; glClear (GL_COLOR_BUFFER_BIT); glRectd (-0.5, -0.5, 0.5, 0.5); } static void on_reshape_func (GLFWwindow* window, int width, int height) { std::cout << __func__ << " " << width << " " << height << std::endl; MyApp* that = static_cast(glfwGetWindowUserPointer (window)); std::cout << "Test userPointer: " << (window == that->m_window ? "ok" : "bad") << std::endl; glViewport(0, 0, width, height); } static void on_error_func (int error, const char* description) { std::cerr << "Error: " << description << std::endl; } public: MyApp() { if (!glfwInit()) { std::cerr << "GLFW: initialization failed" << std::endl; return; } glfwSetErrorCallback (on_error_func); m_window = glfwCreateWindow (640, 480, "Reshape window", NULL, NULL); if (!m_window) { std::cerr << "GLFW: window creation failed" << std::endl; return; } // Les callbacks pour GLFW étant statiques, on mémorise l'instance glfwSetWindowUserPointer (m_window, this); glfwSetWindowSizeCallback (m_window, on_reshape_func); glfwMakeContextCurrent (m_window); m_ok = true; } void run() { while (m_ok && !glfwWindowShouldClose (m_window)) { displayGL(); glfwSwapBuffers (m_window); glfwWaitEvents(); } } ~MyApp() { if (m_window) glfwDestroyWindow (m_window); glfwTerminate(); } }; // MyApp int main() { MyApp app; app.run(); }