#include #include #include #include #include #include #include #include #include #include #include #define GLFW_INCLUDE_ES2 #include #define NK_INCLUDE_FIXED_TYPES #define NK_INCLUDE_STANDARD_IO #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 "../../nuklear.h" #include "nuklear_glfw_gl3.h" #define MAX_VERTEX_BUFFER 512 * 1024 #define MAX_ELEMENT_BUFFER 128 * 1024 #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]) int result = 1; GLFWwindow* win; struct nk_context *ctx; struct nk_color background; int inFullscreen = 0; int wasFullscreen = 0; int width = 0; int height = 0; void render(); void error_callback(int error, const char* description); void render() { /* Input */ glfwPollEvents(); nk_glfw3_new_frame(); /* GUI */ {struct nk_panel layout; if (nk_begin(ctx, &layout, "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", NK_BUTTON_DEFAULT)) 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; nk_layout_row_dynamic(ctx, 25, 1); nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1); {struct nk_panel combo; 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, &combo, background, 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);} /* Draw */ {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]); /* IMPORTANT: `nk_glfw_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_glfw3_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER); glfwSwapBuffers(win);} } void error_callback(int error, const char* description) { fprintf(stderr, "Error %d: %s\n", error, description); } void windowSizeCallback(GLFWwindow* window, int width, int height) { int isInFullscreen = EM_ASM_INT_V(return !!(document.fullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement || document.msFullscreenElement)); if (isInFullscreen && !wasFullscreen) { printf("Successfully transitioned to fullscreen mode!\n"); wasFullscreen = isInFullscreen; } if (wasFullscreen && !isInFullscreen) { printf("Exited fullscreen. Test succeeded.\n"); result = 1; #ifdef REPORT_RESULT REPORT_RESULT(); #endif wasFullscreen = isInFullscreen; emscripten_cancel_main_loop(); return; } } int main() { /* Setup win */ glfwSetErrorCallback(error_callback); if (!glfwInit()) { result = 0; printf("Could not create window. Test failed.\n"); #ifdef REPORT_RESULT REPORT_RESULT(); #endif return -1; } glfwWindowHint(GLFW_RESIZABLE , 1); win = glfwCreateWindow(1024, 1024, "GLFW resizing test - windowed", NULL, NULL); if (!win) { result = 0; printf("Could not create window. Test failed.\n"); #ifdef REPORT_RESULT REPORT_RESULT(); #endif glfwTerminate(); return -1; } glfwMakeContextCurrent(win); /* Install callbacks */ glfwSetWindowSizeCallback(win, windowSizeCallback); /* Nuklear */ ctx = nk_glfw3_init(win, NK_GLFW3_INSTALL_CALLBACKS); /* Load Fonts: if none of these are loaded a default font will be used */ {struct nk_font_atlas *atlas; nk_glfw3_font_stash_begin(&atlas); /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/ /*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 14, 0);*/ /*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_glfw3_font_stash_end(); /*nk_style_set_font(ctx, &droid->handle);*/} background = nk_rgb(190,205,255); /* Main loop */ emscripten_set_main_loop(render, 0, 1); nk_glfw3_shutdown(); glfwTerminate(); return 0; }