view LightClone/Source/Mediator.cpp @ 58:5e382dee4c70

Rename CodePanel/CodeSlot to ProgramPanel/ActionSlot
author koryspansel <koryspansel@bendbroadband.com>
date Fri, 30 Sep 2011 22:39:08 -0700
parents dc1f4a668d50
children 44dcff5abf12
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)
	{
		ErrorCode eCode = GraphicsDevice::Create(kWindow.GetHandle(), ScreenSizeX, ScreenSizeY, &pGraphicsDevice);
		if(eCode != Error_Success)
		{
			TRACE("Error: Failed to initialize graphics device\n");
			
			Terminate();
			return Error_Fail;
		}

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

			Terminate();
			return Error_Fail;
		}

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

			Terminate();
			return Error_Fail;
		}

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

			Terminate();
			return Error_Fail;
		}

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

			Terminate();
			return Error_Fail;
		}
	}
	
	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);
	}
}