main.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* nuklear - 1.32.0 - public domain */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <stdarg.h>
  6. #include <string.h>
  7. #include <math.h>
  8. #include <assert.h>
  9. #include <math.h>
  10. #include <limits.h>
  11. #include <time.h>
  12. #include <GL/glew.h>
  13. #include <SDL2/SDL.h>
  14. #include <SDL2/SDL_opengl.h>
  15. #define NK_INCLUDE_FIXED_TYPES
  16. #define NK_INCLUDE_STANDARD_IO
  17. #define NK_INCLUDE_STANDARD_VARARGS
  18. #define NK_INCLUDE_DEFAULT_ALLOCATOR
  19. #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  20. #define NK_INCLUDE_FONT_BAKING
  21. #define NK_INCLUDE_DEFAULT_FONT
  22. #define NK_IMPLEMENTATION
  23. #define NK_SDL_GL3_IMPLEMENTATION
  24. #include "../../nuklear.h"
  25. #include "nuklear_sdl_gl3.h"
  26. #define WINDOW_WIDTH 800
  27. #define WINDOW_HEIGHT 600
  28. #define MAX_VERTEX_MEMORY 512 * 1024
  29. #define MAX_ELEMENT_MEMORY 128 * 1024
  30. #define UNUSED(a) (void)a
  31. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  32. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  33. #define LEN(a) (sizeof(a)/sizeof(a)[0])
  34. /* ===============================================================
  35. *
  36. * EXAMPLE
  37. *
  38. * ===============================================================*/
  39. /* This are some code examples to provide a small overview of what can be
  40. * done with this library. To try out an example uncomment the include
  41. * and the corresponding function. */
  42. /*#include "../style.c"*/
  43. /*#include "../calculator.c"*/
  44. /*#include "../overview.c"*/
  45. /*#include "../node_editor.c"*/
  46. /* ===============================================================
  47. *
  48. * DEMO
  49. *
  50. * ===============================================================*/
  51. int main(int argc, char* argv[])
  52. {
  53. /* Platform */
  54. SDL_Window *win;
  55. SDL_GLContext glContext;
  56. struct nk_color background;
  57. int win_width, win_height;
  58. int running = 1;
  59. /* GUI */
  60. struct nk_context *ctx;
  61. /* SDL setup */
  62. SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0");
  63. SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_EVENTS);
  64. SDL_GL_SetAttribute (SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
  65. SDL_GL_SetAttribute (SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  66. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  67. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  68. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  69. win = SDL_CreateWindow("Demo",
  70. SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  71. WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_ALLOW_HIGHDPI);
  72. glContext = SDL_GL_CreateContext(win);
  73. SDL_GetWindowSize(win, &win_width, &win_height);
  74. /* OpenGL setup */
  75. glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
  76. glewExperimental = 1;
  77. if (glewInit() != GLEW_OK) {
  78. fprintf(stderr, "Failed to setup GLEW\n");
  79. exit(1);
  80. }
  81. ctx = nk_sdl_init(win);
  82. /* Load Fonts: if none of these are loaded a default font will be used */
  83. /* Load Cursor: if you uncomment cursor loading please hide the cursor */
  84. {struct nk_font_atlas *atlas;
  85. nk_sdl_font_stash_begin(&atlas);
  86. /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
  87. /*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 16, 0);*/
  88. /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
  89. /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
  90. /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
  91. /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
  92. nk_sdl_font_stash_end();
  93. /*nk_style_load_all_cursors(ctx, atlas->cursors);*/
  94. /*nk_style_set_font(ctx, &roboto->handle);*/}
  95. /* style.c */
  96. /*set_style(ctx, THEME_WHITE);*/
  97. /*set_style(ctx, THEME_RED);*/
  98. /*set_style(ctx, THEME_BLUE);*/
  99. /*set_style(ctx, THEME_DARK);*/
  100. background = nk_rgb(28,48,62);
  101. while (running)
  102. {
  103. /* Input */
  104. SDL_Event evt;
  105. nk_input_begin(ctx);
  106. while (SDL_PollEvent(&evt)) {
  107. if (evt.type == SDL_QUIT) goto cleanup;
  108. nk_sdl_handle_event(&evt);
  109. } nk_input_end(ctx);
  110. /* GUI */
  111. if (nk_begin(ctx, "Demo1", nk_rect(50, 50, 200, 200),
  112. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  113. NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  114. {
  115. enum {EASY, HARD};
  116. static int op = EASY;
  117. static int property = 20;
  118. nk_layout_row_static(ctx, 30, 80, 1);
  119. if (nk_button_label(ctx, "button"))
  120. printf("button pressed!\n");
  121. nk_layout_row_dynamic(ctx, 30, 2);
  122. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  123. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  124. nk_layout_row_dynamic(ctx, 22, 1);
  125. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  126. }
  127. nk_end(ctx);
  128. /* -------------- EXAMPLES ---------------- */
  129. /*calculator(ctx);*/
  130. /*overview(ctx);*/
  131. /*node_editor(ctx);*/
  132. /* ----------------------------------------- */
  133. /* Draw */
  134. {float bg[4];
  135. nk_color_fv(bg, background);
  136. SDL_GetWindowSize(win, &win_width, &win_height);
  137. glViewport(0, 0, win_width, win_height);
  138. glClear(GL_COLOR_BUFFER_BIT);
  139. glClearColor(bg[0], bg[1], bg[2], bg[3]);
  140. /* IMPORTANT: `nk_sdl_render` modifies some global OpenGL state
  141. * with blending, scissor, face culling, depth test and viewport and
  142. * defaults everything back into a default state.
  143. * Make sure to either a.) save and restore or b.) reset your own state after
  144. * rendering the UI. */
  145. nk_sdl_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_MEMORY, MAX_ELEMENT_MEMORY);
  146. SDL_GL_SwapWindow(win);}
  147. }
  148. cleanup:
  149. nk_sdl_shutdown();
  150. SDL_GL_DeleteContext(glContext);
  151. SDL_DestroyWindow(win);
  152. SDL_Quit();
  153. return 0;
  154. }