Lately after playing a couple of games (after a long long time) I thought I should learn to make games because writing games involves lots of challenges which could be interesting.
This tutorial demonstrates how easy it is to draw a 2D image in DirectX 8 and above where
DirectDraw is not available.
Why this Tutorial?
Why do we need to know how to draw 2d in DirectX 8. One primary reason is that, the component DirectDraw which was available till DirectX 7 is no more there from DirectX 8 onwards. The official documentation of Microsoft says that DirectX 8 to DirectX 7 is like what win95 was to win3.11. DirectX 8 onwards concentrates more on 3D rather than 2D, so they merged 2D with 3D.
Since I was starting to learn DirectX, I thought 2D would be a better way to start before I get into the complexity of 3D. I searched the internet for any good example to render 2D in DirectX, but most of the samples used
DirectDraw has little support from DirectX 8 onwards and I guess there is no documentation for
DirectDraw either in DirectX 8 SDK.
I came across this new interface
ID3DXSprite, it is part of
Direct3DX. I tried to look for a good example using this interface, but could not find any. Even the samples provided by DirectX do not have any. So I did some experiments and came up with this example. So I thought it would be a good idea to post here as well for anyone who wants to do 2D using DirectX 8 and not using
DirectDraw in DirectX 8.
Few Things on ID3DXSprite
- Some say that this interface is slow and some would like to implement their own way of rendering a 2D graphics in 3D using the quards. I would say that there is no need to re-invent the wheel. This interface is quite efficient and does the job quite nicely.
- In DirectX 9, you can also do alpha bending using this interface.
- Don't get confused with the interface name. This interface does not do too much with the sprite, it just draws a 2D image.
ID3DXSprite is fast, so use it whenever you like it.
- One instance of
ID3DXSprite can be used to render all 2D images in the app. No need to create many instances of
ID3DXSprite for each sprite image.
Little about ID3DXSprite?
The three important methods are
Begin - Prepares the interface to draw a texture.
This should be always called before drawing.
Draw - Draws the sprite.
LPDIRECT3DTEXTURE8 pSrcTexture, CONST RECT* pSrcRect, CONST D3DXVECTOR2* pScaling, CONST D3DXVECTOR2* pRotationCenter, FLOAT Rotation, CONST D3DVECTOR2* pTranslation, D3DCOLOR Color );End - Ends the sprite drawing and restores any settings used to render the texture.
How to Use ID3DXSprite?
Usually one interface is enough to render all 2D images, unless you have a specific reason to create more.
D3DXCreateSprite(...) is used to create an instance of
Load the texture using any of the techniques.
g_d3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
g_d3dDevice->Present( NULL, NULL, NULL, NULL );
About the Sample
The sample code attached is very simple and easy to read. It does nothing jazzy except use the interface to draw the sprite.
The sample bounces the ball around the window.
Hope you find this tutorial useful.