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

时间:2015-04-15 19:57:21

标签: c++ sdl sdl-2

我制作了一个小型游戏引擎作为辅助项目,但由于某种原因,我得到了一个非常奇怪的问题。

与精灵有关。我把精灵加载得很好,我似乎很好。但是当精灵的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);

}

这是图片:

enter image description here

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);
}

1 个答案:

答案 0 :(得分:4)

您通过指定SDL_Rect destRect.x = y

来错误地设置目的地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);
}