Search code examples
csdlram

Raising RAM in load BMP function SDL


I'm using SDL to code a simple game, and i have a big problem - i'm trying to do some animations with function, in the fuction i call static int which keeps raising every game tick, and dependant on value of static int i change my variable image (with image=SDL_LoadBMP(myfile)), it's working great, but after 10 minutes of running a program, which had been working with 50MB of memory before without this really simple animation, ram usage of my program is starting to get bigger and bigger, and as i said, after 10 minutes it's 3GB and keeps raising every animation occur(so, like every 3 seconds).

Weird thing is also that i have other image which animation is a little bit simplier - i change my image upon clicking any arrow (still in main), and then call function, so after a second it gives back the initial image to a variable(it's giving image back in function), and it's working great - with that i mean - even if i keep clicking arrows, memory usage is constant.

my function looks like that:

void func(obj* image)
{
static int time1;
time1++;
if(time1>1000)
{
time1=0;
SDL_FreeSurface(image->image); //this doesn't change anything
image->image=SDL_LoadBMP("path");
}
else if(time1>800)
image->image=SDL_LoadBMP("path2");
else if(time1>600)
image->image=SDL_LoadBMP("path3");
else if(time1>400)
image->image=SDL_LoadBMP("path4");
}

typedef struct {
    SDL_Surface* image;
}obj;

int main()
{
obj struct;
func(&struct);
}

ofc it's fulfilled with all this SDL library calls to make a window etc

https://i.ibb.co/YBcvjnF/Bez-tytu-u.png


Solution

  • If I understand correctly you're making SDL_Surface* over and over again, you never call SDL_FreeSurface()(info).

    You need to load a some point all the BMP needed to play the animation into SDL_Surface* then reuse these BMP(s).

    In your main (or into an init function) you need to store into an array or pointers the BMP images.

    // Somewhere on one of your struct
    SDL_Surface *animationImages[4];
    
    // Then in an init function you do
    animationImages[0] = SDL_LoadBMP("path");
    animationImages[1] = SDL_LoadBMP("path2");
    animationImages[2] = SDL_LoadBMP("path3");
    animationImages[3] = SDL_LoadBMP("path4");
    
    // And finally 
    void func(obj* image) {
        static int time1;
        time1++;
    
        if (time1>1000) {
            time1 = 0;
            image->image = animationImages[0];
        } else if (time1>800) {
            image->image = animationImages[1];
        } else if (time1>600) {
            image->image = animationImages[2];
        } else if (time1>400) {
            image->image = animationImages[3];
        }
    }
    

    And before the end of your game or when you don't need these animationImages anymore call SDL_FreeSurface() for each SDL_Surface* you have created.

    // In a specific function used to clean up allocated stuff you do
    SDL_FreeSurface(animationImages[0]);
    SDL_FreeSurface(animationImages[1]);
    SDL_FreeSurface(animationImages[2]);
    SDL_FreeSurface(animationImages[3]);