ImageViewerMain.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <AnKi/AnKi.h>
  6. using namespace anki;
  7. class TextureViewerUiNode : public SceneNode
  8. {
  9. public:
  10. ImageViewerUi m_ui;
  11. TextureViewerUiNode(const SceneNodeInitInfo& inf)
  12. : SceneNode(inf)
  13. {
  14. UiComponent* uic = newComponent<UiComponent>();
  15. uic->init(
  16. [](UiCanvas& canvas, void* ud) {
  17. static_cast<TextureViewerUiNode*>(ud)->draw(canvas);
  18. },
  19. this);
  20. m_ui.m_open = true;
  21. }
  22. private:
  23. ImFont* m_font = nullptr;
  24. void draw(UiCanvas& canvas)
  25. {
  26. if(!m_font)
  27. {
  28. m_font = canvas.addFont("EngineAssets/UbuntuRegular.ttf");
  29. }
  30. ImGui::PushFont(m_font, 16.0f);
  31. ImGui::SetWindowPos(Vec2(0.0f, 0.0f));
  32. ImGui::SetWindowSize(canvas.getSizef());
  33. m_ui.drawWindow(canvas, Vec2(0.0f), canvas.getSizef(), ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoMove);
  34. ImGui::PopFont();
  35. }
  36. };
  37. class MyApp : public App
  38. {
  39. public:
  40. const Char* m_imageFilename;
  41. MyApp(U32 argc, Char** argv, const Char* imageFilename)
  42. : App("ImageViewer", argc, argv)
  43. , m_imageFilename(imageFilename)
  44. {
  45. }
  46. Error userPreInit() override
  47. {
  48. g_cvarWindowFullscreen = 0;
  49. g_cvarRsrcDataPaths = ANKI_SOURCE_DIRECTORY;
  50. return Error::kNone;
  51. }
  52. Error userPostInit() override
  53. {
  54. // Load the texture
  55. ImageResourcePtr image;
  56. ANKI_CHECK(ResourceManager::getSingleton().loadResource(m_imageFilename, image, false));
  57. // Change window name
  58. String title;
  59. title.sprintf("%s %u x %u Mips %u Format %s", m_imageFilename, image->getTexture().getWidth(), image->getTexture().getHeight(),
  60. image->getTexture().getMipmapCount(), getFormatInfo(image->getTexture().getFormat()).m_name);
  61. NativeWindow::getSingleton().setWindowTitle(title);
  62. // Create the node
  63. TextureViewerUiNode* node = SceneGraph::getSingleton().newSceneNode<TextureViewerUiNode>("TextureViewer");
  64. node->m_ui.m_image = std::move(image);
  65. return Error::kNone;
  66. }
  67. Error userMainLoop(Bool& quit, [[maybe_unused]] Second elapsedTime) override
  68. {
  69. Input& input = Input::getSingleton();
  70. if(input.getKey(KeyCode::kEscape) > 0)
  71. {
  72. quit = true;
  73. }
  74. return Error::kNone;
  75. }
  76. };
  77. ANKI_MAIN_FUNCTION(myMain)
  78. int myMain(int argc, char* argv[])
  79. {
  80. if(argc < 2)
  81. {
  82. ANKI_LOGE("Wrong number of arguments");
  83. return 1;
  84. }
  85. Array<Char*, 32> args;
  86. U32 argCount = 0;
  87. args[argCount++] = argv[0];
  88. for(I32 i = 2; i < argc; ++i)
  89. {
  90. args[argCount++] = argv[i];
  91. }
  92. MyApp* app = new MyApp(argCount, args.getBegin(), argv[1]);
  93. const Error err = app->mainLoop();
  94. delete app;
  95. if(err)
  96. {
  97. ANKI_LOGE("Error reported. Bye!!");
  98. return 1;
  99. }
  100. else
  101. {
  102. ANKI_LOGI("Bye!!");
  103. return 0;
  104. }
  105. }