📄 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;
// --------------------------------------------
}
// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
CGame::~CGame()
{
CleanUp();
}
// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CGame::Destroy(CGame* pGame)
{
pGame->CleanUp();
}
// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
boolean CGame::Init()
{
// start clean game data
m_pScene = NULL;
for (byte xx=0; xx<MAX_SHIPS; xx++)
m_pShips[xx] = 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.57f);
//--------------------------------------------------------------
// 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()
{
// generate automatically a sky box within the root node
// note that "up", "dn", "lf", "rt", "ft", "bk"
// (up, down, left, right, front and back textures for the box)
// will be appended by the method to the provided file name.
// ex: sky_up.tga, sky_dn.tga, ...
if (!m_pScene->GenSkyBox("cloudy_.tga", ITOX(64)))
return FALSE;
// 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 (!m_pScene->LoadModel("spaceship1.mob", 0.1f, 0.1f, 0.1f)) return FALSE;
if (!m_pScene->LoadTexture("spaceship1.tga")) return FALSE;
if (!m_pScene->LoadModel("spaceship2.mob", 0.1f, 0.1f, 0.1f)) return FALSE;
if (!m_pScene->LoadTexture("spaceship2.tga")) return FALSE;
// distribute the ships randomly over the landscape,
// also adding them to the root of the scenegraph
char mult = 1;
byte rndx, rndy, rndz;
for (byte xx=0; xx<MAX_SHIPS; xx++)
{
m_pShips[xx] = new CSceneItem(m_pScene, nmod+(xx&1), ntex+(xx&1));
m_pScene->AddItem(m_pScene->Root(), m_pShips[xx]);
GETRAND(&rndx,1);
GETRAND(&rndy,1);
GETRAND(&rndz,1);
int px = 20 + (rndz & 127) * mult;
int py = 25 + (rndy & 127);
int pz = - xx * 128 - (rndz & 127);
m_pShips[xx]->TranslateTo(ITOX(px), ITOX(py), ITOX(pz));
m_pShips[xx]->RotateTo(0,ITOX(180),0);
m_pShips[xx]->UpdateDirection();
mult *= -1;
}
return TRUE;
}
// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CGame::CleanUp()
{
// release camera
DeletePtr(m_pSpr8x13);
// release camera
DeletePtr(m_pCam);
// release ships
for (byte xx=0; xx<MAX_SHIPS; xx++)
DeletePtr(m_pShips[xx]);
// 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
// -----------------------------------------------------------------------------------------
if (m_kLEFT) m_pCam->AddRotation(0, ITOX(3), 0);
if (m_kRIGHT) m_pCam->AddRotation(0, ITOX(-3), 0);
#define SPEED 5
#if 0
//
// camera look around and move
//
if (m_kUP) m_pCam->AddTranslation(m_pCam->m_Direction[0]*SPEED, -m_pCam->m_Direction[1]*SPEED, -m_pCam->m_Direction[2]*SPEED);
if (m_kDOWN) m_pCam->AddTranslation(-m_pCam->m_Direction[0], m_pCam->m_Direction[1], m_pCam->m_Direction[2]);
#else
//
// camera look around only
//
//if (m_kSELECT) m_pCam->AddTranslation(m_pCam->m_Direction[0]*SPEED, -m_pCam->m_Direction[1]*SPEED, -m_pCam->m_Direction[2]*SPEED);
if (m_kUP) m_pCam->AddRotation(ITOX(3), 0, 0);
if (m_kDOWN) m_pCam->AddRotation(-ITOX(3), 0, 0);
#endif
m_pCam->UpdateDirection();
// -----------------------------------------------------------------------------------------
// render simplistic demo scene
// -----------------------------------------------------------------------------------------
m_pScene->Render(m_pScene->Root(), m_pCam);
// -----------------------------------------------------------------------------------------
// move the ships through space
// -----------------------------------------------------------------------------------------
#if 1
for (byte xx=0; xx<MAX_SHIPS; xx++)
{
m_pShips[xx]->AddTranslation(0,0,ITOX(15));
if (m_pShips[xx]->Position()[2] > ITOX(500))
{
m_pShips[xx]->Position()[2] -= ITOX(1100);
m_pShips[xx]->Position()[1] *= -1;
m_pShips[xx]->Position()[0] *= -1;
}
}
#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -