|
|

楼主 |
发表于 2007-4-27 09:49:00
|
显示全部楼层
Re:请教:HGE 引擎中的渲染句柄有什么用?谢谢!
#include "include\hge.h"
#include "include\hgesprite.h"
#include "include\hgefont.h"
#include "include\hgeparticle.h"
HGE *hge=0;
hgeSprite* spr;
hgeSprite* spt;
hgeSprite* tar;
hgeFont* fnt;
hgeParticleSystem* par;
//理纹和声音句柄
//需要明白在 HGE 中理纹说的就是一幅大图片,里面可能含有小图片,
//如疯狂坦克,下面还包含有很多小图片
HTEXTURE tex;
HEFFECT snd;
// HGE render target handle
HTARGET target; //渲染目标的句柄
float x=100.0f, y=100.0f;
float dx=0.0f, dy=0.0f;
const float speed=90;
const float friction=0.98f;
void boom() { ////撞击时发出声音
int pan=int((x-256)/2.56f);
float pitch=(dx*dx+dy*dy)*0.0005f+0.2f;
hge->Effect_PlayEx(snd,100,pan,pitch);
}
// This function will be called by HGE
// when the application receives focus.
// We use it here to update the render
// target's texture handle that may change
// when the focus is lost.
bool FocusGainFunc() //这里不知道是什么意思?
{
if(tar && target) tar->SetTexture(hge->Target_GetTexture(target));
return false;
}
bool FrameFunc()
{
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>496) {x=496-(x-496);dx=-dx;boom();}
if(x<16) {x=16+16-x;dx=-dx;boom();}
if(y>496) {y=496-(y-496);dy=-dy;boom();}
if(y<16) {y=16+16-y;dy=-dy;boom();}
// Update particle system
par->info.nEmission=(int)(dx*dx+dy*dy);
par->MoveTo(x,y);
par->Update(dt);
return false;
}
bool RenderFunc()
{
int i;
// Render graphics to the texture
hge->Gfx_BeginScene(target); //开始渲染 ,target 为渲染目标句柄
hge->Gfx_Clear(0); // 需要清除的渲染目标的ARGB颜色值
par->Render(); //渲染
spr->Render(x, y); //渲染
hge->Gfx_EndScene(); //渲染结束
// Now put several instances of the rendered texture to the screen
hge->Gfx_BeginScene(); //开始渲染
hge->Gfx_Clear(0); // 需要清除的渲染目标的ARGB颜色值
for(i=0;i<6;i++)
{
tar->SetColor(0xFFFFFF | (((5-i)*40+55)<<24));
//SetColor 设置颜色为特别的扭曲的点
tar->RenderEx(i*100.0f, i*50.0f, i*M_PI/8, 1.0f-i*0.1f);
//RenderEx 是高级渲染 第一个参数是 x 坐标,第一个参数是 y 坐标,第三个参数是 缩反复比例 ,第四个参数是旋转角度
}
fnt->printf(5, 5, HGETEXT_LEFT, "dt:%.3f\nFPS:%d (constant)", hge->Timer_GetDelta(), hge->Timer_GetFPS());
hge->Gfx_EndScene();
return false;
}
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
hge = hgeCreate(HGE_VERSION);
hge->System_SetState(HGE_LOGFILE, "hge_tut04.log");
hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
hge->System_SetState(HGE_FOCUSGAINFUNC, FocusGainFunc);
hge->System_SetState(HGE_TITLE, "HGE Tutorial 04 - Using render targets");
hge->System_SetState(HGE_FPS, 100);
hge->System_SetState(HGE_WINDOWED, true);
hge->System_SetState(HGE_SCREENWIDTH, 800);
hge->System_SetState(HGE_SCREENHEIGHT, 600);
hge->System_SetState(HGE_SCREENBPP, 32);
tar=0; //精灵缓冲初始化为 0
target=0; //渲染句柄初始化为 0
//Engine 初始化
if(hge->System_Initiate()) {
snd=hge->Effect_Load("menu.wav"); //载入声音
tex=hge->Texture_Load("particles.png");//载入图片
if(!snd || !tex) //如果没有意外
{
// If one of the data files is not found, display
// an error message and shutdown.
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);
hge->System_Shutdown(); //关闭程序
hge->Release(); //释放资源
return 0; //结束循环
}
spr=new hgeSprite(tex, 96, 64, 32, 32); //创建精灵
spr->SetColor(0xFFFFA000); //给精灵颜色 颜色格式是 0xAARRGGBB
spr->SetHotSpot(16,16); //设置的锚坐标,也就是设置图片的中心
fnt=new hgeFont("font1.fnt"); //载入字体
spt=new hgeSprite(tex, 32, 32, 32, 32); //得到粒子的图片
spt->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE);
spt->SetHotSpot(16,16);
par=new hgeParticleSystem("trail.psi",spt); //trail.psi 这个文件设置图片 spt 运行的路径和效果
par->Fire(); //启动粒子系统
// Create a render target and a sprite for it
target=hge->Target_Create(512,512,false); //创建渲染句柄
tar=new hgeSprite(hge->Target_GetTexture(target),0,0,512,512); //返回渲染目标的纹理句柄。
tar->SetBlendMode(BLEND_COLORMUL | BLEND_ALPHAADD | BLEND_NOZWRITE);
// Let's rock now!
//启动 Engine 开始主循环
hge->System_Start();
// Delete created objects and free loaded resources
delete par;
delete fnt;
delete spt;
delete spr;
delete tar;
hge->Target_Free(target);
hge->Texture_Free(tex);
hge->Effect_Free(snd);
}
// Clean up and shutdown
hge->System_Shutdown();
hge->Release();
return 0;
}
全部程序 |
|