0
\$\begingroup\$

This is the way I render the score in my main game , and I made this new project to understand how to use it... It works , the score is updated and it display on the screen but the problem is memory... it increase forever , from 20mb when I start untill it runs out of memory..it nevers stop. And in the main game it's working at 2fps... I hate TTF I just can't understand how it works and how should I do it... every tutorial is in c++ and I have to make the game in pure C for college...Help.

#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>

int main(int argc, char ** argv)
{
    bool quit = false;
    SDL_Event event;

    SDL_Init(SDL_INIT_VIDEO);
    TTF_Init();

    SDL_Window * window = SDL_CreateWindow("Example",SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640,480, 0);
    SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
    SDL_Surface * surface;
    SDL_Texture * texture;

    int score = 0;
    char buffer[50];
    int texW = 0;
    int texH = 0;
    TTF_Font * font = TTF_OpenFont("font3.ttf", 25);
    SDL_Color color = { 255, 255, 255 };


    while (!quit)

    {
        SDL_RenderClear(renderer);

        SDL_WaitEvent(&event);
        switch (event.type)
        {
        case SDL_QUIT:
            quit = true;
            break;
        }

        score += 100;
        sprintf(buffer, "SCORE: %d", score);

        surface = TTF_RenderText_Solid(font, buffer, color);
        texture = SDL_CreateTextureFromSurface(renderer, surface);
        SDL_QueryTexture(texture, NULL, NULL, &texW, &texH);

        SDL_FreeSurface(surface);
        SDL_Rect dstrect = { 0, 0, texW,texH };
        SDL_RenderCopy(renderer, texture, NULL, &dstrect);

        SDL_RenderPresent(renderer);
    }


    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_DestroyTexture(texture);
    TTF_CloseFont(font);

    TTF_Quit();
    SDL_Quit();

    return 0;
}
\$\endgroup\$
1
  • \$\begingroup\$ Of course memory leaks. You never free texture in the loop. \$\endgroup\$ Jan 3 '17 at 19:30
0
\$\begingroup\$

It's been a while since I've done any SDL but I think your problem is here: texture = SDL_CreateTextureFromSurface(renderer, surface); You're creating a texture and then never freeing it.

If you dig into the SDL2 source code you'll find that in SDL_render.c an SDL_Texture gets allocated on the heap every time you call SDL_CreateTexture* (texture = (SDL_Texture *) SDL_calloc(1, sizeof(*texture));)

What will probably solve your leak, and save some frames, would be to only update the texture when the score updates and to free the existing texture before you allocate a new one.

if (prevScore != currentScore)
{
  SDL_DestroyTexture(scoreTexture);
  //Create new score texture
}

//Regardless of score change, render the score
\$\endgroup\$
5
  • \$\begingroup\$ ok but what's with the prevScore and currentScore? \$\endgroup\$
    – FIREHIVE
    Jan 3 '17 at 19:53
  • \$\begingroup\$ That's just one way to track score changes; just for the sake of example. Every frame you could change prevScore to the value of currentScore at the end of the frame and before that, if currentScore was changed during the frame, there was a change in score so you should update the texture. Or you could just have a dedicated method for updating score that would set a boolean like scoreChanged and if that was set to true you could update the score texture and then set scoreChanged back to false. \$\endgroup\$
    – Honeybunch
    Jan 3 '17 at 19:54
  • \$\begingroup\$ I found the problem in my main game that was causing the memory leaks , I was using open font on every frame but never closing it. Now my game uses 60mb of Ram . The problem is it runs at 2fps , I think it's about what you said , it have to update the texture when the score updates. But I`m dumb and don't understand how to use the prevScore and current score. One last thing , can you do it for the example code I posted above? Please help me :(. \$\endgroup\$
    – FIREHIVE
    Jan 3 '17 at 21:08
  • \$\begingroup\$ Like I said, the prevScore and currentScore variables were just to provide an example of how to detect a change in score. I suggest you open a new question with more code directly relevant to the specific frame drop issue you are having. \$\endgroup\$
    – Honeybunch
    Jan 3 '17 at 21:42
  • \$\begingroup\$ I found why , normaly I have 2000 fps , when I load the font , create the surface , and create the texture every fps , it drops at 300 fps , because it happens every loop. Now I just have to detect a change in score and I don't know how :P . \$\endgroup\$
    – FIREHIVE
    Jan 3 '17 at 21:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.