📄 game.cpp
字号:
// ==========================================================================================================
//
// BREW v2.0+ OPENGLES MICROENGINE
// Sample application
//
// -------------------------------------------------------------
// Please note that this is a general use 3D engine, best suited
// for 3D games development. Nevertheless, the core engine tries
// to provide only low-to-mid-level features, so that it is the
// developer responsibility to implement high-level features.
// The core is built in a way to turn it into an easy task to do.
// -------------------------------------------------------------
//
// Written by Vander Nunes
//
// ==========================================================================================================
#include "game.h"
// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
int AEEClsCreateInstance(AEECLSID ClsId, IShell *pIShell, IModule *po, void **ppObj)
{
*ppObj = NULL;
if (ClsId == AEECLSID_GAME)
if (AEEApplet_New(sizeof(CGame), ClsId, pIShell, po, (IApplet**)ppObj, (AEEHANDLER)CGame::HandleInitEvent,(PFNFREEAPPDATA)CGame::Destroy) == TRUE)
return AEE_SUCCESS;
return EFAILED;
}
// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
CGame::CGame()
{
m_key_state = 0;
m_kSELECT = m_kLEFT = m_kRIGHT = m_kUP = m_kDOWN = m_kCLEAR = FALSE;
m_klastSELECT = FALSE;
// --------------------------------------------
}
// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
CGame::~CGame()
{
CleanUp();
}
// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CGame::Destroy(CGame* pGame)
{
pGame->CleanUp();
}
// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
boolean CGame::Init()
{
// start clean game data
m_pScene = NULL;
m_pModel = NULL;
// initialize the 3D engine
m_pEngine = new CEngine(&m_applet);
if (!m_pEngine) return FALSE;
if (!m_pEngine->Init()) return FALSE;
// instanciate the camera
m_pCam = new CCamera(m_pEngine);
m_pCam->Setup(1.0f, 500.0f, 1.0f);
m_pCam->TranslateTo(0,0,ITOX(13));
m_pCam->UpdateDirection();
//--------------------------------------------------------------
// Setup a basic scenegraph.
//--------------------------------------------------------------
// create an empty scenegraph
m_pScene = new CSceneGraph(m_pEngine);
m_jGameStatus = STATUS_START;
m_dwFrame = 0;
NextFrame(this);
return TRUE;
}
// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
boolean CGame::LoadResources()
{
// get the number of already loaded models and textures
// (we're going to use it ahead)
word nmod = m_pScene->ModelCount();
word ntex = m_pScene->TextureCount();
// load two ships and two textures into the scenegraph library
// (these will be models 1,2 and textures 1,2 in the library)
#if 1
if (!m_pScene->LoadModel("ferrari.mob", 3.0f, 3.0f, 3.0f)) return FALSE;
#else
#if 1
m_pScene->AddEmptyModel();
m_pScene->Model(nmod)->MakeSphere(4, 20, 16, FALSE);
#else
if (!m_pScene->LoadModel("plane.mob", 0.25f, 0.25f, 0.25f)) return FALSE;
#endif
#endif
if (!m_pScene->LoadTexture("fskin.tga")) return FALSE;
if (!m_pScene->LoadTexture("envmap.tga")) return FALSE;
m_pModel = new CSceneItem(m_pScene, nmod++, ntex, ntex+1, ENVMAPMODE_MULTIPLY);
ntex += 2;
m_pScene->AddItem(m_pScene->Root(), m_pModel);
m_pModel->TranslateTo(0, 0, 0);
m_pModel->RotateTo(0,0,0);
m_pModel->UpdateDirection();
return TRUE;
}
// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CGame::CleanUp()
{
// release camera
DeletePtr(m_pSpr8x13);
// release camera
DeletePtr(m_pCam);
// release ships
DeletePtr(m_pModel);
// release scene
DeletePtr(m_pScene);
// release engine
DeletePtr(m_pEngine);
}
// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
boolean CGame::HandleInitEvent(CGame* pGame, AEEEvent event, uint16 wParam, uint32 dwParam)
{
switch (event)
{
case EVT_APP_START:
{
return pGame->Init();
}
case EVT_APP_STOP:
{
return TRUE;
}
}
return pGame->HandleEvent(event, wParam, dwParam);
}
// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
boolean CGame::HandleEvent(AEEEvent event, uint16 wParam, uint32 dwParam)
{
switch (event)
{
case EVT_APP_SUSPEND:
{
ISHELL_CancelTimer(m_applet.m_pIShell, NULL, (void *)this);
return TRUE;
}
case EVT_APP_RESUME:
{
NextFrame(this);
return TRUE;
}
case EVT_APP_NO_SLEEP:
{
return TRUE;
}
case EVT_KEY_PRESS:
{
m_key_state = 1;
m_key_wParam = wParam;
KeyPressEvent(wParam);
return TRUE;
}
case EVT_KEY_RELEASE:
{
m_key_state = 0;
m_key_wParam = wParam;
KeyReleaseEvent(wParam);
return TRUE;
}
case EVT_KEY:
{
return TRUE;
}
}
return TRUE;
}
// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
boolean CGame::KeyPressEvent(uint16 wParam)
{
switch(wParam)
{
case AVK_SELECT:
m_kSELECT = true;
return TRUE;
case AVK_LEFT:
m_kLEFT = true;
return TRUE;
case AVK_RIGHT:
m_kRIGHT = true;
return TRUE;
case AVK_UP:
m_kUP = true;
return TRUE;
case AVK_DOWN:
m_kDOWN = true;
return TRUE;
case AVK_1:
return TRUE;
case AVK_2:
return TRUE;
case AVK_3:
return TRUE;
case AVK_4:
return TRUE;
case AVK_5:
return TRUE;
case AVK_6:
return TRUE;
case AVK_7:
return TRUE;
case AVK_8:
return TRUE;
case AVK_9:
return TRUE;
case AVK_STAR:
return TRUE;
case AVK_0:
return TRUE;
case AVK_POUND:
return TRUE;
case AVK_CLR:
m_kCLEAR = true;
return TRUE;
}
return TRUE;
}
// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
boolean CGame::KeyReleaseEvent(uint16 wParam)
{
switch(wParam)
{
case AVK_SELECT:
m_kSELECT = false;
return TRUE;
case AVK_LEFT:
m_kLEFT = false;
return TRUE;
case AVK_RIGHT:
m_kRIGHT = false;
return TRUE;
case AVK_UP:
m_kUP = false;
return TRUE;
case AVK_DOWN:
m_kDOWN = false;
return TRUE;
case AVK_1:
return TRUE;
case AVK_2:
return TRUE;
case AVK_3:
return TRUE;
case AVK_4:
return TRUE;
case AVK_5:
return TRUE;
case AVK_6:
return TRUE;
case AVK_7:
return TRUE;
case AVK_8:
return TRUE;
case AVK_9:
return TRUE;
case AVK_STAR:
return TRUE;
case AVK_0:
return TRUE;
case AVK_POUND:
return TRUE;
case AVK_CLR:
m_kCLEAR = false;
return TRUE;
}
return TRUE;
}
// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CGame::NextFrame(CGame* pGame)
{
IDISPLAY_Backlight(pGame->m_applet.m_pIDisplay, TRUE);
// ----------------------------------------------------------------------------------
pGame->RenderFrame();
// ----------------------------------------------------------------------------------
ISHELL_SetTimer(pGame->m_applet.m_pIShell, 33, (PFNNOTIFY)NextFrame, (void *)pGame);
}
// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CGame::RenderFrame()
{
// -----------------------------------------------------------------------------------------
// clear everything
// -----------------------------------------------------------------------------------------
#if 1
glClearColorx(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
#else
glClear(GL_DEPTH_BUFFER_BIT);
#endif
switch(m_jGameStatus)
{
case STATUS_START:
{
RenderStart();
break;
}
case STATUS_LOADING:
{
if (!LoadResources()) ISHELL_CloseApplet(m_applet.m_pIShell, 0);
m_dwFrame = 0;
m_jGameStatus = STATUS_PLAYING;
break;
}
case STATUS_PLAYING:
{
RenderPlaying();
break;
}
}
// -----------------------------------------------------------------------------------------
//
// swap buffers and update screen
m_pEngine->SwapBuffers();
IDISPLAY_Update(m_applet.m_pIDisplay);
//
// -----------------------------------------------------------------------------------------
}
// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CGame::RenderStart()
{
if (m_dwFrame == 0)
{
if (m_kCLEAR || m_kSELECT) return;
// load 8x13 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ font sprite
m_pSpr8x13 = new CSprite(m_pEngine);
m_pSpr8x13->LoadFromVfs("game.dat", "font_8x13.bmp");
int16 jFrm8x13[58*6];
int yy, pp=0, f8x13offx=0;
for (yy=0; yy<58; yy++)
{
jFrm8x13[pp++] = f8x13offx;
jFrm8x13[pp++] = 0;
jFrm8x13[pp++] = 0;
jFrm8x13[pp++] = 0;
jFrm8x13[pp++] = 8;
jFrm8x13[pp++] = 13;
f8x13offx += 8;
}
m_pSpr8x13->Config(58, jFrm8x13);
}
if (m_dwFrame)
{
m_pSpr8x13->DrawText32To90((m_pEngine->ScreenWidth()-88)>>1, (m_pEngine->ScreenHeight()-13)>>1, "- LOADING -");
m_jGameStatus = STATUS_LOADING;
}
m_dwFrame++;
}
// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CGame::RenderPlaying()
{
if (m_kCLEAR) ISHELL_CloseApplet(m_applet.m_pIShell, 0);
// -----------------------------------------------------------------------------------------
// process keyboard
// -----------------------------------------------------------------------------------------
#define SPEED 3
#if 0
// change camera distance
if (m_kDOWN) if (m_pCam->m_Position[2] > ITOX(13)) m_pCam->AddTranslation(0,0,ITOX(-SPEED));
if (m_kUP) if (m_pCam->m_Position[2] < ITOX(30)) m_pCam->AddTranslation(0,0,ITOX(SPEED));
#else
// rotate model around X
if (m_kUP) m_pModel->AddRotation(ITOX(SPEED), 0, 0);
if (m_kDOWN) m_pModel->AddRotation(ITOX(-SPEED), 0, 0);
#endif
// rotate model around Y
if (m_kLEFT) m_pModel->AddRotation(0, ITOX(SPEED), 0);
if (m_kRIGHT) m_pModel->AddRotation(0, ITOX(-SPEED), 0);
m_pModel->UpdateDirection();
// change environment map mode
if (m_kSELECT)
{
if (!m_klastSELECT)
{
m_pModel->SetEnvMapMode(m_pModel->EnvMapMode()+1);
if (m_pModel->EnvMapMode() > 3) m_pModel->SetEnvMapMode(0);
m_klastSELECT = TRUE;
}
} else m_klastSELECT = FALSE;
// -----------------------------------------------------------------------------------------
// render simplistic demo scene
// -----------------------------------------------------------------------------------------
m_pScene->Render(m_pScene->Root(), m_pCam, FALSE);
// show hint text
m_pSpr8x13->DrawText32To90((m_pEngine->ScreenWidth()-64)>>1, 2, "'SELECT'");
m_pSpr8x13->DrawText32To90((m_pEngine->ScreenWidth()-96)>>1, 16, "CHANGES MODE");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -