2
0

main.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include <iostream>
  2. #include <irrlicht.h>
  3. #include "exampleHelper.h"
  4. using namespace irr;
  5. static IrrlichtDevice *device = nullptr;
  6. static int test_fail = 0;
  7. void test_irr_array();
  8. void test_irr_string();
  9. static video::E_DRIVER_TYPE chooseDriver(core::stringc arg_)
  10. {
  11. if (arg_ == "null")
  12. return video::EDT_NULL;
  13. if (arg_ == "ogles1")
  14. return video::EDT_OGLES1;
  15. if (arg_ == "ogles2")
  16. return video::EDT_OGLES2;
  17. if (arg_ == "opengl")
  18. return video::EDT_OPENGL;
  19. if (arg_ == "opengl3")
  20. return video::EDT_OPENGL3;
  21. std::cerr << "Unknown driver type: " << arg_.c_str() << ". Trying OpenGL." << std::endl;
  22. return video::EDT_OPENGL;
  23. }
  24. static inline void check(bool ok, const char *msg)
  25. {
  26. if (!ok) {
  27. test_fail++;
  28. device->getLogger()->log((core::stringc("FAILED TEST: ") + msg).c_str(), ELL_ERROR);
  29. }
  30. }
  31. void run_unit_tests()
  32. {
  33. std::cout << "Running unit tests:" << std::endl;
  34. try {
  35. test_irr_array();
  36. test_irr_string();
  37. } catch (const std::exception &e) {
  38. std::cerr << e.what() << std::endl;
  39. test_fail++;
  40. }
  41. std::cout << std::endl;
  42. }
  43. int main(int argc, char *argv[])
  44. {
  45. run_unit_tests();
  46. SIrrlichtCreationParameters p;
  47. p.DriverType = chooseDriver(argc > 1 ? argv[1] : "");
  48. p.WindowSize = core::dimension2du(640, 480);
  49. p.Vsync = true;
  50. p.LoggingLevel = ELL_DEBUG;
  51. device = createDeviceEx(p);
  52. if (!device)
  53. return 1;
  54. {
  55. u32 total = 0;
  56. device->getOSOperator()->getSystemMemory(&total, nullptr);
  57. core::stringc message = core::stringc("Total RAM in MiB: ") + core::stringc(total >> 10);
  58. device->getLogger()->log(message.c_str(), ELL_INFORMATION);
  59. check(total > 130 * 1024, "RAM amount");
  60. }
  61. device->setWindowCaption(L"Hello World!");
  62. device->setResizable(true);
  63. video::IVideoDriver *driver = device->getVideoDriver();
  64. scene::ISceneManager *smgr = device->getSceneManager();
  65. gui::IGUIEnvironment *guienv = device->getGUIEnvironment();
  66. guienv->addStaticText(L"sample text", core::rect<s32>(10, 10, 110, 22), false);
  67. gui::IGUIButton *button = guienv->addButton(
  68. core::rect<s32>(10, 30, 110, 30 + 32), 0, -1, L"sample button",
  69. L"sample tooltip");
  70. gui::IGUIEditBox *editbox = guienv->addEditBox(L"",
  71. core::rect<s32>(10, 70, 60, 70 + 16));
  72. const io::path mediaPath = getExampleMediaPath();
  73. auto mesh_file = device->getFileSystem()->createAndOpenFile(mediaPath + "coolguy_opt.x");
  74. check(mesh_file, "mesh file loading");
  75. scene::IAnimatedMesh *mesh = smgr->getMesh(mesh_file);
  76. check(mesh, "mesh loading");
  77. if (mesh_file)
  78. mesh_file->drop();
  79. if (mesh) {
  80. video::ITexture *tex = driver->getTexture(mediaPath + "cooltexture.png");
  81. check(tex, "texture loading");
  82. scene::IAnimatedMeshSceneNode *node = smgr->addAnimatedMeshSceneNode(mesh);
  83. if (node) {
  84. node->forEachMaterial([tex](video::SMaterial &mat) {
  85. mat.Lighting = false;
  86. mat.setTexture(0, tex);
  87. });
  88. node->setFrameLoop(0, 29);
  89. node->setAnimationSpeed(30);
  90. }
  91. }
  92. smgr->addCameraSceneNode(0, core::vector3df(0, 4, 5), core::vector3df(0, 2, 0));
  93. s32 n = 0;
  94. SEvent event;
  95. device->getTimer()->start();
  96. while (device->run()) {
  97. if (device->getTimer()->getTime() >= 1000) {
  98. device->getTimer()->setTime(0);
  99. ++n;
  100. if (n == 1) { // Tooltip display
  101. bzero(&event, sizeof(SEvent));
  102. event.EventType = irr::EET_MOUSE_INPUT_EVENT;
  103. event.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
  104. event.MouseInput.X = button->getAbsolutePosition().getCenter().X;
  105. event.MouseInput.Y = button->getAbsolutePosition().getCenter().Y;
  106. device->postEventFromUser(event);
  107. } else if (n == 2) // Text input focus
  108. guienv->setFocus(editbox);
  109. else if (n == 3) { // Keypress for Text input
  110. bzero(&event, sizeof(SEvent));
  111. event.EventType = irr::EET_KEY_INPUT_EVENT;
  112. event.KeyInput.Char = L'a';
  113. event.KeyInput.Key = KEY_KEY_A;
  114. event.KeyInput.PressedDown = true;
  115. device->postEventFromUser(event);
  116. event.KeyInput.PressedDown = false;
  117. device->postEventFromUser(event);
  118. } else
  119. device->closeDevice();
  120. }
  121. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH,
  122. video::SColor(255, 100, 100, 150));
  123. smgr->drawAll();
  124. guienv->drawAll();
  125. driver->endScene();
  126. }
  127. check(core::stringw(L"a") == editbox->getText(), "EditBox text");
  128. device->getLogger()->log("Done.", ELL_INFORMATION);
  129. device->drop();
  130. return test_fail > 0 ? 1 : 0;
  131. }