raylib-nuklear-font.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**********************************************************************************************
  2. *
  3. * raylib-nuklear-font - Example of using Nuklear with a custom Raylib font.
  4. *
  5. * LICENSE: zlib/libpng
  6. *
  7. * raylib-nuklear 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 = 800;
  36. const int screenHeight = 450;
  37. const int fontSize = 18;
  38. InitWindow(screenWidth, screenHeight, "[raylib-nuklear] font example");
  39. Font font = LoadFontEx("resources/anonymous_pro_bold.ttf", fontSize, NULL, 0);
  40. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  41. //--------------------------------------------------------------------------------------
  42. /* GUI */
  43. struct nk_colorf bg = ColorToNuklearF(SKYBLUE);
  44. struct nk_context *ctx = InitNuklearEx(font, fontSize);
  45. // Main game loop
  46. while (!WindowShouldClose()) // Detect window close button or ESC key
  47. {
  48. // Update
  49. UpdateNuklear(ctx);
  50. // GUI
  51. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
  52. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  53. NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  54. {
  55. enum {EASY, HARD};
  56. static int op = EASY;
  57. static int property = 20;
  58. nk_layout_row_static(ctx, 30, 80, 1);
  59. if (nk_button_label(ctx, "button"))
  60. TraceLog(LOG_INFO, "button pressed");
  61. nk_layout_row_dynamic(ctx, 30, 2);
  62. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  63. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  64. nk_layout_row_dynamic(ctx, 25, 1);
  65. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  66. nk_layout_row_dynamic(ctx, 20, 1);
  67. nk_label(ctx, "background:", NK_TEXT_LEFT);
  68. nk_layout_row_dynamic(ctx, 25, 1);
  69. if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
  70. nk_layout_row_dynamic(ctx, 120, 1);
  71. bg = nk_color_picker(ctx, bg, NK_RGBA);
  72. nk_layout_row_dynamic(ctx, 25, 1);
  73. bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
  74. bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
  75. bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
  76. bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
  77. nk_combo_end(ctx);
  78. }
  79. }
  80. nk_end(ctx);
  81. // Draw
  82. //----------------------------------------------------------------------------------
  83. BeginDrawing();
  84. ClearBackground(ColorFromNuklearF(bg));
  85. DrawNuklear(ctx);
  86. EndDrawing();
  87. //----------------------------------------------------------------------------------
  88. }
  89. // De-Initialization
  90. //--------------------------------------------------------------------------------------
  91. UnloadNuklear(ctx); // Unload the Nuklear GUI
  92. UnloadFont(font);
  93. CloseWindow();
  94. //--------------------------------------------------------------------------------------
  95. return 0;
  96. }