PSP程序开发例子解析(四)Splines

阅读更多
第四个例子讲述了曲线的作用 制作一些RoadPoint时 会用到 让一个图片对象按照曲线运行
代码如下GameApp.cpp:
#include 

#include 
#include 
#include 
#include 
#include 

#include "GameApp.h"


GameApp::GameApp()
{
mFont = NULL;
mSpline = NULL;
mTexBg = NULL;
mTexPlane = NULL;
mBg = NULL;
mPlane = NULL;

}


GameApp::~GameApp()
{
}


void GameApp::Create()
{

mFont = new JLBFont("f3", 16, true);

mSpline = new JSpline();       // Load spline and scale it down a little bit because the 
mSpline->Load("sample.spl", 0.60f, 0.56f);   // spline was originally created for 800x600 size screen.
mSpline->GeneratePixels();       // Generate all the points on the spline.
//加载曲线 根据曲线文件加载 文件中定义了很多路点 自动通过GeneratePixels算出曲线每个点的值 需要缩放一下

JRenderer* renderer = JRenderer::GetInstance();

mTexBg = renderer->LoadTexture("bg.jpg");
mBg = new JQuad(mTexBg, 0, 0, SCREEN_WIDTH_F, SCREEN_HEIGHT_F);

mTexPlane = renderer->LoadTexture("sprites.png");   // Load the plane and set up the animation frames.
mPlane = new JGameObject(mTexPlane, 1, 1, 63, 63);//根据第一个图片位置设置第一个Frame
mPlane->AddFrame(1,65,63,63,1000);//中间的Frame
mPlane->AddFrame(1,129,63,63,3000);//最后的Frame
mPlane->SetHotSpot(32, 32);//设置中心

mPlane->SetRenderFlags(RENDER_FLAG_ANGLE);     // Make the plane facing the direction it is moving to.
//使飞机可以旋转

mIndex = 0;
mCounter = 0.0;

Point pt;
mSpline->GetPixel(pt, mIndex);//获取第0-index点的坐标Point
mPlane->SetPosition(pt.x, pt.y);//设置飞机的坐标

mSpline->GetPixel(pt, mIndex+1);//获取下一个1-index点的坐标Point
mPlane->SetDirection(pt.x, pt.y);//设置方向为从0-index坐标指向1-index坐标

mShowSpline = false;//不显示曲线

}


void GameApp::Destroy()
{

SAFE_DELETE(mFont);
SAFE_DELETE(mSpline);
SAFE_DELETE(mTexBg);
SAFE_DELETE(mTexPlane);
SAFE_DELETE(mBg);
SAFE_DELETE(mPlane);

}


void GameApp::Update()
{

JGE* engine = JGE::GetInstance();

if (engine->GetButtonClick(PSP_CTRL_TRIANGLE))   // do a screen shot when the TRIANGLE button is pressed
{
   char s[80];
   sprintf(s, "ms0:/screenshot.png");
   JRenderer::GetInstance()->ScreenShot(s);
}

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

if (engine->GetButtonClick(PSP_CTRL_CIRCLE))
{
   mShowSpline = !mShowSpline;
}

float dt = engine->GetDelta();//获取引擎运行的时间Delta

mPlane->Update(dt);//将飞机更新到Delta时间应该显示的图片

mCounter += 100*dt;//根据运行的时间来设置当前应该显示的位置 根据执行频率*100 比如1毫秒刷新一次 则略过0.1个index
int curr = (int) mCounter;
if (mIndex != curr)          // Update position of the plane
{
   if (curr >= mSpline->GetPixelCount())//如果当前的运行次数>了整个曲线的index点的次数 回到起始点
   {
    mCounter = 0.0f;
    curr = 0;
   }

   mIndex = curr;//赋当前的index位置的值

   Point pt;
   mSpline->GetPixel(pt, mIndex);//取得index所在的点
   mPlane->SetPosition(pt.x, pt.y);//设置飞机

   mSpline->GetPixel(pt, mIndex+1);//取得index+1的点
   mPlane->SetDirection(pt.x, pt.y);//从index指向index+1的方向
}
//Update方式是不断循环来执行 无限循环 直到按了叉键
}


void GameApp::Render()
{

JRenderer* renderer = JRenderer::GetInstance();

renderer->RenderQuad(mBg, 0, 0);

   mFont->SetColor(ARGB(255,255,255,255));
   mFont->DrawString("Press O to show spline", 5.0f, 130.0f, JGETEXT_LEFT); 

if (mShowSpline)
   mSpline->Render(0, 0);//封装了整个绘制曲线的方法Render一下

mPlane->Render();//Render一下飞机 飞机的Sprite方法好像有问题 不能动态显示当前祯 这几个例子都有点小问题 质量不高= =!
Sleep(1);
}


void GameApp::Pause()
{

}


void GameApp::Resume()
{

}

 

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