screenshot.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. static irr::u8 aa = 0; // AntiAlias used in testShotsInShots
  6. // Tests screenshots features
  7. static bool testShots(video::E_DRIVER_TYPE type)
  8. {
  9. IrrlichtDevice *device = createDevice(type, core::dimension2d<u32>(160, 120), 32);
  10. if (!device)
  11. return true;
  12. video::IVideoDriver* driver = device->getVideoDriver();
  13. scene::ISceneManager * smgr = device->getSceneManager();
  14. logTestString("Testing driver %ls\n", driver->getName());
  15. scene::IAnimatedMesh* mesh = smgr->getMesh("../media/sydney.md2");
  16. scene::IAnimatedMeshSceneNode* node;
  17. if (!mesh)
  18. return false;
  19. node = smgr->addAnimatedMeshSceneNode(mesh);
  20. if (!node)
  21. return false;
  22. stabilizeScreenBackground(driver);
  23. node->setPosition(core::vector3df(20, 0, 30));
  24. node->setMaterialFlag(video::EMF_LIGHTING, false);
  25. node->setMaterialTexture(0, driver->getTexture("../media/sydney.bmp"));
  26. node->setLoopMode(false);
  27. smgr->addCameraSceneNode();
  28. node->setMD2Animation(scene::EMAT_DEATH_FALLBACK);
  29. node->setCurrentFrame((f32)(node->getEndFrame()));
  30. node->setAnimationSpeed(0);
  31. device->run();
  32. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(255, 255, 255, 0));
  33. smgr->drawAll();
  34. driver->endScene();
  35. for (s32 i=0; i<video::ECF_UNKNOWN; ++i)
  36. {
  37. video::IImage* img = driver->createScreenShot((video::ECOLOR_FORMAT)i);
  38. logTestString("Color Format %d %ssupported\n", i, (img && img->getColorFormat() == i)?"":"un");
  39. #if 0 // Enable for a quick check while testing.
  40. // If someone adds comparison images please use another scene without animation
  41. // and maybe a texture using the full color spectrum.
  42. if ( img )
  43. {
  44. irr::core::stringc screenshotFilename = "results/";
  45. screenshotFilename += shortDriverName(driver);
  46. screenshotFilename += "screenshot";
  47. screenshotFilename += core::stringc(i);
  48. screenshotFilename += ".png";
  49. driver->writeImageToFile(img, screenshotFilename.c_str());
  50. }
  51. #endif
  52. if (img)
  53. img->drop();
  54. }
  55. device->closeDevice();
  56. device->run();
  57. device->drop();
  58. return true;
  59. }
  60. // render some recognizable stuff
  61. static void drawSomeStuff(video::IVideoDriver* driver)
  62. {
  63. driver->draw2DRectangle(core::recti(5,5,155,115),
  64. video::SColor(255, 100, 0, 0),
  65. video::SColor(255, 0, 200, 0),
  66. video::SColor(255, 0, 0, 200),
  67. video::SColor(255, 20, 150, 150));
  68. driver->draw2DLine(core::position2di(10,10), core::position2di(150,110), video::SColor(255,250,50,0));
  69. driver->draw2DLine(core::position2di(0,120), core::position2di(80,60), video::SColor(255,50,50,250));
  70. }
  71. // Make a screenshot, then draw it again (scaled down) together with the stuff the screenshot was made from
  72. static bool testShotsOfShots(video::E_DRIVER_TYPE type)
  73. {
  74. SIrrlichtCreationParameters params;
  75. params.AntiAlias = aa;
  76. params.WindowSize = core::dimension2du(160, 120);
  77. params.DriverType = type;
  78. IrrlichtDevice *device = createDeviceEx(params);
  79. if (!device)
  80. return true; // in case the driver type does not exist
  81. video::IVideoDriver* driver = device->getVideoDriver();
  82. logTestString("Testing driver %ls\n", driver->getName());
  83. stabilizeScreenBackground(driver);
  84. device->run();
  85. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(255,100,101,140));
  86. drawSomeStuff(driver);
  87. driver->endScene();
  88. video::IImage* img = driver->createScreenShot();
  89. bool result = true;
  90. if ( img )
  91. {
  92. video::ITexture * screenshot = driver->addTexture(io::path("firstScreenshot"), img);
  93. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(255,100,101,140));
  94. drawSomeStuff(driver);
  95. driver->draw2DImage(screenshot, core::recti(0, 30, 80, 90), core::recti(screenshot->getOriginalSize()), 0, 0, 0);
  96. driver->endScene();
  97. img->drop();
  98. irr::core::stringc name("-shotsInShots");
  99. name += irr::core::stringc((int)aa);
  100. name += ".png";
  101. result = takeScreenshotAndCompareAgainstReference(driver, name.c_str());
  102. if ( !result )
  103. {
  104. logTestString("driver color format: %s\n", video::ColorFormatNames[driver->getColorFormat()]);
  105. }
  106. }
  107. else
  108. {
  109. logTestString("Failed to create a screenshot");
  110. result = false;
  111. }
  112. device->closeDevice();
  113. device->run();
  114. device->drop();
  115. return result;
  116. }
  117. bool screenshot()
  118. {
  119. bool result = true;
  120. TestWithAllHWDrivers(testShots);
  121. aa = 0;
  122. TestWithAllDrivers(testShotsOfShots);
  123. aa = 2; // testing something that might cause troubles with Nouveau on some systems
  124. TestWithAllDrivers(testShotsOfShots);
  125. return result;
  126. }