main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* nuklear - v1.00 - 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. /* macros */
  13. #define WINDOW_WIDTH 1200
  14. #define WINDOW_HEIGHT 800
  15. #define MAX_VERTEX_MEMORY 512 * 1024
  16. #define MAX_ELEMENT_MEMORY 128 * 1024
  17. #define NK_INCLUDE_FIXED_TYPES
  18. #define NK_INCLUDE_STANDARD_IO
  19. #define NK_INCLUDE_DEFAULT_ALLOCATOR
  20. #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  21. #define NK_INCLUDE_FONT_BAKING
  22. #define NK_INCLUDE_DEFAULT_FONT
  23. #define NK_IMPLEMENTATION
  24. #define NK_ALLEGRO_IMPLEMENTATION
  25. #include "../../nuklear.h"
  26. #include "nuklear_allegro.h"
  27. #define UNUSED(a) (void)a
  28. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  29. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  30. #define LEN(a) (sizeof(a)/sizeof(a)[0])
  31. /* ===============================================================
  32. *
  33. * EXAMPLE
  34. *
  35. * ===============================================================*/
  36. /* This are some code examples to provide a small overview of what can be
  37. * done with this library. To try out an example uncomment the include
  38. * and the corresponding function. */
  39. /*#include "../style.c"*/
  40. /*#include "../calculator.c"*/
  41. /*#include "../overview.c"*/
  42. /*#include "../node_editor.c"*/
  43. /* ===============================================================
  44. *
  45. * DEMO
  46. *
  47. * ===============================================================*/
  48. int
  49. main(void)
  50. {
  51. int running = 1;
  52. struct nk_context *ctx;
  53. struct nk_color background;
  54. ALLEGRO_DISPLAY *display;
  55. ALLEGRO_EVENT_QUEUE *queue;
  56. /* Allegro */
  57. al_init();
  58. al_install_keyboard();
  59. al_install_mouse();
  60. al_init_primitives_addon();
  61. display = al_create_display(WINDOW_WIDTH, WINDOW_HEIGHT);
  62. al_set_window_title(display, "nuklear");
  63. queue = al_create_event_queue();
  64. al_register_event_source(queue, al_get_display_event_source(display));
  65. al_register_event_source(queue, al_get_keyboard_event_source());
  66. al_register_event_source(queue, al_get_mouse_event_source());
  67. ctx = nk_allegro_init(display, MAX_VERTEX_MEMORY, MAX_ELEMENT_MEMORY);
  68. /* Load Fonts: if none of these are loaded a default font will be used */
  69. {struct nk_font_atlas *atlas;
  70. nk_allegro_font_stash_begin(&atlas);
  71. /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
  72. /*struct nk_font *robot = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Robot-Regular.ttf", 14, 0);*/
  73. /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
  74. /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
  75. /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
  76. /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
  77. nk_allegro_font_stash_end();
  78. /*nk_style_set_font(ctx, &droid->handle);*/}
  79. /* style.c */
  80. /*set_style(ctx, THEME_WHITE);*/
  81. /*set_style(ctx, THEME_RED);*/
  82. /*set_style(ctx, THEME_BLUE);*/
  83. /*set_style(ctx, THEME_DARK);*/
  84. background = nk_rgb(28,48,62);
  85. while (running)
  86. {
  87. /* Input */
  88. ALLEGRO_EVENT evt;
  89. nk_input_begin(ctx);
  90. while (al_get_next_event(queue, &evt)) {
  91. if (evt.type == ALLEGRO_EVENT_DISPLAY_CLOSE) goto cleanup;
  92. nk_allegro_handle_event(&evt);
  93. }
  94. nk_input_end(ctx);
  95. /* GUI */
  96. {struct nk_panel layout;
  97. if (nk_begin(ctx, &layout, "Demo", nk_rect(50, 50, 200, 200),
  98. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  99. NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  100. {
  101. enum {EASY, HARD};
  102. static int op = EASY;
  103. static int property = 20;
  104. nk_layout_row_static(ctx, 30, 80, 1);
  105. if (nk_button_label(ctx, "button", NK_BUTTON_DEFAULT))
  106. fprintf(stdout, "button pressed\n");
  107. nk_layout_row_dynamic(ctx, 30, 2);
  108. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  109. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  110. nk_layout_row_dynamic(ctx, 25, 1);
  111. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  112. {struct nk_panel combo;
  113. nk_layout_row_dynamic(ctx, 20, 1);
  114. nk_label(ctx, "background:", NK_TEXT_LEFT);
  115. nk_layout_row_dynamic(ctx, 25, 1);
  116. if (nk_combo_begin_color(ctx, &combo, background, 400)) {
  117. nk_layout_row_dynamic(ctx, 120, 1);
  118. background = nk_color_picker(ctx, background, NK_RGBA);
  119. nk_layout_row_dynamic(ctx, 25, 1);
  120. background.r = (nk_byte)nk_propertyi(ctx, "#R:", 0, background.r, 255, 1,1);
  121. background.g = (nk_byte)nk_propertyi(ctx, "#G:", 0, background.g, 255, 1,1);
  122. background.b = (nk_byte)nk_propertyi(ctx, "#B:", 0, background.b, 255, 1,1);
  123. background.a = (nk_byte)nk_propertyi(ctx, "#A:", 0, background.a, 255, 1,1);
  124. nk_combo_end(ctx);
  125. }}
  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. al_clear_to_color(al_map_rgba_f(bg[0], bg[1], bg[2], bg[3]));
  137. nk_allegro_render(NK_ANTI_ALIASING_ON);
  138. al_flip_display();}
  139. }
  140. cleanup:
  141. /* Cleanup */
  142. if (queue) al_destroy_event_queue(queue);
  143. if (display) al_destroy_display(display);
  144. nk_allegro_shutdown();
  145. return 0;
  146. }