2010-10-04 17:32:39 +00:00
|
|
|
//========================================================================
|
|
|
|
// Context sharing test program
|
2016-06-01 14:17:06 +00:00
|
|
|
// Copyright (c) Camilla Berglund <elmindreda@glfw.org>
|
2010-10-04 17:32:39 +00:00
|
|
|
//
|
|
|
|
// This software is provided 'as-is', without any express or implied
|
|
|
|
// warranty. In no event will the authors be held liable for any damages
|
|
|
|
// arising from the use of this software.
|
|
|
|
//
|
|
|
|
// Permission is granted to anyone to use this software for any purpose,
|
|
|
|
// including commercial applications, and to alter it and redistribute it
|
|
|
|
// freely, subject to the following restrictions:
|
|
|
|
//
|
|
|
|
// 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
// claim that you wrote the original software. If you use this software
|
|
|
|
// in a product, an acknowledgment in the product documentation would
|
|
|
|
// be appreciated but is not required.
|
|
|
|
//
|
|
|
|
// 2. Altered source versions must be plainly marked as such, and must not
|
|
|
|
// be misrepresented as being the original software.
|
|
|
|
//
|
|
|
|
// 3. This notice may not be removed or altered from any source
|
|
|
|
// distribution.
|
|
|
|
//
|
|
|
|
//========================================================================
|
|
|
|
//
|
|
|
|
// This program is used to test sharing of objects between contexts
|
|
|
|
//
|
|
|
|
//========================================================================
|
|
|
|
|
2015-10-14 01:11:20 +00:00
|
|
|
#include <glad/glad.h>
|
2013-05-22 20:46:34 +00:00
|
|
|
#include <GLFW/glfw3.h>
|
2010-10-04 17:32:39 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2010-10-04 19:08:42 +00:00
|
|
|
#define WIDTH 400
|
|
|
|
#define HEIGHT 400
|
2013-01-24 18:30:31 +00:00
|
|
|
#define OFFSET 50
|
2010-10-04 19:08:42 +00:00
|
|
|
|
2013-01-05 20:13:28 +00:00
|
|
|
static GLFWwindow* windows[2];
|
2012-08-03 14:20:52 +00:00
|
|
|
|
2012-12-30 00:42:14 +00:00
|
|
|
static void error_callback(int error, const char* description)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Error: %s\n", description);
|
|
|
|
}
|
|
|
|
|
2013-05-30 15:19:12 +00:00
|
|
|
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
|
2010-10-04 22:12:25 +00:00
|
|
|
{
|
2011-01-03 21:22:14 +00:00
|
|
|
if (action == GLFW_PRESS && key == GLFW_KEY_ESCAPE)
|
2015-08-23 17:30:04 +00:00
|
|
|
glfwSetWindowShouldClose(window, GLFW_TRUE);
|
2012-08-03 14:20:52 +00:00
|
|
|
}
|
|
|
|
|
2013-01-05 20:13:28 +00:00
|
|
|
static GLFWwindow* open_window(const char* title, GLFWwindow* share, int posX, int posY)
|
2010-10-04 17:32:39 +00:00
|
|
|
{
|
2013-01-05 20:13:28 +00:00
|
|
|
GLFWwindow* window;
|
2010-10-04 17:32:39 +00:00
|
|
|
|
2015-08-23 17:30:04 +00:00
|
|
|
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
|
2012-09-27 19:37:36 +00:00
|
|
|
window = glfwCreateWindow(WIDTH, HEIGHT, title, NULL, share);
|
2010-10-04 17:32:39 +00:00
|
|
|
if (!window)
|
|
|
|
return NULL;
|
|
|
|
|
2012-08-10 13:29:45 +00:00
|
|
|
glfwMakeContextCurrent(window);
|
2015-10-14 01:11:20 +00:00
|
|
|
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
|
2012-08-10 13:29:45 +00:00
|
|
|
glfwSwapInterval(1);
|
2013-01-24 18:30:31 +00:00
|
|
|
glfwSetWindowPos(window, posX, posY);
|
|
|
|
glfwShowWindow(window);
|
2012-08-10 13:29:45 +00:00
|
|
|
|
2012-10-28 12:45:11 +00:00
|
|
|
glfwSetKeyCallback(window, key_callback);
|
2010-10-04 17:32:39 +00:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GLuint create_texture(void)
|
|
|
|
{
|
|
|
|
int x, y;
|
|
|
|
char pixels[256 * 256];
|
|
|
|
GLuint texture;
|
|
|
|
|
|
|
|
glGenTextures(1, &texture);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture);
|
|
|
|
|
|
|
|
for (y = 0; y < 256; y++)
|
|
|
|
{
|
|
|
|
for (x = 0; x < 256; x++)
|
|
|
|
pixels[y * 256 + x] = rand() % 256;
|
|
|
|
}
|
|
|
|
|
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, 256, 256, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, pixels);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_quad(GLuint texture)
|
|
|
|
{
|
2010-10-04 21:13:33 +00:00
|
|
|
int width, height;
|
2013-06-03 10:51:57 +00:00
|
|
|
glfwGetFramebufferSize(glfwGetCurrentContext(), &width, &height);
|
2010-10-04 21:13:33 +00:00
|
|
|
|
|
|
|
glViewport(0, 0, width, height);
|
|
|
|
|
2010-10-04 17:32:39 +00:00
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
2014-06-10 12:35:50 +00:00
|
|
|
glOrtho(0.f, 1.f, 0.f, 1.f, 0.f, 1.f);
|
2010-10-04 17:32:39 +00:00
|
|
|
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, texture);
|
|
|
|
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
|
|
|
|
|
|
|
glBegin(GL_QUADS);
|
|
|
|
|
|
|
|
glTexCoord2f(0.f, 0.f);
|
|
|
|
glVertex2f(0.f, 0.f);
|
|
|
|
|
|
|
|
glTexCoord2f(1.f, 0.f);
|
|
|
|
glVertex2f(1.f, 0.f);
|
|
|
|
|
|
|
|
glTexCoord2f(1.f, 1.f);
|
|
|
|
glVertex2f(1.f, 1.f);
|
|
|
|
|
|
|
|
glTexCoord2f(0.f, 1.f);
|
|
|
|
glVertex2f(0.f, 1.f);
|
|
|
|
|
|
|
|
glEnd();
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2013-01-24 18:30:31 +00:00
|
|
|
int x, y, width;
|
2010-10-04 17:32:39 +00:00
|
|
|
GLuint texture;
|
|
|
|
|
2012-12-30 00:42:14 +00:00
|
|
|
glfwSetErrorCallback(error_callback);
|
|
|
|
|
2012-02-07 13:58:58 +00:00
|
|
|
if (!glfwInit())
|
2010-10-04 17:32:39 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
2013-01-24 18:30:31 +00:00
|
|
|
windows[0] = open_window("First", NULL, OFFSET, OFFSET);
|
2010-10-04 17:32:39 +00:00
|
|
|
if (!windows[0])
|
|
|
|
{
|
2012-10-22 01:20:16 +00:00
|
|
|
glfwTerminate();
|
2010-10-04 17:32:39 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2010-10-04 17:35:28 +00:00
|
|
|
// This is the one and only time we create a texture
|
2010-10-04 22:34:50 +00:00
|
|
|
// It is created inside the first context, created above
|
|
|
|
// It will then be shared with the second context, created below
|
2010-10-04 17:32:39 +00:00
|
|
|
texture = create_texture();
|
|
|
|
|
2013-01-24 18:30:31 +00:00
|
|
|
glfwGetWindowPos(windows[0], &x, &y);
|
|
|
|
glfwGetWindowSize(windows[0], &width, NULL);
|
|
|
|
|
2012-10-27 22:23:28 +00:00
|
|
|
// Put the second window to the right of the first one
|
2013-01-24 18:30:31 +00:00
|
|
|
windows[1] = open_window("Second", windows[0], x + width + OFFSET, y);
|
2010-10-04 17:32:39 +00:00
|
|
|
if (!windows[1])
|
|
|
|
{
|
2012-10-22 01:20:16 +00:00
|
|
|
glfwTerminate();
|
2010-10-04 17:32:39 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2012-11-22 19:16:48 +00:00
|
|
|
// Set drawing color for both contexts
|
2012-02-06 15:27:56 +00:00
|
|
|
glfwMakeContextCurrent(windows[0]);
|
|
|
|
glColor3f(0.6f, 0.f, 0.6f);
|
2012-11-22 19:16:48 +00:00
|
|
|
glfwMakeContextCurrent(windows[1]);
|
|
|
|
glColor3f(0.6f, 0.6f, 0.f);
|
2012-02-06 15:27:56 +00:00
|
|
|
|
2012-11-22 19:16:48 +00:00
|
|
|
glfwMakeContextCurrent(windows[0]);
|
2010-10-04 19:08:42 +00:00
|
|
|
|
2013-03-01 12:45:12 +00:00
|
|
|
while (!glfwWindowShouldClose(windows[0]) &&
|
|
|
|
!glfwWindowShouldClose(windows[1]))
|
2010-10-04 17:32:39 +00:00
|
|
|
{
|
2011-07-27 14:01:27 +00:00
|
|
|
glfwMakeContextCurrent(windows[0]);
|
2010-10-04 17:32:39 +00:00
|
|
|
draw_quad(texture);
|
|
|
|
|
2011-07-27 14:01:27 +00:00
|
|
|
glfwMakeContextCurrent(windows[1]);
|
2010-10-04 17:32:39 +00:00
|
|
|
draw_quad(texture);
|
2012-08-06 16:13:37 +00:00
|
|
|
|
|
|
|
glfwSwapBuffers(windows[0]);
|
|
|
|
glfwSwapBuffers(windows[1]);
|
2010-10-04 17:32:39 +00:00
|
|
|
|
|
|
|
glfwWaitEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
glfwTerminate();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|