Sign in to follow this  
psiko_scweek

SDL issues? New at this, please help?

Recommended Posts

Ok, Im trying to get my game to load its levels from a file and so far its working without a problem cept im experiencing slowdowns from my loading code. here is what I have for the actual file loading.
[SOURCE]
void io_file_load(const std::string & filename)
{
    int temp_int;
    char temp_char[30];
    SDL_Surface *temp_sur = NULL;
    SDL_Surface *temp_backsur = NULL;
    std::string temp_text;

    std::ifstream infile(filename.c_str());
    if (infile)
   {
   infile >>temp_text;
   infile >> temp_char;
   if (temp_text != gmp_backgroundimage)
   {
   temp_backsur = gfx_load_image(temp_char,0);
   //if (Background.graphic != NULL) {gmp_fadeoutandin(Background.graphic,temp_backsur);};
   Background.setGraphic(temp_backsur);
   gmp_backgroundimage = temp_text;
   }
   infile >> NumberofUsedBlocks;
   for(int i = 1;i<=NumberofUsedBlocks;i++)
      {
      infile >> temp_char;
      temp_sur = gfx_load_image(temp_char,0);
      VerticalBlue[i].setGraphic(temp_sur);
      infile >> temp_int;
      VerticalBlue[i].setX(118+(40*temp_int));
      infile >> temp_int;
      VerticalBlue[i].setY(13+(40*temp_int));
      infile >> VerticalBlue[i].selected;
      infile >> VerticalBlue[i].direction;
      }
   }
}
[/SOURCE]
[/source] like i said, it works without a problem, cept there are slowdowns. Its extremely noticable if I use the gmp_fadeoutandin function. Im thinking it has to deal with how im loading my images from the file, but im not sure. any help would be great. here is the gmp_fadeoutandin code
[SOURCE]
void gmp_fadeoutandin(SDL_Surface *fadeouttemp1, SDL_Surface *fadeouttemp2)
{
for(int i = 255, b = 0;i>0;i = i-2,b = b+2)
   {
   SDL_SetAlpha(fadeouttemp1, SDL_SRCALPHA, i);
   gfx_apply_surface(0,0,fadeouttemp1,gfx_screen);
   SDL_SetAlpha(fadeouttemp2, SDL_SRCALPHA, b);
   gfx_apply_surface(0,0,fadeouttemp2,gfx_screen);
   gfx_flipscreen();
   }
}
[/SOURCE]
[/source] and again the code works and doesnt slow down here, but after the game slows down to a crawl. any help would be great. thanks ok I think i know that the problem is. I believe i may have a memory leak thats caysing my game to run so slow. But it seems like everytime I call SDL_FreeSurface, the game crashes. any advice? [Edited by - psiko_scweek on October 8, 2006 11:42:50 AM]

Share this post


Link to post
Share on other sites
Yeah, find out why it crashes! You can't just ignore the need to free surfaces. Beyond that it's very hard to say, because all your key functionality like surface management and rendering is not in the code you posted.

Share this post


Link to post
Share on other sites
Does the setBackground function that you're calling create a copy of the surface it's passed, or just a copy of the pointer?

If the latter, than you should put the call to SDL_FreeSurface in the destructor of that class. If you call it anywhere else it's likely that it will crash.

But, yeah, like Kyloton said, post some more code.

Share this post


Link to post
Share on other sites
ok the background is an object in my game. Here is the code the sets up the objects.


/////////// INITIALIZE THE OBJECT STRUCTURES ///////////
class Object
{
protected:
int x, y;
int graphicwidth;
int graphicheight;
int height, width;


public:
Object();
~Object();
void step();
void draw();
void setGraphicHeight(int tempHeigt);
void setGraphicWidth(int tempWidth);
int getGraphicHeight();
int getGraphicWidth();
void setX(int tempX);
void setY(int tempY);
int getX();
int getY();
int direction;
void setGraphic(SDL_Surface *graphic);
SDL_Surface *graphic;
};

/////////// SETUP THE OBJECTS CONSTRUCTOR ///////////
Object::Object()
{
x = 0;
y = 0;
graphic = NULL;
graphicwidth = 0;
graphicheight = 0;
}

/////////// SETUP THE OBJECTS DESTRUCTOR ///////////
Object::~Object()
{
if (graphic != NULL)
{SDL_FreeSurface(graphic);}
}

/////////// SETUP THE OBJECTS DRAW ///////////
void Object::draw()
{gfx_apply_surface(x,y,graphic,gfx_screen);}

/////////// SET THE GRAPHICS FOR THE OBJECT ///////////
void Object::setGraphic(SDL_Surface *setgraphic)
{
graphic = setgraphic;
graphicwidth = graphic->w;
graphicheight = graphic->h;
}

Share this post


Link to post
Share on other sites
I see one major problem. In your io_file_load function, you load a surface into temp_sur. You then pass this surface to VerticalBlue[i].setGraphic in a for loop. Since the setGraphic function only copies the pointer to a surface, all of these surface pointers point to the same surface.

Then, when one of them goes out of scope, the destructor is called. This frees the surface that this array all have in their graphic. Since you do not set it to NULL, however, every object in this array will try to free the surface, likely causing a crash.

You should modify the destructor so that it sets the surface to NULL after freeing it. Hope that helps.

[Edited by - Simian Man on October 10, 2006 2:59:10 PM]

Share this post


Link to post
Share on other sites
thanks, ill try that. You have to forgive me, like i said im new at this :-) so ill take all the help i can get!

so the destructor should look like:


Object::~Object()
{
if (graphic != NULL)
{SDL_FreeSurface(graphic);graphic = NULL;}
}



also, in this function:


void gmp_splashscreenrun(SDL_Surface *splashtemp)
{
Object SplashScreen;
SplashScreen.setGraphic(splashtemp);
SplashScreen.setX((SCR_W/2)-(SplashScreen.getGraphicWidth()/2));
SplashScreen.setY((SCR_H/2)-(SplashScreen.getGraphicHeight()/2));
for(int i = 0;i<255;i++)
{
SDL_FillRect( gfx_screen, &gfx_screen->clip_rect, SDL_MapRGB( gfx_screen->format, 0xFF, 0xFF, 0xFF ) );
SDL_SetAlpha(SplashScreen.graphic, SDL_SRCALPHA, i);
SDL_Delay(10);
if (i == 254)
{SDL_Delay(25);}
SplashScreen.draw();
gfx_flipscreen();
}
for(int i = 255;i>0;i--)
{
SDL_FillRect( gfx_screen, &gfx_screen->clip_rect, SDL_MapRGB( gfx_screen->format, 0xFF, 0xFF, 0xFF ) );
SDL_SetAlpha(SplashScreen.graphic, SDL_SRCALPHA, i);
SDL_Delay(10);
if (i == 1)
{SDL_Delay(25);}
SplashScreen.draw();
gfx_flipscreen();
}
}



the game crashes right when SDL_FreeSurface is called. (the destructor of the object) I dont understand why, the surface I pass through the function is ONLY used for this functon and nowhere else. any other suggestions?

again anything would be great, im trying my best to learn this stuff!

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this