Created
March 12, 2019 09:39
-
-
Save letarg0/a931dc9b4599e6526ba3d1aaec7b7557 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* nuklear - 1.32.0 - public domain */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <stdarg.h> | |
#include <string.h> | |
#include <math.h> | |
#include <assert.h> | |
#include <math.h> | |
#include <limits.h> | |
#include <time.h> | |
#include <GL/glew.h> | |
#include <SDL2/SDL.h> | |
#include <SDL2/SDL_opengl.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_SDL_GL3_IMPLEMENTATION | |
#include "nu/nuklear.h" | |
#include "nu/nuklear_sdl_gl3.h" | |
#define WINDOW_WIDTH 1000 | |
#define WINDOW_HEIGHT 800 | |
#define MAX_VERTEX_MEMORY 512 * 1024 | |
#define MAX_ELEMENT_MEMORY 128 * 1024 | |
/* =============================================================== | |
* | |
* DEMO | |
* | |
* ===============================================================*/ | |
int main(void) | |
{ | |
/* Platform */ | |
SDL_Window *win; | |
SDL_GLContext glContext; | |
int win_width, win_height; | |
int running = 1; | |
/* GUI */ | |
struct nk_context *ctx; | |
struct nk_colorf bg; | |
/* SDL setup */ | |
SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0"); | |
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_EVENTS); | |
SDL_GL_SetAttribute (SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); | |
SDL_GL_SetAttribute (SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); | |
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); | |
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
win = SDL_CreateWindow("Demo", | |
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, | |
WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_ALLOW_HIGHDPI); | |
glContext = SDL_GL_CreateContext(win); | |
SDL_GetWindowSize(win, &win_width, &win_height); | |
/* OpenGL setup */ | |
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); | |
glewExperimental = 1; | |
if (glewInit() != GLEW_OK) { | |
fprintf(stderr, "Failed to setup GLEW\n"); | |
exit(1); | |
} | |
ctx = nk_sdl_init(win); | |
/* Load Fonts: if none of these are loaded a default font will be used */ | |
/* Load Cursor: if you uncomment cursor loading please hide the cursor */ | |
{struct nk_font_atlas *atlas; | |
nk_sdl_font_stash_begin(&atlas); | |
/*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/ | |
nk_sdl_font_stash_end(); | |
/*nk_style_load_all_cursors(ctx, atlas->cursors);*/ | |
/*nk_style_set_font(ctx, &roboto->handle);*/} | |
/* style.c */ | |
#ifdef INCLUDE_STYLE | |
/*set_style(ctx, THEME_WHITE);*/ | |
/*set_style(ctx, THEME_RED);*/ | |
/*set_style(ctx, THEME_BLUE);*/ | |
/*set_style(ctx, THEME_DARK);*/ | |
#endif | |
bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f; | |
while (running) | |
{ | |
/* Input */ | |
SDL_Event evt; | |
nk_input_begin(ctx); | |
while (SDL_PollEvent(&evt)) { | |
if (evt.type == SDL_QUIT) goto cleanup; | |
nk_sdl_handle_event(&evt); | |
} nk_input_end(ctx); | |
/* 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")) | |
printf("button pressed! r=%f g=%f b=%f\n",(bg.r*255.0f),(bg.g*255.0f),(bg.b*255.0f)); | |
nk_layout_row_dynamic(ctx, 30, 2); | |
if (nk_option_label(ctx, "easy", op == EASY)) op = EASY; | |
if (nk_option_label(ctx, "hard", op == HARD)) op = HARD; | |
nk_layout_row_dynamic(ctx, 22, 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, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) { | |
nk_layout_row_dynamic(ctx, 120, 1); | |
bg = nk_color_picker(ctx, bg, NK_RGBA); | |
nk_layout_row_dynamic(ctx, 25, 1); | |
bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f); | |
bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f); | |
bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f); | |
bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f); | |
nk_combo_end(ctx); | |
} | |
} | |
case NK_COMMAND_TRIANGLE: { | |
const struct nk_command_triangle *t = (const struct nk_command_triangle*)cmd; | |
nk_draw_list_stroke_triangle(&ctx->draw_list, nk_vec2(t->a.x, t->a.y), | |
nk_vec2(t->b.x, t->b.y), nk_vec2(t->c.x, t->c.y), t->color, | |
t->line_thickness); | |
} break; | |
nk_end(ctx); | |
/* Draw */ | |
SDL_GetWindowSize(win, &win_width, &win_height); | |
glViewport(0, 0, win_width, win_height); | |
glClear(GL_COLOR_BUFFER_BIT); | |
glClearColor(bg.r, bg.g, bg.b, bg.a); | |
// SDL_SetRenderDrawColor( renderer, 170, 80, 89 , 1); | |
// SDL_RenderDrawLine( renderer, 0, 0, 400, 400); | |
/* IMPORTANT: `nk_sdl_render` modifies some global OpenGL state | |
* with blending, scissor, face culling, depth test and viewport and | |
* defaults everything back into a default state. | |
* Make sure to either a.) save and restore or b.) reset your own state after | |
* rendering the UI. */ | |
nk_sdl_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_MEMORY, MAX_ELEMENT_MEMORY); | |
SDL_GL_SwapWindow(win); | |
} | |
cleanup: | |
nk_sdl_shutdown(); | |
SDL_GL_DeleteContext(glContext); | |
SDL_DestroyWindow(win); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment