Click here to Skip to main content
13,594,066 members
Click here to Skip to main content
Add your own
alternative version


25 bookmarked
Posted 6 Aug 2006
Licenced CPOL

Drawing 2d in DirectX 8 and Above using ID3DXSprite

, 5 Nov 2006
Rate this:
Please Sign up or sign in to vote.
The tutorial demonstrates the usage of ID3DXSprite to render a 2D image using Directx 8.
Sample Image - 2dSprite.jpg


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 instead. 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,  // The texture to draw.
   CONST RECT* pSrcRect,    // The src rect 
   CONST D3DXVECTOR2* pScaling,   // Scaling
   CONST D3DXVECTOR2* pRotationCenter,  // The rotation center if rotation is used.
   FLOAT Rotation,    // Rotatino in radians.
   CONST D3DVECTOR2* pTranslation,  // Translation, i.e. moving the object to this place.
   D3DCOLOR Color    // Color to module the image pixels.
 );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 ID3DXSprite.

Load the texture using any of the techniques.

Rendering sequence.

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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Prakash Nadar
Software Developer (Senior)
United States United States
He is just a simple guy!!!

You may also be interested in...

Comments and Discussions

GeneralGreat piece of code thank you Pin
addicted_to_cpp9-Feb-09 15:19
memberaddicted_to_cpp9-Feb-09 15:19 
GeneralJust a black screen Pin
kishmet5-Nov-06 5:34
memberkishmet5-Nov-06 5:34 
GeneralRe: Just a black screen Pin
Mr.Prakash5-Nov-06 5:42
memberMr.Prakash5-Nov-06 5:42 
GeneralRe: Just a black screen Pin
kishmet5-Nov-06 6:43
memberkishmet5-Nov-06 6:43 
GeneralRe: Just a black screen Pin
Mr.Prakash5-Nov-06 19:58
memberMr.Prakash5-Nov-06 19:58 
GeneralRe: Just a black screen Pin
kishmet5-Nov-06 23:33
memberkishmet5-Nov-06 23:33 
QuestionRe: Just a black screen Pin
ceejeeb11-Nov-06 3:49
memberceejeeb11-Nov-06 3:49 
GeneralRe: Just a black screen Pin
ceejeeb11-Nov-06 4:22
memberceejeeb11-Nov-06 4:22 
AnswerRe: Just a black screen Pin
kishmet13-Nov-06 0:47
memberkishmet13-Nov-06 0:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02-2016 | 2.8.180618.1 | Last Updated 5 Nov 2006
Article Copyright 2006 by Prakash Nadar
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid