PocketMatrix forums

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

Copy a Surface

Thierry,

Would it be possible to have a function would create an exact copy of a Surface, same width, height, and pitch, with an option to copy the contents also?

In other words, create a copy of the Back Surface (or any surface) so that memcpy can be used.

Thanks,

Pam
Are we talking PF or PHAL here?
I think this is essentially what Pam's looking for, but using memcpy rather than SetRenderTarget and Blit...
Code:
Surface *CopySurface(Surface *_pOriginal, bool _bCopyPixels)
{
Surface *pTemp = GetDisplay()->CreateSurface(_pOriginal->GetWidth(), _pOriginal->GetHeight());
if(_bCopyPixels)
{
GetDisplay()->SetRenderTarget(pTemp);
GetDisplay()->Blit(0,0,_pOriginal);
GetDisplay()->SetRenderTarget(0);
pTemp->SetColorMask(_pOriginal->GetColorMask());
}
return pTemp;
}


Of course, that's a PF convention. I'm not sure how it would be done with just PHAL.

-John
Oh sorry, PocketHAL.

Pam
If you have two surfaces A and B and they have the same dimensions, then they will have the same pitch (unless one of them is the backbuffer returned by PHAL::Display).

So if this indeed your case, you can simply do:

Code:

Surface* Copy( const Surface* src )
{
Surface* dst = new Surface( src->GetWidth(), src->GetHeight() );

if (!dst || !dst.GetPixels())
{
// Out of memory
delete dst;
return 0;
}
else
{
// Copy the surface
memcpy( dst->GetPixels(), src->GetPixels(), src->GetHeight() * src->GetPitch() );
return dst;
}
}



That should do it. If you are dealing with the backbuffer, you will need to check if the pitches match. If they do, you can use the above code. If they dont, you will have to copy one row at a time. I believe there is code in PF 0.6 that does exactly this (Raster2D::Blit or something like that).
Thierry wrote:
That should do it. If you are dealing with the backbuffer, you will need to check if the pitches match. If they do, you can use the above code. If they dont, you will have to copy one row at a time. I believe there is code in PF 0.6 that does exactly this (Raster2D::Blit or something like that).


Yeah, I was specifically hoping to copy to the backbuffer with one memcpy, and was looking for a way to create a surface that would have the same pitch.

Pam
Pam wrote:Yeah, I was specifically hoping to copy to the backbuffer with one memcpy, and was looking for a way to create a surface that would have the same pitch.


Then you can do:

Code:
unsigned width = backbuffer->GetPitch() / 2;
unsigned height = backbuffer->GetHeight();
Surface* copy = new Surface( width, height );


This will work as long as the backbuffer pitch is a multiple of 32 bits, and I believe this is always true (I've never seen otherwise).
Oh, I see! Thanks, that makes sense.

Pam
Pam, why do you need to do this? Surely you can render directly into the backbuffer instead of your temporary surface?

If you do this so that you can keep the backbuffer content from frame to frame, then you should be using the SWAP_COPY mode of PHAL.
Well the idea was to create an effect, like the water or fire effects that typically use two buffers. You loop through one buffer to figure out what goes in the second buffer, Blit the second buffer, then swap and do the whole thing again.

Of course one wouldn't often use the backbuffer for this, only if it was a fullscreen effect.

The other thread about the 3x3 matrix blur got me thinking about it.

Pam
I guess there is a need for double-buffering semantic then. This would mean implementing SWAP_FLIP and adding a way to access the previous backbuffer. It would not only allow for your water effect, but for any other filtering effect.
After thinking about it more, and trying it out, I doubt one would do this kind of thing without creating two temp surfaces and swapping which one is blitted to the back buffer.

It works, but of course anything else that you put on the back buffer, like a FPS counter gets filtered too.

I could be wrong, but I also think performance is better using two temp surfaces because moving data between a temp surface and the backbuffer is slower.

Thanks anyway, as usual I spoke before I thought it through :wink:

Pam