修复渲染问题
This commit is contained in:
135
src/main.cpp
135
src/main.cpp
@@ -1,8 +1,5 @@
|
||||
//
|
||||
// Created by 46944 on 25-5-21.
|
||||
//
|
||||
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
|
||||
#include "SDL3/SDL_init.h"
|
||||
#include "SDL3/SDL_render.h"
|
||||
@@ -12,58 +9,88 @@
|
||||
#include "SDL3_ttf/SDL_ttf.h"
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
SDL_Window* window = SDL_CreateWindow("Hello World", 640, 480, 0);
|
||||
if (window == nullptr) {
|
||||
SDL_Log("Could not create window: %s", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
SDL_Renderer* renderer = SDL_CreateRenderer(window, "direct3d11");
|
||||
if (renderer == nullptr) {
|
||||
SDL_Log("Could not create renderer: %s", SDL_GetError());
|
||||
SDL_DestroyWindow(window);
|
||||
return 1;
|
||||
}
|
||||
TTF_Init();
|
||||
auto text_engine = TTF_CreateRendererTextEngine(renderer);
|
||||
if (text_engine == nullptr) {
|
||||
SDL_Log("Could not create text engine: %s", SDL_GetError());
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
return 1;
|
||||
}
|
||||
TTF_Font* font = TTF_OpenFont("C:/Windows/Fonts/Arial.ttf", 24);
|
||||
if (font == nullptr) {
|
||||
SDL_Log("Could not open font: %s", SDL_GetError());
|
||||
TTF_DestroyRendererTextEngine(text_engine);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
return 1;
|
||||
}
|
||||
SDL_Color color = { 255, 255, 255, 255 };
|
||||
auto text_surface = TTF_RenderText_Solid(font, "Hello World", 0, color);
|
||||
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, text_surface);
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
SDL_Window* window = SDL_CreateWindow("Hello World", 640, 480, 0);
|
||||
if (window == nullptr) {
|
||||
SDL_Log("Could not create window: %s", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
SDL_Renderer* renderer = SDL_CreateRenderer(window, "direct3d11");
|
||||
if (renderer == nullptr) {
|
||||
SDL_Log("Could not create renderer: %s", SDL_GetError());
|
||||
SDL_DestroyWindow(window);
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
|
||||
TTF_Init();
|
||||
|
||||
SDL_RenderTexture(renderer, texture, nullptr, nullptr);
|
||||
TTF_Font* font = TTF_OpenFont("C:/Windows/Fonts/Arial.ttf", 24);
|
||||
if (font == nullptr) {
|
||||
SDL_Log("Could not open font: %s", SDL_GetError());
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
return 1;
|
||||
}
|
||||
// 设置字体hinting为轻量级
|
||||
TTF_SetFontHinting(font, TTF_HINTING_LIGHT);
|
||||
// 设置字体为 SDF 模式
|
||||
// TTF_SetFontSDF(font, true);
|
||||
// 设置渲染器的混合模式
|
||||
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
|
||||
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_RenderPresent(renderer);
|
||||
SDL_Color color = { 255, 255, 255, 255 }; // 白色文字
|
||||
auto text_surface = TTF_RenderText_Blended(font, "Hello World", 0, color);
|
||||
if (text_surface == nullptr) {
|
||||
SDL_Log("Could not render text: %s", SDL_GetError());
|
||||
TTF_CloseFont(font);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_Event e;
|
||||
while (true) {
|
||||
if (!SDL_PollEvent(&e)) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||
continue;
|
||||
}
|
||||
if (e.type == SDL_EVENT_QUIT) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
TTF_Quit();
|
||||
return 0;
|
||||
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, text_surface);
|
||||
SDL_DestroySurface(text_surface); // 释放surface
|
||||
|
||||
if (texture == nullptr) {
|
||||
SDL_Log("Could not create texture: %s", SDL_GetError());
|
||||
TTF_CloseFont(font);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// SDL3 新方法获取纹理尺寸
|
||||
float text_width, text_height;
|
||||
SDL_GetTextureSize(texture, &text_width, &text_height);
|
||||
SDL_FRect dest_rect = { 100, 100, text_width, text_height };
|
||||
|
||||
SDL_Event e;
|
||||
bool running = true;
|
||||
while (running) {
|
||||
while (SDL_PollEvent(&e)) {
|
||||
if (e.type == SDL_EVENT_QUIT) {
|
||||
running = false;
|
||||
}
|
||||
}
|
||||
|
||||
// 正确的渲染顺序
|
||||
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_DestroyTexture(texture);
|
||||
TTF_CloseFont(font);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
TTF_Quit();
|
||||
SDL_Quit();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user