1 //========================================================================
2 // Context sharing test program
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 program is used to test sharing of objects between contexts
27 //
28 //========================================================================
29
30 #include <glad/glad.h>
31 #include <GLFW/glfw3.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>
35
36 #define WIDTH 400
37 #define HEIGHT 400
38 #define OFFSET 50
39
40 static GLFWwindow* windows[2];
41
error_callback(int error,const char * description)42 static void error_callback(int error, const char* description)
43 {
44 fprintf(stderr, "Error: %s\n", description);
45 }
46
key_callback(GLFWwindow * window,int key,int scancode,int action,int mods)47 static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
48 {
49 if (action == GLFW_PRESS && key == GLFW_KEY_ESCAPE)
50 glfwSetWindowShouldClose(window, GLFW_TRUE);
51 }
52
open_window(const char * title,GLFWwindow * share,int posX,int posY)53 static GLFWwindow* open_window(const char* title, GLFWwindow* share, int posX, int posY)
54 {
55 GLFWwindow* window;
56
57 glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
58 window = glfwCreateWindow(WIDTH, HEIGHT, title, NULL, share);
59 if (!window)
60 return NULL;
61
62 glfwMakeContextCurrent(window);
63 gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
64 glfwSwapInterval(1);
65 glfwSetWindowPos(window, posX, posY);
66 glfwShowWindow(window);
67
68 glfwSetKeyCallback(window, key_callback);
69
70 return window;
71 }
72
create_texture(void)73 static GLuint create_texture(void)
74 {
75 int x, y;
76 char pixels[256 * 256];
77 GLuint texture;
78
79 glGenTextures(1, &texture);
80 glBindTexture(GL_TEXTURE_2D, texture);
81
82 for (y = 0; y < 256; y++)
83 {
84 for (x = 0; x < 256; x++)
85 pixels[y * 256 + x] = rand() % 256;
86 }
87
88 glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 256, 256, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels);
89 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
90 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
91
92 return texture;
93 }
94
draw_quad(GLuint texture)95 static void draw_quad(GLuint texture)
96 {
97 int width, height;
98 glfwGetFramebufferSize(glfwGetCurrentContext(), &width, &height);
99
100 glViewport(0, 0, width, height);
101
102 glMatrixMode(GL_PROJECTION);
103 glLoadIdentity();
104 glOrtho(0.f, 1.f, 0.f, 1.f, 0.f, 1.f);
105
106 glEnable(GL_TEXTURE_2D);
107 glBindTexture(GL_TEXTURE_2D, texture);
108 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
109
110 glBegin(GL_QUADS);
111
112 glTexCoord2f(0.f, 0.f);
113 glVertex2f(0.f, 0.f);
114
115 glTexCoord2f(1.f, 0.f);
116 glVertex2f(1.f, 0.f);
117
118 glTexCoord2f(1.f, 1.f);
119 glVertex2f(1.f, 1.f);
120
121 glTexCoord2f(0.f, 1.f);
122 glVertex2f(0.f, 1.f);
123
124 glEnd();
125 }
126
main(int argc,char ** argv)127 int main(int argc, char** argv)
128 {
129 int x, y, width;
130 GLuint texture;
131
132 glfwSetErrorCallback(error_callback);
133
134 if (!glfwInit())
135 exit(EXIT_FAILURE);
136
137 windows[0] = open_window("First", NULL, OFFSET, OFFSET);
138 if (!windows[0])
139 {
140 glfwTerminate();
141 exit(EXIT_FAILURE);
142 }
143
144 // This is the one and only time we create a texture
145 // It is created inside the first context, created above
146 // It will then be shared with the second context, created below
147 texture = create_texture();
148
149 glfwGetWindowPos(windows[0], &x, &y);
150 glfwGetWindowSize(windows[0], &width, NULL);
151
152 // Put the second window to the right of the first one
153 windows[1] = open_window("Second", windows[0], x + width + OFFSET, y);
154 if (!windows[1])
155 {
156 glfwTerminate();
157 exit(EXIT_FAILURE);
158 }
159
160 // Set drawing color for both contexts
161 glfwMakeContextCurrent(windows[0]);
162 glColor3f(0.6f, 0.f, 0.6f);
163 glfwMakeContextCurrent(windows[1]);
164 glColor3f(0.6f, 0.6f, 0.f);
165
166 glfwMakeContextCurrent(windows[0]);
167
168 while (!glfwWindowShouldClose(windows[0]) &&
169 !glfwWindowShouldClose(windows[1]))
170 {
171 glfwMakeContextCurrent(windows[0]);
172 draw_quad(texture);
173
174 glfwMakeContextCurrent(windows[1]);
175 draw_quad(texture);
176
177 glfwSwapBuffers(windows[0]);
178 glfwSwapBuffers(windows[1]);
179
180 glfwWaitEvents();
181 }
182
183 glfwTerminate();
184 exit(EXIT_SUCCESS);
185 }
186
187