view LightClone/Source/Mediator.cpp @ 15:ee1c2510096d

Work on GUI system
author koryspansel <koryspansel@bendbroadband.com>
date Wed, 14 Sep 2011 11:04:18 -0700
parents 7081e8e6008c
children d27c06bd8ce1
line wrap: on
line source

/*
 * Mediator
 */

#include "Mediator.h"
#include "VertexTypes.h"

#pragma warning(disable:4355)

/*
 * fUpdatePeriod
 */
static const float fUpdatePeriod = 1.0f / 60.0f;

/*
 * Mediator
 */
Mediator::Mediator() : kWindow(this)
{
	pGraphicsDevice	= NULL;
}

/*
 * Run
 */
ErrorCode Mediator::Run()
{
	ErrorCode eCode = Initialize();
	if(eCode == Error_Success)
	{
		float fAccumulator = 0.0f;

		kClock.Reset();

		while(kWorld.IsActive())
		{
			ProcessMessages();

			fAccumulator += Min(kClock.GetElapsed(), fUpdatePeriod);
			while(fAccumulator >= fUpdatePeriod)
			{
				Update(fUpdatePeriod);
				fAccumulator -= fUpdatePeriod;
			}

			//if(updated)
			{
				Render();
			}
		}

		Terminate();
	}

	return eCode;
}

/*
 * OnMessage
 */
int32 Mediator::OnMessage(Window* pInstance, uint32 nMessage, WPARAM wParam, LPARAM lParam)
{
	if(nMessage == WM_CLOSE)
	{
		pInstance->Terminate();
		return 0;
	}
	else
	
	if(nMessage == WM_DESTROY)
	{
		PostQuitMessage(0);
		return 0;
	}

	return DefWindowProc(pInstance->GetHandle(), nMessage, wParam, lParam);
}

/*
 * Initialize
 */
ErrorCode Mediator::Initialize()
{
	ErrorCode eCode = kWindow.Initialize();
	if(eCode == Error_Success)
	{
		ErrorCode eCode = GraphicsDevice::Create(kWindow.GetHandle(), ScreenSizeX, ScreenSizeY, &pGraphicsDevice);
		if(eCode != Error_Success)
		{
			Terminate();
			return Error_Fail;
		}

		eCode = kContext.Initialize(pGraphicsDevice);
		if(eCode != Error_Success)
		{
			Terminate();
			return Error_Fail;
		}

		eCode = kResourceManager.Initialize(pGraphicsDevice);
		if(eCode != Error_Success)
		{
			Terminate();
			return Error_Fail;
		}

		eCode = kInputManager.Initialize(kWindow.GetHandle());
		if(eCode != Error_Success)
		{
			Terminate();
			return Error_Fail;
		}

		eCode = kEventSystem.AddSource(&kInputManager);
		if(eCode != Error_Success)
		{
			Terminate();
			return Error_Fail;
		}

		eCode = kWorld.Initialize(&kEventSystem, &kResourceManager, &kInputManager);
		if(eCode != Error_Success)
		{
			Terminate();
			return Error_Fail;
		}
	}
	
	return eCode;
}

/*
 * Terminate
 */
void Mediator::Terminate()
{
	kWorld.Terminate();
	kInputManager.Terminate();
	kResourceManager.Terminate();
	kContext.Terminate();

	GraphicsDevice::Destroy(pGraphicsDevice);
}

/*
 * Update
 */
void Mediator::Update(float fElapsed)
{
	kEventSystem.Update(fElapsed);

	kWorld.Update(fElapsed);
}

/*
 * Render
 */
void Mediator::Render()
{
	kWorld.Render(kContext);
}

/*
 * ProcessMessages
 */
void Mediator::ProcessMessages()
{
	MSG kMessage;

	while(PeekMessage(&kMessage, NULL, 0, 0, PM_REMOVE))
	{
		if(kMessage.message == WM_QUIT)
		{
			kWorld.Deactivate();
			break;
		}

		TranslateMessage(&kMessage);
		DispatchMessage(&kMessage);
	}
}