Wednesday, October 15, 2008

Tutorial 4 : Displaying Sprites

Its been ages since the game dev related post so I thought writing a tutorial for displaying the sprites should remedy that.

The Sprite class is a wrapper around the D3dXSprite class and will contain all the code necessary to display a sprite on screen.

The member variables required for implementing the sprite class are shown below.

   1: LPD3DXSPRITE        m_pSprite;    
   2: LPDIRECT3DTEXTURE9  m_pTexture;    
   3: UINT                m_uiHeight;    
   4: UINT                m_uiWidth;   
   5: D3DXVECTOR3         m_vScale;    
   6: D3DXVECTOR3         m_vPosition;    
   7: D3DXMATRIX          m_mScaleMatrix;
Most of the variables are self-explanatory. m_pSprite contains the DirectX sprite object that will be created and m_pTexture, the texture to be loaded/mapped to this sprite.

Now, the first step is to initialize the sprite. This can be done in the following manner.



   1: void cSprite::Init( LPDIRECT3DDEVICE9 const pDevice, LPCTSTR strFilename )    
   2: {    
   3:     if (m_pSprite)    
   4:     {    
   5:         Cleanup();    
   6:     }

   7:     // Create the Sprite    
   8:     if (FAILED(    D3DXCreateSprite(pDevice, &m_pSprite)))     
   9:     {    
  10:         //error    
  11:     }

  13:     // Create the texture associated with this sprite    
  14:     if(FAILED(D3DXCreateTextureFromFile(pDevice, strFilename, &m_pTexture)))    
  15:     {    
  16:          MessageBox(NULL, strFilename, _T("Texture creation failed"), MB_OK ) ;    
  17:          PostQuitMessage(0);    
  18:     }
  20:     D3DXIMAGE_INFO imageInfo;    // contents of the image file        
  21:      
  22:     // get the contents of the image file    
  23:     D3DXGetImageInfoFromFile(strFilename, &imageInfo);   
  24:     
  25:     //get the image height and width    
  26:     m_uiHeight = imageInfo.Height;    
  27:     m_uiWidth = imageInfo.Width;    
  28: }
Once we are done with the initialization, we need to render the sprite on screen, otherwise what is the use.



   1: void cSprite::DrawSprite( LPDIRECT3DDEVICE9 const pDevice, const D3DXVECTOR3& vPosition, const DWORD dwFlags /*= NULL*/, const D3DCOLOR& tint /*= WHITE*/, const RECT* pSrcRect /*= NULL*/ )    
   2: {    
   3:     
   4:     // get the new position and create the transform matrix    
   5:     if (m_vPosition != vPosition)    
   6:     {    
   7:         D3DXMATRIX transMatrix;    
   8:         D3DXMatrixTranslation(&transMatrix, vPosition.x, vPosition.y, vPosition.z);    
   9:         D3DXMatrixMultiply(&transMatrix, &m_mScaleMatrix, &transMatrix);    
  10:         m_vPosition = vPosition ;    
  11:         m_pSprite->SetTransform(&transMatrix);   
  12:     }    
  13:      
  14:     // draw the sprite    
  15:     m_pSprite->Begin(dwFlags);    
  16:     m_pSprite->Draw(m_pTexture, pSrcRect, NULL, NULL, tint);     
  17:     m_pSprite->End();    
  18: }
The DrawSprite function is the most important function. It will contain all the code necessary to display our sprite. The arguments passed to it should be self- explanatory. Incase they are not, drop me a line and I will elaborate.
Using the translation matrix, we can set the position of the sprite and the scaling matrix as the name implies is used for scaling our image. Care needs to be taken to first translate the image and then scale it, else you might get some weird behavior.
Next a call to Begin, Draw and End and we are done drawing the sprite to the screen.

The last part remaining is to release the resources when we are done using them. This is as follows


   1: void cSprite::Cleanup()    
   2: {    
   3:     // release the texture    
   4:     SAFE_RELEASE(m_pTexture);    
   5:      
   6:     // release the sprite    
   7:     SAFE_RELEASE(m_pSprite);    
   8: }
Care needs to be taken to release the texture first and then the sprite.
The other functions can be added as per requirements. These may be for scaling the image, getting the position etc and should be trivial to implement.

After this, you should be able to display sprites(moving or static) on your screen.Till the next tutorial, fight on