SDL3 提供了 SDL_RenderGeometry
函式繪製幾何圖形,用法和 OpenGL 差不多,先定義頂點資料,然後根據頂點資料繪製幾何圖形。
繪製三角形的程式碼如下:
std::array<SDL_Vertex, 3> origin_vertices = {
SDL_Vertex { { 150, 100 }, { 1.0f, 0.0f, 0.0f, 1.0f } }, // top
SDL_Vertex { { 000, 300 }, { 0.0f, 1.0f, 0.0f, 1.0f } }, // left bottom
SDL_Vertex { { 300, 300 }, { 0.0f, 0.0f, 1.0f, 1.0f } } // right bottom
};
SDL_RenderGeometry(renderer, nullptr, origin_vertices.data(), origin_vertices.size(), nullptr, 0);
效果如圖:
注意上面的頂點定義,座標雖然是浮點數但表示的是畫素值,顏色則做了歸一化(取值範圍 0.0~1.0)這一點和 SDL2 不同,SDL2 中顏色取值範圍是 0~255.
可以透過實時修改頂點座標實現運動效果。首先修改事件迴圈,由重繪事件 SDL_EVENT_WINDOW_EXPOSED
觸發渲染改成空閒時自動渲染,對應的讀取事件的方式也由 SDL_WaitEvent
改為 SDL_PollEvent
:
SDL_Event event {};
bool keep_going = true;
while (keep_going) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_QUIT:
keep_going = false;
break;
case SDL_EVENT_KEY_DOWN: {
keep_going = keep_going && (event.key.keysym.sym != SDLK_ESCAPE);
break;
}
}
}
SDL_SetRenderDrawColor(renderer, 16, 0, 16, 255);
SDL_RenderClear(renderer);
SDL_RenderGeometry(renderer, nullptr, origin_vertices.data(), origin_vertices.size(), nullptr, 0);
SDL_RenderPresent(renderer);
}
使用 SDL_GetTicks
獲取時間,定義計算三角形位置所需變數:
uint64_t last_tickets = SDL_GetTicks();
float position = 0.0f;
float direction = 1.0f;
在 while
迴圈中增加位置計算和修改頂點資料的程式碼:
while(keep_going) {
...
uint64_t current_ticks = SDL_GetTicks();
float delta_time = (current_ticks - last_tickets) / 1000.0f;
last_tickets = current_ticks;
position += 200.0f * delta_time * direction;
int width = 0;
SDL_GetRenderOutputSize(renderer, &width, nullptr);
float max_position = static_cast<float>(width) - (origin_vertices[2].position.x - origin_vertices[1].position.x);
if (position > max_position) {
direction = -1.0f;
} else if (position < 0.0f) {
position = 0.0f;
direction = 1.0f;
}
std::vector<SDL_Vertex> vertices;
for (const auto& vertex : origin_vertices) {
vertices.push_back(vertex);
vertices.back().position.x += position;
}
...
SDL_RenderGeometry(renderer, nullptr, vertices.data(), vertices.size(), nullptr, 0);
...
}
這樣我們就得到了一個在視窗上水平往復運動的三角形: