/* Timer et couleur CC BY-SA Edouard.Thiel@univ-amu.fr - 24/12/2024 */ #include #include #include const double FRAMES_PER_SEC = 30.0; const double ANIM_DURATION = 4.0; class MyApp { bool m_ok = false; GLFWwindow* m_window = nullptr; double m_angle = 0; void displayGL() { //std::cout << __func__ << std::endl; glClear (GL_COLOR_BUFFER_BIT); glLoadIdentity(); glRotated (m_angle, 0, 0, 1); // change la couleur en fonction du temps double time = glfwGetTime(); // durée depuis init double slice = time / ANIM_DURATION; double r = slice - std::floor(slice); // partie fractionnaire glColor3d (r, 0.5, 0.2); 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_key_func (GLFWwindow* window, int key, int scancode, int action, int mods) { std::cout << __func__ << " " << key << " " << scancode << " " << action << " " << mods << std::endl; // action = GLFW_PRESS ou GLFW_REPEAT ou GLFW_RELEASE if (action == GLFW_RELEASE) return; MyApp* that = static_cast(glfwGetWindowUserPointer (window)); int trans_key = translate_qwerty_to_azerty (key, scancode); switch (trans_key) { case GLFW_KEY_SPACE : that->m_angle++; break; case GLFW_KEY_ESCAPE : that->m_ok = false; break; } } static int translate_qwerty_to_azerty (int key, int scancode) { // https://www.glfw.org/docs/latest/group__keys.html // QWERTY -> AZERTY switch (key) { case GLFW_KEY_Q : return GLFW_KEY_A; case GLFW_KEY_A : return GLFW_KEY_Q; case GLFW_KEY_W : return GLFW_KEY_Z; case GLFW_KEY_Z : return GLFW_KEY_W; case GLFW_KEY_SEMICOLON : return GLFW_KEY_M; } // Détection des différences non corrigées const char* name = glfwGetKeyName (key, scancode); if (name != NULL) { int capital = toupper(name[0]); if (capital != key) { std::cout << __func__ << " DIFF " << capital << " " << key << std::endl; } } return key; } 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, "Timer et couleur", 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); glfwSetKeyCallback (m_window, on_key_func); glfwMakeContextCurrent (m_window); m_ok = true; } void run() { while (m_ok && !glfwWindowShouldClose (m_window)) { displayGL(); glfwSwapBuffers (m_window); //glfwWaitEvents(); glfwWaitEventsTimeout (1.0/FRAMES_PER_SEC); } } ~MyApp() { if (m_window) glfwDestroyWindow (m_window); glfwTerminate(); } }; // MyApp int main() { MyApp app; app.run(); }