通过hgeAnimation动画精灵类可以实现精灵动画,此处重点说一下构造函数的参数
hgeAnimation( HTEXTURE tex, int nframes, float FPS, float x, float y, float w, float h);
其中:
tex:存放动画的texture,可以通过hge->Texture_Load()加载;注意此处加载的是一个张图片,里面存放了每一帧的动画,如下图:
图中存放了包含20帧动画的图片。
nframes:指定tex中包含的动画帧数,上图即为20;
FPS:动画播放速度,以帧\秒计数;
x:第一帧动画的x坐标,一般为0;
y:第一帧动画的y坐标,一般为0,;
w:每一帧动画的宽度;
h:每一帧动画的高度;
以上图为例,可以如下设置参数:
hgeAnimation* g_animat = new hgeAnimation(g_tex, 20, 20, 0, 0, 139,91);
#include "..\..\include\hge.h" #include "..\..\include\hgesprite.h" #include "..\..\include\hgeanim.h" HGE *hge=0; HTEXTURE g_tex; hgeAnimation* g_animat; // Handle for a sound effect HEFFECT snd; // Some "gameplay" variables and constants float x=100.0f, y=100.0f; float dx=0.0f, dy=0.0f; const float speed=90; const float friction=0.98f; // This function plays collision sound with // parameters based on sprite position and speed void boom() { int pan=int((x-400)/4); float pitch=(dx*dx+dy*dy)*0.0005f+0.2f; hge->Effect_PlayEx(snd,100,pan,pitch); } bool FrameFunc() { // Get the time elapsed since last call of FrameFunc(). // This will help us to synchronize on different // machines and video modes. float dt=hge->Timer_GetDelta(); // Process keys if (hge->Input_GetKeyState(HGEK_ESCAPE)) return true; if (hge->Input_GetKeyState(HGEK_LEFT)) dx-=speed*dt; if (hge->Input_GetKeyState(HGEK_RIGHT)) dx+=speed*dt; if (hge->Input_GetKeyState(HGEK_UP)) dy-=speed*dt; if (hge->Input_GetKeyState(HGEK_DOWN)) dy+=speed*dt; // Do some movement calculations and collision detection dx*=friction; dy*=friction; x+=dx; y+=dy; if(x>784) {x=784-(x-784);dx=-dx;boom();} if(x<16) {x=16+16-x;dx=-dx;boom();} if(y>584) {y=584-(y-584);dy=-dy;boom();} if(y<16) {y=16+16-y;dy=-dy;boom();} g_animat->Update(dt); // Continue execution return false; } // This function will be called by HGE when // the application window should be redrawn. // Put your rendering code here. bool RenderFunc() { // Begin rendering quads. // This function must be called // before any actual rendering. hge->Gfx_BeginScene(); hge->Gfx_Clear(0); // Clear screen with black color g_animat->Render(x, y); // End rendering and update the screen hge->Gfx_EndScene(); // RenderFunc should always return false return false; } int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) { // Get HGE interface hge = hgeCreate(HGE_VERSION); // Set up log file, frame function, render function and window title hge->System_SetState(HGE_LOGFILE, "hge_tut02.log"); hge->System_SetState(HGE_FRAMEFUNC, FrameFunc); hge->System_SetState(HGE_RENDERFUNC, RenderFunc); hge->System_SetState(HGE_TITLE, "HGE Tutorial 02 - Using input, sound and rendering"); // Set up video mode hge->System_SetState(HGE_WINDOWED, true); hge->System_SetState(HGE_SCREENWIDTH, 800); hge->System_SetState(HGE_SCREENHEIGHT, 600); hge->System_SetState(HGE_SCREENBPP, 32); if(hge->System_Initiate()) { // Load sound and texture snd=hge->Effect_Load("menu.wav"); g_tex=hge->Texture_Load("fish01_01.png"); if(!snd || !g_tex) { // If one of the data files is not found, display // an error message and shutdown. MessageBox(NULL, "Can't load MENU.WAV or PARTICLES.PNG", "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL); hge->System_Shutdown(); hge->Release(); return 0; } g_animat = new hgeAnimation(g_tex, 20, 20, 0, 0, 139,91); //g_animat->SetColor(0xFFFFA000); //g_animat->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE); g_animat->SetSpeed(10); g_animat->Play(); // Let's rock now! hge->System_Start(); // Free loaded texture and sound hge->Effect_Free(snd); } else MessageBox(NULL, hge->System_GetErrorMessage(), "Error", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL); // Clean up and shutdown delete g_animat; hge->System_Shutdown(); hge->Release(); return 0; }