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
相关产品推荐
相关产品推荐

