这种序列帧动画要求每一帧的宽高必须一一致,否则动画播起来会出问题。

需要的图片类似图1.1 会把所有的动作拼接到一张图上,这样做也是为了节省内存和减少DrawCall,切换动作的时候只需要重新计算图片的UV,然后把算出来的UV作为新区域贴在原来的那张纹理面片上即可。

图1.1

Animate是Animation的一部分,一个Animation可以保存多个Animate,也可以理解成Animate只是一个动画片段,Animation是完整的动画控制器。这个结构其实有点像Unity的AnimateClip和Animator。

不多说,上代码,自己去看代码吧,实现不是很难,不多讲了。

程序1.1

Animate.h 完整代码

#pragma oncenamespace U2D
{enum MovementEvent{START,COMPLETE,};class CAnimate :public CPicture{friend class CAnimation;private:char name[50]; //动画名UINT frameWidth;//每一帧的宽度UINT frameHeight;//每一帧的高度UINT rows;//总行数UINT cols;//总列数UINT startIndex;//开始的索引编号UINT endIndex;//结束的索引编号UINT curIndex;//当前的索引编号bool isPlaying;//是否播放bool isLoop;//是否循环UINT frameSpeed;//每一帧的速度UINT frameTimer;//每一帧的时间animate_selector listener_movementEvent; //动画事件监听public:CAnimate(char *fileName, UINT rows, UINT cols);CAnimate(CCTexture *tex, UINT rows, UINT cols);void setName(char *name) { strcpy(this->name, name); }char *getName() { return this->name; }void setFrameIndex(UINT startIndex, UINT endIndex){this->startIndex = startIndex;this->endIndex = endIndex;curIndex = startIndex;}Vector2 getFrameSize() { return Vector2(frameWidth, frameHeight); }UINT getFrameCount() { return endIndex - startIndex + 1; }void play() { isPlaying = true; }void stop() { isPlaying = false; }void repeat(bool isLoop) { this->isLoop = isLoop; }void setDelayUnit(UINT frameSpeed) { this->frameSpeed = frameSpeed; }void setMovementEventCallFunc(animate_selector fun_movementEvent);//设置动画事件的回调函数void draw();void all_Anchor_0_0();void all_Anchor_05_05();public:CAnimate() {}~CAnimate(void) {}};}

程序1.2

Animate.cpp 完整代码

#include "Engine.h"namespace U2D
{CAnimate::CAnimate(char *fileName, UINT rows, UINT cols):CPicture(fileName){strcmp(this->name, fileName);this->rows = rows;this->cols = cols;isPlaying = true;isLoop = true;frameSpeed = 10;frameWidth = pTexture->getWidth() / cols;frameHeight = pTexture->getHeight() / rows;startIndex = 0;endIndex = rows*cols - 1;curIndex = startIndex;}CAnimate::CAnimate(CCTexture *tex, UINT rows, UINT cols):CPicture(tex){strcmp(this->name, tex->getName());this->rows = rows;this->cols = cols;isPlaying = true;isLoop = true;frameSpeed = 10;frameWidth = tex->getWidth() / cols;frameHeight = tex->getHeight() / rows;startIndex = 0;endIndex = rows*cols - 1;curIndex = startIndex;}void CAnimate::setMovementEventCallFunc(animate_selector fun_movementEvent){this->listener_movementEvent = fun_movementEvent;}void CAnimate::draw(){if (isPlaying){if (frameTimer++%frameSpeed == 0){if (curIndex == startIndex){if (listener_movementEvent){if (parent != NULL){(parent->*listener_movementEvent)(this, MovementEvent::START);}else(parent->*listener_movementEvent)(this, MovementEvent::START);}}if (curIndex == endIndex + 1){if (isLoop){curIndex = startIndex;}else{isPlaying = false;curIndex = endIndex;}if (listener_movementEvent){if (parent != NULL){if (parent != NULL){(parent->*listener_movementEvent)(this, MovementEvent::COMPLETE);}else(parent->*listener_movementEvent)(this, MovementEvent::COMPLETE);}}}(&srcRect,curIndex%cols*frameWidth,curIndex / cols*frameHeight,(curIndex%cols + 1)*frameWidth,(curIndex / cols + 1)*frameHeight);curIndex++;}}CPicture::draw();}void CAnimate::all_Anchor_0_0(){}void CAnimate::all_Anchor_05_05(){}}

程序1.3

Animation.h 完整代码

#pragma oncenamespace U2D {class CAnimation :public CElement{friend class CPlayer;protected:list<CAnimate*> animateList;                   //动画的链表用名字判断,有就返回,没有就new一个CAnimate *animate;                              //当前播放的动画animation_selector listener_movementEvent;     //动画集事件监听void animateEvent(CAnimate* animate, MovementEvent type);UINT rows;UINT cols;char*fileName;public:CAnimation();CAnimate* addAnimate(char *animName, char *fileName, UINT rows, UINT cols);CAnimate* addAnimate(char *animName, CCTexture *tex, UINT rows, UINT cols);CAnimate* findAnimate(char *animName);void setAnimate(char *animName);Vector2 getFrameSize() { return animate->getFrameSize(); }UINT getFrameCount() { return animate->getFrameCount(); }void play() { animate->play(); }void stop() { animate->stop(); }void repeat(bool isLoop) { animate->repeat(isLoop); }void setDelayUnit(UINT frameSpeed) { animate->setDelayUnit(frameSpeed); }void setFrameIndex(UINT startIndex, UINT endIndex) { return animate->setFrameIndex(startIndex, endIndex); }void setName_FrameIndex(char*name, UINT startIndex, UINT endIndex);void setMovementEventCallFunc(animation_selector fun_movementEvent);void draw();void setCurrentAllanchor(float ox, float oy);RECT getBoundBox();~CAnimation();};
}

程序1.4

Animation.cpp 完整代码

#include "Engine.h"namespace U2D
{CAnimation::CAnimation(){animate = NULL;listener_movementEvent = NULL;}CAnimate* CAnimation::addAnimate(char *animName, char *fileName, UINT rows, UINT cols){CAnimate* anim = findAnimate(animName);//查找这个动画名字,如果找不到就会返回空if (anim != NULL)                     //如果不为空就说明找到了return anim;this->rows = rows;this->cols = cols;this->fileName = fileName;anim = new CAnimate(fileName, rows, cols); //如果为空就new一个对象压进去。并且把名字设置好anim->setName(animName);                    //设置动画名字anim->setParent(this);                       //CAnimation是CNode的子类,子类拥有父类的所有数据,所以压入CAnimation就等同于压入了CNode。anim->setMovementEventCallFunc(animate_selector(&CAnimation::animateEvent));//回调函数animateList.push_back(anim);              //压进链表setAnimate(animName);                     //设置动画return animateList.back();                    //把刚压进去的对象返回出去}CAnimate* CAnimation::addAnimate(char *animName, CCTexture *tex, UINT rows, UINT cols){CAnimate* anim = findAnimate(animName);if (anim != NULL)return anim;this->rows = rows;this->cols = cols;anim = new CAnimate(tex, rows, cols);anim->setName(animName);anim->setParent(this);anim->setMovementEventCallFunc(animate_selector(&CAnimation::animateEvent));animateList.push_back(anim);setAnimate(animName);return animateList.back();}CAnimate* CAnimation::findAnimate(char *animName){list<CAnimate*>::iterator iter;for (iter = animateList.begin(); iter != animateList.end(); iter++){if (strcmp((*iter)->name, animName) == 0){return *iter;}}return NULL;}void CAnimation::setAnimate(char *animName){animate = findAnimate(animName);animate->startIndex = animate->startIndex;//这个写的不对、明天去参考伟哥的写法//animate->curIndex = animate->startIndex;animate->isPlaying = true;animate->frameTimer = 0;}void CAnimation::setName_FrameIndex(char*name, UINT startIndex, UINT endIndex){CAnimate* anim = findAnimate(name);anim = new CAnimate(fileName, rows, cols);              //如果为空就new一个对象压进去。并且把名字设置好anim->setName(name);                               //设置动画名字anim->setParent(this);                               //CAnimation是CNode的子类,子类拥有父类的所有数据,所以压入CAnimation就等同于压入了CNode。anim->setFrameIndex(startIndex, endIndex);anim->frameTimer = 0;animateList.push_back(anim);                     //压进链表setAnimate(name);                                 //设置动画}void CAnimation::draw(){Matrix3 scaleMatrix;Matrix3 rotateMatrix;Matrix3 transMatrix;//放缩图片Scale(scaleMatrix, scale.x, scale.y);//水平翻转if (flip == true)scaleMatrix._11 *= -1;//旋转图片Rotate(rotateMatrix, angle);// 平移图片到我们的指定位置Translate(transMatrix, pos.x, pos.y);local_matrix = scaleMatrix*rotateMatrix*transMatrix;if (parent == NULL){world_color = local_color;world_matrix = local_matrix;}else{sColor col = parent->getWorldColor();world_color.r = local_color.r*col.r;world_color.g = local_color.g*col.g;world_color.b = local_color.b*col.b;world_color.a = local_color.a*col.a;world_matrix = local_matrix*parent->getWorldMatrix();}if (visible == false)return;animate->draw();}void CAnimation::setCurrentAllanchor(float ox, float oy){}void CAnimation::setMovementEventCallFunc(animation_selector fun_movementEvent){listener_movementEvent = fun_movementEvent;}void CAnimation::animateEvent(CAnimate* animate, MovementEvent type){if (listener_movementEvent){if (parent != NULL)(parent->*listener_movementEvent)(this, type, animate->name);else(this->*listener_movementEvent)(this, type, animate->name);}}RECT CAnimation::getBoundBox(){return animate->getBoundBox();}CAnimation::~CAnimation(){}}

谢谢大家

从0开发游戏引擎之游戏引擎中2D序列帧动画控制器的实现相关推荐

  1. 用AS3.0开发flash版SLG游戏-1

    说到经典的战棋游戏,我还是最痴迷于曹操传,以前也曾因为研究曹操传MOD而废寖忘食,也做过一个曹操传的MOD小热一时, 却没想到曹操传MOD已经发展到现在这种地步,无论是画面还是技术都有了大规模的突破 ...

  2. Cocos2d-x 3.0 开发(八)骨骼动画的动态换肤

    1.   概述 游戏中人物的状态会发生改变,而这种改变通常要通过局部的变化来表现出来.比如获得一件装备后人物形象的改变,或者战斗中武器.防具的损坏等.这些变化的实现就要通过动态换肤来实现.在接下来的这 ...

  3. 实习小白::(转) Cocos2d-x 3.0 开发(八)骨骼动画的动态换肤

    1.   概述 游戏中人物的状态会发生改变,而这种改变通常要通过局部的变化来表现出来.比如获得一件装备后人物形象的改变,或者战斗中武器.防具的损坏等.这些变化的实现就要通过动态换肤来实现.在接下来的这 ...

  4. 从0开发游戏引擎之引擎Win32平台的Platform类实现

    这个引擎是基于win32架构写的, Platform可以理解成是整个引擎的入口.控制着引擎的整个生命周期,内部主要完成了openGL.win32窗口句柄的初始化:键盘鼠标事件分发:主循环控制.关闭程序 ...

  5. 手把手教你架构3d游戏引擎pdf_游戏开发中的算法

    游戏技术这条路,可深可浅.你可以满足于完成GamePlay玩法层面的东西,你也可以满足于架构和框架设计层面的东西,你也可以醉心于了解某一游戏引擎带来的掌控感.但是,我们不该止步于此,止步与目前所见或所 ...

  6. 使用Html5+C#+微信 开发移动端游戏详细教程 :(三)使用html5引擎搭建游戏框架...

    教程里的案例我们是通过H5游戏引擎开发,目前H5的游戏引擎比较好用的是白鹭,不过对于新手来说白鹭的开发环境和工具使用过于复杂,这里推荐一个国内大神编写的游戏引擎:lufylegend. 直接在页面引入 ...

  7. unity游戏开发毕设_《毕业设计(论文)-基于Unity游戏引擎的游戏设计》.doc

    学 号070125分类号本科生毕业论文(设计) 题目: 基于Unity游戏引擎的游戏设计 院(系) 电子与信息工程系专业 计算机科学与技术班级2007级学 生 姓 名指导教师(职称)提 交 时 间 二 ...

  8. 开发一个Canvas小游戏 实现一个游戏“引擎”

    前言 这个游戏其实在三四年前就写了,中间还重构过好几次,之前都是用简单的面向对象和函数式编程来写,游戏中的元素关系到还是分的挺开,但是游戏的渲染,运算等逻辑分的不够清晰,整个逻辑基本都是自顶向下的流水 ...

  9. 游戏引擎与游戏引擎开发入门

    早想写一点游戏设计的文章与大家交流,一是经验的问题,二是公司正在紧张的游戏制作期,实在抽不出多少时间,一直没有动手,今天忽然头脑发热,写了一段,以后准备陆续写一些游戏创意,策划,制作,流程管理,和制作 ...

最新文章

  1. android通过uri得到文件对象,安卓[android] 通过Uri获取File文件
  2. ajax csrf php,Laravel中Ajax调用时的CSRF对策
  3. Android 5.0+高级动画开发 矢量图动画 轨迹动画 路径变换
  4. C++平衡二叉树(AVL树)
  5. JDK Bug系统浪费时间
  6. HttpClient_用Apache HttpClient实现URL重定向
  7. Fedora server wifi
  8. BZOJ4480[JSOI2013]快乐的jyy
  9. PCA9685 多舵机控制器的编程
  10. 毛星云opencv之DrawLine函数写法
  11. 报表开发工具FastReport开源代码2020首发更新!邀您免费下载
  12. 计算机是如何储存信息的,计算机是如何储存信息的
  13. PS 渐变工具(锥形【线性渐变】和圆形【径向渐变】)
  14. ❤️腾讯面试,万字攻略详解, offer到碗里来❤️
  15. 2020年全球程序员收入出炉,国内程序员的收入也不低!北京以10万美元的薪资排名第十!
  16. C语言的stdio.h文件
  17. oracle表独立数据文件,oracle表空间及数据文件
  18. 另一个jar包引不了_《嫌疑人X的献身》:一个天才的陨落
  19. 盒子模型的通俗化理解
  20. Python之父:python根本没有那么难,边玩边学

热门文章

  1. 2k显示器哪一款好用
  2. Centos 下Docker容器安装vim
  3. JAVA程序设计2018_重庆大学网院2018年Java程序设计 ( 第1次 )
  4. 局域网ip部署web网站
  5. rpm卸载软件包时报依赖错误
  6. java实验三20135104
  7. 教你如何快速查询24小时内没有揽件信息的单号
  8. 观看理想国的day2
  9. pro、pre、test、dev环境区别
  10. CS224n 词的向量表示word2vec 之skipgram (softmax)