Convert gamma test to Nuklear

This commit is contained in:
Camilla Berglund 2016-09-26 14:57:03 +02:00
parent 28889954ef
commit 450d0d69af
2 changed files with 81 additions and 102 deletions

View File

@ -21,7 +21,6 @@ set(TINYCTHREAD "${GLFW_SOURCE_DIR}/deps/tinycthread.h"
add_executable(clipboard clipboard.c ${GETOPT} ${GLAD})
add_executable(events events.c ${GETOPT} ${GLAD})
add_executable(msaa msaa.c ${GETOPT} ${GLAD})
add_executable(gamma gamma.c ${GETOPT} ${GLAD})
add_executable(glfwinfo glfwinfo.c ${GETOPT} ${GLAD})
add_executable(iconify iconify.c ${GETOPT} ${GLAD})
add_executable(joysticks joysticks.c ${GLAD})
@ -30,6 +29,7 @@ add_executable(reopen reopen.c ${GLAD})
add_executable(cursor cursor.c ${GLAD})
add_executable(empty WIN32 MACOSX_BUNDLE empty.c ${TINYCTHREAD} ${GLAD})
add_executable(gamma WIN32 MACOSX_BUNDLE gamma.c ${GLAD})
add_executable(icon WIN32 MACOSX_BUNDLE icon.c ${GLAD})
add_executable(sharing WIN32 MACOSX_BUNDLE sharing.c ${GETOPT} ${GLAD})
add_executable(tearing WIN32 MACOSX_BUNDLE tearing.c ${GETOPT} ${GLAD})
@ -41,9 +41,10 @@ add_executable(windows WIN32 MACOSX_BUNDLE windows.c ${GETOPT} ${GLAD})
target_link_libraries(empty "${CMAKE_THREAD_LIBS_INIT}" "${RT_LIBRARY}")
target_link_libraries(threads "${CMAKE_THREAD_LIBS_INIT}" "${RT_LIBRARY}")
set(WINDOWS_BINARIES empty icon sharing tearing threads timeout title windows)
set(CONSOLE_BINARIES clipboard events msaa gamma glfwinfo
iconify joysticks monitors reopen cursor)
set(WINDOWS_BINARIES empty gamma icon sharing tearing threads timeout title
windows)
set(CONSOLE_BINARIES clipboard events msaa glfwinfo iconify joysticks monitors
reopen cursor)
if (VULKAN_FOUND)
add_executable(vulkan WIN32 vulkan.c ${ICON})
@ -65,6 +66,7 @@ endif()
if (APPLE)
set_target_properties(empty PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Empty Event")
set_target_properties(gamma PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Gamma")
set_target_properties(sharing PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Sharing")
set_target_properties(tearing PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Tearing")
set_target_properties(threads PROPERTIES MACOSX_BUNDLE_BUNDLE_NAME "Threads")

View File

@ -31,33 +31,22 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#define NK_IMPLEMENTATION
#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
#define NK_INCLUDE_STANDARD_VARARGS
#include <nuklear.h>
#define NK_GLFW_GL2_IMPLEMENTATION
#include <nuklear_glfw_gl2.h>
#include <stdio.h>
#include <stdlib.h>
#include "getopt.h"
#define STEP_SIZE 0.1f
static GLfloat gamma_value = 1.0f;
static void usage(void)
{
printf("Usage: gamma [-h] [-f]\n");
printf("Options:\n");
printf(" -f create full screen window\n");
printf(" -h show this help\n");
}
static void set_gamma(GLFWwindow* window, float value)
{
GLFWmonitor* monitor = glfwGetWindowMonitor(window);
if (!monitor)
monitor = glfwGetPrimaryMonitor();
gamma_value = value;
printf("Gamma: %f\n", gamma_value);
glfwSetGamma(monitor, gamma_value);
}
static int windowed_xpos, windowed_ypos, windowed_width, windowed_height;
static void error_callback(int error, const char* description)
{
@ -66,115 +55,103 @@ static void error_callback(int error, const char* description)
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (action != GLFW_PRESS)
return;
switch (key)
{
case GLFW_KEY_ESCAPE:
{
if (action == GLFW_PRESS && key == GLFW_KEY_ESCAPE)
glfwSetWindowShouldClose(window, GLFW_TRUE);
break;
}
case GLFW_KEY_KP_ADD:
case GLFW_KEY_UP:
case GLFW_KEY_Q:
{
set_gamma(window, gamma_value + STEP_SIZE);
break;
}
case GLFW_KEY_KP_SUBTRACT:
case GLFW_KEY_DOWN:
case GLFW_KEY_W:
{
if (gamma_value - STEP_SIZE > 0.f)
set_gamma(window, gamma_value - STEP_SIZE);
break;
}
}
}
static void framebuffer_size_callback(GLFWwindow* window, int width, int height)
static void chart_ramp_array(struct nk_context* nk,
struct nk_color color,
int count, unsigned short int* values)
{
glViewport(0, 0, width, height);
if (nk_chart_begin_colored(nk, NK_CHART_LINES,
color, nk_rgb(255, 255, 255),
count, 0, 65535))
{
int i;
for (i = 0; i < count; i++)
{
char buffer[1024];
if (nk_chart_push(nk, values[i]))
{
snprintf(buffer, sizeof(buffer), "#%u: %u (%0.5f) ",
i, values[i], values[i] / 65535.f);
nk_tooltip(nk, buffer);
}
}
nk_chart_end(nk);
}
}
int main(int argc, char** argv)
{
int width, height, ch;
GLFWmonitor* monitor = NULL;
GLFWwindow* window;
struct nk_context* nk;
struct nk_font_atlas* atlas;
float gamma_value = 1.f;
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
while ((ch = getopt(argc, argv, "fh")) != -1)
{
switch (ch)
{
case 'h':
usage();
exit(EXIT_SUCCESS);
case 'f':
monitor = glfwGetPrimaryMonitor();
break;
default:
usage();
exit(EXIT_FAILURE);
}
}
if (monitor)
{
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
glfwWindowHint(GLFW_REFRESH_RATE, mode->refreshRate);
glfwWindowHint(GLFW_RED_BITS, mode->redBits);
glfwWindowHint(GLFW_GREEN_BITS, mode->greenBits);
glfwWindowHint(GLFW_BLUE_BITS, mode->blueBits);
width = mode->width;
height = mode->height;
}
else
{
width = 200;
height = 200;
}
window = glfwCreateWindow(width, height, "Gamma Test", monitor, NULL);
window = glfwCreateWindow(800, 400, "Gamma Test", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
set_gamma(window, 1.f);
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
glfwSwapInterval(1);
glfwSetKeyCallback(window, key_callback);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
nk = nk_glfw3_init(window, NK_GLFW3_INSTALL_CALLBACKS);
nk_glfw3_font_stash_begin(&atlas);
nk_glfw3_font_stash_end();
glClearColor(0.5f, 0.5f, 0.5f, 0);
glfwSetKeyCallback(window, key_callback);
while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
int width, height;
struct nk_panel layout;
struct nk_rect area;
glfwSwapBuffers(window);
glfwWaitEvents();
glfwGetFramebufferSize(window, &width, &height);
area = nk_rect(0.f, 0.f, (float) width, (float) height);
glClear(GL_COLOR_BUFFER_BIT);
nk_glfw3_new_frame();
if (nk_begin(nk, &layout, "", area, 0))
{
const GLFWgammaramp* ramp = glfwGetGammaRamp(monitor);
nk_window_set_bounds(nk, area);
nk_layout_row_dynamic(nk, 30, 2);
if (nk_slider_float(nk, 0.1f, &gamma_value, 5.f, 0.1f))
glfwSetGamma(monitor, gamma_value);
nk_labelf(nk, NK_TEXT_LEFT, "%0.1f", gamma_value);
nk_layout_row_end(nk);
nk_layout_row_dynamic(nk, height - 60.f, 3);
chart_ramp_array(nk, nk_rgb(255, 0, 0), ramp->size, ramp->red);
chart_ramp_array(nk, nk_rgb(0, 255, 0), ramp->size, ramp->green);
chart_ramp_array(nk, nk_rgb(0,0, 255), ramp->size, ramp->blue);
nk_layout_row_end(nk);
}
nk_end(nk);
nk_glfw3_render(NK_ANTI_ALIASING_ON, 10000, 1000);
glfwSwapBuffers(window);
glfwWaitEventsTimeout(1.0);
}
nk_glfw3_shutdown();
glfwTerminate();
exit(EXIT_SUCCESS);
}