Camilla Berglund | afcb2ab | 2010-10-04 19:32:39 +0200 | [diff] [blame] | 1 | //======================================================================== |
| 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 Berglund | 2213450 | 2012-06-05 23:55:10 +0200 | [diff] [blame] | 30 | #define GLFW_INCLUDE_GLU |
Camilla Berglund | afcb2ab | 2010-10-04 19:32:39 +0200 | [diff] [blame] | 31 | #include <GL/glfw3.h> |
| 32 | |
| 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | |
Camilla Berglund | 2899e87 | 2010-10-04 21:08:42 +0200 | [diff] [blame] | 36 | #define WIDTH 400 |
| 37 | #define HEIGHT 400 |
| 38 | |
Camilla Berglund | 2972cdf | 2012-08-03 16:20:52 +0200 | [diff] [blame] | 39 | static GLFWwindow windows[2]; |
Camilla Berglund | 8901833 | 2012-09-13 17:29:07 +0200 | [diff] [blame] | 40 | static GLboolean closed = GL_FALSE; |
Camilla Berglund | 2972cdf | 2012-08-03 16:20:52 +0200 | [diff] [blame] | 41 | |
Camilla Berglund | 1585c8b | 2010-10-05 00:12:25 +0200 | [diff] [blame] | 42 | static void key_callback(GLFWwindow window, int key, int action) |
| 43 | { |
Marcus | e3cb563 | 2011-01-03 22:22:14 +0100 | [diff] [blame] | 44 | if (action == GLFW_PRESS && key == GLFW_KEY_ESCAPE) |
Camilla Berglund | 8901833 | 2012-09-13 17:29:07 +0200 | [diff] [blame] | 45 | closed = GL_TRUE; |
Camilla Berglund | 1585c8b | 2010-10-05 00:12:25 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Camilla Berglund | 2972cdf | 2012-08-03 16:20:52 +0200 | [diff] [blame] | 48 | static int window_close_callback(GLFWwindow window) |
| 49 | { |
Camilla Berglund | 8901833 | 2012-09-13 17:29:07 +0200 | [diff] [blame] | 50 | closed = GL_TRUE; |
| 51 | return GL_FALSE; |
Camilla Berglund | 2972cdf | 2012-08-03 16:20:52 +0200 | [diff] [blame] | 52 | } |
| 53 | |
m@bitsnbites.eu | c9f4ded | 2012-10-28 00:23:28 +0200 | [diff] [blame^] | 54 | static GLFWwindow open_window(const char* title, GLFWwindow share, int posX, int posY) |
Camilla Berglund | afcb2ab | 2010-10-04 19:32:39 +0200 | [diff] [blame] | 55 | { |
| 56 | GLFWwindow window; |
| 57 | |
m@bitsnbites.eu | c9f4ded | 2012-10-28 00:23:28 +0200 | [diff] [blame^] | 58 | glfwWindowHint(GLFW_POSITION_X, posX); |
| 59 | glfwWindowHint(GLFW_POSITION_Y, posY); |
Camilla Berglund | aff30d0 | 2012-08-06 17:56:41 +0200 | [diff] [blame] | 60 | window = glfwCreateWindow(WIDTH, HEIGHT, GLFW_WINDOWED, title, share); |
Camilla Berglund | afcb2ab | 2010-10-04 19:32:39 +0200 | [diff] [blame] | 61 | if (!window) |
| 62 | return NULL; |
| 63 | |
Camilla Berglund | 2f095cc | 2012-08-10 15:29:45 +0200 | [diff] [blame] | 64 | glfwMakeContextCurrent(window); |
| 65 | glfwSwapInterval(1); |
| 66 | |
Camilla Berglund | 18d71c2 | 2012-10-28 13:45:11 +0100 | [diff] [blame] | 67 | glfwSetWindowCloseCallback(window, window_close_callback); |
| 68 | glfwSetKeyCallback(window, key_callback); |
Camilla Berglund | afcb2ab | 2010-10-04 19:32:39 +0200 | [diff] [blame] | 69 | |
| 70 | return window; |
| 71 | } |
| 72 | |
| 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 | |
| 95 | static void draw_quad(GLuint texture) |
| 96 | { |
Camilla Berglund | 419f9f1 | 2010-10-04 23:13:33 +0200 | [diff] [blame] | 97 | int width, height; |
Camilla Berglund | c1ab73b | 2011-07-27 16:01:27 +0200 | [diff] [blame] | 98 | glfwGetWindowSize(glfwGetCurrentContext(), &width, &height); |
Camilla Berglund | 419f9f1 | 2010-10-04 23:13:33 +0200 | [diff] [blame] | 99 | |
| 100 | glViewport(0, 0, width, height); |
| 101 | |
Camilla Berglund | afcb2ab | 2010-10-04 19:32:39 +0200 | [diff] [blame] | 102 | 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 Berglund | afcb2ab | 2010-10-04 19:32:39 +0200 | [diff] [blame] | 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 | |
| 127 | int main(int argc, char** argv) |
| 128 | { |
Camilla Berglund | afcb2ab | 2010-10-04 19:32:39 +0200 | [diff] [blame] | 129 | GLuint texture; |
| 130 | |
Camilla Berglund | 0c3b1b5 | 2012-02-07 14:58:58 +0100 | [diff] [blame] | 131 | if (!glfwInit()) |
Camilla Berglund | afcb2ab | 2010-10-04 19:32:39 +0200 | [diff] [blame] | 132 | { |
| 133 | fprintf(stderr, "Failed to initialize GLFW: %s\n", glfwErrorString(glfwGetError())); |
| 134 | exit(EXIT_FAILURE); |
| 135 | } |
| 136 | |
m@bitsnbites.eu | c9f4ded | 2012-10-28 00:23:28 +0200 | [diff] [blame^] | 137 | windows[0] = open_window("First", NULL, 0, 0); |
Camilla Berglund | afcb2ab | 2010-10-04 19:32:39 +0200 | [diff] [blame] | 138 | if (!windows[0]) |
| 139 | { |
| 140 | fprintf(stderr, "Failed to open first GLFW window: %s\n", glfwErrorString(glfwGetError())); |
Camilla Berglund | d68acb7 | 2012-10-22 03:20:16 +0200 | [diff] [blame] | 141 | |
| 142 | glfwTerminate(); |
Camilla Berglund | afcb2ab | 2010-10-04 19:32:39 +0200 | [diff] [blame] | 143 | exit(EXIT_FAILURE); |
| 144 | } |
| 145 | |
Camilla Berglund | 8dff22d | 2010-10-04 19:35:28 +0200 | [diff] [blame] | 146 | // This is the one and only time we create a texture |
Camilla Berglund | 7e7672a | 2010-10-05 00:34:50 +0200 | [diff] [blame] | 147 | // It is created inside the first context, created above |
| 148 | // It will then be shared with the second context, created below |
Camilla Berglund | afcb2ab | 2010-10-04 19:32:39 +0200 | [diff] [blame] | 149 | texture = create_texture(); |
| 150 | |
m@bitsnbites.eu | c9f4ded | 2012-10-28 00:23:28 +0200 | [diff] [blame^] | 151 | // Put the second window to the right of the first one |
| 152 | windows[1] = open_window("Second", windows[0], WIDTH + 50, 0); |
Camilla Berglund | afcb2ab | 2010-10-04 19:32:39 +0200 | [diff] [blame] | 153 | if (!windows[1]) |
| 154 | { |
| 155 | fprintf(stderr, "Failed to open second GLFW window: %s\n", glfwErrorString(glfwGetError())); |
Camilla Berglund | d68acb7 | 2012-10-22 03:20:16 +0200 | [diff] [blame] | 156 | |
| 157 | glfwTerminate(); |
Camilla Berglund | afcb2ab | 2010-10-04 19:32:39 +0200 | [diff] [blame] | 158 | exit(EXIT_FAILURE); |
| 159 | } |
| 160 | |
Camilla Berglund | ba3a605 | 2012-02-06 16:27:56 +0100 | [diff] [blame] | 161 | // 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 Berglund | 8901833 | 2012-09-13 17:29:07 +0200 | [diff] [blame] | 166 | while (!closed) |
Camilla Berglund | afcb2ab | 2010-10-04 19:32:39 +0200 | [diff] [blame] | 167 | { |
Camilla Berglund | c1ab73b | 2011-07-27 16:01:27 +0200 | [diff] [blame] | 168 | glfwMakeContextCurrent(windows[0]); |
Camilla Berglund | afcb2ab | 2010-10-04 19:32:39 +0200 | [diff] [blame] | 169 | draw_quad(texture); |
Camilla Berglund | afcb2ab | 2010-10-04 19:32:39 +0200 | [diff] [blame] | 170 | |
Camilla Berglund | c1ab73b | 2011-07-27 16:01:27 +0200 | [diff] [blame] | 171 | glfwMakeContextCurrent(windows[1]); |
Camilla Berglund | afcb2ab | 2010-10-04 19:32:39 +0200 | [diff] [blame] | 172 | draw_quad(texture); |
Camilla Berglund | 585a840 | 2012-08-06 18:13:37 +0200 | [diff] [blame] | 173 | |
| 174 | glfwSwapBuffers(windows[0]); |
| 175 | glfwSwapBuffers(windows[1]); |
Camilla Berglund | afcb2ab | 2010-10-04 19:32:39 +0200 | [diff] [blame] | 176 | |
| 177 | glfwWaitEvents(); |
| 178 | } |
| 179 | |
| 180 | glfwTerminate(); |
| 181 | exit(EXIT_SUCCESS); |
| 182 | } |
| 183 | |