2
0

Ui.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright (C) 2009-2018, 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/Config.h>
  7. #include <anki/util/HighRezTimer.h>
  8. #include <anki/Ui.h>
  9. #include <anki/Input.h>
  10. #include <anki/core/StagingGpuMemoryManager.h>
  11. namespace anki
  12. {
  13. class Label : public UiImmediateModeBuilder
  14. {
  15. public:
  16. using UiImmediateModeBuilder::UiImmediateModeBuilder;
  17. void build(CanvasPtr canvas) final
  18. {
  19. nk_context* ctx = &canvas->getNkContext();
  20. if(nk_begin(ctx,
  21. "Window name",
  22. nk_rect(10, 10, 200, 500),
  23. NK_WINDOW_BORDER | NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE | NK_WINDOW_MINIMIZABLE | NK_WINDOW_TITLE))
  24. {
  25. nk_layout_row_dynamic(ctx, 30, 1);
  26. canvas->pushFont(canvas->getDefaultFont(), 10);
  27. nk_label(ctx, "Label0", NK_TEXT_ALIGN_LEFT);
  28. canvas->popFont();
  29. canvas->pushFont(canvas->getDefaultFont(), 60);
  30. nk_label(ctx, "Label1", NK_TEXT_ALIGN_LEFT);
  31. canvas->popFont();
  32. }
  33. nk_end(ctx);
  34. }
  35. };
  36. ANKI_TEST(Ui, Ui)
  37. {
  38. Config cfg;
  39. initConfig(cfg);
  40. cfg.set("window.vsync", 1);
  41. cfg.set("window.debugContext", 0);
  42. cfg.set("width", 1024);
  43. cfg.set("height", 760);
  44. NativeWindow* win = createWindow(cfg);
  45. Input* in = new Input();
  46. GrManager* gr = createGrManager(cfg, win);
  47. PhysicsWorld* physics;
  48. ResourceFilesystem* fs;
  49. ResourceManager* resource = createResourceManager(cfg, gr, physics, fs);
  50. UiManager* ui = new UiManager();
  51. ANKI_TEST_EXPECT_NO_ERR(in->init(win));
  52. StagingGpuMemoryManager* stagingMem = new StagingGpuMemoryManager();
  53. ANKI_TEST_EXPECT_NO_ERR(stagingMem->init(gr, cfg));
  54. HeapAllocator<U8> alloc(allocAligned, nullptr);
  55. ANKI_TEST_EXPECT_NO_ERR(ui->init(allocAligned, nullptr, resource, gr, stagingMem, in));
  56. {
  57. FontPtr font;
  58. ANKI_TEST_EXPECT_NO_ERR(
  59. ui->newInstance(font, "engine_data/UbuntuRegular.ttf", std::initializer_list<U32>{10, 20, 30, 60}));
  60. CanvasPtr canvas;
  61. ANKI_TEST_EXPECT_NO_ERR(ui->newInstance(canvas, font, 30, win->getWidth(), win->getHeight()));
  62. IntrusivePtr<Label> label;
  63. ANKI_TEST_EXPECT_NO_ERR(ui->newInstance(label));
  64. Bool done = false;
  65. while(!done)
  66. {
  67. ANKI_TEST_EXPECT_NO_ERR(in->handleEvents());
  68. HighRezTimer timer;
  69. timer.start();
  70. canvas->handleInput();
  71. if(in->getKey(KeyCode::ESCAPE))
  72. {
  73. done = true;
  74. }
  75. canvas->beginBuilding();
  76. label->build(canvas);
  77. canvas->endBuilding();
  78. TexturePtr presentTex = gr->acquireNextPresentableTexture();
  79. FramebufferPtr fb;
  80. {
  81. TextureViewInitInfo init;
  82. init.m_texture = presentTex;
  83. TextureViewPtr view = gr->newTextureView(init);
  84. FramebufferInitInfo fbinit;
  85. fbinit.m_colorAttachmentCount = 1;
  86. fbinit.m_colorAttachments[0].m_clearValue.m_colorf = {{1.0, 0.0, 1.0, 1.0}};
  87. fbinit.m_colorAttachments[0].m_textureView = view;
  88. fb = gr->newFramebuffer(fbinit);
  89. }
  90. CommandBufferInitInfo cinit;
  91. cinit.m_flags = CommandBufferFlag::GRAPHICS_WORK | CommandBufferFlag::SMALL_BATCH;
  92. CommandBufferPtr cmdb = gr->newCommandBuffer(cinit);
  93. cmdb->setTextureBarrier(presentTex,
  94. TextureUsageBit::NONE,
  95. TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE,
  96. TextureSubresourceInfo());
  97. cmdb->beginRenderPass(fb, {{TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE}}, {});
  98. canvas->appendToCommandBuffer(cmdb);
  99. cmdb->endRenderPass();
  100. cmdb->setTextureBarrier(presentTex,
  101. TextureUsageBit::FRAMEBUFFER_ATTACHMENT_WRITE,
  102. TextureUsageBit::PRESENT,
  103. TextureSubresourceInfo());
  104. cmdb->flush();
  105. gr->swapBuffers();
  106. stagingMem->endFrame();
  107. timer.stop();
  108. const F32 TICK = 1.0 / 30.0;
  109. if(timer.getElapsedTime() < TICK)
  110. {
  111. HighRezTimer::sleep(TICK - timer.getElapsedTime());
  112. }
  113. }
  114. }
  115. delete ui;
  116. delete stagingMem;
  117. delete resource;
  118. delete physics;
  119. delete fs;
  120. GrManager::deleteInstance(gr);
  121. delete in;
  122. delete win;
  123. }
  124. } // end namespace anki