docking_example.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*******************************************************************************************
  2. *
  3. * raylib-extras [ImGui] example - Docking example
  4. *
  5. * This is an example of using the ImGui docking features that are part of docking branch
  6. * You must replace the default imgui with the code from the docking branch for this to work
  7. * https://github.com/ocornut/imgui/tree/docking
  8. *
  9. * Copyright (c) 2024 Jeffery Myers
  10. *
  11. ********************************************************************************************/
  12. #include "raylib.h"
  13. #include "raymath.h"
  14. #include "imgui.h"
  15. #include "rlImGui.h"
  16. // DPI scaling functions
  17. float ScaleToDPIF(float value)
  18. {
  19. return GetWindowScaleDPI().x * value;
  20. }
  21. int ScaleToDPII(int value)
  22. {
  23. return int(GetWindowScaleDPI().x * value);
  24. }
  25. int main(int argc, char* argv[])
  26. {
  27. // Initialization
  28. //--------------------------------------------------------------------------------------
  29. int screenWidth = 1280;
  30. int screenHeight = 800;
  31. // do not set the FLAG_WINDOW_HIGHDPI flag, that scales a low res framebuffer up to the native resolution.
  32. // use the native resolution and scale your geometry.
  33. SetConfigFlags(FLAG_MSAA_4X_HINT | FLAG_VSYNC_HINT | FLAG_WINDOW_RESIZABLE);
  34. InitWindow(screenWidth, screenHeight, "raylib-Extras [ImGui] example - Docking");
  35. SetTargetFPS(144);
  36. rlImGuiSetup(true);
  37. bool run = true;
  38. bool showDemoWindow = true;
  39. // if the linked ImGui has docking, enable it.
  40. // this will only be true if you use the docking branch of ImGui.
  41. #ifdef IMGUI_HAS_DOCK
  42. ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_DockingEnable;
  43. #endif
  44. // Main game loop
  45. while (!WindowShouldClose() && run) // Detect window close button or ESC key, or a quit from the menu
  46. {
  47. BeginDrawing();
  48. ClearBackground(DARKGRAY);
  49. // draw something to the raylib window below the GUI.
  50. DrawCircle(GetScreenWidth() / 2, GetScreenHeight() / 2, GetScreenHeight() * 0.45f, DARKGREEN);
  51. // start ImGui content
  52. rlImGuiBegin();
  53. // if you want windows to dock to the viewport, call this.
  54. #ifdef IMGUI_HAS_DOCK
  55. ImGui::DockSpaceOverViewport(0, NULL, ImGuiDockNodeFlags_PassthruCentralNode); // set ImGuiDockNodeFlags_PassthruCentralNode so that we can see the raylib contents behind the dockspace
  56. #endif
  57. // show a simple menu bar
  58. if (ImGui::BeginMainMenuBar())
  59. {
  60. if (ImGui::BeginMenu("File"))
  61. {
  62. if (ImGui::MenuItem("Quit"))
  63. run = false;
  64. ImGui::EndMenu();
  65. }
  66. if (ImGui::BeginMenu("Window"))
  67. {
  68. if (ImGui::MenuItem("Demo Window", nullptr, showDemoWindow))
  69. showDemoWindow = !showDemoWindow;
  70. ImGui::EndMenu();
  71. }
  72. ImGui::EndMainMenuBar();
  73. }
  74. // show some windows
  75. if (showDemoWindow)
  76. ImGui::ShowDemoWindow(&showDemoWindow);
  77. if (ImGui::Begin("Test Window"))
  78. {
  79. ImGui::TextUnformatted("Another window");
  80. }
  81. ImGui::End();
  82. // end ImGui Content
  83. rlImGuiEnd();
  84. EndDrawing();
  85. //----------------------------------------------------------------------------------
  86. }
  87. rlImGuiShutdown();
  88. // De-Initialization
  89. //--------------------------------------------------------------------------------------
  90. CloseWindow(); // Close window and OpenGL context
  91. //--------------------------------------------------------------------------------------
  92. return 0;
  93. }