main.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 include
  33. * and the corresponding function. */
  34. /*#include "../style.c"*/
  35. /*#include "../calculator.c"*/
  36. #include "../overview.c"
  37. /*#include "../node_editor.c"*/
  38. /* ===============================================================
  39. *
  40. * DEMO
  41. *
  42. * ===============================================================*/
  43. static void error_callback(int e, const char *d)
  44. {printf("Error %d: %s\n", e, d);}
  45. int main(void)
  46. {
  47. /* Platform */
  48. ALLEGRO_DISPLAY *display = NULL;
  49. ALLEGRO_EVENT_QUEUE *event_queue = NULL;
  50. NkAllegro5Font *font;
  51. struct nk_context *ctx;
  52. if (!al_init()) {
  53. fprintf(stdout, "failed to initialize allegro5!\n");
  54. exit(1);
  55. }
  56. al_install_mouse();
  57. al_set_mouse_wheel_precision(150);
  58. al_install_keyboard();
  59. al_set_new_display_flags(ALLEGRO_WINDOWED|ALLEGRO_RESIZABLE|ALLEGRO_OPENGL);
  60. al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST);
  61. al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST);
  62. display = al_create_display(WINDOW_WIDTH, WINDOW_HEIGHT);
  63. if (!display) {
  64. fprintf(stdout, "failed to create display!\n");
  65. exit(1);
  66. }
  67. event_queue = al_create_event_queue();
  68. if (!event_queue) {
  69. fprintf(stdout, "failed to create event_queue!\n");
  70. al_destroy_display(display);
  71. exit(1);
  72. }
  73. al_register_event_source(event_queue, al_get_display_event_source(display));
  74. al_register_event_source(event_queue, al_get_mouse_event_source());
  75. al_register_event_source(event_queue, al_get_keyboard_event_source());
  76. font = nk_allegro5_font_create_from_file("../../../extra_font/Roboto-Regular.ttf", 12, 0);
  77. ctx = nk_allegro5_init(font, display, WINDOW_WIDTH, WINDOW_HEIGHT);
  78. /* style.c */
  79. /*set_style(ctx, THEME_WHITE);*/
  80. /*set_style(ctx, THEME_RED);*/
  81. /*set_style(ctx, THEME_BLUE);*/
  82. /*set_style(ctx, THEME_DARK);*/
  83. while(1)
  84. {
  85. bool get_event;
  86. ALLEGRO_EVENT ev;
  87. ALLEGRO_TIMEOUT timeout;
  88. al_init_timeout(&timeout, 0.06);
  89. get_event = al_wait_for_event_until(event_queue, &ev, &timeout);
  90. if (get_event && ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
  91. break;
  92. }
  93. /* Very Important: Always do nk_input_begin / nk_input_end even if
  94. there are no events, otherwise internal nuklear state gets messed up */
  95. nk_input_begin(ctx);
  96. if (get_event) {
  97. while (get_event) {
  98. nk_allegro5_handle_event(&ev);
  99. get_event = al_get_next_event(event_queue, &ev);
  100. }
  101. }
  102. nk_input_end(ctx);
  103. /* GUI */
  104. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
  105. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  106. NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  107. {
  108. enum {EASY, HARD};
  109. static int op = EASY;
  110. static int property = 20;
  111. nk_layout_row_static(ctx, 30, 80, 1);
  112. if (nk_button_label(ctx, "button"))
  113. fprintf(stdout, "button pressed\n");
  114. nk_layout_row_dynamic(ctx, 30, 2);
  115. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  116. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  117. nk_layout_row_dynamic(ctx, 22, 1);
  118. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  119. }
  120. nk_end(ctx);
  121. /* -------------- EXAMPLES ---------------- */
  122. /*calculator(ctx);*/
  123. overview(ctx);
  124. /*node_editor(ctx);*/
  125. /* ----------------------------------------- */
  126. /* Draw */
  127. al_clear_to_color(al_map_rgb(19, 43, 81));
  128. /* IMPORTANT: `nk_allegro5_render` changes the target backbuffer
  129. to the display set at initialization and does not restore it.
  130. Change it if you want to draw somewhere else. */
  131. nk_allegro5_render();
  132. al_flip_display();
  133. }
  134. nk_allegro5_font_del(font);
  135. nk_allegro5_shutdown();
  136. al_destroy_display(display);
  137. al_destroy_event_queue(event_queue);
  138. return 0;
  139. }