Search code examples
c++sdl-2pongsdl-ttf

SDL show score on screen


Working on a Pong clone. Having serious issues with trying to display the score on the screen. Alot of things I've found are using images but I just want to use text to display the score number. I'm trying to use the SDL TTF library to load a font and display it but it won't display correctly. I found this question How to blit Score on screen in SDL? and the reply said to use SDL_BlitSurface() which I tried and I just got a build error (assuming I was doing it correctly)

Here's the function I call for drawing the score:

void Pong::drawScore(){
    leftScoreChar = leftScore;
    rightScoreChar = rightScore;

    SDL_Color text_color = {255, 255, 255};

    score = TTF_RenderText_Solid(font,
                                 &leftScoreChar,
                                 text_color);

    score2 = TTF_RenderText_Solid(font,
                                 &rightScoreChar,
                                 text_color);

    leftScoreText = SDL_CreateTextureFromSurface(renderer, score);
    rightScoreText = SDL_CreateTextureFromSurface(renderer, score2);

    SDL_RenderCopy(renderer, leftScoreText, NULL, &scoreA);
    SDL_RenderCopy(renderer, rightScoreText, NULL, &scoreB);
}

Which when run outputs this: https://goo.gl/dZxDEa

Aplogies, I would put an image in the post but apparently I can't.

And the score won't display unless the integer storing the score is made equal to 1 for some reason and displays zero. And the score is deffinatly increasing cause I have the game output the score to the console to make sure. So what am I doing wrong that's making my score display incorrectly and have some 00 thing?


Solution

  • There are a number of ways to do this. You can do it via an SDL_Surface, or a SDL_Texture. I'll illustrate both. (Adapt as necessary.)

    int fontsize = 24;
    int t_width = 0; // width of the loaded font-texture
    int t_height = 0; // height of the loaded font-texture
    SDL_Color text_color = {0,0,0};
    string fontpath = "my font path";
    string text = "text I want to display";
    TTF_Font* font = TTF_OpenFont(fontpath.c_str(), fontsize);
    SDL_Texture* ftexture = NULL; // our font-texture
    
    // check to see that the font was loaded correctly
    if (font == NULL) {
        cerr << "Failed the load the font!\n";
        cerr << "SDL_TTF Error: " << TTF_GetError() << "\n";
    }
    else {
        // now create a surface from the font
        SDL_Surface* text_surface = TTF_RenderText_Solid(font, text.c_str(), text_color);
    
        // render the text surface
        if (text_surface == NULL) {
            cerr << "Failed to render text surface!\n";
            cerr << "SDL_TTF Error: " << TTF_GetError() << "\n";
        }
        else {
            // create a texture from the surface
            ftexture = SDL_CreateTextureFromSurface(renderer, text_surface);
    
            if (ftexture == NULL) {
                cerr << "Unable to create texture from rendered text!\n";
            }
            else {
                t_width = text_surface->w; // assign the width of the texture
                t_height = text_surface->h; // assign the height of the texture
    
                // clean up after ourselves (destroy the surface)
                SDL_FreeSurface(surface);
            }
        }
    }
    

    Note, you can simply stop using just the surface alone. However, as the surface is software rendered, texture is arguably better as it is loaded in VRAM. (Read more here: Difference between surface and texture (SDL / general))

    Then, all you have to do is render it (similar to this):

    int x = 0;
    int y = 0;
    SDL_Rect dst = {x, y, t_width, t_height};
    SDL_RenderCopy(renderer, ftexture, NULL, &dst); // renderer is a variable of the type `SDL_Renderer*`
    

    Lastly, remember that the order in how you display things matters!