Skip to content

Animation示例,没完全实现的那种 #2

Description

@PoPCoRN-Jim

//Animation模块,保存单个角色所有动画帧。路径+旋转+次序+png。
#include<graphics.h>
#include<conio.h>
#include
#include
#include
using namespace std;

#pragma comment(lib, "MSIMG32.LIB")
inline void putimage_alpha(int x, int y, IMAGE* img) //WinGDI方法实现png透明层显示,参阅WinGUI-AlphaBlend,色深要求32位
{
int width = img->getwidth();
int hight = img->getheight();

AlphaBlend(GetImageHDC(NULL), x, y, width, hight,
	GetImageHDC(img), 0, 0, width, hight, { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA });

}

struct Point
{
int x, y, r;
};

class Animation //Animation类,保存一份帧动画
{
public:
Animation(wstring path, int frameNum, double fps=0.5, int r=0) //Animation构造,路径-帧总数-帧率-角度,需按数字顺序命名32位png图像
{
for (int frame = 0; frame < frameNum; frame++)
{
IMAGE* newFrame = new IMAGE(); //为单帧创建new IMAGE,使用后需要清理内存
wstring dirPath = path + to_wstring(r) + to_wstring(frame + 1) + L".png";
loadimage(newFrame,dirPath.c_str());
framePtr.push_back(newFrame);
}
this->frameNum = frameNum;
this->fps = fps;
}
~Animation()
{
for (int i = 0; i < framePtr.size(); i++)
{
delete framePtr[i];
}
}

void AnimFramePlay(int x, int y,int fNum)			//单帧播放,坐标-帧
{
	putimage_alpha(x, y, framePtr[fNum]);
	//putimage(800, y, framePtr[fNum]);
}

private:
vector<IMAGE*> framePtr; //vector存储单帧的IMAGE指针
int frameNum;
double fps;
};

class Animator //Animator类,存储单个角色所有动画
{
public:
Animator(wstring picPath) //为一个角色生成所有动画组合
{
for (int i=0; i<8; i++)
{
Animation* idle = new Animation(picPath,1,0.5,i);
Animation* atta = new Animation(picPath,3,0.5,i);
Idle.push_back(idle);
Attack.push_back(atta);
}
}
~Animator()
{
for (int i=0; i<8; i++)
{
delete Idle[i];
delete Attack[i];
}
}

void AnimPlay(int status, int x, int y, int r)
{
	switch (status)
	{
	case 0:
		Idle[r]->AnimFramePlay(x, y, 0);
		break;
	case 1:
		if (frameAttack<3)
			Attack[r]->AnimFramePlay(x, y, frameAttack++);
		if (frameAttack>=3)
		{
			status = 0;
			frameAttack = 0;
		}
		break;
	}
}

private:
vector<Animation*> Idle; //Anim数组保存Animation指针
vector<Animation*> Attack;
int frameAttack = 0;
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions