2016-07-31 74 views
-2

我正在尝试使用SDL(C++)为一张图片(320宽度,64高度)生成一个动画,每帧的长度为64×64。动画在SDL(C++)中不正确?

我的问题是,我的整个图片(320×64)挤在64×64的空间,而不是每帧显示。

我有一个与方法绘制()

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

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

backbuffer.DrawAnimatedSprite(*this); 
} 

方法DrawAnimatedSprite是这样的绘制动画的AnimatedSprite类,

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

所以任何人有任何想法,可能是错的? 谢谢!

回答

1

这个问题确实与你使用SDL_RenderCopy的方式有关,但你应该提供一个srcrect和一个dstrect,一个是你希望你的图像被渲染的目的地,另一个是你想要的纹理的一部分在那里呈现。

例如,如果你精灵表是(320×64),用(64×64)子画面的尺寸和要在您的fillRect的坐标(x,y)提供到在屏幕上打印的第一帧你应该做像这样:

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

希望这会有所帮助。

1

SDL_RenderCopy

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

你传递fillRectdstrect,不srcrect

+0

对不起,我不太明白,那我该如何改进呢? @LogicStuff – Wilheim

+0

尝试用'&fillRect'交换'0'。 – LogicStuff

+0

我用'&fillRect'交换'0',但那里没有任何东西。 @LogicStuff – Wilheim