main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 <limits.h>
  10. #include <time.h>
  11. #include <allegro5/allegro.h>
  12. #define WINDOW_WIDTH 1200
  13. #define WINDOW_HEIGHT 800
  14. #define NK_INCLUDE_FIXED_TYPES
  15. #define NK_INCLUDE_STANDARD_IO
  16. #define NK_INCLUDE_STANDARD_VARARGS
  17. #define NK_INCLUDE_DEFAULT_ALLOCATOR
  18. #define NK_IMPLEMENTATION
  19. #define NK_ALLEGRO5_IMPLEMENTATION
  20. #include "../../nuklear.h"
  21. #include "nuklear_allegro5.h"
  22. #define UNUSED(a) (void)a
  23. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  24. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  25. #define LEN(a) (sizeof(a)/sizeof(a)[0])
  26. /* ===============================================================
  27. *
  28. * EXAMPLE
  29. *
  30. * ===============================================================*/
  31. /* This are some code examples to provide a small overview of what can be
  32. * done with this library. To try out an example uncomment the defines */
  33. /*#define INCLUDE_ALL */
  34. /*#define INCLUDE_STYLE */
  35. /*#define INCLUDE_CALCULATOR */
  36. /*#define INCLUDE_CANVAS */
  37. #define INCLUDE_OVERVIEW
  38. /*#define INCLUDE_CONFIGURATOR */
  39. /*#define INCLUDE_NODE_EDITOR */
  40. #ifdef INCLUDE_ALL
  41. #define INCLUDE_STYLE
  42. #define INCLUDE_CALCULATOR
  43. #define INCLUDE_CANVAS
  44. #define INCLUDE_OVERVIEW
  45. #define INCLUDE_CONFIGURATOR
  46. #define INCLUDE_NODE_EDITOR
  47. #endif
  48. #ifdef INCLUDE_STYLE
  49. #include "../../demo/common/style.c"
  50. #endif
  51. #ifdef INCLUDE_CALCULATOR
  52. #include "../../demo/common/calculator.c"
  53. #endif
  54. #ifdef INCLUDE_CANVAS
  55. #include "../../demo/common/canvas.c"
  56. #endif
  57. #ifdef INCLUDE_OVERVIEW
  58. #include "../../demo/common/overview.c"
  59. #endif
  60. #ifdef INCLUDE_CONFIGURATOR
  61. #include "../../demo/common/style_configurator.c"
  62. #endif
  63. #ifdef INCLUDE_NODE_EDITOR
  64. #include "../../demo/common/node_editor.c"
  65. #endif
  66. /* ===============================================================
  67. *
  68. * DEMO
  69. *
  70. * ===============================================================*/
  71. int main(void)
  72. {
  73. /* Platform */
  74. ALLEGRO_DISPLAY *display = NULL;
  75. ALLEGRO_EVENT_QUEUE *event_queue = NULL;
  76. NkAllegro5Font *font;
  77. struct nk_context *ctx;
  78. #ifdef INCLUDE_CONFIGURATOR
  79. static struct nk_color color_table[NK_COLOR_COUNT];
  80. memcpy(color_table, nk_default_color_style, sizeof(color_table));
  81. #endif
  82. if (!al_init()) {
  83. fprintf(stdout, "failed to initialize allegro5!\n");
  84. exit(1);
  85. }
  86. al_install_mouse();
  87. al_set_mouse_wheel_precision(150);
  88. al_install_keyboard();
  89. al_set_new_display_flags(ALLEGRO_WINDOWED|ALLEGRO_RESIZABLE|ALLEGRO_OPENGL);
  90. al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST);
  91. al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST);
  92. display = al_create_display(WINDOW_WIDTH, WINDOW_HEIGHT);
  93. if (!display) {
  94. fprintf(stdout, "failed to create display!\n");
  95. exit(1);
  96. }
  97. event_queue = al_create_event_queue();
  98. if (!event_queue) {
  99. fprintf(stdout, "failed to create event_queue!\n");
  100. al_destroy_display(display);
  101. exit(1);
  102. }
  103. al_register_event_source(event_queue, al_get_display_event_source(display));
  104. al_register_event_source(event_queue, al_get_mouse_event_source());
  105. al_register_event_source(event_queue, al_get_keyboard_event_source());
  106. font = nk_allegro5_font_create_from_file("../../extra_font/Roboto-Regular.ttf", 12, 0);
  107. ctx = nk_allegro5_init(font, display, WINDOW_WIDTH, WINDOW_HEIGHT);
  108. while(1)
  109. {
  110. bool get_event;
  111. ALLEGRO_EVENT ev;
  112. ALLEGRO_TIMEOUT timeout;
  113. al_init_timeout(&timeout, 0.06);
  114. get_event = al_wait_for_event_until(event_queue, &ev, &timeout);
  115. if (get_event && ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
  116. break;
  117. }
  118. /* Very Important: Always do nk_input_begin / nk_input_end even if
  119. there are no events, otherwise internal nuklear state gets messed up */
  120. nk_input_begin(ctx);
  121. if (get_event) {
  122. while (get_event) {
  123. nk_allegro5_handle_event(&ev);
  124. get_event = al_get_next_event(event_queue, &ev);
  125. }
  126. }
  127. nk_input_end(ctx);
  128. /* GUI */
  129. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
  130. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  131. NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  132. {
  133. enum {EASY, HARD};
  134. static int op = EASY;
  135. static int property = 20;
  136. nk_layout_row_static(ctx, 30, 80, 1);
  137. if (nk_button_label(ctx, "button"))
  138. fprintf(stdout, "button pressed\n");
  139. nk_layout_row_dynamic(ctx, 30, 2);
  140. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  141. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  142. nk_layout_row_dynamic(ctx, 22, 1);
  143. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  144. }
  145. nk_end(ctx);
  146. /* -------------- EXAMPLES ---------------- */
  147. #ifdef INCLUDE_CALCULATOR
  148. calculator(ctx);
  149. #endif
  150. #ifdef INCLUDE_CANVAS
  151. canvas(ctx);
  152. #endif
  153. #ifdef INCLUDE_OVERVIEW
  154. overview(ctx);
  155. #endif
  156. #ifdef INCLUDE_CONFIGURATOR
  157. style_configurator(ctx, color_table);
  158. #endif
  159. #ifdef INCLUDE_NODE_EDITOR
  160. node_editor(ctx);
  161. #endif
  162. /* ----------------------------------------- */
  163. /* Draw */
  164. al_clear_to_color(al_map_rgb(19, 43, 81));
  165. /* IMPORTANT: `nk_allegro5_render` changes the target backbuffer
  166. to the display set at initialization and does not restore it.
  167. Change it if you want to draw somewhere else. */
  168. nk_allegro5_render();
  169. al_flip_display();
  170. }
  171. nk_allegro5_font_del(font);
  172. nk_allegro5_shutdown();
  173. al_destroy_display(display);
  174. al_destroy_event_queue(event_queue);
  175. return 0;
  176. }