2D Scrolling with DirectX


Many 2D games require a scrolling background. Vertical and horizontal shooters tend to use it to portray movement over a terrain or space etc.

Simulating Motion

If we simply wish to give the impression of motion we can use a repeating texture.

We will use the features of the Direct3D sprite interfaces. They allow us to position our texture anywhere on the screen and also select a rectangular area of the texture to display. In order to simulate movement we will need to draw at least two sprites. Two sprites are needed if the texture is larger than the screen area. If it is smaller you will need more. The best way to explain this is by working through an example.


In this example our screen area is 256 wide by 400 high. Our texture is 256 wide by 512 high. Since our texture is bigger than the screen area we need to select a rectangular area to draw. If we place the sprite at 0,0 we will need to draw a rectangular area from 512-400=112 to 512 down e.g.

RECT rct;

D3DXVECTOR3 pos(0,0,0);
gSprite->Draw(texture, &rct, NULL,&pos,0xFFFFFFFF);

The texture with the area drawn to the screen outlined in black: