1 //========================================================================
2 // Simple multi-window test
3 // Copyright (c) Camilla Berglund <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
30 #include <glad/glad.h>
31 #include <GLFW/glfw3.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>
35
36 #include "getopt.h"
37
38 static const char* titles[] =
39 {
40 "Red",
41 "Green",
42 "Blue",
43 "Yellow"
44 };
45
46 static const struct
47 {
48 float r, g, b;
49 } colors[] =
50 {
51 { 0.95f, 0.32f, 0.11f },
52 { 0.50f, 0.80f, 0.16f },
53 { 0.f, 0.68f, 0.94f },
54 { 0.98f, 0.74f, 0.04f }
55 };
56
usage(void)57 static void usage(void)
58 {
59 printf("Usage: windows [-h] [-b]\n");
60 printf("Options:\n");
61 printf(" -b create decorated windows\n");
62 printf(" -h show this help\n");
63 }
64
error_callback(int error,const char * description)65 static void error_callback(int error, const char* description)
66 {
67 fprintf(stderr, "Error: %s\n", description);
68 }
69
key_callback(GLFWwindow * window,int key,int scancode,int action,int mods)70 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
71 {
72 if (action != GLFW_PRESS)
73 return;
74
75 switch (key)
76 {
77 case GLFW_KEY_SPACE:
78 {
79 int xpos, ypos;
80 glfwGetWindowPos(window, &xpos, &ypos);
81 glfwSetWindowPos(window, xpos, ypos);
82 break;
83 }
84
85 case GLFW_KEY_ESCAPE:
86 glfwSetWindowShouldClose(window, GLFW_TRUE);
87 break;
88 }
89 }
90
main(int argc,char ** argv)91 int main(int argc, char** argv)
92 {
93 int i, ch;
94 int decorated = GLFW_FALSE;
95 int running = GLFW_TRUE;
96 GLFWwindow* windows[4];
97
98 while ((ch = getopt(argc, argv, "bh")) != -1)
99 {
100 switch (ch)
101 {
102 case 'b':
103 decorated = GLFW_TRUE;
104 break;
105 case 'h':
106 usage();
107 exit(EXIT_SUCCESS);
108 default:
109 usage();
110 exit(EXIT_FAILURE);
111 }
112 }
113
114 glfwSetErrorCallback(error_callback);
115
116 if (!glfwInit())
117 exit(EXIT_FAILURE);
118
119 glfwWindowHint(GLFW_DECORATED, decorated);
120 glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
121
122 for (i = 0; i < 4; i++)
123 {
124 int left, top, right, bottom;
125
126 windows[i] = glfwCreateWindow(200, 200, titles[i], NULL, NULL);
127 if (!windows[i])
128 {
129 glfwTerminate();
130 exit(EXIT_FAILURE);
131 }
132
133 glfwSetKeyCallback(windows[i], key_callback);
134
135 glfwMakeContextCurrent(windows[i]);
136 gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
137 glClearColor(colors[i].r, colors[i].g, colors[i].b, 1.f);
138
139 glfwGetWindowFrameSize(windows[i], &left, &top, &right, &bottom);
140 glfwSetWindowPos(windows[i],
141 100 + (i & 1) * (200 + left + right),
142 100 + (i >> 1) * (200 + top + bottom));
143 }
144
145 for (i = 0; i < 4; i++)
146 glfwShowWindow(windows[i]);
147
148 while (running)
149 {
150 for (i = 0; i < 4; i++)
151 {
152 glfwMakeContextCurrent(windows[i]);
153 glClear(GL_COLOR_BUFFER_BIT);
154 glfwSwapBuffers(windows[i]);
155
156 if (glfwWindowShouldClose(windows[i]))
157 running = GLFW_FALSE;
158 }
159
160 glfwWaitEvents();
161 }
162
163 glfwTerminate();
164 exit(EXIT_SUCCESS);
165 }
166
167