Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Sprite.h"
- using namespace City;
- Sprite::Sprite(const std::string& fileName)
- {
- HRESULT hResult;
- m_surface = nullptr;
- //Get width and height of bitmap
- hResult = D3DXGetImageInfoFromFile(fileName.c_str(), &m_imageInfo);
- //Make sure that the call to D3DXGetImageInfoFromFile succeeded
- if FAILED (hResult)
- return;
- //Create offscreen surface that will hold the bitmap
- hResult = g_pDevice->CreateOffscreenPlainSurface( m_imageInfo.Width, m_imageInfo.Height, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &m_surface, NULL );
- if ( FAILED( hResult ) )
- {
- if(m_surface)
- m_surface->Release();
- return;
- }
- //Load bitmap into surface created earlier
- hResult = D3DXLoadSurfaceFromFile( m_surface, NULL, NULL, fileName.c_str( ), NULL, D3DX_DEFAULT, 0, NULL );
- if ( FAILED( hResult ) )
- {
- if(m_surface)
- m_surface->Release();
- m_surface = nullptr;
- return;
- }
- }
- Sprite::~Sprite()
- {
- if(m_surface)
- m_surface->Release();
- }
- void Sprite::Draw(const Vec2i& pos, const Vec2i& size)
- {
- IDirect3DSurface9* backbuffer = NULL;
- g_pDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO, &backbuffer);
- RECT rect;
- rect.left = 0;
- rect.bottom = 0;
- rect.right = rect.left + m_imageInfo.Width;
- rect.top = rect.bottom + m_imageInfo.Height;
- RECT rect2;
- rect2.left = pos.x;
- rect2.bottom = pos.y;
- rect2.right = rect2.left + size.x;
- rect2.top = rect2.bottom + size.y;
- g_pDevice->StretchRect( m_surface, //the source surface
- &rect, //the source rectangle
- backbuffer, //the destination surface
- &rect2, //teh destinaition rectangle
- D3DTEXF_NONE); // the filter to apply
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement