PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Scrolling a surface?

Hi,

I would like to know if it's possible to scroll the content of a surface horizontally or vertically in PF? If so can you explain to me how it is done?
If you are talking about scrolling a surface on another, Blit() will do.

If you are talking about a single surface and want to scroll down or right, you can probably pull it off with Blit() too. Otherwise you will need to write your own code to do it.
I just want to simulate a 2d side-scroller type scrolling using a background surface that will scroll from right to left to simulate movement.
Assuming the background it a lot bigger than the screen (or actually 2x is enough)...

Move the source rect in the opposite direction.

I think this would work
Code:

static int x=0;

x++;
if(x > bgimage->GetWidth() - display->GetWidth())
x=0;

Rect srcrect;
srcrect.Set(x, 0, x+display->GetWidth(), display->GetHeight());

display->Blit(0,0, bgimage, &srcrect);



If you make your image where the last bit is the same as the first bit, then you won't notice when it flips back around to x=0;

EDIT:I can't remember for sure if the rect is passed as a pointer or not...
Thanks a lot, that did the trick. I had to blit the surface twice to scroll within the surface but it guided me in the right direction.
using pocketHAL I wrote some test-code to perform parallax scrolling of a starry foreground ontop of a nebula in the background; so 3 surfaces were sandwiched ontop of each other.
All 3 surfaces were exact screen size.

Code:
//increment the parallax counter
iParallaxCount++;
if (iParallaxCount == 10) iParallaxCount = 1;

//BACKGROUND NEBULA
SetRectToBlitAndTarget( rectToBlit, 0, 0, 320, 240, pointTargetStart, 0, 0 );
BlitRtoS( background_nebula_240, 240, surface, pointTargetStart, rectToBlit );


//BACK STARFIELD
if (iParallaxCount == 9)
{
//increment to next x offset
iBackStarfield_xOffset++;
if (iBackStarfield_xOffset == 240) iBackStarfield_xOffset = 0;
}
//draw first half
SetRectToBlitAndTarget( rectToBlit, iBackStarfield_xOffset, 0, 320, 240-iBackStarfield_xOffset, pointTargetStart, 0, 0 );
BlitRtoSMasked( back_starfield_240_320, 240, surface, pointTargetStart, rectToBlit, COLOR_BLACK );
//draw 2nd half
SetRectToBlitAndTarget( rectToBlit, 0, 0, 320, iBackStarfield_xOffset, pointTargetStart, (240-iBackStarfield_xOffset), 0 );
BlitRtoSMasked( back_starfield_240_320, 240, surface, pointTargetStart, rectToBlit, COLOR_BLACK );


//FRONT STARFIELD
if ((iParallaxCount == 2) || (iParallaxCount == 4) || (iParallaxCount == 6) || (iParallaxCount == 8))
{
//increment to next x offset
iFrontStarfield_xOffset++;
if (iFrontStarfield_xOffset == 240) iFrontStarfield_xOffset = 0;
}
//draw first half
SetRectToBlitAndTarget( rectToBlit, iFrontStarfield_xOffset, 0, 320, 240-iFrontStarfield_xOffset, pointTargetStart, 0, 0 );
BlitRtoSMasked( front_starfield_240_320, 240, surface, pointTargetStart, rectToBlit, COLOR_BLACK );
//draw 2nd half
SetRectToBlitAndTarget( rectToBlit, 0, 0, 320, iFrontStarfield_xOffset, pointTargetStart, (240-iFrontStarfield_xOffset), 0 );
BlitRtoSMasked( front_starfield_240_320, 240, surface, pointTargetStart, rectToBlit, COLOR_BLACK );


The code above is responsible for controlling the blitting. iParallaxCount is used as a delay mechanism.
I draw the background nebula first, ontop of that the slow-moving star-field and ontop of that the fast-moving star-field. COLOR_BLACK is used within the star-field to act as a mask; the mask is not blitted to screen.

SetRectToBlitAndTarget() just sets up the variable 'rectToBlit' with all the necessary co-ordinates. BlitRtoSMasked() is then passed 'rectToBlit' as well as the GFX address to perform the blitting. In pocketFrog you probably have blitting code already that you can use.

Also, since the Starfield_xOffset is incremented, the effect is that the starfield scrolls from right to left. Attached is the executable compiled for ARM PPC 2003.

EDIT:
some ideas.. since most of the screen is black I could probably have just drawn the background nebula and ontop of that drawn the stars as sprites. That would mean less wasted processing of mask color.
Attachments:
project_002.zip (48 KB)
Abiemann,

If you double the width (or height) of your image (and repeat the image on the extra space), you can combine the
//draw first half
//draw 2nd half
as one blit.

I imagine that the benefits of this may be small, but I think it may make enough of a difference to warrant doing.

If you have some code to measure the FPS, why not try it and see (I'd like to be proven right or wrong as well).
fast_rx wrote:Abiemann,

If you double the width (or height) of your image (and repeat the image on the extra space), you can combine the
//draw first half
//draw 2nd half
as one blit.

I imagine that the benefits of this may be small, but I think it may make enough of a difference to warrant doing.

If you have some code to measure the FPS, why not try it and see (I'd like to be proven right or wrong as well).


ah yeah! I was so fixated on using 240x320 that I didn't think about that.

Attached is the updated project where I've mirrored the scrolling images horizontally... it seems smoother
Attachments:
project_002_480.zip (53 KB)
Abiemann: Thank you so much for taking the time to do this.