raylib-nuklear-demo.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 <math.h>
  10. #include <limits.h>
  11. #include <time.h>
  12. #include "raylib.h"
  13. #if defined(PLATFORM_WEB)
  14. #include <emscripten/emscripten.h>
  15. #endif
  16. //#define NK_INCLUDE_FIXED_TYPES
  17. //#define NK_INCLUDE_STANDARD_IO
  18. #define NK_INCLUDE_STANDARD_VARARGS
  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 RAYLIB_NUKLEAR_IMPLEMENTATION
  24. #define RAYLIB_NUKLEAR_INCLUDE_DEFAULT_FONT
  25. #include "raylib-nuklear.h"
  26. #define WINDOW_WIDTH 1200
  27. #define WINDOW_HEIGHT 800
  28. /* ===============================================================
  29. *
  30. * EXAMPLE
  31. *
  32. * ===============================================================*/
  33. /* This are some code examples to provide a small overview of what can be
  34. * done with this library. To try out an example uncomment the defines */
  35. #define INCLUDE_ALL
  36. /*#define INCLUDE_STYLE */
  37. /*#define INCLUDE_CALCULATOR */
  38. /*#define INCLUDE_CANVAS */
  39. /*#define INCLUDE_OVERVIEW */
  40. /*#define INCLUDE_NODE_EDITOR */
  41. #ifdef INCLUDE_ALL
  42. //#define INCLUDE_STYLE
  43. #define INCLUDE_CALCULATOR
  44. #define INCLUDE_CANVAS
  45. #define INCLUDE_OVERVIEW
  46. //#define INCLUDE_NODE_EDITOR
  47. #endif
  48. #ifdef INCLUDE_STYLE
  49. #include "../vendor/nuklear/demo/style.c"
  50. #endif
  51. #ifdef INCLUDE_CALCULATOR
  52. #include "../vendor/nuklear/demo/common/calculator.c"
  53. #endif
  54. #ifdef INCLUDE_CANVAS
  55. #include "../vendor/nuklear/demo/common/canvas.c"
  56. #endif
  57. #ifdef INCLUDE_OVERVIEW
  58. #include "../vendor/nuklear/demo/common/overview.c"
  59. #endif
  60. #ifdef INCLUDE_NODE_EDITOR
  61. #include "../vendor/nuklear/demo/common/node_editor.c"
  62. #endif
  63. //----------------------------------------------------------------------------------
  64. // Global Variables Definition
  65. //----------------------------------------------------------------------------------
  66. struct nk_context *ctx;
  67. struct nk_colorf bg;
  68. //----------------------------------------------------------------------------------
  69. // Module Functions Declaration
  70. //----------------------------------------------------------------------------------
  71. void UpdateDrawFrame(void); // Update and Draw one frame
  72. //----------------------------------------------------------------------------------
  73. // Main Entry Point
  74. //----------------------------------------------------------------------------------
  75. int main(void) {
  76. // Initialization
  77. //--------------------------------------------------------------------------------------
  78. InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "[raylib-nuklear] demo");
  79. SetTargetFPS(60);
  80. /* GUI */
  81. const int fontSize = 13;
  82. Font font = LoadFontFromNuklear(fontSize);
  83. ctx = InitNuklearEx(font, fontSize);
  84. bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
  85. /* style.c */
  86. #ifdef INCLUDE_STYLE
  87. /*set_style(ctx, THEME_WHITE);*/
  88. /*set_style(ctx, THEME_RED);*/
  89. /*set_style(ctx, THEME_BLUE);*/
  90. /*set_style(ctx, THEME_DARK);*/
  91. #endif
  92. //--------------------------------------------------------------------------------------
  93. // Main game loop
  94. #if defined(PLATFORM_WEB)
  95. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  96. #else
  97. while (!WindowShouldClose()) // Detect window close button or ESC key
  98. {
  99. UpdateDrawFrame();
  100. }
  101. #endif
  102. //--------------------------------------------------------------------------------------
  103. // De-Initialization
  104. //--------------------------------------------------------------------------------------
  105. UnloadNuklear(ctx); // Unload the Nuklear GUI
  106. CloseWindow();
  107. //--------------------------------------------------------------------------------------
  108. return 0;
  109. }
  110. void UpdateDrawFrame(void) {
  111. /* Input */
  112. UpdateNuklear(ctx);
  113. /* GUI */
  114. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
  115. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  116. NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  117. {
  118. enum {EASY, HARD};
  119. static int op = EASY;
  120. static int property = 20;
  121. nk_layout_row_static(ctx, 30, 80, 1);
  122. if (nk_button_label(ctx, "button"))
  123. TraceLog(LOG_INFO, "button pressed!");
  124. nk_layout_row_dynamic(ctx, 30, 2);
  125. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  126. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  127. nk_layout_row_dynamic(ctx, 22, 1);
  128. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  129. nk_layout_row_dynamic(ctx, 20, 1);
  130. nk_label(ctx, "background:", NK_TEXT_LEFT);
  131. nk_layout_row_dynamic(ctx, 25, 1);
  132. if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
  133. nk_layout_row_dynamic(ctx, 120, 1);
  134. bg = nk_color_picker(ctx, bg, NK_RGBA);
  135. nk_layout_row_dynamic(ctx, 25, 1);
  136. bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
  137. bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
  138. bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
  139. bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
  140. nk_combo_end(ctx);
  141. }
  142. }
  143. nk_end(ctx);
  144. /* -------------- EXAMPLES ---------------- */
  145. #ifdef INCLUDE_CALCULATOR
  146. calculator(ctx);
  147. #endif
  148. #ifdef INCLUDE_CANVAS
  149. canvas(ctx);
  150. #endif
  151. #ifdef INCLUDE_OVERVIEW
  152. overview(ctx);
  153. #endif
  154. #ifdef INCLUDE_NODE_EDITOR
  155. node_editor(ctx);
  156. #endif
  157. /* ----------------------------------------- */
  158. /* Draw */
  159. BeginDrawing();
  160. {
  161. ClearBackground(ColorFromNuklearF(bg));
  162. DrawNuklear(ctx);
  163. }
  164. EndDrawing();
  165. }