Help with horizontal scrolling

Started by
2 comments, last by Skizz 16 years, 5 months ago
Hi everyone, I currently have a function that I am using to scroll a background vertically. I am trying to convert this code so that I can scroll a background horizontally from right to left, but no matter what I try, I cannot figure out how to switch the code around other than the obvious changing of tops and bottoms to lefts and rights. Any help is appreciated. the function: //background RECT RECT rct; rct.left = 0; rct.right = gTextureWidth; //background vector D3DXVECTOR3 pos(0,0,0); // Top sprite rct.top = gTextureHeight-gOffset; rct.bottom = gTextureHeight; sprite_handler->Draw(bg, &rct, NULL,&pos,0xFFFFFFFF); // Bottom sprite rct.top=0; rct.bottom=gScreenHeight-gOffset; pos.y = (float)gOffset; sprite_handler->Draw(bg, &rct, NULL,&pos,0xFFFFFFFF); // background scrolling gOffset += 4; if (gOffset > gTextureHeight) gOffset=0; thanks, Jim
Advertisement
If you understood how it scrolled vertically, getting it scroll horizontally would be trivial. The scrolling is done by drawing the image twice:
|--------||        | <- first image|        |********** -|*        *  |- = distance = gOffset*--------* -|*        **        * <- * = screen**********|        | <- second image|--------|

Rather than drawing the whole image twice, only the visible portion of each image is drawn. Once you've determined how the co-ordinates are calculated, rotate the diagram to work out the new co-ordinates (effectively swap top/left, bottom/right, width/height).

Skizz
Skizz, I changed the function to this:

//background RECT
RECT rct;
rct.top = 0;
rct.bottom = gTextureHeight;

//background vector
D3DXVECTOR3 pos(0,0,0);

// Top sprite
rct.left = gTextureWidth-gOffset;
rct.right = gTextureWidth;

sprite_handler->Draw(bg, &rct, NULL,&pos,0xFFFFFFFF);

// Bottom sprite
rct.left=0;
rct.right=gScreenWidth-gOffset;

pos.x = (float)gOffset;
sprite_handler->Draw(bg, &rct, NULL,&pos,0xFFFFFFFF);

// background scrolling
gOffset += 4;
if (gOffset > gTextureWidth)
gOffset=0;

While this code works for scrolling the background from left to right, no matter what I change, I cannot get it to scroll properly from right to left. I thought to do this i would just have to change gOffset += 4; to gOffset -= 4;, but this does not work, and just distorts the images. I looked at this code for over 3 hours, and I absolutely cannot figure it out, hopefully someone can PLEASE explain the proper way to do this.
The value for gOffset must be bounded:

0 <= gOffset < gTextureWidth (or gTextureHeight for vertical scrolling)

Consider what would happen should gOffset lie outside those bounds - the rectangle for the first draw command will is:

rct.left = gTextureWidth-gOffset;
rct.right = gTextureWidth;

and if gOffset is < 0 then the left edge will be to the right of the right edge, which is clearly nonsense. So, the update to gOffset should really be:

gOffset -= 4;
if (gOffset < 0)
gOffset=gTextureHeight;

But you should really check the value is in the required bounds:

if (gOffset < 0)
gOffset=gTextureHeight;
if (gOffset > gTextureHeight)
gOffset=0;

Skizz

This topic is closed to new replies.

Advertisement