为什么我的 SDL/C++ 图片消失了?

Why are my SDL/C++ images disappearing?

我正在制作一个小游戏引擎作为副业,但出于某种原因,我遇到了一个非常奇怪的问题。

与精灵有关。我正在很好地加载精灵,我似乎渲染得很好。但是,当精灵的 x、y 彼此靠近时,其中一个瓷砖要么消失,要么走错位置。通常无论是 x 还是 y 都会相差 32。

void SpriteLoader::loadSprites(SDL_Renderer* renderer)
{
    loadSprite("assets/grass.png", "grass", renderer);
    loadSprite("assets/grass_full.png", "grass2", renderer);
}

void SpriteLoader::renderSprites(SDL_Renderer* renderer)
{
    drawSprite("grass", 0, 0, 32, 32, renderer);
    drawSprite("grass2", 64, 32, 32, 32, renderer);

}

图片如下:

void SpriteLoader::drawSprite(std::string id, int x, int y, int width, int height, SDL_Renderer* renderer, SDL_RendererFlip flip)
{
    SDL_Rect srcRect;
    SDL_Rect destRect;

    srcRect.x = 0;
    srcRect.y = 0;
    srcRect.w = destRect.w = width;
    srcRect.h = destRect.h = height;
    destRect.x = y;
    destRect.y = y;

    SDL_RenderCopyEx(renderer, textures[id], &srcRect, &destRect, 0, 0, flip);
}

您通过分配 destRect.x = y 错误地设置了目的地 SDL_Rect,它应该是 destRect.x = x

void SpriteLoader::drawSprite(std::string id, int x, int y, int width, int height, SDL_Renderer* renderer, SDL_RendererFlip flip)
{
    SDL_Rect srcRect;
    SDL_Rect destRect;

    srcRect.x = 0;
    srcRect.y = 0;
    srcRect.w = destRect.w = width;
    srcRect.h = destRect.h = height;
    destRect.x = x;   // THIS WAS THE ERROR (You had destRect.x = y;)
    destRect.y = y;

    SDL_RenderCopyEx(renderer, textures[id], &srcRect, &destRect, 0, 0, flip);
}