動畫制作
遊戲動畫如何制作呢?
我的了解就是,動畫就是一幅幅圖檔不斷替換就形成了動畫。
當然,每一副圖檔要顯示一段時間。
所有定義一個動畫的實作類,代碼如下:
#ifndef SKANIMATION_H_
#define SKANIMATION_H_
#include "SkComm.h"
#include "SkImage.h"
#include <vector>
using namespace std;
namespace sk_park {
class SkAnimation {
public:
SkAnimation();
//擷取對應時刻點應該繪制的圖檔
SkSurface * getShow(Sint64 iMSecTime);
//設定動畫開始播放的時間點,動畫從此時間點後會循環播放
void setBeginTime(Sint64 iBeginMSec);
//添加動畫圖檔,按照順序添加每一幅圖檔,并指定該圖檔顯示的毫秒數
void addPic(SkSurface & stPic, Sint32 iMSec);
//清除動畫内容
void clear();
//動畫的位置資訊,隻作為成員屬性,動畫繪制的位置需要在繪制時候指定
Sint32 m_iPosX;
Sint32 m_iPosY;
//動畫大小,隻作為成員屬性,動畫繪制的大小需要在m_vctPic中指定
Sint32 m_iWidth;
Sint32 m_iHeight;
//繪制的圖檔
vector<SkSurface> m_vctPic;
//繪制圖檔的時長
vector<Sint32> m_vctMSec;
//動畫開始時間
Sint64 m_iBeginMSec;
//動畫總時長
Sint32 m_iTotalMSec;
};
}
extern sk_park::SkAnimation g_SkAnimation;
#endif /* SKANIMATION_H_ */
實作如下:
#include "pch.h"
#include "SkAnimation.h"
using namespace sk_park;
SkAnimation::SkAnimation() {
m_iBeginMSec = 0;
m_iPosX = 0;
m_iPosY = 0;
m_iTotalMSec = 0;
}
void SkAnimation::setBeginTime(Sint64 iBeginMSec) {
m_iBeginMSec = iBeginMSec;
}
SkSurface * SkAnimation::getShow(Sint64 iMSecTime) {
if (m_iBeginMSec == 0) {
m_iBeginMSec = iMSecTime;
}
Sint32 iMSec = iMSecTime - m_iBeginMSec;
if (iMSec < 0) {
iMSec = 0;
}
if (m_iTotalMSec <= 0) {
return NULL;
}
Sint32 iMSecTmp = iMSec % m_iTotalMSec;
Sint32 iTmp = 0;
for (int i = 0; i < (int) m_vctMSec.size(); i++) {
iTmp += m_vctMSec.at(i);
if (iTmp >= iMSecTmp) {
return &(m_vctPic.at(i));
}
}
return NULL;
}
void SkAnimation::addPic(SkSurface & stPic, Sint32 iMSec) {
if (iMSec > 0) {
m_iTotalMSec += iMSec;
m_vctMSec.push_back(iMSec);
m_vctPic.push_back(stPic);
}
}
void SkAnimation::clear() {
m_vctMSec.clear();
m_vctPic.clear();
m_iTotalMSec = 0;
}
SkAnimation g_SkAnimation;
使用示例:
{
SkImage image;
image.load("pic/p0.png");
m_testAnimation.addPic(image.m_skSurface, 100);
}
{
SkImage image;
image.load("pic/p1.png");
m_testAnimation.addPic(image.m_skSurface, 100);
}
{
SkImage image;
image.load("pic/p2.png");
m_testAnimation.addPic(image.m_skSurface, 100);
}
{
SkImage image;
image.load("pic/p3.png");
m_testAnimation.addPic(image.m_skSurface, 100);
}
m_testAnimation.setBeginTime(g_SkGameTimer.m_siGameLastTime);
運作效果: