PocketMatrix forums

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

Copying pBackBuffer, erasing pBackBuffer and copy back?

I edited this topic to make a bit more sense...
This issue has been brought up before, but I just didn't understand the sample code given.

I have pBackBuffer containing a lot of FillRect's.
To give a scrolling effect, I would like to move the whole pBackBuffer to the left a certain amount of pixel's on the X coord.
I am currently using BltFast and setting the destX to -10:
GDBLTFASTFX bltfastfx;
pBackBuffer->BltFast(-10,0, &pBackBuffer, NULL,NULL,&bltfx);

It shifts the whole pBackBuffer to the left 10 pixels.
But it seems like it doesn't clear the buffer right before it moves it, so everything is "streaking" horizontally.

How can I shift the pBackBuffer by x,y...but let it "clean up" the screen before it moves everything, so the screen doesnt have remnants of the screen before it was moved?

If you can answer this, please supply some code I could use to do this, since I learn to code visually. Thanks in advance. :)
InexorableTash · 06 Jun 2004, 17:56 UTC
Consider this array shifting function, which tries to shift everything in the array up by 10 slots:

Code:

int a[100] = { 0, 1, 2, 3, ... 99 };
for( int i = 0; i < 90; i++ )
{
a[i + 10] = a[i];
}


Trace this through, and see happens when i gets to 10.

You're seeing the same thing - the effect of a copy from a surface where the destination and target ranges overlap is undefined - you're overwriting some pixels that you later need to read from.

You have two choices - write your own pixel copy routine that does the memory copies in the correct order (fastest), or use another surface (less code).

(I actually use this effect in the hobby game I've been toying with to do some cool blurring for a sniper scope. I scale-blit the backbuffer onto itself with a smaller size and transparency, and repeat this 4 times so I get 16 copies streaked together with maximum distortion around the edges and minimal distortion in the center of the screen. To be perfectly accurate and predictable I should do this using two buffers, but doing it in one buffer increases the blur effect on parts of the screen so I left it that way!)
Yeah, I was thinking of moving the whole screen pixel by pixel, but it just didn't seem "right."

A couple of other people have run across this problem and solved it with help and on their own, but they really didn't show the code behind their comments. This is what I did to move the backbuffer 20 pixels (x coord) to the left:

Code:

HRESULT CMyApplication::ProcessNextFrame(CGapiSurface* pBackBuffer, DWORD dwFlags)
{
//when 480 and 320 are used, its due to the Palm Zodiac's widescreen 480x320
//edit as needed

GDBLTFX bltfx;
GDFILLRECTFX fillrectfx;

// rectangled screen used for filling the screen with color
RECT rect;

// the destination rectangled screen
// this rect will show what section to write to on the screen
RECT destrect; // the destination rectangled screen

// the source rectangled screen
// this rect will show what section of the screen to retrieve for the destrect
RECT srcrect;

//sets the viewport to the entire backbuffer
pBackBuffer->SetClipper(NULL);

//***Add the code to do this ONCE so the screen will be setup before its moved***
//set the background green
rect.left = 0;
rect.right = 480;
rect.top = 0;
rect.bottom = 320;
pBackBuffer->FillRect(&rect,RGB(0,255,0),NULL,&fillrectfx);
//set a red stripe down the middle
//the red stripe will appear to move as the screen moves to the left
rect.left = 240;
rect.right = 280;
rect.top = 0;
rect.bottom = 320;
pBackBuffer->FillRect(&rect,RGB(255,0,0),NULL,&fillrectfx);
//***END of the code needed only once***

//sets the source screen to 480x320, the whole screen
srcrect.left = 0;
srcrect.right = 480;
srcrect.top = 0;
srcrect.bottom = 320;

//sets the destination screen
//by setting the .left destination to -20
//and the source screen being 0
//this will move the screen to the left by 20 pixels
//the destination .right can be edited for your needs
destrect.left = -20;
destrect.right = 460;
destrect.top = 0;
destrect.bottom = 320;

//this is where the actual movement of the screens are made
//it "blits" the srcrect to the destrect.
pBackBuffer->Blt(&destrect, pBackBuffer, &srcrect, NULL, &bltfx);

//if you dont put this code in the ProcessNextFrame event
//then you will need to flip the buffer manually
//to do this you would use: m_pDisplay->Flip();

//***you might want to add a pause/delay here
//because if not, it could move too fast to see***

//needed for the ProcessNextFrame event
return S_OK;
}


Works for me just fine. Hope this helps others.

-Fraggler