SDL(C++) 中的动画不正确?

animation not correctly in SDL(C++)?

我正在尝试使用 SDL(c++) 为每帧 64×64 的图片(320 宽度,64 高度)制作动画。

我的问题是我的整张图片 (320×64) 被压缩到 64×64 space,而不是每帧显示。

我有一个 AnimatedSprite class 用于使用方法 Draw()

绘制动画
AnimatedSprite::Draw(BackBuffer& backbuffer)
{
//set frame width
this->SetFrameWidth(64);

//set x coordinate
this->SetX(frameCoordinateContainer[m_currentFrame-1]);

backbuffer.DrawAnimatedSprite(*this);
}

DrawAnimatedSprite的方法是这样的,

void BackBuffer::DrawAnimatedSprite(AnimatedSprite& animatedSprite)
{
SDL_Rect fillRect;
fillRect.x = animatedSprite.GetX();
fillRect.y = animatedSprite.GetY();
fillRect.w = animatedSprite.getFrameWidth();
fillRect.h = animatedSprite.GetHeight();
SDL_RenderCopy(m_pRenderer, animatedSprite.GetTexture()->GetTexture(), 0, &fillRect);
}

所以有人知道哪里可能出错了吗? 谢谢!

SDL_RenderCopy:

int SDL_RenderCopy(SDL_Renderer*   renderer,
                   SDL_Texture*    texture,
                   const SDL_Rect* srcrect,
                   const SDL_Rect* dstrect)

您将 fillRect 传递给 dstrect,而不是 srcrect

确实是你使用的方式有问题SDL_RenderCopy,但是你应该同时提供一个srcrect和一个dstrect,一个是你想要渲染图片的目的地,另一个是什么部分您想要在那里渲染的纹理。

例如,如果您的精灵 sheet 是 (320×64),精灵大小为 (64x64),并且您想在屏幕上的坐标 (x, y) 处打印第一帧 fillRect 你应该这样做:

SDL_Rect clip;
clip.x = 0;
clip.y = 0;
clip.w = 64;
clip.h = 64;

SDL_Rect fillRect;
fillRect.x = animatedSprite.GetX();
fillRect.y = animatedSprite.GetY();
fillRect.w = animatedSprite.getFrameWidth();
fillRect.h = animatedSprite.GetHeight();

SDL_RenderCopy(m_pRenderer, animatedSprite.GetTexture()->GetTexture(), &clip, &fillRect);

希望对您有所帮助。