// A simple multi-window example using GLFW // This code is based on https://github.com/glfw/glfw/blob/master/examples/windows.c #include #include #include // A function to handle window resizing void window_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); } // A function to handle window closing void window_close_callback(GLFWwindow* window) { printf("Window %p closed\n", window); } // A function to handle key input void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) glfwSetWindowShouldClose(window, GLFW_TRUE); } int main(void) { // Initialize GLFW if (!glfwInit()) { printf("Failed to initialize GLFW\n"); exit(EXIT_FAILURE); } // Create two windows with OpenGL contexts GLFWwindow* window1 = glfwCreateWindow(640, 480, "Window 1", NULL, NULL); if (!window1) { printf("Failed to create window 1\n"); glfwTerminate(); exit(EXIT_FAILURE); } GLFWwindow* window2 = glfwCreateWindow(640, 480, "Window 2", NULL, NULL); if (!window2) { printf("Failed to create window 2\n"); glfwDestroyWindow(window1); glfwTerminate(); exit(EXIT_FAILURE); } // Set the callbacks for each window glfwSetWindowSizeCallback(window1, window_size_callback); glfwSetWindowCloseCallback(window1, window_close_callback); glfwSetKeyCallback(window1, key_callback); glfwSetWindowSizeCallback(window2, window_size_callback); glfwSetWindowCloseCallback(window2, window_close_callback); glfwSetKeyCallback(window2, key_callback); // Set the clear colors for each window const struct { float r, g, b; } colors[] = { { 0.95f, 0.32f, 0.11f }, { 0.50f, 0.80f, 0.16f } }; // Loop until both windows are closed while (!glfwWindowShouldClose(window1) && !glfwWindowShouldClose(window2)) { // Clear and swap buffers for each window for (int i = 0; i < 2; i++) { GLFWwindow* window = (i == 0) ? window1 : window2; glfwMakeContextCurrent(window); glClearColor(colors[i].r, colors[i].g, colors[i].b, 1.f); glClear(GL_COLOR_BUFFER_BIT); glfwSwapBuffers(window); } // Poll for and process events glfwPollEvents(); } // Destroy the windows and terminate GLFW glfwDestroyWindow(window1); glfwDestroyWindow(window2); glfwTerminate(); }