draw2DImage.cpp 5.6 KB

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