3

所以我对 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

结果

这就是结果,这正是我想要完成的,但我只是在寻找一种更有效的方法。如果没有,我会坚持这个。

4

1 回答 1

1

有多种解决方案。我给你两个。

1.

创建屏幕大小的表面。用阿尔法填充它。在每个玩家移动时,将它的当前位置绘制到这个表面上——所以每个移动都会为你添加额外的数据到这个可能的掩码中。然后在屏幕上对该表面进行 blit(注意 blit 顺序)。在您的情况下,可以通过禁用 alpha 并最初用白色填充表面,然后首先对其进行 blitting 来改进它。顺便说一句,使用这种方法,您可以在翻转后跳过屏幕清除。

我建议从这个开始。

2.

不容易,但可能更有效(这取决于)。保存玩家实际改变移动方向的数组点。之后,您需要在这些点之间绘制链线。然而,SDL 中没有用于绘制线条的内置函数;也许在 SDL_gfx 中有,我从未尝试过。如果您稍后使用 OpenGL 后端,这种方法可能会更好;使用 SDL(或任何其他普通的 2D 绘图库),它并不太有用。

于 2013-11-26T09:08:46.430 回答