1
0
Fork 0
mirror of https://github.com/gwm17/glfw.git synced 2024-11-23 02:38:52 -05:00

Convert reopen test to GL2

This commit is contained in:
Camilla Berglund 2016-09-21 02:44:27 +02:00
parent dfd1c85916
commit a280c97321

View File

@ -40,16 +40,35 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "linmath.h"
static const char* vertex_shader_text =
"uniform mat4 MVP;\n"
"attribute vec2 vPos;\n"
"void main()\n"
"{\n"
" gl_Position = MVP * vec4(vPos, 0.0, 1.0);\n"
"}\n";
static const char* fragment_shader_text =
"void main()\n"
"{\n"
" gl_FragColor = vec4(1.0);\n"
"}\n";
static const vec2 vertices[4] =
{
{ -0.5f, -0.5f },
{ 0.5f, -0.5f },
{ 0.5f, 0.5f },
{ -0.5f, 0.5f }
};
static void error_callback(int error, const char* description) static void error_callback(int error, const char* description)
{ {
fprintf(stderr, "Error: %s\n", description); fprintf(stderr, "Error: %s\n", description);
} }
static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
static void window_close_callback(GLFWwindow* window) static void window_close_callback(GLFWwindow* window)
{ {
printf("Close callback triggered\n"); printf("Close callback triggered\n");
@ -69,40 +88,6 @@ static void key_callback(GLFWwindow* window, int key, int scancode, int action,
} }
} }
static GLFWwindow* open_window(int width, int height, GLFWmonitor* monitor)
{
double base;
GLFWwindow* window;
base = glfwGetTime();
window = glfwCreateWindow(width, height, "Window Re-opener", monitor, NULL);
if (!window)
return NULL;
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
glfwSwapInterval(1);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetWindowCloseCallback(window, window_close_callback);
glfwSetKeyCallback(window, key_callback);
if (monitor)
{
printf("Opening full screen window on monitor %s took %0.3f seconds\n",
glfwGetMonitorName(monitor),
glfwGetTime() - base);
}
else
{
printf("Opening regular window took %0.3f seconds\n",
glfwGetTime() - base);
}
return window;
}
static void close_window(GLFWwindow* window) static void close_window(GLFWwindow* window)
{ {
double base = glfwGetTime(); double base = glfwGetTime();
@ -113,6 +98,7 @@ static void close_window(GLFWwindow* window)
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
int count = 0; int count = 0;
double base;
GLFWwindow* window; GLFWwindow* window;
srand((unsigned int) time(NULL)); srand((unsigned int) time(NULL));
@ -122,12 +108,17 @@ int main(int argc, char** argv)
if (!glfwInit()) if (!glfwInit())
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
for (;;) for (;;)
{ {
int width, height; int width, height;
GLFWmonitor* monitor = NULL; GLFWmonitor* monitor = NULL;
GLuint vertex_shader, fragment_shader, program, vertex_buffer;
GLint mvp_location, vpos_location;
if (count % 2 == 0) if (count & 1)
{ {
int monitorCount; int monitorCount;
GLFWmonitor** monitors = glfwGetMonitors(&monitorCount); GLFWmonitor** monitors = glfwGetMonitors(&monitorCount);
@ -146,27 +137,81 @@ int main(int argc, char** argv)
height = 480; height = 480;
} }
window = open_window(width, height, monitor); base = glfwGetTime();
window = glfwCreateWindow(width, height, "Window Re-opener", monitor, NULL);
if (!window) if (!window)
{ {
glfwTerminate(); glfwTerminate();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
glMatrixMode(GL_PROJECTION); if (monitor)
glOrtho(-1.f, 1.f, -1.f, 1.f, 1.f, -1.f); {
glMatrixMode(GL_MODELVIEW); printf("Opening full screen window on monitor %s took %0.3f seconds\n",
glfwGetMonitorName(monitor),
glfwGetTime() - base);
}
else
{
printf("Opening regular window took %0.3f seconds\n",
glfwGetTime() - base);
}
glfwSetWindowCloseCallback(window, window_close_callback);
glfwSetKeyCallback(window, key_callback);
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
glfwSwapInterval(1);
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
glCompileShader(vertex_shader);
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, &fragment_shader_text, NULL);
glCompileShader(fragment_shader);
program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);
mvp_location = glGetUniformLocation(program, "MVP");
vpos_location = glGetAttribLocation(program, "vPos");
glGenBuffers(1, &vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(vpos_location);
glVertexAttribPointer(vpos_location, 2, GL_FLOAT, GL_FALSE,
sizeof(vertices[0]), (void*) 0);
glfwSetTime(0.0); glfwSetTime(0.0);
while (glfwGetTime() < 5.0) while (glfwGetTime() < 5.0)
{ {
float ratio;
int width, height;
mat4x4 m, p, mvp;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float) height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix(); mat4x4_ortho(p, -ratio, ratio, -1.f, 1.f, 0.f, 1.f);
glRotatef((GLfloat) glfwGetTime() * 100.f, 0.f, 0.f, 1.f);
glRectf(-0.5f, -0.5f, 1.f, 1.f); mat4x4_identity(m);
glPopMatrix(); mat4x4_rotate_Z(m, m, (float) glfwGetTime());
mat4x4_mul(mvp, p, m);
glUseProgram(program);
glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat*) mvp);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();