/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     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: sgx/src/sgx/core/gamecode/base_engine.cpp

*/

#include "sgx/core/core.hpp"

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

void
CSGXBaseEngine::initialize()
{
}

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

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

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

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

	return FALSE;
}

void 
CSGXBaseEngine::unregisterScenarioObjects() {
	m_ScenarioObjects.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() {
	unregisterScenarioObjects();
	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_ScenarioObjects.begin();
	while(it != m_ScenarioObjects.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_ScenarioObjects.begin();
	while(it != m_ScenarioObjects.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_ScenarioObjects.begin();
	while(it != m_ScenarioObjects.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_ScenarioObjects.begin();
	while(cit != m_ScenarioObjects.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_ScenarioObjects.begin();
	while(cit != m_ScenarioObjects.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_ScenarioObjects.begin();
	while(cit != m_ScenarioObjects.end()) {
		(*cit)->postDraw();
		++cit;
	}
	//
	cit = m_GlobalObjects.begin();
	while(cit != m_GlobalObjects.end()) {
		(*cit)->postDraw();
		++cit;
	}
}

