2017-01-15 23:04:51 +03:00
|
|
|
/* nuklear - 1.32.0 - public domain */
|
2016-04-11 02:34:59 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <math.h>
|
2016-04-29 18:06:31 +03:00
|
|
|
#include <limits.h>
|
|
|
|
#include <time.h>
|
2016-04-11 02:34:59 +03:00
|
|
|
|
|
|
|
#include <GL/glew.h>
|
|
|
|
#include <SDL2/SDL.h>
|
2016-04-19 14:46:06 +03:00
|
|
|
#include <SDL2/SDL_opengl.h>
|
2016-04-11 02:34:59 +03:00
|
|
|
|
|
|
|
#define NK_INCLUDE_FIXED_TYPES
|
|
|
|
#define NK_INCLUDE_STANDARD_IO
|
2016-08-06 18:44:00 +03:00
|
|
|
#define NK_INCLUDE_STANDARD_VARARGS
|
2016-04-11 02:34:59 +03:00
|
|
|
#define NK_INCLUDE_DEFAULT_ALLOCATOR
|
|
|
|
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
|
|
|
|
#define NK_INCLUDE_FONT_BAKING
|
|
|
|
#define NK_INCLUDE_DEFAULT_FONT
|
2016-04-30 18:09:01 +03:00
|
|
|
#define NK_IMPLEMENTATION
|
|
|
|
#define NK_SDL_GL3_IMPLEMENTATION
|
|
|
|
#include "../../nuklear.h"
|
|
|
|
#include "nuklear_sdl_gl3.h"
|
2016-04-11 02:34:59 +03:00
|
|
|
|
2016-10-30 00:23:46 +03:00
|
|
|
#define WINDOW_WIDTH 800
|
|
|
|
#define WINDOW_HEIGHT 600
|
2016-04-11 02:34:59 +03:00
|
|
|
|
|
|
|
#define MAX_VERTEX_MEMORY 512 * 1024
|
|
|
|
#define MAX_ELEMENT_MEMORY 128 * 1024
|
|
|
|
|
2016-04-29 18:06:31 +03:00
|
|
|
#define UNUSED(a) (void)a
|
|
|
|
#define MIN(a,b) ((a) < (b) ? (a) : (b))
|
|
|
|
#define MAX(a,b) ((a) < (b) ? (b) : (a))
|
|
|
|
#define LEN(a) (sizeof(a)/sizeof(a)[0])
|
|
|
|
|
2016-04-28 15:38:44 +03:00
|
|
|
/* ===============================================================
|
|
|
|
*
|
|
|
|
* EXAMPLE
|
|
|
|
*
|
|
|
|
* ===============================================================*/
|
|
|
|
/* This are some code examples to provide a small overview of what can be
|
|
|
|
* done with this library. To try out an example uncomment the include
|
|
|
|
* and the corresponding function. */
|
|
|
|
/*#include "../style.c"*/
|
|
|
|
/*#include "../calculator.c"*/
|
|
|
|
/*#include "../overview.c"*/
|
|
|
|
/*#include "../node_editor.c"*/
|
|
|
|
|
|
|
|
/* ===============================================================
|
|
|
|
*
|
|
|
|
* DEMO
|
|
|
|
*
|
|
|
|
* ===============================================================*/
|
2016-04-11 02:34:59 +03:00
|
|
|
int
|
2016-04-20 04:46:48 +03:00
|
|
|
main(int argc, char* argv[])
|
2016-04-11 02:34:59 +03:00
|
|
|
{
|
|
|
|
/* Platform */
|
|
|
|
SDL_Window *win;
|
|
|
|
SDL_GLContext glContext;
|
|
|
|
struct nk_color background;
|
|
|
|
int win_width, win_height;
|
|
|
|
int running = 1;
|
|
|
|
|
|
|
|
/* GUI */
|
|
|
|
struct nk_context *ctx;
|
|
|
|
|
|
|
|
/* SDL setup */
|
2016-05-05 19:38:06 +03:00
|
|
|
SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0");
|
2016-04-11 02:34:59 +03:00
|
|
|
SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_EVENTS);
|
2016-04-19 15:05:59 +03:00
|
|
|
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);
|
2016-04-11 02:34:59 +03:00
|
|
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
|
|
|
win = SDL_CreateWindow("Demo",
|
|
|
|
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
2016-05-05 19:38:06 +03:00
|
|
|
WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_ALLOW_HIGHDPI);
|
2016-04-11 02:34:59 +03:00
|
|
|
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 */
|
2016-07-15 19:04:50 +03:00
|
|
|
/* Load Cursor: if you uncomment cursor loading please hide the cursor */
|
2016-04-11 02:34:59 +03:00
|
|
|
{struct nk_font_atlas *atlas;
|
|
|
|
nk_sdl_font_stash_begin(&atlas);
|
|
|
|
/*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
|
2016-04-21 11:54:01 +03:00
|
|
|
/*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 16, 0);*/
|
2016-04-11 02:34:59 +03:00
|
|
|
/*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
|
|
|
|
/*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
|
|
|
|
/*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
|
|
|
|
/*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
|
|
|
|
nk_sdl_font_stash_end();
|
2016-07-15 19:04:50 +03:00
|
|
|
/*nk_style_load_all_cursors(ctx, atlas->cursors);*/
|
2016-04-21 11:54:01 +03:00
|
|
|
/*nk_style_set_font(ctx, &roboto->handle)*/;}
|
2016-04-11 02:34:59 +03:00
|
|
|
|
2016-04-28 15:38:44 +03:00
|
|
|
/* style.c */
|
|
|
|
/*set_style(ctx, THEME_WHITE);*/
|
|
|
|
/*set_style(ctx, THEME_RED);*/
|
|
|
|
/*set_style(ctx, THEME_BLUE);*/
|
|
|
|
/*set_style(ctx, THEME_DARK);*/
|
|
|
|
|
2016-04-11 02:34:59 +03:00
|
|
|
background = nk_rgb(28,48,62);
|
|
|
|
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);
|
|
|
|
|
2016-10-30 00:23:46 +03:00
|
|
|
|
2016-04-11 02:34:59 +03:00
|
|
|
/* GUI */
|
2016-10-30 00:23:46 +03:00
|
|
|
if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
|
2016-04-11 02:34:59 +03:00
|
|
|
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
|
2016-10-30 00:23:46 +03:00
|
|
|
NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
|
2016-04-11 02:34:59 +03:00
|
|
|
{
|
2016-10-30 00:23:46 +03:00
|
|
|
nk_menubar_begin(ctx);
|
|
|
|
nk_layout_row_begin(ctx, NK_STATIC, 25, 2);
|
|
|
|
nk_layout_row_push(ctx, 45);
|
|
|
|
if (nk_menu_begin_label(ctx, "FILE", NK_TEXT_LEFT, nk_vec2(120, 200))) {
|
|
|
|
nk_layout_row_dynamic(ctx, 30, 1);
|
|
|
|
nk_menu_item_label(ctx, "OPEN", NK_TEXT_LEFT);
|
|
|
|
nk_menu_item_label(ctx, "CLOSE", NK_TEXT_LEFT);
|
|
|
|
nk_menu_end(ctx);
|
|
|
|
}
|
|
|
|
nk_layout_row_push(ctx, 45);
|
|
|
|
if (nk_menu_begin_label(ctx, "EDIT", NK_TEXT_LEFT, nk_vec2(120, 200))) {
|
|
|
|
nk_layout_row_dynamic(ctx, 30, 1);
|
|
|
|
nk_menu_item_label(ctx, "COPY", NK_TEXT_LEFT);
|
|
|
|
nk_menu_item_label(ctx, "CUT", NK_TEXT_LEFT);
|
|
|
|
nk_menu_item_label(ctx, "PASTE", NK_TEXT_LEFT);
|
|
|
|
nk_menu_end(ctx);
|
|
|
|
}
|
|
|
|
nk_layout_row_end(ctx);
|
|
|
|
nk_menubar_end(ctx);
|
|
|
|
|
2016-04-11 02:34:59 +03:00
|
|
|
enum {EASY, HARD};
|
|
|
|
static int op = EASY;
|
|
|
|
static int property = 20;
|
|
|
|
nk_layout_row_static(ctx, 30, 80, 1);
|
2016-07-15 19:04:50 +03:00
|
|
|
if (nk_button_label(ctx, "button"))
|
2016-04-11 02:34:59 +03:00
|
|
|
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, "hard", op == HARD)) op = HARD;
|
2016-04-21 11:54:01 +03:00
|
|
|
nk_layout_row_dynamic(ctx, 25, 1);
|
2016-04-11 02:34:59 +03:00
|
|
|
nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
|
|
|
|
}
|
2016-10-30 00:23:46 +03:00
|
|
|
nk_end(ctx);
|
2016-04-11 02:34:59 +03:00
|
|
|
|
2016-04-28 15:38:44 +03:00
|
|
|
/* -------------- EXAMPLES ---------------- */
|
|
|
|
/*calculator(ctx);*/
|
|
|
|
/*overview(ctx);*/
|
|
|
|
/*node_editor(ctx);*/
|
|
|
|
/* ----------------------------------------- */
|
|
|
|
|
2016-04-11 02:34:59 +03:00
|
|
|
/* Draw */
|
|
|
|
{float bg[4];
|
|
|
|
nk_color_fv(bg, background);
|
|
|
|
SDL_GetWindowSize(win, &win_width, &win_height);
|
|
|
|
glViewport(0, 0, win_width, win_height);
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glClearColor(bg[0], bg[1], bg[2], bg[3]);
|
2016-04-26 19:24:43 +03:00
|
|
|
/* IMPORTANT: `nk_sdl_render` modifies some global OpenGL state
|
2016-04-29 14:16:18 +03:00
|
|
|
* 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. */
|
2016-04-11 02:34:59 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|