main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <stdarg.h>
  5. #include <string.h>
  6. #include <math.h>
  7. #include <assert.h>
  8. #include <math.h>
  9. #include <limits.h>
  10. #include <time.h>
  11. #include <emscripten.h>
  12. #define GLFW_INCLUDE_ES2
  13. #include <GLFW/glfw3.h>
  14. #define NK_INCLUDE_FIXED_TYPES
  15. #define NK_INCLUDE_STANDARD_IO
  16. #define NK_INCLUDE_DEFAULT_ALLOCATOR
  17. #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  18. #define NK_INCLUDE_FONT_BAKING
  19. #define NK_INCLUDE_DEFAULT_FONT
  20. #define NK_IMPLEMENTATION
  21. #define NK_GLFW_GL3_IMPLEMENTATION
  22. #include "../../nuklear.h"
  23. #include "nuklear_glfw_gl3.h"
  24. #define MAX_VERTEX_BUFFER 512 * 1024
  25. #define MAX_ELEMENT_BUFFER 128 * 1024
  26. #define UNUSED(a) (void)a
  27. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  28. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  29. #define LEN(a) (sizeof(a)/sizeof(a)[0])
  30. int result = 1;
  31. GLFWwindow* win;
  32. struct nk_context *ctx;
  33. struct nk_color background;
  34. int inFullscreen = 0;
  35. int wasFullscreen = 0;
  36. int width = 0;
  37. int height = 0;
  38. void render();
  39. void error_callback(int error, const char* description);
  40. void render() {
  41. /* Input */
  42. glfwPollEvents();
  43. nk_glfw3_new_frame();
  44. /* GUI */
  45. {struct nk_panel layout;
  46. if (nk_begin(ctx, &layout, "Demo", nk_rect(50, 50, 230, 250),
  47. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  48. NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  49. {
  50. enum {EASY, HARD};
  51. static int op = EASY;
  52. static int property = 20;
  53. nk_layout_row_static(ctx, 30, 80, 1);
  54. if (nk_button_label(ctx, "button", NK_BUTTON_DEFAULT))
  55. fprintf(stdout, "button pressed\n");
  56. nk_layout_row_dynamic(ctx, 30, 2);
  57. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  58. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  59. nk_layout_row_dynamic(ctx, 25, 1);
  60. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  61. {struct nk_panel combo;
  62. nk_layout_row_dynamic(ctx, 20, 1);
  63. nk_label(ctx, "background:", NK_TEXT_LEFT);
  64. nk_layout_row_dynamic(ctx, 25, 1);
  65. if (nk_combo_begin_color(ctx, &combo, background, 400)) {
  66. nk_layout_row_dynamic(ctx, 120, 1);
  67. background = nk_color_picker(ctx, background, NK_RGBA);
  68. nk_layout_row_dynamic(ctx, 25, 1);
  69. background.r = (nk_byte)nk_propertyi(ctx, "#R:", 0, background.r, 255, 1,1);
  70. background.g = (nk_byte)nk_propertyi(ctx, "#G:", 0, background.g, 255, 1,1);
  71. background.b = (nk_byte)nk_propertyi(ctx, "#B:", 0, background.b, 255, 1,1);
  72. background.a = (nk_byte)nk_propertyi(ctx, "#A:", 0, background.a, 255, 1,1);
  73. nk_combo_end(ctx);
  74. }}
  75. }
  76. nk_end(ctx);}
  77. /* Draw */
  78. {float bg[4];
  79. nk_color_fv(bg, background);
  80. glfwGetWindowSize(win, &width, &height);
  81. glViewport(0, 0, width, height);
  82. glClear(GL_COLOR_BUFFER_BIT);
  83. glClearColor(bg[0], bg[1], bg[2], bg[3]);
  84. /* IMPORTANT: `nk_glfw_render` modifies some global OpenGL state
  85. * with blending, scissor, face culling, depth test and viewport and
  86. * defaults everything back into a default state.
  87. * Make sure to either a.) save and restore or b.) reset your own state after
  88. * rendering the UI. */
  89. nk_glfw3_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER);
  90. glfwSwapBuffers(win);}
  91. }
  92. void error_callback(int error, const char* description)
  93. {
  94. fprintf(stderr, "Error %d: %s\n", error, description);
  95. }
  96. void windowSizeCallback(GLFWwindow* window, int width, int height)
  97. {
  98. int isInFullscreen = EM_ASM_INT_V(return !!(document.fullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement || document.msFullscreenElement));
  99. if (isInFullscreen && !wasFullscreen)
  100. {
  101. printf("Successfully transitioned to fullscreen mode!\n");
  102. wasFullscreen = isInFullscreen;
  103. }
  104. if (wasFullscreen && !isInFullscreen)
  105. {
  106. printf("Exited fullscreen. Test succeeded.\n");
  107. result = 1;
  108. #ifdef REPORT_RESULT
  109. REPORT_RESULT();
  110. #endif
  111. wasFullscreen = isInFullscreen;
  112. emscripten_cancel_main_loop();
  113. return;
  114. }
  115. }
  116. int main()
  117. {
  118. /* Setup win */
  119. glfwSetErrorCallback(error_callback);
  120. if (!glfwInit())
  121. {
  122. result = 0;
  123. printf("Could not create window. Test failed.\n");
  124. #ifdef REPORT_RESULT
  125. REPORT_RESULT();
  126. #endif
  127. return -1;
  128. }
  129. glfwWindowHint(GLFW_RESIZABLE , 1);
  130. win = glfwCreateWindow(1024, 1024, "GLFW resizing test - windowed", NULL, NULL);
  131. if (!win)
  132. {
  133. result = 0;
  134. printf("Could not create window. Test failed.\n");
  135. #ifdef REPORT_RESULT
  136. REPORT_RESULT();
  137. #endif
  138. glfwTerminate();
  139. return -1;
  140. }
  141. glfwMakeContextCurrent(win);
  142. /* Install callbacks */
  143. glfwSetWindowSizeCallback(win, windowSizeCallback);
  144. /* Nuklear */
  145. ctx = nk_glfw3_init(win, NK_GLFW3_INSTALL_CALLBACKS);
  146. /* Load Fonts: if none of these are loaded a default font will be used */
  147. {struct nk_font_atlas *atlas;
  148. nk_glfw3_font_stash_begin(&atlas);
  149. /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
  150. /*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 14, 0);*/
  151. /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
  152. /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
  153. /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
  154. /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
  155. nk_glfw3_font_stash_end();
  156. /*nk_style_set_font(ctx, &droid->handle);*/}
  157. background = nk_rgb(190,205,255);
  158. /* Main loop */
  159. emscripten_set_main_loop(render, 0, 1);
  160. nk_glfw3_shutdown();
  161. glfwTerminate();
  162. return 0;
  163. }