You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SDL2 TTF文本渲染异常:红色Hello World不显示及噪点问题

SDL2渲染文本黑屏及后续噪点问题

我正在开发基于SDL2的C程序,尝试通过将SDL_Surface转换为SDL_Texture,再调用SDL_RenderCopy()在屏幕上渲染文本。最初程序仅显示预期的黑屏,红色的"Hello World!"文本完全未出现。fonts目录与g编译后的可执行文件处于同一工作目录。

修改代码后(感谢genpfault),文本已能部分渲染,但文本后方出现奇怪的噪点,推测与SDL_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_RenderCopy(renderer, textTexture, nullptr, &rect);

        SDL_UpdateWindowSurface(window);
    }

    SDL_Quit();
    TTF_Quit();

    return 0;
}

修改后的代码

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

噪点效果

噪点效果

内容的提问来源于stack exchange,提问作者Ard_Flamingo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 22:12:11