PSP程序开发例子解析(十)HGEDistortionMesh

HGEDistortionMesh例子应用了HGE包中的内容 用来对图片进行扭曲变形处理
#include <stdio.h>

 
#include <JGE.h>
#include <JRenderer.h>
#include <JLBFont.h>

#include <hge/hgedistort.h>
#include <hge/hgefont.h>

#include "GameApp.h"


//-------------------------------------------------------------------------------------
// Constructor. Variables can be initialized here.
//
//-------------------------------------------------------------------------------------
GameApp::GameApp()
{
mTex = NULL;//人物图片Tex
mDistortionMesh = NULL;//扭曲工具类
mFont = NULL;//字体

   mRows=8;//扭曲参数
   mCols=8;//应该是粒度或强度
   mCellw=(float)(TEXTURE_WIDTH/(mCols-1));//均匀区分图片为N个Cell cell宽度
   mCellh=(float)(TEXTURE_HEIGHT/(mRows-1));//Cell高度
   mMeshx=(SCREEN_WIDTH_F-TEXTURE_WIDTH)/2;//扭曲后的图片位置
   mMeshy=(SCREEN_HEIGHT_F-TEXTURE_HEIGHT)/2;//在屏幕中央
}


//-------------------------------------------------------------------------------------
// Destructor.
//
//-------------------------------------------------------------------------------------
GameApp::~GameApp()
{

}


//-------------------------------------------------------------------------------------
// This is the init callback function. You should load and create your in-game 
// resources here.
// 
//-------------------------------------------------------------------------------------
void GameApp::Create()
{

JRenderer* renderer = JRenderer::GetInstance();  
mTex=renderer->LoadTexture("texture.jpg");//加载人物图片

// Create a distortion mesh
mDistortionMesh=new hgeDistortionMesh(mCols, mRows);//设置扭曲的分块8*8的分块
mDistortionMesh->SetTexture(mTex);//用图片来扭曲
mDistortionMesh->SetTextureRect(0,0,TEXTURE_WIDTH,TEXTURE_HEIGHT);//设置要扭曲的大小
mDistortionMesh->Clear(ARGB(0xFF,0xFF,0xFF,0xFF));//清楚扭曲区

// Load a font
mFont=new hgeFont("font1.fnt");//加载显示文字的字体
}


//-------------------------------------------------------------------------------------
// This is the clean up callback function. You should delete all your in-game 
// resources, for example texture and quads, here.
// 
//-------------------------------------------------------------------------------------
void GameApp::Destroy()
{

SAFE_DELETE(mTex);

SAFE_DELETE(mDistortionMesh);

SAFE_DELETE(mFont);

}


