screenshotD3D9.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "gfx/D3D9/screenshotD3D9.h"
  24. #include "gfx/D3D9/gfxD3D9Device.h"
  25. #include <d3d9.h>
  26. #include <d3dx9core.h>
  27. #include <d3dx9tex.h>
  28. GBitmap* ScreenShotD3D::_captureBackBuffer()
  29. {
  30. #ifdef TORQUE_OS_XENON
  31. return NULL;
  32. #else
  33. LPDIRECT3DDEVICE9 D3DDevice = dynamic_cast<GFXD3D9Device *>(GFX)->getDevice();
  34. IDirect3DSurface9 * backBuffer;
  35. D3DDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer );
  36. // Figure the size we're snagging.
  37. D3DSURFACE_DESC desc;
  38. backBuffer->GetDesc(&desc);
  39. Point2I size;
  40. size.x = desc.Width;
  41. size.y = desc.Height;
  42. // set up the 2 copy surfaces
  43. GFXTexHandle tex[2];
  44. IDirect3DSurface9 *surface[2];
  45. tex[0].set( size.x, size.y, GFXFormatR8G8B8X8, &GFXDefaultRenderTargetProfile, avar("%s() - tex[0] (line %d)", __FUNCTION__, __LINE__) );
  46. tex[1].set( size.x, size.y, GFXFormatR8G8B8X8, &GFXSystemMemProfile, avar("%s() - tex[1] (line %d)", __FUNCTION__, __LINE__) );
  47. // grab the top level surface of tex 0
  48. GFXD3D9TextureObject *to = (GFXD3D9TextureObject *) &(*tex[0]);
  49. D3D9Assert( to->get2DTex()->GetSurfaceLevel( 0, &surface[0] ), NULL );
  50. // use StretchRect because it allows a copy from a multisample surface
  51. // to a normal rendertarget surface
  52. D3DDevice->StretchRect( backBuffer, NULL, surface[0], NULL, D3DTEXF_NONE );
  53. // grab the top level surface of tex 1
  54. to = (GFXD3D9TextureObject *) &(*tex[1]);
  55. D3D9Assert( to->get2DTex()->GetSurfaceLevel( 0, &surface[1] ), NULL );
  56. // copy the data from the render target to the system memory texture
  57. D3DDevice->GetRenderTargetData( surface[0], surface[1] );
  58. // Allocate a GBitmap and copy into it.
  59. GBitmap *gb = new GBitmap(size.x, size.y);
  60. D3DLOCKED_RECT r;
  61. D3DSURFACE_DESC d;
  62. surface[1]->GetDesc(&d);
  63. surface[1]->LockRect( &r, NULL, D3DLOCK_READONLY);
  64. // We've got the X8 in there so we have to manually copy stuff.
  65. ColorI c;
  66. for(S32 i=0; i<size.y; i++)
  67. {
  68. const U8 *a = ((U8*)r.pBits) + i * size.x * 4;
  69. for(S32 j=0; j<size.x; j++)
  70. {
  71. c.blue = *(a++);
  72. c.green = *(a++);
  73. c.red = *(a++);
  74. a++; // Ignore X.
  75. gb->setColor(j, i, c);
  76. }
  77. }
  78. surface[1]->UnlockRect();
  79. // Also save it out with D3DX
  80. //D3DXSaveSurfaceToFile( dT( "testScreen.png" ), D3DXIFF_PNG, surface[1], NULL, NULL );
  81. // release the COM pointers
  82. surface[0]->Release();
  83. surface[1]->Release();
  84. backBuffer->Release();
  85. return gb;
  86. #endif
  87. }