2016-07-31 26 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