c++ SDL2 TTF文本未按预期呈现

bqf10yzr  于 2023-05-30  发布在  其他
关注(0)|答案(1)|浏览(121)

我一直在开发一个利用SDL 2的程序,我一直在尝试使用SDL_Surface将文本转换为SDL_Texture,然后使用SDL_RenderCopy()进行渲染。它只显示一个黑屏(如预期),但红色的“Hello World!“文本不会出现。这也是C++,澄清一下。
这是我的代码:

#include <iostream>
#include <SDL.h>
#include <SDL_ttf.h>

int main()
{
    bool quit = false;

    SDL_Event event;

    SDL_Init(SDL_INIT_EVERYTHING);
    TTF_Init();

    SDL_Window *window = SDL_CreateWindow("GUI Text Editor", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);

    if(window == NULL)
    {
        std::cout << "window is null" << SDL_GetError();
    }

    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

    if(renderer == NULL)
    {
        std::cout << "renderer is null" << SDL_GetError();
    }

    SDL_Color red = {255, 0, 0, 255};

    TTF_Font *roboto = TTF_OpenFont("fonts/Roboto-Regular.ttf", 24);

    if(roboto == NULL)
    {
        std::cout << "font is null " << SDL_GetError();
    }

    SDL_Surface *textSurface = TTF_RenderText_Solid(roboto, "Hello World!", red);

    if(textSurface == NULL)
    {
        std::cout << "surface is null " << SDL_GetError();
    }

    SDL_Texture *textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);

    if(textTexture == NULL)
    {
        std::cout << "texture is null " << SDL_GetError();
    }

    SDL_Rect rect;
    rect.x = 0;
    rect.y = 0;
    rect.w = 300;
    rect.h = 300;

    while (!quit)
    {
        while (SDL_PollEvent(&event) > 0)
        {
            switch (event.type)
            {
                case SDL_QUIT:
                    quit = true;
                    break;
            }
        }

        SDL_RenderCopy(renderer, textTexture, nullptr, &rect);

        SDL_UpdateWindowSurface(window);
    }

    SDL_Quit();
    TTF_Quit();

    return 0;
}

(The fonts文件夹与g++编译的可执行文件位于同一个工作目录中)
我期待着红色的“Hello World!”文本出现,但唉,它没有。
编辑:多亏了genpfault,我才能够得到文本的渲染,但是文本背后有奇怪的静态特性-可能是rect
我的新代码:

#include <iostream>
#include <SDL.h>
#include <SDL_ttf.h>

int main()
{
    bool quit = false;

    SDL_Event event;

    SDL_Init(SDL_INIT_EVERYTHING);
    TTF_Init();

    SDL_Window *window = SDL_CreateWindow("GUI Text Editor", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);

    if(window == NULL)
    {
        std::cout << "window is null" << SDL_GetError();
    }

    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

    if(renderer == NULL)
    {
        std::cout << "renderer is null" << SDL_GetError();
    }

    SDL_Color red = {255, 0, 0, 255};

    TTF_Font *roboto = TTF_OpenFont("fonts/Roboto-Regular.ttf", 24);

    if(roboto == NULL)
    {
        std::cout << "font is null " << SDL_GetError();
    }

    SDL_Surface *textSurface = TTF_RenderText_Solid(roboto, "Hello World!", red);

    if(textSurface == NULL)
    {
        std::cout << "surface is null " << SDL_GetError();
    }

    SDL_Texture *textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);

    if(textTexture == NULL)
    {
        std::cout << "texture is null " << SDL_GetError();
    }

    SDL_Rect rect;
    rect.x = 0;
    rect.y = 0;
    rect.w = 300;
    rect.h = 300;

    while (!quit)
    {
        while (SDL_PollEvent(&event) > 0)
        {
            switch (event.type)
            {
                case SDL_QUIT:
                    quit = true;
                    break;
            }
        }

        SDL_RenderPresent(renderer);

        SDL_RenderCopy(renderer, textTexture, nullptr, &rect);

        SDL_UpdateWindowSurface(window);
    }

    SDL_Quit();
    TTF_Quit();

    return 0;
}

静态:

7kqas0il

7kqas0il1#

我不太清楚您是如何同时使用SDL_CreateRendererSDL_UpdateWindowSurface的,但以下是您问题的简短答案:将SDL_UpdateWindowSurface(window);替换为SDL_RenderPresent(renderer);,并在适当的地方调用SDL_RenderClear(可能就在SDL_RenderCopy之前)。
如果你像你在评论中说的那样正在学习Lazy Foo的教程,我猜你还没有学习到the TTF section,因为它基本上做了我上面说的事情。我只能假设你跳到搜索"how to render text in SDL2",并试图适应你发现的东西,而不理解它的作用。请注意,该帖子中的第二个答案为您提供了一个完整的工作示例。
我建议密切关注教程或放弃它来尝试其他东西。例如,深入研究SDL wiki以准确了解每个函数的作用,并在Web上搜索任何不明显的内容,例如SDL_RenderPresent vs SDL_UpdateWindowSurface之间的差异。

相关问题