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

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

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


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

 

395331a801714fe892f7d7be1fad33db.jpg


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


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


程序1.1


Animate.h 完整代码


#pragma once
namespace 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 once
namespace 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()
  {
  }
}


谢谢大家

相关文章
|
8月前
|
编解码 自然语言处理 文字识别
LLaDA2.0-Uni 开源: 打破 AR 桎梏,dLLM定义原生多模态统一新范式
LLaDA2.0-Uni是全球首个开源的多模态MoE离散扩散大模型(dLLM),以16B参数统一实现图像理解、生成与编辑。突破性采用全离散扩散建模,摆脱自回归依赖,支持并行解码与任意分辨率;语义视觉Token+定制Diffusion Decoder,8步即出高质量图。已在21项基准登顶,全面开源。
1042 1
LLaDA2.0-Uni 开源: 打破 AR 桎梏,dLLM定义原生多模态统一新范式
|
机器学习/深度学习 并行计算 算法
PINN驱动的三维声波波动方程求解(Matlab代码实现)
PINN驱动的三维声波波动方程求解(Matlab代码实现)
777 7
|
存储 安全 BI
硬盘有坏道怎么办?硬盘坏道如何屏蔽?
本文简单介绍一下坏道的类型,然后介绍检测工具、坏道屏蔽方法,感兴趣的用户可以尝试。
|
人工智能 监控 JavaScript
Playwright初学指南 (3):深入解析交互操作
本文深度解析Playwright如何通过智能等待、自动重试等机制解决Web自动化中60%的交互失败问题。从基础点击/输入到高级拖拽/iframe操作,提供企业级解决方案和性能优化技巧,帮助开发者实现98%的操作成功率,打造稳定高效的自动化测试体系。
|
12月前
|
算法 搜索推荐 定位技术
《独立游戏社区支持的生态构建与共创赋能指南》
本文聚焦独立游戏项目如何依托自有网站构建高粘性社区支持生态,指出网站并非传统展示窗口,而是连接开发者与玩家的价值共生枢纽。文章结合实践路径,提出需打破单向信息输出模式,通过场景化互动设计、“研发温度+玩家价值”双轨内容体系、分层参与机制,让玩家从体验者转变为共建者。同时强调透明化反馈处理与常态化互动的核心作用,主张搭建线上线下联动的立体生态,整合社区资源,最终实现开发者与玩家的价值共生,为缺乏大厂资源的独立游戏项目,提供破圈突围的切实可行路径。
585 10
|
传感器 人工智能 机器人
具身智能9大开源工具全景解析:人形机器人开发必备指南
本文旨在对具身智能、人形机器人、协作机器人、AI机器人、端到端AI系统、AI Agent、AI Agentic、空间智能或世界模型等前沿领域中具有重要影响力的开源软件产品或工具进行深入分析,重点聚焦于支持这些先进AI能力实现的工具、平台和框架。
4400 8
|
人工智能 自然语言处理 小程序
让小程序拥有“视觉之眼“:DeepSeek图像识别实战指南
本文介绍如何通过DeepSeek计算机视觉技术,赋予小程序“看懂世界”的能力。从构建视觉感知系统、训练专属视觉词典到创造会思考的界面,详细讲解了实现智能相册、植物识别器和老旧照片修复等功能的步骤。最后探讨性能优化与安全合规要点,展望未来视觉智能应用的无限可能。
第三章:什么是 BACnet/IP 网络
BACnet/IP 网络是一个或多个 IP 子网(IP 域)的集合,这些子网分配有单个 BACnet 网络号。BACnet 互联网络由两个或多个 BACnet 网络组成。这些网络可能是 BACnet/IP 网络,也可能使用其他指定的技术。此标准还支持以类似于 IP 子网的方式包含 IP 多播组,如下文中所述。
1238 0
第三章:什么是 BACnet/IP 网络
Visio绘制论文文献技术路线图流程图
Visio绘制论文文献技术路线图流程图
1039 1
|
存储 数据管理 数据处理
处理STM32 DMA方式下的HAL_UART_ERROR_ORE错误
通过正确配置UART和DMA、实现有效的错误处理回调函数以及优化数据处理和缓冲区管理,可以有效处理STM32中DMA方式下的 `HAL_UART_ERROR_ORE`错误。这些方法确保了数据的高效传输和处理,避免了因数据溢出导致的通信中断和数据丢失。希望这些解决方案能够帮助您在实际应用中更好地应对和解决此类问题。
3257 0