raylib-nuklear-example.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**********************************************************************************************
  2. *
  3. * raylib-nuklear-example - Example of using Nuklear with Raylib.
  4. *
  5. * LICENSE: zlib/libpng
  6. *
  7. * nuklear_raylib is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  8. * BSD-like license that allows static linking with closed source software:
  9. *
  10. * Copyright (c) 2020 Rob Loach (@RobLoach)
  11. *
  12. * This software is provided "as-is", without any express or implied warranty. In no event
  13. * will the authors be held liable for any damages arising from the use of this software.
  14. *
  15. * Permission is granted to anyone to use this software for any purpose, including commercial
  16. * applications, and to alter it and redistribute it freely, subject to the following restrictions:
  17. *
  18. * 1. The origin of this software must not be misrepresented; you must not claim that you
  19. * wrote the original software. If you use this software in a product, an acknowledgment
  20. * in the product documentation would be appreciated but is not required.
  21. *
  22. * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
  23. * as being the original software.
  24. *
  25. * 3. This notice may not be removed or altered from any source distribution.
  26. *
  27. **********************************************************************************************/
  28. #include "raylib.h"
  29. #define RAYLIB_NUKLEAR_IMPLEMENTATION
  30. #include "raylib-nuklear.h"
  31. int main(void)
  32. {
  33. // Initialization
  34. //--------------------------------------------------------------------------------------
  35. const int screenWidth = 700;
  36. const int screenHeight = 394;
  37. InitWindow(screenWidth, screenHeight, "[raylib-nuklear] example");
  38. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  39. //--------------------------------------------------------------------------------------
  40. /* GUI */
  41. struct nk_colorf bg = ColorToNuklearF(SKYBLUE);
  42. struct nk_context *ctx = InitNuklear(0);
  43. // Main game loop
  44. while (!WindowShouldClose()) // Detect window close button or ESC key
  45. {
  46. // Update
  47. UpdateNuklear(ctx);
  48. // GUI
  49. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
  50. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  51. NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  52. {
  53. enum {EASY, HARD};
  54. static int op = EASY;
  55. static int property = 20;
  56. nk_layout_row_static(ctx, 30, 80, 1);
  57. if (nk_button_label(ctx, "button"))
  58. TraceLog(LOG_INFO, "button pressed");
  59. nk_layout_row_dynamic(ctx, 30, 2);
  60. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  61. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  62. nk_layout_row_dynamic(ctx, 25, 1);
  63. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  64. nk_layout_row_dynamic(ctx, 20, 1);
  65. nk_label(ctx, "background:", NK_TEXT_LEFT);
  66. nk_layout_row_dynamic(ctx, 25, 1);
  67. if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
  68. nk_layout_row_dynamic(ctx, 120, 1);
  69. bg = nk_color_picker(ctx, bg, NK_RGBA);
  70. nk_layout_row_dynamic(ctx, 25, 1);
  71. bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
  72. bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
  73. bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
  74. bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
  75. nk_combo_end(ctx);
  76. }
  77. }
  78. nk_end(ctx);
  79. // Draw
  80. //----------------------------------------------------------------------------------
  81. BeginDrawing();
  82. ClearBackground(ColorFromNuklearF(bg));
  83. DrawNuklear(ctx);
  84. EndDrawing();
  85. //----------------------------------------------------------------------------------
  86. }
  87. // De-Initialization
  88. //--------------------------------------------------------------------------------------
  89. UnloadNuklear(ctx); // Unload the Nuklear GUI
  90. CloseWindow();
  91. //--------------------------------------------------------------------------------------
  92. return 0;
  93. }