ioScene.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright (C) 2008-2012 Colin MacDonald
  2. // No rights reserved: this software is in the public domain.
  3. #include "testUtils.h"
  4. using namespace irr;
  5. using namespace core;
  6. using namespace scene;
  7. using namespace video;
  8. using namespace io;
  9. using namespace gui;
  10. // Tests save scene.
  11. static bool saveScene(void)
  12. {
  13. IrrlichtDevice *device = createDevice( EDT_NULL, dimension2d<u32>(160, 120), 32);
  14. assert_log(device);
  15. if (!device)
  16. return false;
  17. ISceneManager * smgr = device->getSceneManager();
  18. ISkinnedMesh* mesh = (ISkinnedMesh*)smgr->getMesh("../media/ninja.b3d");
  19. if (!mesh)
  20. return false;
  21. IAnimatedMeshSceneNode* node1 = smgr->addAnimatedMeshSceneNode(mesh);
  22. if (node1)
  23. {
  24. node1->setPosition(vector3df(-3, -3, 10));
  25. node1->setMaterialFlag(EMF_LIGHTING, false);
  26. node1->setAnimationSpeed(0.f);
  27. node1->setCurrentFrame(10.f);
  28. node1->setDebugDataVisible(irr::scene::EDS_BBOX_BUFFERS);
  29. }
  30. ISkinnedMesh* mesh2 = (ISkinnedMesh*)smgr->getMesh(device->getFileSystem()->getAbsolutePath("../media/dwarf.x"));
  31. if (!mesh2)
  32. return false;
  33. IAnimatedMeshSceneNode* node2 = smgr->addAnimatedMeshSceneNode(mesh2);
  34. if (node2)
  35. {
  36. node2->setPosition(vector3df(33, -93, 120));
  37. node2->setMaterialFlag(EMF_LIGHTING, false);
  38. node2->setAnimationSpeed(10.f);
  39. node2->setCurrentFrame(2.f);
  40. }
  41. IAnimatedMeshSceneNode* node3 = smgr->addAnimatedMeshSceneNode(mesh2, node2);
  42. if (node3)
  43. {
  44. node3->setPosition(vector3df(-88, -300, 150));
  45. node3->setMaterialFlag(EMF_LIGHTING, false);
  46. node3->setAnimationSpeed(0.f);
  47. node3->setCurrentFrame(12.f);
  48. }
  49. smgr->addCameraSceneNode();
  50. logTestString("Test scene.irr");
  51. smgr->saveScene("results/scene.irr");
  52. bool result = xmlCompareFiles(device->getFileSystem(), "results/scene.irr", "media/scene.irr");
  53. logTestString("Test scene2.irr");
  54. smgr->saveScene("results/scene2.irr", 0, node3);
  55. result &= xmlCompareFiles(device->getFileSystem(), "results/scene2.irr", "media/scene2.irr");
  56. device->closeDevice();
  57. device->run();
  58. device->drop();
  59. return result;
  60. }
  61. static bool loadScene(void)
  62. {
  63. IrrlichtDevice *device = createDevice(video::EDT_BURNINGSVIDEO,
  64. core::dimension2du(160,120), 32);
  65. if (!device)
  66. return false;
  67. IVideoDriver* driver = device->getVideoDriver();
  68. ISceneManager* smgr = device->getSceneManager();
  69. // load scene from example, with correct relative path
  70. device->getFileSystem()->changeWorkingDirectoryTo("results");
  71. smgr->loadScene("../../media/example.irr");
  72. smgr->addCameraSceneNode(0, core::vector3df(0,0,-50));
  73. device->getFileSystem()->changeWorkingDirectoryTo("..");
  74. bool result = false;
  75. device->run();
  76. device->getTimer()->setTime(666); // scene has animations and current scene seems to be saved at that time ... really - best result with just that number :-)
  77. if (driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(0, 80, 80, 80)))
  78. {
  79. smgr->drawAll();
  80. driver->endScene();
  81. // we need to be very sloppy, because the animators will produce a different
  82. // start depending on the actual loading time. 97% seems to be safe, as removing
  83. // an object produces values around 95%
  84. result = takeScreenshotAndCompareAgainstReference(driver, "-loadScene.png", 97.4f);
  85. if (!result)
  86. logTestString("Rendering the loaded scene failed.\n");
  87. }
  88. ISceneNode* node = smgr->getSceneNodeFromId(128);
  89. if (!node)
  90. result=false;
  91. else if (result) // only check if scene was correctly loaded
  92. {
  93. result &= (node->getChildren().size()==0);
  94. if (!result)
  95. logTestString("Node has an illegal child node.\n");
  96. device->getSceneManager()->loadScene("results/scene2.irr", 0, node);
  97. result &= (node->getChildren().size()!=0);
  98. if (!result)
  99. logTestString("Loading second scene as child failed.\n");
  100. }
  101. device->closeDevice();
  102. device->run();
  103. device->drop();
  104. return result;
  105. }
  106. bool ioScene(void)
  107. {
  108. bool result = saveScene();
  109. result &= loadScene();
  110. return result;
  111. }