Nuklear
A Nuklear é uma API de GUI de modo imediato, multiplataforma, escrita em C89.
Exemplos
Nuklear + GLFW3
(Tirado do repositório oficial da Nuklear)
//#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_IMPLEMENTATION
#define NK_GLFW_GL3_IMPLEMENTATION
#include "3rd party/Nuklear/nuklear.h"
#include "3rd party/Nuklear/nuklear_glfw_gl3.h"
using namespace std;
static void error_callback(int e, const char *d)
{printf("Error %d: %s\n", e, d);}
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define MAX_VERTEX_BUFFER 512 * 1024
#define MAX_ELEMENT_BUFFER 128 * 1024
int main(int argc, char *argv[])
{
static GLFWwindow *win;
int width = 0, height = 0;
struct nk_context *ctx;
struct nk_color background;
if (!glfwInit()) {
fprintf(stdout, "[GFLW] failed to init!\n");
exit(1);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
win = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Demo", NULL, NULL);
glfwMakeContextCurrent(win);
glfwGetWindowSize(win, &width, &height);
/* OpenGL */
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glewExperimental = 1;
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to setup GLEW\n");
exit(1);
}
ctx = nk_glfw3_init(win, NK_GLFW3_INSTALL_CALLBACKS);
{
struct nk_font_atlas *atlas;
nk_glfw3_font_stash_begin(&atlas);
nk_glfw3_font_stash_end();
}
background = nk_rgb(28, 48, 62);
while( !glfwWindowShouldClose(win) ){
/* Input */
glfwPollEvents();
nk_glfw3_new_frame();
/* GUI */
if( nk_begin( ctx, "Demo", nk_rect( 50, 50, 230, 250 ),
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE) )
{
enum{EASY, HARD};
static int op = EASY;
static int property = 20;
nk_layout_row_static(ctx, 30, 80, 1);
if( nk_button_label(ctx, "button") ){
fprintf(stdout, "Button pressed\n");
}
nk_layout_row_dynamic( ctx, 30, 2 );
if( nk_option_label( ctx, "easy", op == EASY ) )
{
op = EASY;
}
if( nk_option_label( ctx, "easy", op == HARD ) )
{
op = HARD;
}
nk_layout_row_dynamic( ctx, 25, 1 );
nk_property_int( ctx, "Compression:", 0, &property, 100, 10, 1 );
nk_layout_row_dynamic( ctx, 20, 1 );
nk_label( ctx, "background:", NK_TEXT_LEFT );
nk_layout_row_dynamic( ctx, 25, 1 );
if( nk_combo_begin_color( ctx, background, nk_vec2( nk_widget_width( ctx ), 400 ) ) )
{
nk_layout_row_dynamic( ctx, 120, 1 );
background = nk_color_picker( ctx, background, NK_RGBA );
nk_layout_row_dynamic( ctx, 25, 1 );
background.r = (nk_byte)nk_propertyi(ctx, "#R: ", 0, background.r, 255, 1, 1);
background.g = (nk_byte)nk_propertyi(ctx, "#G: ", 0, background.g, 255, 1, 1);
background.b = (nk_byte)nk_propertyi(ctx, "#B: ", 0, background.b, 255, 1, 1);
background.a = (nk_byte)nk_propertyi(ctx, "#A: ", 0, background.a, 255, 1, 1);
nk_combo_end(ctx);
}
}
nk_end(ctx);
{
float bg[4];
nk_color_fv(bg, background);
glfwGetWindowSize( win, &width, &height );
glViewport( 0, 0, width, height );
glClear( GL_COLOR_BUFFER_BIT );
glClearColor( bg[0], bg[1], bg[2], bg[3] );
// nk_glfw3_render change the rendering state
nk_glfw3_render( NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER );
glfwSwapBuffers(win);
}
}
nk_glfw3_shutdown();
glfwTerminate();
return 0;
}
Explicação do exemplo
#define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_STANDARD_VARARGS
#define NK_INCLUDE_DEFAULT_ALLOCATOR
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
#define NK_INCLUDE_FONT_BAKING
#define NK_INCLUDE_DEFAULT_FONT
#define NK_IMPLEMENTATION
#define NK_GLFW_GL3_IMPLEMENTATION
Antes de incluir o arquivo Nuklear.h há algumas definições que devem ser feitas. Não há necessidade de se incluir todos os defines como nesse exemplo, mas no mínimo NK_IMPLEMENTATION deve ser definido. Além disso, outros arquivos que incluem Nuklear.h devem também incluir essas mesmas definições para evitar erros tanto de compilação e até mesmo de execução em algumas situações.
Links
https://github.com/vurtun/nuklear Repositório oficial da Nuklear
revisão da página: 2, última edição: 05 Jan 2018 23:56





