115 lines
4.0 KiB
C++
115 lines
4.0 KiB
C++
#include <thread>
|
|
#include <chrono>
|
|
|
|
#include "SDL3/SDL_init.h"
|
|
#include "SDL3/SDL_render.h"
|
|
#include "SDL3/SDL_video.h"
|
|
#include "SDL3/SDL_log.h"
|
|
#include "SDL3/SDL_timer.h"
|
|
#include "SDL3/SDL_gpu.h"
|
|
#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, "direct3d12,metal,vulkan");
|
|
if (renderer == nullptr) {
|
|
SDL_Log("Could not create renderer: %s", SDL_GetError());
|
|
SDL_DestroyWindow(window);
|
|
return 1;
|
|
}
|
|
auto gpu_device = (SDL_GPUDevice*)SDL_GetPointerProperty(SDL_GetRendererProperties(renderer), SDL_PROP_RENDERER_GPU_DEVICE_POINTER, NULL);
|
|
|
|
TTF_Init();
|
|
|
|
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);
|
|
SDL_DestroyWindow(window);
|
|
return 1;
|
|
}
|
|
// 设置字体hinting为轻量级
|
|
TTF_SetFontHinting(font, TTF_HINTING_LIGHT_SUBPIXEL);
|
|
// 设置字体为 SDF 模式
|
|
// TTF_SetFontSDF(font, true);
|
|
// 设置渲染器的混合模式
|
|
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
|
|
|
|
SDL_Color color = { 255, 255, 255, 255 }; // 白色文字
|
|
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);
|
|
SDL_DestroyRenderer(renderer);
|
|
SDL_DestroyWindow(window);
|
|
return 1;
|
|
}
|
|
|
|
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 = { 0, 0, text_width, text_height };
|
|
|
|
SDL_GPUShaderCreateInfo shader_info;
|
|
shader_info.code_size = 10;
|
|
shader_info.code = nullptr; // 这里需要填入实际的着色器代码
|
|
shader_info.entrypoint = "main";
|
|
shader_info.format = SDL_GPU_SHADERFORMAT_DXIL;
|
|
shader_info.stage = SDL_GPU_SHADERSTAGE_FRAGMENT;
|
|
shader_info.num_samplers = 0;
|
|
shader_info.num_storage_textures = 0;
|
|
shader_info.num_uniform_buffers = 0;
|
|
shader_info.num_storage_buffers = 0;
|
|
auto shader = SDL_CreateGPUShader(gpu_device, &shader_info);
|
|
|
|
SDL_GPURenderStateDesc desc;
|
|
SDL_INIT_INTERFACE(&desc);
|
|
desc.fragment_shader = shader;
|
|
auto render_state = SDL_CreateGPURenderState(renderer, &desc);
|
|
|
|
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); // 最后呈现
|
|
SDL_RenderLines(renderer, nullptr, 0); // 确保没有多余的线条
|
|
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;
|
|
}
|