view LightClone/Source/Mediator.cpp @ 63:44dcff5abf12

Work on ServiceProvider
author koryspansel
date Tue, 04 Oct 2011 12:04:09 -0700
parents dc1f4a668d50
children 3507bd831c7f
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 = fUpdatePeriod;

		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()
{
	InitializeTrace(TraceFlag_Debug | TraceFlag_File);

	ErrorCode eCode = kWindow.Initialize();
	if(eCode != Error_Success)
	{
		TRACE("Error: Failed to initialize window\n");

		Terminate();
		return eCode;
	}

	eCode = GraphicsDevice::Create(kWindow.GetHandle(), ScreenSizeX, ScreenSizeY, &pGraphicsDevice);
	if(eCode != Error_Success)
	{
		TRACE("Error: Failed to initialize graphics device\n");
		
		Terminate();
		return eCode;
	}

	eCode = kContext.Initialize(pGraphicsDevice);
	if(eCode != Error_Success)
	{
		TRACE("Error: Failed to initialize render context\n");

		Terminate();
		return eCode;
	}

	eCode = kResourceManager.Initialize(pGraphicsDevice);
	if(eCode != Error_Success)
	{
		TRACE("Error: Failed to initialize resource manager\n");

		Terminate();
		return eCode;
	}

	eCode = kInputManager.Initialize(kWindow.GetHandle());
	if(eCode != Error_Success)
	{
		TRACE("Error: Failed to initialize input manager\n");

		Terminate();
		return eCode;
	}

	eCode = kWorld.Initialize(&kResourceManager, &kInputManager);
	if(eCode != Error_Success)
	{
		TRACE("Error: Failed to initialize world\n");

		Terminate();
		return eCode;
	}

	eCode = kServiceProvider.AddService("ResourceManager", &kResourceManager);
	if(eCode != Error_Success)
	{
		TRACE("Error: Failed to add resource manager service\n");

		Terminate();
		return eCode;
	}

	eCode = kServiceProvider.AddService("InputManager", &kInputManager);
	if(eCode != Error_Success)
	{
		TRACE("Error: Failed to add input manager service\n");

		Terminate();
		return eCode;
	}	
	
	return eCode;
}

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

	GraphicsDevice::Destroy(pGraphicsDevice);

	TerminateTrace();
}

/*
 * Update
 */
void Mediator::Update(float 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);
	}
}