Ui.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <Tests/Framework/Framework.h>
  6. #include <AnKi/Core/ConfigSet.h>
  7. #include <AnKi/Util/HighRezTimer.h>
  8. #include <AnKi/Ui.h>
  9. #include <AnKi/Input.h>
  10. #include <AnKi/Core/GpuMemoryPools.h>
  11. using namespace anki;
  12. namespace {
  13. class Label : public UiImmediateModeBuilder
  14. {
  15. public:
  16. using UiImmediateModeBuilder::UiImmediateModeBuilder;
  17. Bool m_windowInitialized = false;
  18. U32 m_buttonClickCount = 0;
  19. void build(CanvasPtr canvas) final
  20. {
  21. Vec4 oldBackground = ImGui::GetStyle().Colors[ImGuiCol_WindowBg];
  22. ImGui::GetStyle().Colors[ImGuiCol_WindowBg].w = 0.8f;
  23. ImGui::Begin("ImGui Demo", nullptr);
  24. if(!m_windowInitialized)
  25. {
  26. ImGui::SetWindowPos(Vec2(20, 10));
  27. ImGui::SetWindowSize(Vec2(200, 500));
  28. m_windowInitialized = true;
  29. }
  30. ImGui::Text("Label default size");
  31. canvas->pushFont(canvas->getDefaultFont(), 30);
  32. ImGui::Text("Label size 30");
  33. ImGui::PopFont();
  34. m_buttonClickCount += ImGui::Button("Toggle");
  35. if(m_buttonClickCount & 1)
  36. {
  37. ImGui::Button("Toggled");
  38. }
  39. ImGui::End();
  40. ImGui::GetStyle().Colors[ImGuiCol_WindowBg] = oldBackground;
  41. }
  42. };
  43. } // namespace
  44. ANKI_TEST(Ui, Ui)
  45. {
  46. ConfigSet cfg;
  47. initConfig(cfg);
  48. cfg.setGrVsync(true);
  49. cfg.setGrValidation(false);
  50. cfg.setWidth(1024);
  51. cfg.setHeight(760);
  52. cfg.setRsrcDataPaths("EngineAssets");
  53. NativeWindow* win = createWindow(cfg);
  54. Input* in;
  55. ANKI_TEST_EXPECT_NO_ERR(Input::newInstance(allocAligned, nullptr, win, in));
  56. GrManager* gr = createGrManager(&cfg, win);
  57. PhysicsWorld* physics;
  58. ResourceFilesystem* fs;
  59. ResourceManager* resource = createResourceManager(&cfg, gr, physics, fs);
  60. UiManager* ui = new UiManager();
  61. StagingGpuMemoryPool* stagingMem = new StagingGpuMemoryPool();
  62. ANKI_TEST_EXPECT_NO_ERR(stagingMem->init(gr, cfg));
  63. HeapAllocator<U8> alloc(allocAligned, nullptr);
  64. ANKI_TEST_EXPECT_NO_ERR(ui->init(allocAligned, nullptr, resource, gr, stagingMem, in));
  65. {
  66. FontPtr font;
  67. ANKI_TEST_EXPECT_NO_ERR(ui->newInstance(font, "UbuntuRegular.ttf", Array<U32, 4>{10, 20, 30, 60}));
  68. CanvasPtr canvas;
  69. ANKI_TEST_EXPECT_NO_ERR(ui->newInstance(canvas, font, 20, win->getWidth(), win->getHeight()));
  70. IntrusivePtr<Label> label;
  71. ANKI_TEST_EXPECT_NO_ERR(ui->newInstance(label));
  72. Bool done = false;
  73. while(!done)
  74. {
  75. ANKI_TEST_EXPECT_NO_ERR(in->handleEvents());
  76. HighRezTimer timer;
  77. timer.start();
  78. canvas->handleInput();
  79. if(in->getKey(KeyCode::ESCAPE))
  80. {
  81. done = true;
  82. }
  83. canvas->beginBuilding();
  84. label->build(canvas);
  85. TexturePtr presentTex = gr->acquireNextPresentableTexture();
  86. FramebufferPtr fb;
  87. {
  88. TextureViewInitInfo init;
  89. init.m_texture = presentTex;
  90. TextureViewPtr view = gr->newTextureView(init);
  91. FramebufferInitInfo fbinit;
  92. fbinit.m_colorAttachmentCount = 1;
  93. fbinit.m_colorAttachments[0].m_clearValue.m_colorf = {{1.0, 0.0, 1.0, 1.0}};
  94. fbinit.m_colorAttachments[0].m_textureView = view;
  95. fb = gr->newFramebuffer(fbinit);
  96. }
  97. CommandBufferInitInfo cinit;
  98. cinit.m_flags = CommandBufferFlag::GENERAL_WORK | CommandBufferFlag::SMALL_BATCH;
  99. CommandBufferPtr cmdb = gr->newCommandBuffer(cinit);
  100. cmdb->setTextureBarrier(presentTex, TextureUsageBit::NONE, TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE,
  101. TextureSubresourceInfo());
  102. cmdb->beginRenderPass(fb, {{TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE}}, {});
  103. canvas->appendToCommandBuffer(cmdb);
  104. cmdb->endRenderPass();
  105. cmdb->setTextureBarrier(presentTex, TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE, TextureUsageBit::PRESENT,
  106. TextureSubresourceInfo());
  107. cmdb->flush();
  108. gr->swapBuffers();
  109. stagingMem->endFrame();
  110. timer.stop();
  111. const F32 TICK = 1.0f / 30.0f;
  112. if(timer.getElapsedTime() < TICK)
  113. {
  114. HighRezTimer::sleep(TICK - timer.getElapsedTime());
  115. }
  116. }
  117. }
  118. delete ui;
  119. delete stagingMem;
  120. delete resource;
  121. delete physics;
  122. delete fs;
  123. GrManager::deleteInstance(gr);
  124. Input::deleteInstance(in);
  125. NativeWindow::deleteInstance(win);
  126. }