PocketMatrix forums

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

Surface * passed as argument

I ran across something that doesn't quite make sense to me...

I was trying to pass a "Surface *" into a function which deletes the image and makes a new one. The program wasn't working right, so I stepped through. The pointer was passed, then the image created, but then the new pointer was not passed back out of the function.

I would have thought that anything with a * would be modifiable unless it has the const keyword. The only way I got it to work was by passing a "Surface **".

Is this something about the way a Surface is defined or am I just loosing it?

Here's some code to explain better:
This Works:
Code:
void ScaleTheImg(DisplayDevice * display, Surface * originalimg, Surface ** scaledimg)
{

if(*scaledimg != NULL)
delete scaledimg;

static tempw, temph;
if( (tempw = originalimg->GetWidth() / rblit.scale) < 1) tempw = 1;
if( (temph = originalimg->GetHeight() / rblit.scale) < 1) temph = 1;

*scaledimg = display->CreateSurface(tempw ,temph);

Surface* screen = display->SetRenderTarget(*scaledimg);

Rect r;
r.Set(0, 0, tempw, temph);
display->BlitStretch(r, originalimg);

display->SetRenderTarget(screen);

(*scaledimg)->SetColorMask(Color(255,0,255));

}


This doesn't work:
Code:
void ScaleTheImg(DisplayDevice * display, Surface * originalimg, Surface * scaledimg)
{

if(scaledimg != NULL)
delete scaledimg;

static tempw, temph;
if( (tempw = originalimg->GetWidth() / rblit.scale) < 1) tempw = 1;
if( (temph = originalimg->GetHeight() / rblit.scale) < 1) temph = 1;

scaledimg = display->CreateSurface(tempw ,temph);

Surface* screen = display->SetRenderTarget(scaledimg);

Rect r;
r.Set(0, 0, tempw, temph);
display->BlitStretch(r, originalimg);

display->SetRenderTarget(screen);

scaledimg->SetColorMask(Color(255,0,255));

}
well, it's obvious why the first program will not pass the pointer back:
this pointer is passed in as a copy (per value), so that copy is modified - not the original pointer. your second version will work fine, except that you should do 'delete *scaledimg;' in line 5...

all you'd need to change in your previous version is the last parameter to 'Surface*& scaledimg'. then scaledimg is passed as a reference to a pointer. it can be used in the code just as a normal pointer, but is in fact a pointer to a pointer (such as in your later code).

hope this helps you,
bye,
DANIEL