glfw/tests/modes.c

244 lines
6.0 KiB
C
Raw Normal View History

2012-03-08 00:01:42 +00:00
//========================================================================
// Video mode test
// Copyright (c) Camilla Berglund <elmindreda@elmindreda.org>
//
// 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 test enumerates or verifies video modes
//
//========================================================================
#include <GL/glfw3.h>
#include <stdio.h>
#include <stdlib.h>
#include "getopt.h"
static GLFWwindow window = NULL;
enum Mode
{
LIST_MODE,
TEST_MODE
};
static void usage(void)
{
printf("Usage: modes [-t]\n");
2012-03-08 00:01:42 +00:00
printf(" modes -h\n");
}
static void print_mode(GLFWvidmode* mode)
{
printf("%i x %i x %i (%i %i %i)",
mode->width, mode->height,
mode->redBits + mode->greenBits + mode->blueBits,
mode->redBits, mode->greenBits, mode->blueBits);
}
static void error_callback(int error, const char* description)
{
fprintf(stderr, "Error: %s\n", description);
}
static void window_size_callback(GLFWwindow window, int width, int height)
{
printf("Window resized to %ix%i\n", width, height);
glViewport(0, 0, width, height);
}
static int window_close_callback(GLFWwindow dummy)
{
window = NULL;
return GL_TRUE;
}
2012-05-06 22:44:39 +00:00
static void key_callback(GLFWwindow dummy, int key, int action)
{
if (key == GLFW_KEY_ESCAPE)
{
glfwCloseWindow(window);
window = NULL;
}
}
static GLFWvidmode* get_video_modes(size_t* found)
2012-03-08 00:01:42 +00:00
{
size_t count = 0;
GLFWvidmode* modes = NULL;
for (;;)
{
count += 256;
modes = realloc(modes, sizeof(GLFWvidmode) * count);
*found = glfwGetVideoModes(modes, count);
if (*found < count)
break;
}
return modes;
}
2012-03-08 00:01:42 +00:00
static void list_modes(void)
{
size_t count, i;
GLFWvidmode desktop_mode;
GLFWvidmode* modes = get_video_modes(&count);
glfwGetDesktopMode(&desktop_mode);
2012-03-08 00:01:42 +00:00
printf("Desktop mode: ");
print_mode(&desktop_mode);
2012-03-08 00:01:42 +00:00
putchar('\n');
printf("Available modes:\n");
2012-03-08 00:01:42 +00:00
for (i = 0; i < count; i++)
{
printf("%3u: ", (unsigned int) i);
2012-03-08 00:01:42 +00:00
print_mode(modes + i);
putchar('\n');
}
free(modes);
2012-03-08 00:01:42 +00:00
}
static void test_modes(void)
2012-03-08 00:01:42 +00:00
{
int width, height;
size_t i, count;
GLFWvidmode* modes = get_video_modes(&count);
2012-03-08 00:01:42 +00:00
glfwSetWindowSizeCallback(window_size_callback);
glfwSetWindowCloseCallback(window_close_callback);
2012-05-06 22:44:39 +00:00
glfwSetKeyCallback(key_callback);
2012-03-08 00:01:42 +00:00
for (i = 0; i < count; i++)
{
2012-05-06 22:33:21 +00:00
GLFWvidmode* mode = modes + i;
glfwOpenWindowHint(GLFW_RED_BITS, mode->redBits);
glfwOpenWindowHint(GLFW_GREEN_BITS, mode->greenBits);
glfwOpenWindowHint(GLFW_BLUE_BITS, mode->blueBits);
2012-03-08 00:01:42 +00:00
printf("Opening ");
2012-05-06 22:33:21 +00:00
print_mode(mode);
2012-03-08 00:01:42 +00:00
printf(" window\n");
2012-05-06 22:33:21 +00:00
window = glfwOpenWindow(mode->width, mode->height,
2012-03-08 00:01:42 +00:00
GLFW_FULLSCREEN, "Video Mode Test",
NULL);
if (!window)
{
printf("Failed to enter mode %u: ", (unsigned int) i);
2012-05-06 22:33:21 +00:00
print_mode(mode);
2012-03-08 00:01:42 +00:00
putchar('\n');
continue;
}
glfwSetTime(0.0);
glfwSwapInterval(1);
while (glfwGetTime() < 5.0)
{
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers();
glfwPollEvents();
if (!window)
{
printf("User terminated program\n");
exit(EXIT_SUCCESS);
}
}
2012-05-06 22:33:21 +00:00
if (glfwGetWindowParam(window, GLFW_RED_BITS) != mode->redBits ||
glfwGetWindowParam(window, GLFW_GREEN_BITS) != mode->greenBits ||
glfwGetWindowParam(window, GLFW_BLUE_BITS) != mode->blueBits)
2012-03-08 00:01:42 +00:00
{
printf("*** Color bit mismatch: (%i %i %i) instead of (%i %i %i)\n",
glfwGetWindowParam(window, GLFW_RED_BITS),
glfwGetWindowParam(window, GLFW_GREEN_BITS),
glfwGetWindowParam(window, GLFW_BLUE_BITS),
2012-05-06 22:33:21 +00:00
mode->redBits,
mode->greenBits,
mode->blueBits);
2012-03-08 00:01:42 +00:00
}
glfwGetWindowSize(window, &width, &height);
2012-05-06 22:33:21 +00:00
if (width != mode->width || height != mode->height)
2012-03-08 00:01:42 +00:00
{
printf("*** Size mismatch: %ix%i instead of %ix%i\n",
width, height,
2012-05-06 22:33:21 +00:00
mode->width, mode->height);
2012-03-08 00:01:42 +00:00
}
printf("Closing window\n");
glfwCloseWindow(window);
glfwPollEvents();
window = NULL;
}
free(modes);
2012-03-08 00:01:42 +00:00
}
int main(int argc, char** argv)
{
int ch, mode = LIST_MODE;
2012-03-08 00:01:42 +00:00
while ((ch = getopt(argc, argv, "th")) != -1)
2012-03-08 00:01:42 +00:00
{
switch (ch)
{
case 'h':
usage();
exit(EXIT_SUCCESS);
case 't':
mode = TEST_MODE;
break;
default:
usage();
exit(EXIT_FAILURE);
}
}
argc -= optind;
argv += optind;
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
if (mode == LIST_MODE)
list_modes();
2012-03-08 00:01:42 +00:00
else if (mode == TEST_MODE)
test_modes();
2012-03-08 00:01:42 +00:00
exit(EXIT_SUCCESS);
}