0
\$\begingroup\$

I have a little problem with Updating my Score with TTF. Im a neewbie so dont blame me :P

I Try to Update my Score But the Buffer Get too small after a while and my Game Crashes.

    void CText::Init(const string &_text, int xPos, int yPos, int Width, int Height)
{
    m_Text = _text;
    Font = TTF_OpenFont("arial.ttf", 30);

    SDL_Surface *pTempt = TTF_RenderText_Solid(Font, m_Text.c_str(), color);
    Texture = SDL_CreateTextureFromSurface(m_Renderer, pTempt);



    m_xPos = xPos;
    m_yPos = yPos;

    m_Rect.x = xPos;
    m_Rect.y = yPos;
    m_Rect.w = Width;
    m_Rect.h = Height;
    SDL_FreeSurface(pTempt);
}

void CText::RenderScore(int Score)
{
    sprintf_s(Buffer, "Score: %i", Score);
    SDL_Surface *pTempt = TTF_RenderText_Solid(Font, Buffer, color);
    Texture = SDL_CreateTextureFromSurface(m_Renderer, pTempt);
    SDL_RenderCopy(m_Renderer, Texture, NULL, &m_Rect);
    SDL_FreeSurface(pTempt);
}


//Game Loop 
if (lastscore != currentscore)
        {
            m_Text->Destroy(); //<-- This is SDL_DestroyTexture(Texture);
            lastscore = currentscore;
        }else{
        m_Text->RenderScore(currentscore);}

It shows my score and Updetes it too but I have my char Buffer[50] and after a while it runs out of memory. And i free my font and texture in the destructor.

I thank everybody who helps me out there

\$\endgroup\$
  • \$\begingroup\$ Is the score bigger than 1000000000000000000000000000000000000000000? \$\endgroup\$ – user253751 Nov 27 '19 at 13:03
0
\$\begingroup\$

Every time you call RenderScore, you leak an SDL_Texture by not calling SDL_DestroyTexture(Texture) before reallocating it with SDL_CreateTextureFromSurface.

\$\endgroup\$

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.