windows.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //========================================================================
  2. // Simple multi-window test
  3. // Copyright (c) Camilla Löwy <elmindreda@glfw.org>
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would
  16. // be appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not
  19. // be misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. //
  24. //========================================================================
  25. //
  26. // This test creates four windows and clears each in a different color
  27. //
  28. //========================================================================
  29. #include <glad/gl.h>
  30. #define GLFW_INCLUDE_NONE
  31. #include <GLFW/glfw3.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include "getopt.h"
  35. static const char* titles[] =
  36. {
  37. "Red",
  38. "Green",
  39. "Blue",
  40. "Yellow"
  41. };
  42. static const struct
  43. {
  44. float r, g, b;
  45. } colors[] =
  46. {
  47. { 0.95f, 0.32f, 0.11f },
  48. { 0.50f, 0.80f, 0.16f },
  49. { 0.f, 0.68f, 0.94f },
  50. { 0.98f, 0.74f, 0.04f }
  51. };
  52. static void usage(void)
  53. {
  54. printf("Usage: windows [-h] [-b] [-f] \n");
  55. printf("Options:\n");
  56. printf(" -b create decorated windows\n");
  57. printf(" -f set focus on show off for all but first window\n");
  58. printf(" -h show this help\n");
  59. }
  60. static void error_callback(int error, const char* description)
  61. {
  62. fprintf(stderr, "Error: %s\n", description);
  63. }
  64. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  65. {
  66. if (action != GLFW_PRESS)
  67. return;
  68. switch (key)
  69. {
  70. case GLFW_KEY_SPACE:
  71. {
  72. int xpos, ypos;
  73. glfwGetWindowPos(window, &xpos, &ypos);
  74. glfwSetWindowPos(window, xpos, ypos);
  75. break;
  76. }
  77. case GLFW_KEY_ESCAPE:
  78. glfwSetWindowShouldClose(window, GLFW_TRUE);
  79. break;
  80. }
  81. }
  82. int main(int argc, char** argv)
  83. {
  84. int i, ch;
  85. int decorated = GLFW_FALSE;
  86. int focusOnShow = GLFW_TRUE;
  87. int running = GLFW_TRUE;
  88. GLFWwindow* windows[4];
  89. while ((ch = getopt(argc, argv, "bfh")) != -1)
  90. {
  91. switch (ch)
  92. {
  93. case 'b':
  94. decorated = GLFW_TRUE;
  95. break;
  96. case 'f':
  97. focusOnShow = GLFW_FALSE;
  98. break;
  99. case 'h':
  100. usage();
  101. exit(EXIT_SUCCESS);
  102. default:
  103. usage();
  104. exit(EXIT_FAILURE);
  105. }
  106. }
  107. glfwSetErrorCallback(error_callback);
  108. if (!glfwInit())
  109. exit(EXIT_FAILURE);
  110. glfwWindowHint(GLFW_DECORATED, decorated);
  111. glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
  112. for (i = 0; i < 4; i++)
  113. {
  114. int left, top, right, bottom;
  115. if (i)
  116. glfwWindowHint(GLFW_FOCUS_ON_SHOW, focusOnShow);
  117. windows[i] = glfwCreateWindow(200, 200, titles[i], NULL, NULL);
  118. if (!windows[i])
  119. {
  120. glfwTerminate();
  121. exit(EXIT_FAILURE);
  122. }
  123. glfwSetKeyCallback(windows[i], key_callback);
  124. glfwMakeContextCurrent(windows[i]);
  125. gladLoadGL(glfwGetProcAddress);
  126. glClearColor(colors[i].r, colors[i].g, colors[i].b, 1.f);
  127. glfwGetWindowFrameSize(windows[i], &left, &top, &right, &bottom);
  128. glfwSetWindowPos(windows[i],
  129. 100 + (i & 1) * (200 + left + right),
  130. 100 + (i >> 1) * (200 + top + bottom));
  131. }
  132. for (i = 0; i < 4; i++)
  133. glfwShowWindow(windows[i]);
  134. while (running)
  135. {
  136. for (i = 0; i < 4; i++)
  137. {
  138. glfwMakeContextCurrent(windows[i]);
  139. glClear(GL_COLOR_BUFFER_BIT);
  140. glfwSwapBuffers(windows[i]);
  141. if (glfwWindowShouldClose(windows[i]))
  142. running = GLFW_FALSE;
  143. }
  144. glfwWaitEvents();
  145. }
  146. glfwTerminate();
  147. exit(EXIT_SUCCESS);
  148. }