how to display score using sdl in c 2b 2b

Solutions on MaxInterview for how to display score using sdl in c 2b 2b by the best coders in the world

showing results for - "how to display score using sdl in c 2b 2b"
Aubin
19 Jan 2018
1int fontsize = 24;
2int t_width = 0; // width of the loaded font-texture
3int t_height = 0; // height of the loaded font-texture
4SDL_Color text_color = {0,0,0};
5string fontpath = "my font path";
6string text = "text I want to display";
7TTF_Font* font = TTF_OpenFont(fontpath.c_str(), fontsize);
8SDL_Texture* ftexture = NULL; // our font-texture
9
10// check to see that the font was loaded correctly
11if (font == NULL) {
12    cerr << "Failed the load the font!\n";
13    cerr << "SDL_TTF Error: " << TTF_GetError() << "\n";
14}
15else {
16    // now create a surface from the font
17    SDL_Surface* text_surface = TTF_RenderText_Solid(font, text.c_str(), text_color);
18
19    // render the text surface
20    if (text_surface == NULL) {
21        cerr << "Failed to render text surface!\n";
22        cerr << "SDL_TTF Error: " << TTF_GetError() << "\n";
23    }
24    else {
25        // create a texture from the surface
26        ftexture = SDL_CreateTextureFromSurface(renderer, text_surface);
27
28        if (ftexture == NULL) {
29            cerr << "Unable to create texture from rendered text!\n";
30        }
31        else {
32            t_width = text_surface->w; // assign the width of the texture
33            t_height = text_surface->h; // assign the height of the texture
34
35            // clean up after ourselves (destroy the surface)
36            SDL_FreeSurface(surface);
37        }
38    }
39}
40