main.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_NODE_EDITOR */
  39. #ifdef INCLUDE_ALL
  40. #define INCLUDE_STYLE
  41. #define INCLUDE_CALCULATOR
  42. #define INCLUDE_CANVAS
  43. #define INCLUDE_OVERVIEW
  44. #define INCLUDE_NODE_EDITOR
  45. #endif
  46. #ifdef INCLUDE_STYLE
  47. #include "../../demo/common/style.c"
  48. #endif
  49. #ifdef INCLUDE_CALCULATOR
  50. #include "../../demo/common/calculator.c"
  51. #endif
  52. #ifdef INCLUDE_CANVAS
  53. #include "../../demo/common/canvas.c"
  54. #endif
  55. #ifdef INCLUDE_OVERVIEW
  56. #include "../../demo/common/overview.c"
  57. #endif
  58. #ifdef INCLUDE_NODE_EDITOR
  59. #include "../../demo/common/node_editor.c"
  60. #endif
  61. /* ===============================================================
  62. *
  63. * DEMO
  64. *
  65. * ===============================================================*/
  66. int main(void)
  67. {
  68. /* Platform */
  69. ALLEGRO_DISPLAY *display = NULL;
  70. ALLEGRO_EVENT_QUEUE *event_queue = NULL;
  71. NkAllegro5Font *font;
  72. struct nk_context *ctx;
  73. if (!al_init()) {
  74. fprintf(stdout, "failed to initialize allegro5!\n");
  75. exit(1);
  76. }
  77. al_install_mouse();
  78. al_set_mouse_wheel_precision(150);
  79. al_install_keyboard();
  80. al_set_new_display_flags(ALLEGRO_WINDOWED|ALLEGRO_RESIZABLE|ALLEGRO_OPENGL);
  81. al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST);
  82. al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST);
  83. display = al_create_display(WINDOW_WIDTH, WINDOW_HEIGHT);
  84. if (!display) {
  85. fprintf(stdout, "failed to create display!\n");
  86. exit(1);
  87. }
  88. event_queue = al_create_event_queue();
  89. if (!event_queue) {
  90. fprintf(stdout, "failed to create event_queue!\n");
  91. al_destroy_display(display);
  92. exit(1);
  93. }
  94. al_register_event_source(event_queue, al_get_display_event_source(display));
  95. al_register_event_source(event_queue, al_get_mouse_event_source());
  96. al_register_event_source(event_queue, al_get_keyboard_event_source());
  97. font = nk_allegro5_font_create_from_file("../../../extra_font/Roboto-Regular.ttf", 12, 0);
  98. ctx = nk_allegro5_init(font, display, WINDOW_WIDTH, WINDOW_HEIGHT);
  99. /* style.c */
  100. #ifdef INCLUDE_STYLE
  101. /* ease regression testing during Nuklear release process; not needed for anything else */
  102. #ifdef STYLE_WHITE
  103. set_style(ctx, THEME_WHITE);
  104. #elif defined(STYLE_RED)
  105. set_style(ctx, THEME_RED);
  106. #elif defined(STYLE_BLUE)
  107. set_style(ctx, THEME_BLUE);
  108. #elif defined(STYLE_DARK)
  109. set_style(ctx, THEME_DARK);
  110. #endif
  111. #endif
  112. while(1)
  113. {
  114. bool get_event;
  115. ALLEGRO_EVENT ev;
  116. ALLEGRO_TIMEOUT timeout;
  117. al_init_timeout(&timeout, 0.06);
  118. get_event = al_wait_for_event_until(event_queue, &ev, &timeout);
  119. if (get_event && ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
  120. break;
  121. }
  122. /* Very Important: Always do nk_input_begin / nk_input_end even if
  123. there are no events, otherwise internal nuklear state gets messed up */
  124. nk_input_begin(ctx);
  125. if (get_event) {
  126. while (get_event) {
  127. nk_allegro5_handle_event(&ev);
  128. get_event = al_get_next_event(event_queue, &ev);
  129. }
  130. }
  131. nk_input_end(ctx);
  132. /* GUI */
  133. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
  134. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  135. NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  136. {
  137. enum {EASY, HARD};
  138. static int op = EASY;
  139. static int property = 20;
  140. nk_layout_row_static(ctx, 30, 80, 1);
  141. if (nk_button_label(ctx, "button"))
  142. fprintf(stdout, "button pressed\n");
  143. nk_layout_row_dynamic(ctx, 30, 2);
  144. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  145. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  146. nk_layout_row_dynamic(ctx, 22, 1);
  147. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  148. }
  149. nk_end(ctx);
  150. /* -------------- EXAMPLES ---------------- */
  151. #ifdef INCLUDE_CALCULATOR
  152. calculator(ctx);
  153. #endif
  154. #ifdef INCLUDE_CANVAS
  155. canvas(ctx);
  156. #endif
  157. #ifdef INCLUDE_OVERVIEW
  158. overview(ctx);
  159. #endif
  160. #ifdef INCLUDE_NODE_EDITOR
  161. node_editor(ctx);
  162. #endif
  163. /* ----------------------------------------- */
  164. /* Draw */
  165. al_clear_to_color(al_map_rgb(19, 43, 81));
  166. /* IMPORTANT: `nk_allegro5_render` changes the target backbuffer
  167. to the display set at initialization and does not restore it.
  168. Change it if you want to draw somewhere else. */
  169. nk_allegro5_render();
  170. al_flip_display();
  171. }
  172. nk_allegro5_font_del(font);
  173. nk_allegro5_shutdown();
  174. al_destroy_display(display);
  175. al_destroy_event_queue(event_queue);
  176. return 0;
  177. }