测试包裹文本

This commit is contained in:
daiqingshuang
2025-05-28 18:13:10 +08:00
parent 6ebd3494a0
commit 34a91c6792

View File

@@ -24,7 +24,7 @@ int main(int argc, char* argv[]) {
TTF_Init();
TTF_Font* font = TTF_OpenFont("C:/Windows/Fonts/Arial.ttf", 24);
TTF_Font* font = TTF_OpenFont("C:/Windows/Fonts/msyh.ttc", 24);
if (font == nullptr) {
SDL_Log("Could not open font: %s", SDL_GetError());
SDL_DestroyRenderer(renderer);
@@ -39,7 +39,8 @@ int main(int argc, char* argv[]) {
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_Color color = { 255, 255, 255, 255 }; // 白色文字
auto text_surface = TTF_RenderText_Blended(font, "Hello World", 0, color);
SDL_Color bg_color = { 0, 0, 0, 255 }; // 背景颜色
auto text_surface = TTF_RenderText_LCD_Wrapped(font, "Hello World!\n你好,世界!", 0, color, bg_color, 0);
if (text_surface == nullptr) {
SDL_Log("Could not render text: %s", SDL_GetError());
TTF_CloseFont(font);
@@ -62,7 +63,7 @@ int main(int argc, char* argv[]) {
// SDL3 新方法获取纹理尺寸
float text_width, text_height;
SDL_GetTextureSize(texture, &text_width, &text_height);
SDL_FRect dest_rect = { 100, 100, text_width, text_height };
SDL_FRect dest_rect = { 0, 0, text_width, text_height };
SDL_Event e;
bool running = true;
@@ -74,14 +75,11 @@ int main(int argc, char* argv[]) {
}
// 正确的渲染顺序
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // 黑色背景
SDL_RenderClear(renderer); // 先清屏
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // 黑色背景
SDL_RenderClear(renderer); // 先清屏
SDL_RenderTexture(renderer, texture, nullptr, &dest_rect); // 然后绘制文字
SDL_RenderPresent(renderer); // 最后呈现
std::this_thread::sleep_for(std::chrono::milliseconds(16)); // ~60 FPS
SDL_RenderPresent(renderer); // 最后呈现
std::this_thread::sleep_for(std::chrono::milliseconds(16)); // ~60 FPS
}
// 清理资源