⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 game.cpp

📁 Torus 3D Engine is an open-source OpenGL ES 3D Engine for creating your own games in BREW environmen
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ==========================================================================================================
//
// WORLDMAP 3D FOR BREW MOBILE 2.0+
// Written by Vander Nunes, Dec 2005
//
// ==========================================================================================================

#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_pEngine = NULL;
	m_pScene = NULL;
	m_key_state = 0;
	m_kSELECT = m_kLEFT = m_kRIGHT = m_kUP = m_kDOWN = m_kCLEAR = FALSE;
	m_k2 = m_k4 = m_k6 = m_k8 = FALSE;
	// ----------------------------
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
CGame::~CGame()
{
	CleanUp();
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CGame::Destroy(CGame* pGame)
{
	pGame->CleanUp();
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
void CGame::CleanUp()
{
	DeletePtr(m_pCam);
	DeletePtr(m_pEngine);
	DeletePtr(m_pScene);
	DeletePtr(m_pEarth);
	DeletePtr(m_pSun);
	DeletePtr(m_pMoon);
	DeletePtr(m_pMap);
	DeletePtr(m_pDay);
	DeletePtr(m_pNight);
	DeletePtr(m_pHelp);
	DeletePtr(m_pSpr8x13);
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
boolean CGame::Init()
{
	DBGPRINTF("=======================");
	DBGPRINTF("APPLICATION STARTING UP");
	DBGPRINTF("=======================");

	// start clean
	m_pEarth = NULL;
	m_pSun = NULL;
	m_pMoon = NULL;
	m_iCamLongitude = 0;
	m_iCamLatitude = 0;
	m_iCamDistance = ITOX(60);
	m_jChase = CHASE_EARTH;
	m_dwAddSecs = 0;

	// 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);

	//--------------------------------------------------------------
	// 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("sky_.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 simple plane
	if (!m_pScene->LoadModel("plane.mob", 0.45f, 0.7f, 1.0f)) return FALSE;
	if (!m_pScene->LoadTexture("help.tga")) return FALSE;
	m_pHelp = new CSceneItem(m_pScene, nmod++, ntex++);

	// create earth
	m_pScene->AddEmptyModel();
	m_pScene->Model(nmod)->MakeSphere(EARTH_SIZE, 20, 16, FALSE);
	if (!m_pScene->LoadTexture("earthMap256x256.tga")) return FALSE;
	m_pEarth = new CSceneItem(m_pScene, nmod++, ntex++);

	// create sun
	#if MESH_SUN
		// as mesh
		m_pScene->AddEmptyModel();
		m_pScene->Model(nmod)->MakeSphere(SUN_SIZE, 9, 9, FALSE);
		if (!m_pScene->LoadTexture("sunMap128x128.tga")) return FALSE;
		m_pSun = new CSceneItem(m_pScene, nmod++, ntex++);
		m_pSun->TranslateTo(0,0,0);
	#else
		// as billboard
		if (!m_pScene->LoadModel("plane.mob", 0.1f, 0.1f, 0.1f)) return FALSE;
		if (!m_pScene->LoadTexture("sunflare.tga")) return FALSE;
		m_pSun = new CSceneItem(m_pScene, nmod++, ntex++);
		m_pSun->TranslateTo(0,0,0);
	#endif

	// create moon
	m_pScene->AddEmptyModel();
	m_pScene->Model(nmod)->MakeSphere(MOON_SIZE, 9, 9, FALSE);
	if (!m_pScene->LoadTexture("moonMap128x128.tga")) return FALSE;
	m_pMoon = new CSceneItem(m_pScene, nmod++, ntex++);

	//
	// Load bitmaps for neutral, day and night earth
	//

	m_pMap = new CTGA();
	if (!m_pMap->Load(&m_applet, "game.dat", "earthMap256x256.tga")) return FALSE;
	m_pMap->Unpalettize();

	m_pDay = new CTGA();
	if (!m_pDay->Load(&m_applet, "game.dat", "earthMap256x256.tga")) return FALSE;
	m_pDay->Unpalettize();

	m_pNight = new CTGA();
	if (!m_pNight->Load(&m_applet, "game.dat", "earthLght256x256.tga")) return FALSE;
	m_pNight->Unpalettize();

	// force an entire trace at first frame
	TimeUpdate();
	EarthUpdate();
	MoonUpdate();
	InitRayTrace();
	for (word y=0; y<m_pMap->Height(); y++) RayTrace();

	return TRUE;
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------
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;
		}

		default:
			break;
	}

	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:
		{
			// ### DEBUG
			//m_dwAddSecs += 100;
			//for (word y=0; y<m_pMap->Height(); y++) RayTrace();
			// ### END DEBUG
			return TRUE;
		}

		case AVK_2:
			m_k2 = true;
			return TRUE;
		case AVK_3:
		{
			// ### DEBUG
			//m_dwAddSecs += 86400;
			//for (word y=0; y<m_pMap->Height(); y++) RayTrace();
			// ### END DEBUG
			return TRUE;
		}
		case AVK_4:
			m_k4 = true;
			m_jChase--;
			if (m_jChase < CHASE_EARTH) m_jChase = CHASE_MOON;
			return TRUE;
		case AVK_5:
		{
			// ### DEBUG
			//SaveRawLight("light.raw");
			// ### END DEBUG
			return TRUE;
		}
		case AVK_6:
			m_k6 = true;
			m_jChase++;
			if (m_jChase > CHASE_MOON) m_jChase = CHASE_EARTH;
			return TRUE;
		case AVK_7:
			return TRUE;
		case AVK_8:
			m_k8 = true;
			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;
}


// ----------------------------------------------------------------------------------------------------------
//
//
//
// ----------------------------------------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -