Platform SDK: DirectX

Step 1: Animate the Sprite

[C++]

This section pertains only to application development in Visual Basic. See DirectDraw C/C++ Tutorials.

[Visual Basic]

Since we know the height and width of each instance of the donut in the donut.bmp image, we can separate the bitmap into columns and rows with each cell being a single image of the donut:

ddsd3.lFlags = DDSD_CAPS
ddsd3.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
Set spritesurf = dd.CreateSurfaceFromFile("donut.bmp ", ddsd3)
spriteWidth = 64
spriteHeight = 64
cols = ddsd3.lWidth / spriteWidth
rows = ddsd3.lHeight / spriteHeight

Taking a closer look at the DirectDrawSurface7.BltFast method, we see that srcRect parameter is a RECT type that defines the upper-left and lower-right points of the rectangle to blit from on the source surface. Therefore by randomly specifying a portion or cell of the sprite surface we give the sprite animation:

col = currentFrame Mod cols
row = Int(currentFrame / cols)
rSprite.Left = col * spriteWidth
rSprite.Top = row * spriteHeight
rSprite.Right = rSprite.Left + spriteWidth
rSprite.Bottom = rSprite.Top + spriteHeight

'blt to the backbuffer our animated sprite
ddrval = backbuffer.BltFast(rSprite2.Left, rSprite2.Top, spritesurf, _ 
         rSprite, DDBLTFAST_SRCCOLORKEY Or DDBLTFAST_WAIT)