main.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. while(1)
  100. {
  101. bool get_event;
  102. ALLEGRO_EVENT ev;
  103. ALLEGRO_TIMEOUT timeout;
  104. al_init_timeout(&timeout, 0.06);
  105. get_event = al_wait_for_event_until(event_queue, &ev, &timeout);
  106. if (get_event && ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
  107. break;
  108. }
  109. /* Very Important: Always do nk_input_begin / nk_input_end even if
  110. there are no events, otherwise internal nuklear state gets messed up */
  111. nk_input_begin(ctx);
  112. if (get_event) {
  113. while (get_event) {
  114. nk_allegro5_handle_event(&ev);
  115. get_event = al_get_next_event(event_queue, &ev);
  116. }
  117. }
  118. nk_input_end(ctx);
  119. /* GUI */
  120. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
  121. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  122. NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  123. {
  124. enum {EASY, HARD};
  125. static int op = EASY;
  126. static int property = 20;
  127. nk_layout_row_static(ctx, 30, 80, 1);
  128. if (nk_button_label(ctx, "button"))
  129. fprintf(stdout, "button pressed\n");
  130. nk_layout_row_dynamic(ctx, 30, 2);
  131. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  132. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  133. nk_layout_row_dynamic(ctx, 22, 1);
  134. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  135. }
  136. nk_end(ctx);
  137. /* -------------- EXAMPLES ---------------- */
  138. #ifdef INCLUDE_CALCULATOR
  139. calculator(ctx);
  140. #endif
  141. #ifdef INCLUDE_CANVAS
  142. canvas(ctx);
  143. #endif
  144. #ifdef INCLUDE_OVERVIEW
  145. overview(ctx);
  146. #endif
  147. #ifdef INCLUDE_NODE_EDITOR
  148. node_editor(ctx);
  149. #endif
  150. /* ----------------------------------------- */
  151. /* Draw */
  152. al_clear_to_color(al_map_rgb(19, 43, 81));
  153. /* IMPORTANT: `nk_allegro5_render` changes the target backbuffer
  154. to the display set at initialization and does not restore it.
  155. Change it if you want to draw somewhere else. */
  156. nk_allegro5_render();
  157. al_flip_display();
  158. }
  159. nk_allegro5_font_del(font);
  160. nk_allegro5_shutdown();
  161. al_destroy_display(display);
  162. al_destroy_event_queue(event_queue);
  163. return 0;
  164. }