所以我对 SDL 还很陌生,我正在尝试制作一个小单板滑雪游戏。当玩家在下山时,我想在他身后留下一条淡色的雪迹。目前,我的工作方式是我有一个数组(包含 1000 个元素)来存储玩家的最后位置。然后每一帧,我都有一个循环 1000 次的 for 循环,以在玩家的所有这些最后 1000 个位置绘制轨迹纹理......
我觉得这非常低效,我正在寻找一些更好的选择!
编码:
void Player::draw()
{
if (posIndex >= 1000)
{
posIndex = 0;
}
for (int i = 0; i < 1000; i++) // Loop through all the 1000 past positions of the player
{
// pastPlayerPos is an array of SDL_Rects that stores the players last 1000 positions
// This line calculates teh location to draw the trail texture
SDL_Rect trailRect = {pastPlayerPos[i].x, pastPlayerPos[i].y, 32, 8};
// This draws the trail texture
SDL_RenderCopy(Renderer, Images[IMAGE_TRAIL], NULL, &trailRect);
}
// This draws the player
SDL_Rect drawRect = {(int)x, (int)y, 32, 32};
SDL_RenderCopy(Renderer, Images[0], NULL, &drawRect);
// This is storing the past position
SDL_Rect tempRect = {x, y, 0, 0};
pastPlayerPos[posIndex] = tempRect;
posIndex++; // This is to cycle through the array to store the new position
这就是结果,这正是我想要完成的,但我只是在寻找一种更有效的方法。如果没有,我会坚持这个。