2

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?

Community
  • 1
  • 1
CharlieFan39
  • 29
  • 1
  • 3
  • 6

1 Answers1

2

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!

Community
  • 1
  • 1
jrd1
  • 10,358
  • 4
  • 34
  • 51
  • I've tried copying and pasting this code and ajusting it for my program but now I'm not getting any output from any sort of text on the window when run. – CharlieFan39 Jul 13 '15 at 03:41
  • @CharlieFan39 Did you initialize the corresponding Libraries properly? Did you enable `SDL_RENDERER_ACCELERATED`? – jrd1 Jul 13 '15 at 03:51
  • Yes I have. The way I was trying before gave me incorrect output but for some reason I don't have any output now. I'm so lost with this. – CharlieFan39 Jul 13 '15 at 04:04
  • @CharlieFan39 You and me both, that's code that from a working SDL Project that I did. Without knowing your settings, there isn't much I can comment on. But, here's a shot in the dark: what about your renderer draw color? Did you try changing that? – jrd1 Jul 13 '15 at 04:11
  • My aplogies that's excatly what the problem was. Would there be a way for me to do this for the score of both players without repeating it all again? – CharlieFan39 Jul 13 '15 at 04:16
  • @CharlieFan39 : If you're referring to how to avoid rewriting all that code, then you have two choices: define a function that loads the font and returns a `SDL_Texture*` (i.e. the created texture), or encapsulate it with a class (the above code is actually stripped from a class). I'd argue that using a class is better, and gives you higher code reuse - but, that's another story. – jrd1 Jul 13 '15 at 04:26
  • I think I'll just try doing it as a function. Is there anywhere I could find a good exaple of using it in a class though? Thanks for your help by the way. – CharlieFan39 Jul 13 '15 at 06:00
  • @CharlieFan39: The LazyFoo tutorials. A good place to start is #7 (for the background information), then to see how this information is encapsulated, perhaps the next one to read is #10, and others (this will be dependent on what you want to do). http://lazyfoo.net/tutorials/SDL/index.php Good luck! – jrd1 Jul 13 '15 at 07:56
  • @CharlieFan39: And, you're quite welcome! Thank you as well! – jrd1 Jul 13 '15 at 08:02