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.