/* Dessins et clavier CC BY-SA Edouard.Thiel@univ-amu.fr - 24/12/2024 */ #include #include #include struct Categ { const char *name; int nature; }; Categ categs[] = { { "GL_POINTS", GL_POINTS }, { "GL_LINES", GL_LINES }, { "GL_LINE_STRIP", GL_LINE_STRIP }, { "GL_LINE_LOOP", GL_LINE_LOOP }, { "GL_POLYGON", GL_POLYGON }, { "GL_TRIANGLES", GL_TRIANGLES }, { "GL_QUADS", GL_QUADS }, { "GL_TRIANGLE_STRIP", GL_TRIANGLE_STRIP }, { "GL_QUAD_STRIP", GL_QUAD_STRIP }, { "GL_TRIANGLE_FAN", GL_TRIANGLE_FAN }, }; const int CATEG_SIZE = (sizeof(categs)/sizeof(Categ)); class MyApp { bool m_ok = false; GLFWwindow* m_window = nullptr; int m_categ_num = 1; void displayGL() { //std::cout << __func__ << std::endl; glClear(GL_COLOR_BUFFER_BIT); glBegin(categs[m_categ_num].nature); glColor3d (0.9, 0.6, 0.8); glVertex2d (-0.4,-0.4); glVertex2d (0.4,-0.4); glVertex2d (-0.4,-0.2); glVertex2d (0.4,0.3); glVertex2d (-0.5,0.4); glVertex2d (0.3,0.7); glEnd(); } 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_categ_num++; if (that->m_categ_num >= CATEG_SIZE) that->m_categ_num = 0; std::cout << categs[that->m_categ_num].name << std::endl; 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, "Dessins et clavier", 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(); } } ~MyApp() { if (m_window) glfwDestroyWindow (m_window); glfwTerminate(); } }; // MyApp int main() { MyApp app; app.run(); }