//-------------------------------------------------------------------------------------
// This is the update callback function and is called at each update frame
// before rendering. You should update the game logic here.
//
//-------------------------------------------------------------------------------------
void GameApp::Update()
{

JGE* engine = JGE::GetInstance();

// do a screen shot when the TRIANGLE button is pressed
if (engine->GetButtonClick(PSP_CTRL_TRIANGLE))  
{
   char s[80];

   // save screen shot to root of Memory Stick 
   sprintf(s, "ms0:/screenshot.png");    
   JRenderer::GetInstance()->ScreenShot(s);
}

// exit when the CROSS button is pressed
if (engine->GetButtonClick(PSP_CTRL_CROSS)) 
{
   engine->End();
   return;
}

float dt = engine->GetDelta();   // Get time elapsed since last update.

static float t=0.0f;//函数中的静态变量 第一次进入的时候初始化 再进入的时候就沿用原来的值了
static int trans=2;//这点和Java有点不一样

int i, j, col;
float r, a, dx, dy;

t+=dt;//当前时间 越加越多阿= =????
//按圆按钮 每次改变一个扭曲效果并清屏
if (engine->GetButtonClick(PSP_CTRL_CIRCLE)) 
{
   if(++trans > 2) 
    trans=0;
   mDistortionMesh->Clear(ARGB(0xFF,0xFF,0xFF,0xFF));

}

// Calculate new displacements and coloring for one of the three effects
//trans3种不同的扭曲效果
switch(trans)
{
   case 0: 
    //将整个图片划分成各个Cell 并单独扭曲
    for(i=1;i<mRows-1;i++)
    {
     for(j=1;j<mCols-1;j++)
     {
      //参数分别为(列,行,Cell中的x坐标,Cell中的y坐标,扭曲效果,根据Cell中心扭曲即HGEDISP_NODE)
      mDistortionMesh->SetDisplacement(j,i,cosf(t*10+(i+j)/2)*5,sinf(t*10+(i+j)/2)*5,HGEDISP_NODE);
      //cosf(t*10+(i+j)/2)*5算式根据时间t与Cos余弦函数的乘积 动态改变扭曲的X方向粒度 在一个0~5中变化
      //sinf(t*10+(i+j)/2)*5根据sin函数 改变扭曲y方向的粒度 在0~5之间变化
      //每个Cell是32*32的大小 整个扭曲的图片时256*256的 分为8*8个Cell
     }
    }
    //mDistortionMesh->SetDisplacement(2,2,cosf(t*10+(2+2)/2)*8,sinf(t*10+(2+2)/2)*8,HGEDISP_NODE);
    //mDistortionMesh->SetDisplacement(1,1,cosf(t*10+(1+1)/2)*3,sinf(t*10+(1+1)/2)*3,HGEDISP_NODE);
    break;

   case 1: 
    for(i=0;i<mRows;i++)
    {
     for(j=1;j<mCols-1;j++)
     {
      //变形只在x方向上扭曲 并且只根据j值变化 y方向上为0 所以x方向上是0~15的扭曲粒度 变形很严重
      //依然是从左上角的cell到右下角的cell 效果类似波浪 如果再加上i的值 变形会在上下方向上不一致
      mDistortionMesh->SetDisplacement(j,i,cosf(t*5+(j+i)/2)*15,0,HGEDISP_NODE);
      col=int((cosf(t*5+(i+j)/2)+1)*35);

      // colour doesn't work the same as the original HGE version so the following line is commented out.
      // dis->SetColor(j,i,ARGB(0xFF,col,col,col));
     }
    }
    break;
   //这个变形函数没看明白
   case 2: 
    for(i=0;i<mRows;i++)
    {
     for(j=0;j<mCols;j++)
     {
      r=sqrtf(powf(j-(float)mCols/2,2)+powf(i-(float)mRows/2,2));
      a=r*cosf(t*2)*0.1f;
      dx=sinf(a)*(i*mCellh-TEXTURE_HEIGHT/2)+cosf(a)*(j*mCellw-TEXTURE_WIDTH/2);
      dy=cosf(a)*(i*mCellh-TEXTURE_HEIGHT/2)-sinf(a)*(j*mCellw-TEXTURE_WIDTH/2);
      mDistortionMesh->SetDisplacement(j,i,dx,dy,HGEDISP_CENTER);
      col=int((cos(r+t*4)+1)*40);

      // colour doesn't work the same as the original HGE version so the following line is commented out.
      // dis->SetColor(j,i,ARGB(0xFF, col,(col/2), 0));
     }
    }
    break;
}


}


//-------------------------------------------------------------------------------------
// All rendering operations should be done in Render() only.
// 
//-------------------------------------------------------------------------------------
void GameApp::Render()
{

// get JRenderer instance
JRenderer* renderer = JRenderer::GetInstance();  

// clear screen to black
renderer->ClearScreen(ARGB(0,0,0,0));

// render the mesh
   mDistortionMesh->Render(mMeshx, mMeshy);//绘制扭曲的图片 

   mFont->printf(5, 5, HGETEXT_LEFT, "Press\nCIRCLE!");//显示文字

}


//-------------------------------------------------------------------------------------
// This function is called when the system wants to pause the game. You can set a flag
// here to stop the update loop and audio playback.
//
//-------------------------------------------------------------------------------------
void GameApp::Pause()
{

}


//-------------------------------------------------------------------------------------
// This function is called when the game returns from the pause state.
//
//-------------------------------------------------------------------------------------
void GameApp::Resume()
{

}

 

你可能感兴趣的:(F#,J#,UP)