DX9: Sprites and Fonts

22
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

description

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); - PowerPoint PPT Presentation

Transcript of DX9: Sprites and Fonts

Page 1: 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

Page 2: DX9: Sprites and Fonts

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}

Page 3: DX9: Sprites and Fonts

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()

Page 4: DX9: Sprites and Fonts

HRESULT Begin(DWORD flags)

sprite->Begin(D3DXSPRITE_ALPHABLEND);

HRESULT Draw(LPDIRECT3DTEXTURE9 pSrcTexture, CONST RECT *pSrcRect, D3DXVECTOR3 *center, CONST D3DVECTOR3 *pTranslation, D3DCOLOR Color );

Page 5: DX9: Sprites and Fonts

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();

Page 6: DX9: Sprites and Fonts

• Moving Sprites

• static D3DXMATRIX spriteMatrix;

• D3DXMatrixTransformation2D(&spriteMatrix, NULL, 0.0f, &vScaling1, &vCentre1, fAngle1, &vTranslation1);

Page 7: DX9: Sprites and Fonts

• 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();

Page 8: DX9: Sprites and Fonts

• 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.

Page 9: DX9: Sprites and Fonts
Page 10: DX9: Sprites and Fonts
Page 11: DX9: Sprites and Fonts

• 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.

Page 12: DX9: Sprites and Fonts

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

Page 13: DX9: Sprites and Fonts

• 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.

Page 14: DX9: Sprites and Fonts

• 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.

Page 15: DX9: Sprites and Fonts

• RECT r;

– r.left = 0;

– r.right = 0;

– r.top = 0;

– r.bottom = 0;

Page 16: DX9: Sprites and Fonts

• DrawText

• INT DrawText(

• LPD3DXSPRITE pSprite,

• LPCTSTR pString,

• INT Count,

• LPRECT pRect,

• DWORD Format,

• D3DCOLOR Color

• );

Page 17: DX9: Sprites and Fonts

• Releasing the Font Object

• Don’t forget to release the font object, in the usual way, when you have finished.

• pFont -> Release();

Page 18: DX9: Sprites and Fonts

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.

Page 19: DX9: Sprites and Fonts

• 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

Page 20: DX9: Sprites and Fonts

• Timer Methods

• Loops

• Win32 SetTimer

• timeGetTime, GetTickCount and Performance Counter

Page 21: DX9: Sprites and Fonts

• Performance Counter has greater resolution and accuracy

• GetTickCount lowest overhead

• Performance Counter may not be supported on all machines

Page 22: DX9: Sprites and Fonts

The End