/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2008.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: src/sgx/core/gamecode/base_engine.cpp

*/

#include "sgx/core/core.hpp"

CSGXBaseEngine::CSGXBaseEngine() : CGameObject() {
	m_SuspendCount = 0;
}

void
CSGXBaseEngine::initialize()
{
}

void
CSGXBaseEngine::release()
{
	releaseGameObjects();
	releaseGlobalObjects();
}

void
CSGXBaseEngine::releaseGameObjects()
{
	sgxVector<CGameHandler *>::iterator it = m_EngineObjects.begin();
	
	for(;it!=m_EngineObjects.end();++it) {
		(*it)->release();
		delete (*it);
	}
	m_EngineObjects.clear();
}

tBOOL 
CSGXBaseEngine::registerObject(CGameHandler *ptr)
{
	if (ptr) {
		sgxAssert(find(m_EngineObjects.begin(), m_EngineObjects.end(), ptr) == m_EngineObjects.end());
		m_EngineObjects.push_back(ptr);
		return TRUE;
	}
	return FALSE;
}

tBOOL 
CSGXBaseEngine::unregisterObject(CGameHandler *ptr)
{
	sgxVector<CGameHandler *>::iterator it = m_EngineObjects.begin();
	
	for(;it!=m_EngineObjects.end();++it) {
		if ((*it) == ptr) {
			m_EngineObjects.erase(it);	
			return TRUE;
		}
	}

	return FALSE;
}

void 
CSGXBaseEngine::unregisterObjects() {
	m_EngineObjects.clear();
}

//
//
//

tBOOL 
CSGXBaseEngine::registerGlobalObject(CGameHandler *ptr)
{
	m_GlobalObjects.push_back(ptr);
	return TRUE;
}

tBOOL 
CSGXBaseEngine::unregisterGlobalObject(CGameHandler *ptr)
{
	sgxVector<CGameHandler *>::iterator it = m_GlobalObjects.begin();
	
	for(;it!=m_GlobalObjects.end();++it) {
		if ((*it) == ptr) {
			m_GlobalObjects.erase(it);	
			return TRUE;
		}
	}

	return FALSE;
}

void
CSGXBaseEngine::releaseGlobalObjects()
{
	sgxVector<CGameHandler *>::iterator it = m_GlobalObjects.begin();
	
	for(;it!=m_GlobalObjects.end();++it) {
		(*it)->release();
		delete (*it);
	}
	m_GlobalObjects.clear();
}


void 
CSGXBaseEngine::unregisterGlobalObjects() {
	m_GlobalObjects.clear();
}

//
// And all..
//
void 
CSGXBaseEngine::unregisterAllObjects() {
	unregisterObjects();
	unregisterGlobalObjects();
}

//
// This are the standard methods that re-distribute the method call to 
// all those objects registerObjected with this engine.
//


void 
CSGXBaseEngine::preUpdate(const tREAL32 t) 
{
	sgxVector<CGameHandler *>::iterator it = m_EngineObjects.begin();
	while(it != m_EngineObjects.end()) {
		(*it)->preUpdate(t);
		++it;
	}
	//
	it = m_GlobalObjects.begin();
	while(it != m_GlobalObjects.end()) {
		(*it)->preUpdate(t);
		++it;
	}
}

void 
CSGXBaseEngine::suspend(const tUINT32 frames) 
{
	m_SuspendCount += frames;
}

void 
CSGXBaseEngine::resume() 
{
	m_SuspendCount = 0;
}

tBOOL	
CSGXBaseEngine::isSuspended() {
	return m_SuspendCount == 0 ? FALSE : TRUE;
}

void 
CSGXBaseEngine::update(const tREAL32 t) 
{
	if (m_SuspendCount) {
		--m_SuspendCount;
		return;
	}

	preUpdate(t);
	//
	sgxVector<CGameHandler *>::iterator it = m_EngineObjects.begin();
	while(it != m_EngineObjects.end()) {
		(*it)->update(t);
		++it;
	}
	//
	it = m_GlobalObjects.begin();
	while(it != m_GlobalObjects.end()) {
		(*it)->update(t);
		++it;
	}
	//
	postUpdate(t);
}

void 
CSGXBaseEngine::postUpdate(const tREAL32 t) 
{
	sgxVector<CGameHandler *>::iterator it = m_EngineObjects.begin();
	while(it != m_EngineObjects.end()) {
		(*it)->postUpdate(t);
		++it;
	}
	//
	it = m_GlobalObjects.begin();
	while(it != m_GlobalObjects.end()) {
		(*it)->postUpdate(t);
		++it;
	}
}

//
void 
CSGXBaseEngine::preDraw() const
{
	sgxVector<CGameHandler *>::const_iterator cit = m_EngineObjects.begin();
	while(cit != m_EngineObjects.end()) {
		(*cit)->preDraw();
		++cit;
	}
	//
	cit = m_GlobalObjects.begin();
	while(cit != m_GlobalObjects.end()) {
		(*cit)->preDraw();
		++cit;
	}
}

void 
CSGXBaseEngine::draw() const
{
	preDraw();
	//
	sgxVector<CGameHandler *>::const_iterator cit = m_EngineObjects.begin();
	while(cit != m_EngineObjects.end()) {
		(*cit)->draw();
		++cit;
	}
	//
	cit = m_GlobalObjects.begin();
	while(cit != m_GlobalObjects.end()) {
		(*cit)->draw();
		++cit;
	}
	postDraw();
}

void 
CSGXBaseEngine::postDraw() const
{
	sgxVector<CGameHandler *>::const_iterator cit = m_EngineObjects.begin();
	while(cit != m_EngineObjects.end()) {
		(*cit)->postDraw();
		++cit;
	}
	//
	cit = m_GlobalObjects.begin();
	while(cit != m_GlobalObjects.end()) {
		(*cit)->postDraw();
		++cit;
	}
}

