www.pudn.com > RCApp-src.zip > EnvironmentManager.cpp


/* 
	RedEye Project (http://members.ozemail.com.au/~ndmcevoy/) 
	Copyright (C) 2003  Nick McEvoy 
 
	This library is free software; you can redistribute it and/or 
	modify it under the terms of the GNU Library General Public 
	License as published by the Free Software Foundation; either 
	version 2 of the License, or (at your option) any later version. 
 
	This library is distributed in the hope that it will be useful, 
	but WITHOUT ANY WARRANTY; without even the implied warranty of 
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
	Library General Public License for more details. 
 
	You should have received a copy of the GNU Library General Public 
	License along with this library; if not, write to the Free 
	Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 
	----------------------------------------------------------------- 
 
		Commented for use with Doxygen (http://www.doxygen.org) 
 
	----------------------------------------------------------------- 
*/ 
 
/*! \file EnvironmentManager.cpp 
 *	\brief Environment manager. 
 * 
 *		This file contains the environment manager. 
 */ 
 
// GLUT includes 
#include  
 
// RedEye includes 
#include "EnvironmentManager.h" 
#include "ModelManager.h" 
#include "Engine.h" 
#include "Collision.h" 
#include "RCDefs.h" 
 
//! The one and only instance 
reEnvironmentManager* reEnvironmentManager::mpInstance = NULL; 
 
 
 
 
reEnvironmentManager::reEnvironmentManager() 
{ 
	mpSky = NULL; 
	mpTerrain = NULL; 
} 
 
 
 
 
reEnvironmentManager::~reEnvironmentManager() 
{ 
	DestroyLevel(); 
	mpInstance = NULL; 
} 
 
 
 
 
reEnvironmentManager* 
reEnvironmentManager::GetInstance() 
{ 
	if (mpInstance == NULL) 
	{ 
		mpInstance = new reEnvironmentManager; 
	} 
	return mpInstance; 
} 
 
 
 
 
void 
reEnvironmentManager::CreateLevel() 
{ 
	printf("reEnvironmentManager::CreateLevel() - creating environment...\n"); 
 
	TiXmlNode* pLevel = reGetGameEngine()->GetXMLLevelNode(); 
	if (!pLevel) 
		return; 
 
	// Create sky 
	TiXmlNode* pNode = pLevel->FirstChild("Sky"); 
	if (pNode) 
	{ 
		const char* psFile = reGetXMLAttribute(pNode, "file", ""); 
		mpSky = (reSkyEntity*)reGameEntity::Create(psFile); 
	} 
 
	// Create terrain 
	pNode = pLevel->FirstChild("Terrain"); 
	if (pNode) 
	{ 
		const char* psFile = reGetXMLAttribute(pNode, "file", ""); 
		mpTerrain = (reTerrainEntity*)reGameEntity::Create(psFile); 
		if (mpTerrain) 
		{ 
			reGetWorld()->addKid(mpTerrain); 
		} 
		else // error 
		{ 
			ulSetError(UL_WARNING,  
				 "reEnvironmentManager::CreateLevel() Could not create terrain."); 
		} 
	} 
 
	printf("reEnvironmentManager::CreateLevel() - done\n"); 
} 
 
 
 
 
void 
reEnvironmentManager::DestroyLevel() 
{ 
	printf("reEnvironmentManager::DestroyLevel() - destroying entities...\n"); 
 
	if (mpSky) 
	{ 
		ssgDeRefDelete(mpSky); 
		mpSky = NULL; 
	} 
 
	if (mpTerrain) 
	{ 
		reGetWorld()->removeKid(mpTerrain); 
		ssgDeRefDelete(mpTerrain); 
		mpTerrain = NULL; 
	} 
	 
	printf("reEnvironmentManager::DestroyLevel() - done\n"); 
} 
 
 
 
 
void 
reEnvironmentManager::Update(float fDeltaSec) 
{ 
	if (mpSky) 
		mpSky->Update(fDeltaSec); 
 
	if (mpTerrain) 
		mpTerrain->Update(fDeltaSec); 
} 
 
 
 
 
void 
reEnvironmentManager::PreDraw() 
{ 
	sgCoord CameraPos = reGetGameEngine()->GetCamera()->GetCameraPos(); 
 
	// Setup fog 
	sgVec4 FogCol; 
	sgSetVec4(FogCol, 0.4, 0.6, 0.6, 1.0); 
	float fAltitude = reGetAltitude(reGetWorld(), CameraPos.xyz); 
	if (fAltitude < 0) 
		glFogf(GL_FOG_DENSITY, 0.05); 
	else 
		glFogf(GL_FOG_DENSITY, 0.001); 
	glFogfv(GL_FOG_COLOR, FogCol); 
	glFogf(GL_FOG_START, 0.0); 
	glFogf(GL_FOG_END, 1000.0); 
	glFogi(GL_FOG_MODE, GL_EXP2); 
	glHint(GL_FOG_HINT, GL_NICEST); 
 
	if (mpSky) 
		mpSky->PreDraw(); 
} 
 
 
 
 
void 
reEnvironmentManager::PostDraw() 
{ 
	if (mpSky) 
		mpSky->PostDraw(); 
}