Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.97 KB | None | 0 0
  1. #include "Sprite.h"
  2.  
  3. using namespace City;
  4.  
  5. Sprite::Sprite(const std::string& fileName)
  6. {
  7. HRESULT hResult;
  8. m_surface = nullptr;
  9. //Get width and height of bitmap
  10. hResult = D3DXGetImageInfoFromFile(fileName.c_str(), &m_imageInfo);
  11. //Make sure that the call to D3DXGetImageInfoFromFile succeeded
  12. if FAILED (hResult)
  13. return;
  14.  
  15. //Create offscreen surface that will hold the bitmap
  16. hResult = g_pDevice->CreateOffscreenPlainSurface( m_imageInfo.Width, m_imageInfo.Height, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &m_surface, NULL );
  17.  
  18. if ( FAILED( hResult ) )
  19. {
  20. if(m_surface)
  21. m_surface->Release();
  22. return;
  23. }
  24.  
  25.  
  26. //Load bitmap into surface created earlier
  27. hResult = D3DXLoadSurfaceFromFile( m_surface, NULL, NULL, fileName.c_str( ), NULL, D3DX_DEFAULT, 0, NULL );
  28. if ( FAILED( hResult ) )
  29. {
  30. if(m_surface)
  31. m_surface->Release();
  32. m_surface = nullptr;
  33. return;
  34. }
  35. }
  36.  
  37. Sprite::~Sprite()
  38. {
  39. if(m_surface)
  40. m_surface->Release();
  41. }
  42.  
  43. void Sprite::Draw(const Vec2i& pos, const Vec2i& size)
  44. {
  45. IDirect3DSurface9* backbuffer = NULL;
  46. g_pDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO, &backbuffer);
  47.  
  48. RECT rect;
  49. rect.left = 0;
  50. rect.bottom = 0;
  51. rect.right = rect.left + m_imageInfo.Width;
  52. rect.top = rect.bottom + m_imageInfo.Height;
  53.  
  54.  
  55. RECT rect2;
  56. rect2.left = pos.x;
  57. rect2.bottom = pos.y;
  58. rect2.right = rect2.left + size.x;
  59. rect2.top = rect2.bottom + size.y;
  60.  
  61. g_pDevice->StretchRect( m_surface, //the source surface
  62. &rect, //the source rectangle
  63. backbuffer, //the destination surface
  64. &rect2, //teh destinaition rectangle
  65. D3DTEXF_NONE); // the filter to apply
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement