1 / 22

DX9: Sprites and Fonts

DX9: Sprites and Fonts. Course Information CVG: Programming 3 My Name: Mark Walsh Website: www.activehelix.co.uk/courses Recommended Reading Beginning DirectX Game Programming. Creating a Sprite. HRESULT D3DXCreateSprite(LPDIRECT3DDEVICE9 pDevice, LPD3DXSPRITE *ppSprite);

Download Presentation

DX9: Sprites and Fonts

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DX9: Sprites and Fonts Course Information • CVG: Programming 3 • My Name: Mark Walsh • Website: www.activehelix.co.uk/courses Recommended Reading • Beginning DirectX Game Programming

  2. Creating a Sprite HRESULT D3DXCreateSprite(LPDIRECT3DDEVICE9 pDevice, LPD3DXSPRITE *ppSprite); • pDevice - our device pointer • ppSprite - the address of our sprite pointer LPD3DXSPRITE sprite=NULL; if (SUCCEEDED(D3DXCreateSprite(device,&sprite)) { // created OK }

  3. Drawing a Sprite • To render our sprite we must draw it in our render loop between the scene begin and end calls. To display the sprite we call the following three sprite interfaces: • sprite->Begin(...) • sprite->Draw(...) • sprite->End()

  4. HRESULT Begin(DWORD flags) sprite->Begin(D3DXSPRITE_ALPHABLEND); HRESULT Draw(LPDIRECT3DTEXTURE9 pSrcTexture, CONST RECT *pSrcRect, D3DXVECTOR3 *center, CONST D3DVECTOR3 *pTranslation, D3DCOLOR Color );

  5. D3DXVECTOR3 pos; pos.x=10.0f; pos.y=20.0f; pos.z=0.0f; sprite->Begin(D3DXSPRITE_ALPHABLEND); sprite-> Draw(texture,NULL,NULL,&pos,0xFFFFFFFF); sprite->End();

  6. Moving Sprites • static D3DXMATRIX spriteMatrix; • D3DXMatrixTransformation2D( &spriteMatrix, NULL, 0.0f, &vScaling1, &vCentre1, fAngle1, &vTranslation1);

  7. m_sprite->Begin(D3DXSPRITE_ALPHABLEND); • // Texture being used is 64 by 64: • D3DXVECTOR2 spriteCentre=D3DXVECTOR2(32.0f,32.0f); • // Screen position of the sprite • D3DXVECTOR2 trans=D3DXVECTOR2(50.0f,80.0f); • // Rotate based on the time passed • float rotation=timeGetTime()/500.0f; • // Build our matrix to rotate, scale and position our sprite • D3DXMATRIX mat; • D3DXVECTOR2 scaling(2.0f,2.0f); • // out, scaling centre, scaling rotation, scaling, rotation centre, rotation, translation • D3DXMatrixTransformation2D(&mat,NULL,0.0,&scaling,&spriteCentre,rotation,&trans); • // Tell the sprite about the matrix • m_sprite->SetTransform(&mat); • // Draw the sprite • m_sprite->Draw(m_texture,NULL,NULL,NULL,0xFFFFFFFF)); • // Thats it • m_sprite->End();

  8. Rotation and Scaling of Sprites • Scaling - Scales about the origin in the x,y,(z) directions in two-(three-) dimensions • Rotation - Rotates about the origin in two-dimensions and one of the co-ordinate axes in three-dimensions. The positive direction of rotation is taken as anti-clockwise • Translation - Translates (or moves) by dx,dy,(dz) in two-(three-) dimensions.

  9. Scaling about the origin scale(scalex,scaley) in two-dimensions and scale(scalex,scaley,scalez) in three-dimensions • Rotation anti-clockwise about the origin rotate(theta) in two-dimensions. And three functions rotatex(theta), rotatey(theta) and rotatez(theta) which rotate by theta about the x,y and z co-ordinate axes respectively. Positive direction of rotation is anti-clockwise when looking from the positive axis towards the origin • Translation. translate(dx,dy) in two-dimensions and translate(dx,dy,dz) in three-dimensions.

  10. Displaying Text • To display text you must carry out the following: • Create and ID3DXFont object • Optional: Calculate rectangle size to hold text • Call Direct3D device -> BeginScene() • Draw the text • Call Direct3D device -> EndScene() • Release the ID3DXFont object

  11. Create a Font Object • LPD3DXFONT g_pFont = NULL; • CreateFont • The CreateFont function creates a logical font with the specified characteristics. The logical font can subsequently be selected as the font for any device. • LogFont • Is to initialise our own LogFont structure. • The LOGFONT structure defines the attributes of a font.

  12. Calculating the Text Rectangle • Call the ID3DXFont object with the DT_CALCRECT flag. We then don’t need our rectangle to be sized • Or we can specify the size of our rectangle and use another flag, such as DT_CENTER. This centres the text in the middle of the Rectangle • First we actually need to define a rectangle to hold the results of our calculations.

  13. RECT r; • r.left = 0; • r.right = 0; • r.top = 0; • r.bottom = 0;

  14. DrawText • INT DrawText( • LPD3DXSPRITE pSprite, • LPCTSTR pString, • INT Count, • LPRECT pRect, • DWORD Format, • D3DCOLOR Color • );

  15. Releasing the Font Object • Don’t forget to release the font object, in the usual way, when you have finished. • pFont -> Release();

  16. Need for Time • When designing a game title, the recurring concern is game performance - will the game play fast enough? • An equally important question is whether the game will play smoothly. • No matter how optimized your engine may be, it will never be able to provide a full sense of realism if object motion is not fluid, or if the frame rate is not consistent.

  17. To control the rate of motion of objects, such that their motion per frame is consistent with variations in the time between frames • To throttle the frame rate according to the refresh rate of the monitor • For management of system resource allocation, such as performing low priority tasks at a regular interval • For timing game events

  18. Timer Methods • Loops • Win32 SetTimer • timeGetTime, GetTickCount and Performance Counter

  19. Performance Counter has greater resolution and accuracy • GetTickCount lowest overhead • Performance Counter may not be supported on all machines

  20. The End

More Related