HGE-4 形变网格(DistortionMesh)

  1 /*
  2 ** Haaf's Game Engine 1.8
  3 ** Copyright (C) 2003-2007, Relish Games
  4 ** hge.relishgames.com
  5 **
  6 ** hge_tut05 - Using distortion mesh
  7 */
  8 
  9 
 10 // 复制"particles.png","menu.wav","font1.fnt","font1.png","trail.psi"
 11 
 12 
 13 
 14 #include <hge.h>
 15 #include <hgefont.h>
 16 #include <hgedistort.h>
 17 #include <math.h>
 18 #pragma comment(lib,"hge.lib")
 19 #pragma comment(lib,"hgehelp.lib")
 20 
 21 
 22 HGE *hge=0;
 23 
 24 HTEXTURE            tex;
 25 
 26 // HGE的 形变网格 的指针
 27 hgeDistortionMesh*    dis;
 28 hgeFont*            fnt;
 29 
 30 
 31 const int nRows=16;
 32 const int nCols=16;
 33 const float cellw=512.0f/(nCols-1);//网格中每格宽度
 34 const float cellh=512.0f/(nRows-1);//网格中每格高度
 35 
 36 const float meshx=144;
 37 const float meshy=44;
 38 
 39 
 40 bool FrameFunc()
 41 {
 42     float dt=hge->Timer_GetDelta();
 43     static float t=0.0f;
 44     static int trans=0;
 45 
 46     int i, j, col;
 47     float r, a, dx, dy;
 48 
 49     t+=dt;
 50 
 51     // 和之前不同的按键检测,这次检测空格键
 52     switch(hge->Input_GetKey())
 53     {
 54         case HGEK_ESCAPE:
 55             return true;
 56 
 57         case HGEK_SPACE:
 58             if(++trans > 2) trans=0;
 59             dis->Clear(0xFF000000);
 60             break;
 61     }
 62     
 63     // 为了产生特效而计算位移和颜色
 64     switch(trans)
 65     {
 66         case 0:    for(i=1;i<nRows-1;i++)
 67                     for(j=1;j<nCols-1;j++)
 68                     {
 69                         dis->SetDisplacement(j,i,cosf(t*10+(i+j)/2)*5,sinf(t*10+(i+j)/2)*5,HGEDISP_NODE);
 70                     }
 71                 break;
 72 
 73         case 1:    for(i=0;i<nRows;i++)
 74                     for(j=1;j<nCols-1;j++)
 75                     {
 76                         dis->SetDisplacement(j,i,cosf(t*5+j/2)*15,0,HGEDISP_NODE);
 77                         col=int((cosf(t*5+(i+j)/2)+1)*35);
 78                         dis->SetColor(j,i,0xFF<<24 | col<<16 | col<<8 | col);
 79                     }
 80                 break;
 81 
 82         case 2:    for(i=0;i<nRows;i++)
 83                     for(j=0;j<nCols;j++)
 84                     {
 85                         r=sqrtf(powf(j-(float)nCols/2,2)+powf(i-(float)nRows/2,2));
 86                         a=r*cosf(t*2)*0.1f;
 87                         dx=sinf(a)*(i*cellh-256)+cosf(a)*(j*cellw-256);
 88                         dy=cosf(a)*(i*cellh-256)-sinf(a)*(j*cellw-256);
 89                         // 指定要设置的坐标位置的平移量(列,行,x方向平移量,y方向平移量,平移量引用点)
 90                         // 列坐标和行坐标都是从0开始的.
 91                         // 平移量引用点可以取:HGEDISP_NODE - 引用点是默认点的位置 
 92                         // HGEDISP_TOPLEFT - 引用点是网格的左上角 HGEDISP_CENTER - 引用点是网格的中心 
 93                         dis->SetDisplacement(j,i,dx,dy,HGEDISP_CENTER);
 94                         col=int((cos(r+t*4)+1)*40);
 95                         // 指定要设置的坐标位置的颜色(列,行,颜色)
 96                         dis->SetColor(j,i,0xFF<<24 | col<<16 | (col/2)<<8);
 97                     }
 98                     break;
 99     }
100 
101     return false;
102 }
103 
104 
105 bool RenderFunc()
106 {
107     // 绘制图像
108     hge->Gfx_BeginScene();
109     hge->Gfx_Clear(0);
110     dis->Render(meshx, meshy);
111     fnt->printf(5, 5, HGETEXT_LEFT, "dt:%.3f\nFPS:%d\n\nUse your\nSPACE!", hge->Timer_GetDelta(), hge->Timer_GetFPS());
112     hge->Gfx_EndScene();
113 
114     return false;
115 }
116 
117 
118 int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
119 {
120     hge = hgeCreate(HGE_VERSION);
121 
122     hge->System_SetState(HGE_LOGFILE, "hge_tut05.log");
123     hge->System_SetState(HGE_FRAMEFUNC, FrameFunc);
124     hge->System_SetState(HGE_RENDERFUNC, RenderFunc);
125     hge->System_SetState(HGE_TITLE, "HGE Tutorial 05 - Using distortion mesh");
126     hge->System_SetState(HGE_WINDOWED, true);
127     hge->System_SetState(HGE_SCREENWIDTH, 800);
128     hge->System_SetState(HGE_SCREENHEIGHT, 600);
129     hge->System_SetState(HGE_SCREENBPP, 32);
130     hge->System_SetState(HGE_USESOUND, false);
131 
132     if(hge->System_Initiate()) {
133 
134         // 加载纹理
135         tex=hge->Texture_Load("texture.jpg");
136         if(!tex)
137         {
138             MessageBox(NULL, "Can't load TEXTURE.JPG", "Error", MB_OK | MB_ICONERROR | MB_APPLMODAL);
139             hge->System_Shutdown();
140             hge->Release();
141             return 0;
142         }
143 
144         // 创建一个形变网格
145         dis=new hgeDistortionMesh(nCols, nRows);//网格列,行的数量
146         dis->SetTexture(tex);//设置用于形变的纹理
147         dis->SetTextureRect(0,0,512,512);//设置纹理中用于形变的区域(纹理左上角x,纹理左上角y,宽度,高度)
148         dis->SetBlendMode(BLEND_COLORADD | BLEND_ALPHABLEND | BLEND_ZWRITE);
149         //用指定的颜色和Z序清空网格的平移和着色(颜色=0xFFFFFFFF,Z序=0.5f),=后面的数值为默认值,以后不再说明
150         dis->Clear(0xFF000000);//此处使用的是黑色,默认是白色
151 
152         fnt=new hgeFont("font1.fnt");
153 
154         hge->System_Start();
155 
156         delete fnt;
157         delete dis;
158         hge->Texture_Free(tex);
159     }
160 
161     hge->System_Shutdown();
162     hge->Release();
163     return 0;
164 }

 

转载于:https://www.cnblogs.com/nightfire/articles/2618243.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值