draw2DImage.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include "testUtils.h"
  2. using namespace irr;
  3. namespace
  4. {
  5. bool testWithRenderTarget(video::E_DRIVER_TYPE driverType)
  6. {
  7. // create device
  8. IrrlichtDevice *device = createDevice(driverType, core::dimension2d<u32>(160,120));
  9. if (device == 0)
  10. return true; // could not create selected driver.
  11. video::IVideoDriver* driver = device->getVideoDriver();
  12. if (!driver->queryFeature(video::EVDF_RENDER_TO_TARGET))
  13. {
  14. device->closeDevice();
  15. device->run();
  16. device->drop();
  17. return true;
  18. }
  19. stabilizeScreenBackground(driver);
  20. logTestString("Testing driver %ls\n", driver->getName());
  21. video::ITexture* renderTargetTex = driver->addRenderTargetTexture(core::dimension2d<u32>(64, 64), "BASEMAP");
  22. video::ITexture* renderTargetDepth = driver->addRenderTargetTexture(core::dimension2d<u32>(64, 64), "rtd", video::ECF_D16);
  23. video::IRenderTarget* renderTarget = driver->addRenderTarget();
  24. renderTarget->setTexture(renderTargetTex, renderTargetDepth);
  25. video::ITexture* tex=driver->getTexture("../media/water.jpg");
  26. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(255,255,0,255));//Backbuffer background is pink
  27. //draw the 256x256 water image on the rendertarget:
  28. driver->setRenderTargetEx(renderTarget,video::ECBF_COLOR|video::ECBF_DEPTH,video::SColor(255,0,0,255));//Rendertarget background is blue
  29. driver->draw2DImage(tex, core::position2d<s32>(0,0), core::recti(0,0,32,32));
  30. driver->setRenderTargetEx(0, 0);
  31. //draw the rendertarget on screen:
  32. //this should normally draw a 64x64 image containing a 32x32 image in the top left corner
  33. driver->draw2DImage(renderTargetTex, core::position2d<s32>(0,0));
  34. driver->endScene();
  35. bool result = takeScreenshotAndCompareAgainstReference(driver, "-draw2DImageRTT.png");
  36. device->closeDevice();
  37. device->run();
  38. device->drop();
  39. return result;
  40. }
  41. // Test various special destination rectangles
  42. bool testRectangles(video::E_DRIVER_TYPE driverType)
  43. {
  44. // create device
  45. IrrlichtDevice *device = createDevice(driverType, core::dimension2d<u32>(160,120));
  46. if (device == 0)
  47. return true; // could not create selected driver.
  48. video::IVideoDriver* driver = device->getVideoDriver();
  49. stabilizeScreenBackground(driver);
  50. logTestString("Testing driver %ls\n", driver->getName());
  51. video::ITexture *tex=driver->getTexture("../media/fireball.bmp");
  52. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(255,255,0,255));//Backbuffer background is pink
  53. // draw normal, will be overdrwan in error case
  54. driver->draw2DImage(tex, core::recti(68,32,132,96), core::recti(0,0,64,64));
  55. //draw the image larger
  56. driver->draw2DImage(tex, core::recti(0,0,64,64), core::recti(0,0,32,32));
  57. //draw the image flipped horizontally
  58. driver->draw2DImage(tex, core::recti(132,0,68,64), core::recti(0,0,64,64));
  59. //draw the image smaller
  60. driver->draw2DImage(tex, core::recti(0,64,32,96), core::recti(0,0,64,64));
  61. //draw the image much smaller
  62. driver->draw2DImage(tex, core::recti(36,64,44,72), core::recti(0,0,64,64));
  63. //draw the image flipped horizontally
  64. driver->draw2DImage(tex, core::recti(68,64,132,0), core::recti(0,0,64,64));
  65. driver->endScene();
  66. bool result = takeScreenshotAndCompareAgainstReference(driver, "-draw2DImageRect.png");
  67. device->closeDevice();
  68. device->run();
  69. device->drop();
  70. return result;
  71. }
  72. // draws a complex (interlaced, paletted, alpha) png image
  73. bool testWithPNG(video::E_DRIVER_TYPE driverType)
  74. {
  75. // create device
  76. IrrlichtDevice *device = createDevice(driverType, core::dimension2d<u32>(160,120));
  77. if (device == 0)
  78. return true; // could not create selected driver.
  79. video::IVideoDriver* driver = device->getVideoDriver();
  80. stabilizeScreenBackground(driver);
  81. logTestString("Testing driver %ls\n", driver->getName());
  82. video::ITexture *tex=driver->getTexture("media/RedbrushAlpha-0.25.png");
  83. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(255,40,40,255));//Backbuffer background is blue
  84. driver->draw2DImage(tex, core::recti(0,0,160,120), core::recti(0,0,256,256), 0, 0, true);
  85. driver->endScene();
  86. bool result = takeScreenshotAndCompareAgainstReference(driver, "-draw2DImagePNG.png", 98.f);
  87. device->closeDevice();
  88. device->run();
  89. device->drop();
  90. return result;
  91. }
  92. // draws an image and checks if the written example equals the original image
  93. bool testExactPlacement(video::E_DRIVER_TYPE driverType)
  94. {
  95. // create device
  96. IrrlichtDevice *device = createDevice(driverType, core::dimension2d<u32>(160,120), 32);
  97. if (device == 0)
  98. return true; // could not create selected driver.
  99. video::IVideoDriver* driver = device->getVideoDriver();
  100. if (driver->getColorFormat() != video::ECF_A8R8G8B8 || !driver->queryFeature(video::EVDF_RENDER_TO_TARGET))
  101. {
  102. device->closeDevice();
  103. device->run();
  104. device->drop();
  105. return true;
  106. }
  107. stabilizeScreenBackground(driver);
  108. logTestString("Testing driver %ls\n", driver->getName());
  109. video::ITexture* renderTargetTex = driver->addRenderTargetTexture(core::dimension2d<u32>(32, 32), "rt1");
  110. video::ITexture* renderTargetDepth = driver->addRenderTargetTexture(core::dimension2d<u32>(32, 32), "rtd", video::ECF_D16);
  111. video::IRenderTarget* renderTarget = driver->addRenderTarget();
  112. renderTarget->setTexture(renderTargetTex, renderTargetDepth);
  113. video::ITexture* tex=driver->getTexture("../media/fireball.bmp");
  114. driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(255,40,40,255));//Backbuffer background is blue
  115. driver->setRenderTargetEx(renderTarget, 0, video::ECBF_COLOR | video::ECBF_DEPTH);
  116. driver->draw2DImage(tex, core::recti(0,0,32,32), core::recti(0,0,64,64));
  117. driver->setRenderTargetEx(0, 0, 0);
  118. driver->endScene();
  119. video::IImage* img = driver->createImage(renderTargetTex, core::vector2di(), renderTargetTex->getSize());
  120. driver->writeImageToFile(img, "results/fireball.png");
  121. img->drop();
  122. bool result = fuzzyCompareImages(driver,
  123. driverType == video::EDT_BURNINGSVIDEO ? "media/Burning's Video-fireball.png":"media/fireball.png", "results/fireball.png")>98.25f;
  124. device->closeDevice();
  125. device->run();
  126. device->drop();
  127. return result;
  128. }
  129. }
  130. bool draw2DImage()
  131. {
  132. bool result = true;
  133. TestWithAllDrivers(testWithRenderTarget);
  134. TestWithAllHWDrivers(testWithPNG);
  135. // TODO D3D driver moves image 1 pixel top-left in case of down scaling
  136. TestWithAllDrivers(testExactPlacement);
  137. TestWithAllDrivers(testRectangles);
  138. return result;
  139. }