Jump to content
  • Advertisement
Sign in to follow this  
kev000

seg fault when copying SDL_Surface

This topic is 4050 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What would cause my program to crash when I copy this surface: "mysurface = s;"? ... private: SDL_Surface *mysurface; }; void surfaceClass::init(SDL_Surface *s, SDL_Rect a, int nalpha) { if(s==NULL) { //sdlnerror::errorLoading( "Error loading surface, blitting disabled." ); visibility=0; mysurface=NULL; } else { mysurface = s; //Crashes here!!! myarea.w = mysurface->w; myarea.h = mysurface->h; } } Anyone have any ideas why it would crash at this point?

Share this post


Link to post
Share on other sites
Advertisement
Since it's happening inside a member function and when accessing any members of the same class, I believe you are swimming into some undefined behavior. If you are calling it from a pointer to that class, check if that pointer is valid.


BTW, proper way to add references to a SDL_Surface is by doing:

SDL_Surface *s1,*s2;

s2 = get_surface_from_somewhere();
s1 = s2;
s1->refcount++; // you add this


So, when freeing (it will reduce number of refs and, if zero, will free it):

SDL_FreeSurface(s1);
SDL_FreeSurface(s2); // this would get a segfault, but no more


If you just want to copy content, create a new surface with same format and blit one onto the other.

Share this post


Link to post
Share on other sites
Then you're probably calling the member function on an uninitialised or otherwise invalid pointer to a surfaceClass instance, as mentioned above.

Share this post


Link to post
Share on other sites
SOLVED! Big thanks guys, the surfaceClass object I created was a pointer (surfaceClass *surface) and that was causing the crash. I just initialized it normal and it worked (surfaceClass surface).

quick off topic question:
how do I load a png image (using SDL_Image) that has transparencies and get it to display properly?

Share this post


Link to post
Share on other sites
It should "just work". If it doesn't, explain what exactly you're doing, what you see, and what you expected instead.

Share this post


Link to post
Share on other sites
Here's the code:

int buttonclass::create(int x, int y, string srcfile, string n, SDL_Surface *target, TTF_Font *f,SDL_Color c)
{
[snip]
SDL_Surface* loadedImage = NULL, *tempsurf = NULL;
loadedImage = IMG_Load( srcfile.c_str() );
if( loadedImage != NULL )
{
tempsurf=SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
}
else
{
sdlnerror::errorCreatingButton( srcfile.c_str() );
return -1;
}
SDL_SetAlpha( tempsurf, SDL_RLEACCEL | SDL_SRCALPHA, 255);
mysurface.init(tempsurf,area,0);
[snip]
alphavalue=200;
mysurface.setalpha(200);
ndebug::out("buttonclass::create() finished!",2);
return 1;
}

When it's loaded up into surfaceClass, it will then be blitted directly to the screen.

here is a link to what it looks like now:
http://www.nextabyte.com/nanonymous/forum/files/screenshot_202.png

here is a link to what it is supposed to look like:
http://www.linuxice.com/pngfinals/Main.png

Share this post


Link to post
Share on other sites
Seems to me that you are blitting the entire image to the background with alpha = 200. Alpha should vary by each pixel, so you can get the same effect you see on the other image.

Share this post


Link to post
Share on other sites
I guess my question would be how do I activate the per-pixel alpha? Is there a function that does this or should it work automatically? I have tried blitting the image to the background without any SetAlpha() being done and I get the same results. Is there something I have to do to the target surface before I blit it?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!