blob: 74e11478761af1926bf447d56aab1d4babfb130d [file] [log] [blame]
Camilla Berglundafcb2ab2010-10-04 19:32:39 +02001//========================================================================
2// Context sharing test program
3// Copyright (c) Camilla Berglund <elmindreda@elmindreda.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
Camilla Berglund22134502012-06-05 23:55:10 +020030#define GLFW_INCLUDE_GLU
Camilla Berglundafcb2ab2010-10-04 19:32:39 +020031#include <GL/glfw3.h>
32
33#include <stdio.h>
34#include <stdlib.h>
35
Camilla Berglund2899e872010-10-04 21:08:42 +020036#define WIDTH 400
37#define HEIGHT 400
38
Camilla Berglund2972cdf2012-08-03 16:20:52 +020039static GLFWwindow windows[2];
Camilla Berglund89018332012-09-13 17:29:07 +020040static GLboolean closed = GL_FALSE;
Camilla Berglund2972cdf2012-08-03 16:20:52 +020041
Camilla Berglund1585c8b2010-10-05 00:12:25 +020042static void key_callback(GLFWwindow window, int key, int action)
43{
Marcuse3cb5632011-01-03 22:22:14 +010044 if (action == GLFW_PRESS && key == GLFW_KEY_ESCAPE)
Camilla Berglund89018332012-09-13 17:29:07 +020045 closed = GL_TRUE;
Camilla Berglund1585c8b2010-10-05 00:12:25 +020046}
47
Camilla Berglund2972cdf2012-08-03 16:20:52 +020048static int window_close_callback(GLFWwindow window)
49{
Camilla Berglund89018332012-09-13 17:29:07 +020050 closed = GL_TRUE;
51 return GL_FALSE;
Camilla Berglund2972cdf2012-08-03 16:20:52 +020052}
53
m@bitsnbites.euc9f4ded2012-10-28 00:23:28 +020054static GLFWwindow open_window(const char* title, GLFWwindow share, int posX, int posY)
Camilla Berglundafcb2ab2010-10-04 19:32:39 +020055{
56 GLFWwindow window;
57
m@bitsnbites.euc9f4ded2012-10-28 00:23:28 +020058 glfwWindowHint(GLFW_POSITION_X, posX);
59 glfwWindowHint(GLFW_POSITION_Y, posY);
Camilla Berglundaff30d02012-08-06 17:56:41 +020060 window = glfwCreateWindow(WIDTH, HEIGHT, GLFW_WINDOWED, title, share);
Camilla Berglundafcb2ab2010-10-04 19:32:39 +020061 if (!window)
62 return NULL;
63
Camilla Berglund2f095cc2012-08-10 15:29:45 +020064 glfwMakeContextCurrent(window);
65 glfwSwapInterval(1);
66
Camilla Berglund18d71c22012-10-28 13:45:11 +010067 glfwSetWindowCloseCallback(window, window_close_callback);
68 glfwSetKeyCallback(window, key_callback);
Camilla Berglundafcb2ab2010-10-04 19:32:39 +020069
70 return window;
71}
72
73static 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
95static void draw_quad(GLuint texture)
96{
Camilla Berglund419f9f12010-10-04 23:13:33 +020097 int width, height;
Camilla Berglundc1ab73b2011-07-27 16:01:27 +020098 glfwGetWindowSize(glfwGetCurrentContext(), &width, &height);
Camilla Berglund419f9f12010-10-04 23:13:33 +020099
100 glViewport(0, 0, width, height);
101
Camilla Berglundafcb2ab2010-10-04 19:32:39 +0200102 glMatrixMode(GL_PROJECTION);
103 glLoadIdentity();
104 gluOrtho2D(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
Camilla Berglundafcb2ab2010-10-04 19:32:39 +0200110 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
127int main(int argc, char** argv)
128{
Camilla Berglundafcb2ab2010-10-04 19:32:39 +0200129 GLuint texture;
130
Camilla Berglund0c3b1b52012-02-07 14:58:58 +0100131 if (!glfwInit())
Camilla Berglundafcb2ab2010-10-04 19:32:39 +0200132 {
133 fprintf(stderr, "Failed to initialize GLFW: %s\n", glfwErrorString(glfwGetError()));
134 exit(EXIT_FAILURE);
135 }
136
m@bitsnbites.euc9f4ded2012-10-28 00:23:28 +0200137 windows[0] = open_window("First", NULL, 0, 0);
Camilla Berglundafcb2ab2010-10-04 19:32:39 +0200138 if (!windows[0])
139 {
140 fprintf(stderr, "Failed to open first GLFW window: %s\n", glfwErrorString(glfwGetError()));
Camilla Berglundd68acb72012-10-22 03:20:16 +0200141
142 glfwTerminate();
Camilla Berglundafcb2ab2010-10-04 19:32:39 +0200143 exit(EXIT_FAILURE);
144 }
145
Camilla Berglund8dff22d2010-10-04 19:35:28 +0200146 // This is the one and only time we create a texture
Camilla Berglund7e7672a2010-10-05 00:34:50 +0200147 // It is created inside the first context, created above
148 // It will then be shared with the second context, created below
Camilla Berglundafcb2ab2010-10-04 19:32:39 +0200149 texture = create_texture();
150
m@bitsnbites.euc9f4ded2012-10-28 00:23:28 +0200151 // Put the second window to the right of the first one
152 windows[1] = open_window("Second", windows[0], WIDTH + 50, 0);
Camilla Berglundafcb2ab2010-10-04 19:32:39 +0200153 if (!windows[1])
154 {
155 fprintf(stderr, "Failed to open second GLFW window: %s\n", glfwErrorString(glfwGetError()));
Camilla Berglundd68acb72012-10-22 03:20:16 +0200156
157 glfwTerminate();
Camilla Berglundafcb2ab2010-10-04 19:32:39 +0200158 exit(EXIT_FAILURE);
159 }
160
Camilla Berglundba3a6052012-02-06 16:27:56 +0100161 // Set drawing color for the first context and copy it to the second
162 glfwMakeContextCurrent(windows[0]);
163 glColor3f(0.6f, 0.f, 0.6f);
164 glfwCopyContext(windows[0], windows[1], GL_CURRENT_BIT);
165
Camilla Berglund89018332012-09-13 17:29:07 +0200166 while (!closed)
Camilla Berglundafcb2ab2010-10-04 19:32:39 +0200167 {
Camilla Berglundc1ab73b2011-07-27 16:01:27 +0200168 glfwMakeContextCurrent(windows[0]);
Camilla Berglundafcb2ab2010-10-04 19:32:39 +0200169 draw_quad(texture);
Camilla Berglundafcb2ab2010-10-04 19:32:39 +0200170
Camilla Berglundc1ab73b2011-07-27 16:01:27 +0200171 glfwMakeContextCurrent(windows[1]);
Camilla Berglundafcb2ab2010-10-04 19:32:39 +0200172 draw_quad(texture);
Camilla Berglund585a8402012-08-06 18:13:37 +0200173
174 glfwSwapBuffers(windows[0]);
175 glfwSwapBuffers(windows[1]);
Camilla Berglundafcb2ab2010-10-04 19:32:39 +0200176
177 glfwWaitEvents();
178 }
179
180 glfwTerminate();
181 exit(EXIT_SUCCESS);
182}
183