1
\$\begingroup\$

I have this in my loop , it works but I get verry low fps and game is working in slow motion :

while (!done)
    {
        //Check for events
        done = processEvents(window, &player);
        updateLogic(&player);

        sprintf(buffer, "SCORE: %d", player.currentScore);

        textSurface = TTF_RenderText_Solid(font, buffer, color);
        text = SDL_CreateTextureFromSurface(renderer, textSurface);

        SDL_QueryTexture(text, NULL, NULL, &textW, &textH);
        SDL_FreeSurface(textSurface);

        doRender(renderer, &player);
        SDL_DestroyTexture(text);
    }

I think I need to update those 2 only when score is changing , not every frame..and I don't know how to do it.

textSurface = TTF_RenderText_Solid(font, buffer, color);
text = SDL_CreateTextureFromSurface(renderer, textSurface);
\$\endgroup\$
2
\$\begingroup\$

So the way I would approach this is by adding an additional member bool ScoreChanged to your player instance, and set it when you change the score. An alternative approach would be to save the score at the end of your main loop and compare it against the current value before rendering.

Anyways, once you have that setup, all you need to do is to render the texture. This means that you want to move the initial texture creation and declaration outside of your mainloop. Once you detect a score change, render the surface and translate it to the texture. Remember to destroy to previous texture. Also don't forget to reset the ScoreChanged value, if you went with that approach.

For a larger project you might want to consider caching your prerendered textures of text to a global stash, but something like the above should work just fine here.

\$\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.