c++ - SDL 在屏幕上显示分数

标签 c++ sdl-2 pong sdl-ttf

正在研究 Pong 克隆。尝试在屏幕上显示分数时遇到严重问题。我发现很多东西都使用图像,但我只想使用文本来显示分数。我正在尝试使用 SDL TTF 库加载字体并显示它,但它无法正确显示。我发现这个问题How to blit Score on screen in SDL?回复说使用我尝试过的 SDL_BlitSurface() ,但我刚刚收到构建错误(假设我做得正确)

这是我调用的用于绘制乐谱的函数:

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);
}

运行时输出: https://goo.gl/dZxDEa

抱歉,我会在帖子中添加图片,但显然我不能。

并且除非存储分数的整数由于某种原因等于1并显示零,否则分数不会显示。而且分数肯定会增加,因为我让游戏将分数输出到控制台来确定。那么我做错了什么导致我的分数显示不正确并且有一些 00 的事情?

最佳答案

有多种方法可以做到这一点。 您可以通过 SDL_SurfaceSDL_Texture 来完成此操作。我将对这两者进行说明。 (根据需要进行调整。)

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);
        }
    }
}

请注意,您可以停止仅使用表面。然而,由于表面是软件渲染的,纹理可以说更好,因为它加载在 VRAM 中。 (在此处了解更多信息:Difference between surface and texture (SDL / general))

然后,您所要做的就是渲染它(与此类似):

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*`

最后,请记住显示内容的顺序很重要!

关于c++ - SDL 在屏幕上显示分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31365160/

相关文章:

c++ - 用于用 C++ 编写的编译器的树解析器

C++ : Automatically run function when derived class is constructed

c++ - SDL2.0 键盘动 Sprite

java - 选定的变量在乒乓球游戏中不会改变 - Swing

linux - GLUT 键盘 react 迟钝

python - Pygame Pong 有条件使球弹跳

c++ - 将 Boost 累加器与 Eigen::Vector 类型一起使用

c++ - C++读取大文件

sdl - SDL2 库的静态链接

c++ - SDL_Jostick 正在发送奇怪的整数