2

Pong クローンに取り組んでいます。画面にスコアを表示しようとすると深刻な問題が発生します。私が見つけたものの多くは画像を使用していますが、テキストを使用してスコア番号を表示したいだけです。SDL TTF ライブラリを使用してフォントを読み込んで表示しようとしていますが、正しく表示されません。この質問を見つけました 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 が表示される原因は何でしょうか?

4

1 に答える 1

2

これを行うにはいくつかの方法があります。、またはを介し​​て実行できます。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 に読み込まれるため、間違いなく優れています。(詳細はこちら: Surface と Texture の違い (SDL / 一般) )

次に、レンダリングするだけです (これと同様)。

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

最後に、物事を表示する順序が重要であることを忘れないでください。

于 2015-07-12T07:35:14.353 に答える