1 /* 2 ** Haaf's Game Engine 1.8 3 ** Copyright (C) 2003-2007, Relish Games 4 ** hge.relishgames.com 5 ** 6 ** hge_tut03 - Using helper classes 7 */ 8 9 10 // 从"precompiled"目录下复制"particles.png","menu.wav","font1.fnt" 11 // "font1.png"以及"trail.psi"到程序目录下. 12 // 当然hge.dll和bass.dll也是要复制的. 13 // 说明:在以后的教程中,不在特别指出hge.dll和bass.dll这两个dll 14 // 因为每一个hge程序基本都需要这两个dll,所以请自行复制. 15 // 以后只要没有特别指明文件在哪个目录下,就默认在"precompiled"目录下. 16 //另外说明一点,本教程自带的程序也都放在"precompiled"目录下,所以一般是不需要特意 17 //去复制的,如果你把程序放到了别的地方,那么请复制这些文件. 18 19 #include <hge.h> 20 #include <hgesprite.h> 21 #include <hgefont.h> 22 #include <hgeparticle.h> 23 #pragma comment(lib,"hge.lib") 24 #pragma comment(lib,"hgehelp.lib") 25 26 // HGE接口的指针 27 HGE *hge=0; 28 29 30 // 指向HGE对象的指针 31 hgeSprite* spr;//精灵指针 32 hgeSprite* spt; 33 hgeFont* fnt;//字体 34 hgeParticleSystem* par;//粒子系统 35 36 // HGE资源句柄 37 HTEXTURE tex;//纹理句柄 38 HEFFECT snd;//音效句柄 39 40 // 一些变量和常量 41 float x=100.0f, y=100.0f; 42 float dx=0.0f, dy=0.0f; 43 44 const float speed=90; 45 const float friction=0.98f; 46 47 // 播放音效 48 void boom() { 49 int pan=int((x-400)/4); 50 float pitch=(dx*dx+dy*dy)*0.0005f+0.2f; 51 hge->Effect_PlayEx(snd,100,pan,pitch); 52 } 53 54 bool FrameFunc() 55 { 56 float dt=hge->Timer_GetDelta(); 57 58 // 检查按键是否被按下 59 if (hge->Input_GetKeyState(HGEK_ESCAPE)) return true; 60 if (hge->Input_GetKeyState(HGEK_LEFT)) dx-=speed*dt; 61 if (hge->Input_GetKeyState(HGEK_RIGHT)) dx+=speed*dt; 62 if (hge->Input_GetKeyState(HGEK_UP)) dy-=speed*dt; 63 if (hge->Input_GetKeyState(HGEK_DOWN)) dy+=speed*dt; 64 65 // 移动和碰撞检测 66 dx*=friction; dy*=friction; x+=dx; y+=dy; 67 if(x>784) {x=784-(x-784);dx=-dx;boom();} 68 if(x<16) {x=16+16-x;dx=-dx;boom();} 69 if(y>584) {y=584-(y-584);dy=-dy;boom();} 70 if(y<16) {y=16+16-y;dy=-dy;boom();} 71 72 // 更新粒子系统 73 par->info.nEmission=(int)(dx*dx+dy*dy)*2; 74 par->MoveTo(x,y); 75 par->Update(dt); 76 77 return false; 78 } 79 80 81 bool RenderFunc() 82 { 83 // 渲染 84 hge->Gfx_BeginScene(); 85 hge->Gfx_Clear(0); 86 par->Render(); 87 spr->Render(x, y); 88 fnt->printf(5, 5, HGETEXT_LEFT, "dt:%.3f\nFPS:%d (constant)", hge->Timer_GetDelta(), hge->Timer_GetFPS()); 89 hge->Gfx_EndScene(); 90 91 return false; 92 } 93 94 95 int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) 96 { 97 hge = hgeCreate(HGE_VERSION); 98 99 hge->System_SetState(HGE_LOGFILE, "hge_tut03.log"); 100 hge->System_SetState(HGE_FRAMEFUNC, FrameFunc); 101 hge->System_SetState(HGE_RENDERFUNC, RenderFunc); 102 hge->System_SetState(HGE_TITLE, "HGE Tutorial 03 - Using helper classes"); 103 hge->System_SetState(HGE_FPS, 100); 104 hge->System_SetState(HGE_WINDOWED, true); 105 hge->System_SetState(HGE_SCREENWIDTH, 800); 106 hge->System_SetState(HGE_SCREENHEIGHT, 600); 107 hge->System_SetState(HGE_SCREENBPP, 32); 108 109 if(hge->System_Initiate()) { 110 111 // 加载声音和纹理 112 snd=hge->Effect_Load("menu.wav"); 113 tex=hge->Texture_Load("particles.png"); 114 if(!snd || !tex) 115 { 116 // 只要一个加载失败就提示并退出 117 MessageBox(NULL, "Can't load one of the following files:\nMENU.WAV, PARTICLES.PNG, FONT1.FNT, FONT1.PNG, TRAIL.PSI", "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL); 118 hge->System_Shutdown(); 119 hge->Release(); 120 return 0; 121 } 122 123 // 创建并设置一个精灵实例 124 spr=new hgeSprite(tex, 96, 64, 32, 32); 125 spr->SetColor(0xFFFFA000); 126 spr->SetHotSpot(16,16);// 设置精灵的中心点位置 127 128 // 加载字体 129 fnt=new hgeFont("font1.fnt"); 130 131 // 创建并设置一个粒子系统 132 spt=new hgeSprite(tex, 32, 32, 32, 32); 133 spt->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE); 134 spt->SetHotSpot(16,16); 135 par=new hgeParticleSystem("trail.psi",spt); 136 par->Fire(); 137 138 // 运行 139 hge->System_Start(); 140 141 // 删除和释放资源 142 delete par; 143 delete fnt; 144 delete spt; 145 delete spr; 146 hge->Texture_Free(tex); 147 hge->Effect_Free(snd); 148 } 149 150 // 清理及退出 151 hge->System_Shutdown(); 152 hge->Release(); 153 return 0; 154 }