changeset 8:968341ab1fb2

First pass world re-structuring
author koryspansel
date Fri, 09 Sep 2011 16:18:41 -0700
parents 31373c5bd1b9
children e494c4295dba
files LightClone/LightClone.vcproj LightClone/Source/Controller.cpp LightClone/Source/Controller.h LightClone/Source/GraphicsDevice.cpp LightClone/Source/GraphicsDevice.h LightClone/Source/Interface.cpp LightClone/Source/Interface.h LightClone/Source/Mediator.cpp LightClone/Source/Mediator.h LightClone/Source/Model.cpp LightClone/Source/Model.h LightClone/Source/RenderContext.cpp LightClone/Source/RenderContext.h LightClone/Source/ResourceManager.cpp LightClone/Source/ResourceManager.h LightClone/Source/View.cpp LightClone/Source/View.h LightClone/Source/Window.cpp LightClone/Source/Window.h LightClone/Source/World.cpp LightClone/Source/World.h
diffstat 21 files changed, 1864 insertions(+), 1713 deletions(-) [+]
line wrap: on
line diff
--- a/LightClone/LightClone.vcproj	Fri Sep 09 13:21:22 2011 -0700
+++ b/LightClone/LightClone.vcproj	Fri Sep 09 16:18:41 2011 -0700
@@ -197,10 +197,6 @@
 				>
 			</File>
 			<File
-				RelativePath=".\Source\Controller.cpp"
-				>
-			</File>
-			<File
 				RelativePath=".\Source\Core.cpp"
 				>
 			</File>
@@ -217,6 +213,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\Source\GraphicsDevice.cpp"
+				>
+			</File>
+			<File
 				RelativePath=".\Source\InputManager.cpp"
 				>
 			</File>
@@ -237,11 +237,11 @@
 				>
 			</File>
 			<File
-				RelativePath=".\Source\Model.cpp"
+				RelativePath=".\Source\RenderContext.cpp"
 				>
 			</File>
 			<File
-				RelativePath=".\Source\RenderContext.cpp"
+				RelativePath=".\Source\ResourceManager.cpp"
 				>
 			</File>
 			<File
@@ -257,11 +257,11 @@
 				>
 			</File>
 			<File
-				RelativePath=".\Source\View.cpp"
+				RelativePath=".\Source\VirtualMachine.cpp"
 				>
 			</File>
 			<File
-				RelativePath=".\Source\VirtualMachine.cpp"
+				RelativePath=".\Source\Window.cpp"
 				>
 			</File>
 			<File
@@ -295,10 +295,6 @@
 				>
 			</File>
 			<File
-				RelativePath=".\Source\Controller.h"
-				>
-			</File>
-			<File
 				RelativePath=".\Source\Core.h"
 				>
 			</File>
@@ -315,6 +311,10 @@
 				>
 			</File>
 			<File
+				RelativePath=".\Source\GraphicsDevice.h"
+				>
+			</File>
+			<File
 				RelativePath=".\Source\InputManager.h"
 				>
 			</File>
@@ -331,11 +331,11 @@
 				>
 			</File>
 			<File
-				RelativePath=".\Source\Model.h"
+				RelativePath=".\Source\RenderContext.h"
 				>
 			</File>
 			<File
-				RelativePath=".\Source\RenderContext.h"
+				RelativePath=".\Source\ResourceManager.h"
 				>
 			</File>
 			<File
@@ -351,11 +351,11 @@
 				>
 			</File>
 			<File
-				RelativePath=".\Source\View.h"
+				RelativePath=".\Source\VirtualMachine.h"
 				>
 			</File>
 			<File
-				RelativePath=".\Source\VirtualMachine.h"
+				RelativePath=".\Source\Window.h"
 				>
 			</File>
 			<File
--- a/LightClone/Source/Controller.cpp	Fri Sep 09 13:21:22 2011 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,342 +0,0 @@
-/*
- * Controller
- */
-
-#include "Controller.h"
-
-/*
- * Controller
- */
-Controller::Controller(Model* pModelInstance, View* pViewInstance) : pModel(pModelInstance), pView(pViewInstance)
-{
-}
-
-/*
- * Initialize
- */
-ErrorCode Controller::Initialize()
-{
-	HWND kWindow = pView->GetWindow();
-
-	ErrorCode eCode = pModel->kInputManager.Initialize(kWindow);
-	if(eCode == Error_Success)
-	{
-		pModel->kInputManager.SetBounds(0, 0, (float)ScreenSizeX, (float)ScreenSizeY);
-		pModel->kInputManager.SetMouse(0.5f * ScreenSizeX, 0.5f * ScreenSizeY);
-	}
-
-	return eCode;
-}
-
-/*
- * Terminate
- */
-void Controller::Terminate()
-{
-	pModel->kInputManager.Terminate();
-}
-
-/*
- * Update
- */
-void Controller::Update(float fElapsed)
-{
-	ProcessInput(fElapsed);
-
-	if(pModel->nGameState == GameState_LoadMap)
-	{
-		char kBuffer[256];
-		sprintf_s(kBuffer, "Data\\Maps\\Map%02d.map", pModel->nCurrentLevel++);
-
-		pModel->nGameState = pModel->Load(kBuffer) ? GameState_Active : GameState_Over;
-	}
-	else
-
-	if(pModel->nGameState == GameState_Active)
-	{
-		if(pModel->nSimulationState == SimulationState_Active)
-		{
-			if(pModel->GetBot()->Update(fElapsed))
-			{
-				Environment* pEnvironment = pModel->GetEnvironment();
-				if(pEnvironment->RequirementsMet())
-				{
-					pModel->kDialog.Reset("Some message");
-					pModel->kDialog.AddButton(DialogButton_Ok, "Ok", 0.0f, 0.0f, 0.0f, 0.0f);
-
-					pModel->nGameState = GameState_Complete;
-				}
-			}
-		}
-	}
-}
-
-/*
- * ProcessInput
- */
-void Controller::ProcessInput(float fElapsed)
-{
-	pModel->kInputManager.Update(fElapsed);
-
-	#if defined(_DEBUG)
-	if(pModel->kInputManager.IsKeyDown(DIK_LEFT))
-	{
-		pView->UpdateCameraYaw(0.01f);
-	}
-	else
-
-	if(pModel->kInputManager.IsKeyDown(DIK_RIGHT))
-	{
-		pView->UpdateCameraYaw(-0.01f);
-	}
-
-	if(pModel->kInputManager.IsKeyDown(DIK_UP))
-	{
-		pView->UpdateCameraPitch(0.01f);
-	}
-	else
-
-	if(pModel->kInputManager.IsKeyDown(DIK_DOWN))
-	{
-		pView->UpdateCameraPitch(-0.01f);
-	}
-
-	if(pModel->kInputManager.IsKeyDown(DIK_NEXT))
-	{
-		pView->UpdateCameraDistance(0.1f);
-	}
-	else
-
-	if(pModel->kInputManager.IsKeyDown(DIK_PRIOR))
-	{
-		pView->UpdateCameraDistance(-0.1f);
-	}
-	#endif
-
-	const float fMouseX = pModel->kInputManager.GetMouseX();
-	const float fMouseY = pModel->kInputManager.GetMouseY();
-
-	if(pModel->nGameState == GameState_Active)
-	{
-		if(pModel->kInputManager.IsButtonDown(0) && !pModel->kInputManager.WasButtonDown(0))
-		{
-			int32 nSelection = pModel->kControls.Pick(fMouseX, fMouseY);
-			if(nSelection >= 0)
-			{
-				if(nSelection == ControlButton_Play)
-				{
-					if(pModel->nSimulationState == SimulationState_Idle)
-					{
-						pModel->ResetBot();
-						pModel->GetBot()->Upload(pModel->GetFunction(0), pModel->GetFunctionCount()); 
-
-						pModel->nSimulationState = SimulationState_Active;
-					}
-				}
-				else
-
-				if(nSelection == ControlButton_Stop)
-				{
-					if(pModel->nSimulationState == SimulationState_Active)
-					{
-						pModel->ResetEnvironment();
-						pModel->ResetBot();
-
-						pModel->nSimulationState = SimulationState_Idle;
-					}
-				}
-				else
-
-				if(nSelection == ControlButton_Exit)
-				{
-					pModel->nGameState = GameState_Exit;
-				}
-			}
-			else
-			{
-				// pick against the toolbar
-				nSelection = pModel->kToolbar.Pick(fMouseX, fMouseY);
-				if(nSelection >= 0)
-				{
-					pModel->kDragController.Begin(Action_Forward + nSelection);
-				}
-				else
-				{
-					// pick against the main pane
-					nSelection = pModel->kMain.Pick(fMouseX, fMouseY);
-					if(nSelection >= 0)
-					{
-						Code* pCode = pModel->GetFunction(0);
-						if(!pCode->IsEmptySlot(nSelection))
-						{
-							pModel->kDragController.Begin(pCode->GetSlot(nSelection));
-							pCode->ClearSlot(nSelection);
-						}
-					}
-					else
-					{
-						// pick against the function pane
-						nSelection = pModel->kFunction.Pick(fMouseX, fMouseY);
-						if(nSelection >= 0)
-						{
-							Code* pCode = pModel->GetFunction(pModel->nCurrentFunction + 1);
-							if(!pCode->IsEmptySlot(nSelection))
-							{
-								pModel->kDragController.Begin(pCode->GetSlot(nSelection));
-								pCode->ClearSlot(nSelection);
-							}
-						}
-						else
-						{
-							// pick against the function pane arrows
-							for(uint32 i = 0; i < sizeof(pModel->kArrowBounds) / sizeof(pModel->kArrowBounds[0]); ++i)
-							{
-								if(pModel->kArrowBounds[i].Contains(fMouseX, fMouseY))
-								{
-									const uint32 nCount = pModel->GetFunctionCount() - 1;
-									pModel->nCurrentFunction = (pModel->nCurrentFunction + 2 * (int32)i - 1 + nCount) % nCount;
-								}
-							}
-						}
-					}
-				}
-			}
-		}
-		else
-
-		if(!pModel->kInputManager.IsButtonDown(0) && pModel->kInputManager.WasButtonDown(0))
-		{
-			if(pModel->kDragController.IsActive())
-			{
-				const uint32 nAction = pModel->kDragController.End();
-
-				int32 nSelection = pModel->kMain.Pick(fMouseX, fMouseY);
-				if(nSelection >= 0)
-				{
-					pModel->GetFunction(0)->SetSlot(nSelection, nAction);
-				}
-				else
-				{
-					nSelection = pModel->kFunction.Pick(fMouseX, fMouseY);
-					if(nSelection >= 0)
-					{
-						pModel->GetFunction(pModel->nCurrentFunction + 1)->SetSlot(nSelection, nAction);
-					}
-				}
-			}
-		}
-	}
-	else
-
-	if(pModel->nGameState == GameState_Complete)
-	{
-		if(pModel->kInputManager.IsButtonDown(0) && !pModel->kInputManager.WasButtonDown(0))
-		{
-			for(uint32 i = 0; i < sizeof(pModel->kDialog1Bounds) / sizeof(pModel->kDialog1Bounds[0]); ++i)
-			{
-				if(pModel->kDialog1Bounds[i].Contains(fMouseX, fMouseY))
-				{
-					if(i == 0)
-					{
-						if(pModel->nSimulationState == SimulationState_Active)
-						{
-							pModel->ResetEnvironment();
-							pModel->ResetBot();
-
-							pModel->nSimulationState = SimulationState_Idle;
-						}
-
-						pModel->nGameState = GameState_LoadMap;
-					}
-				}
-			}
-
-			/*
-			int32 nSelection = kDialog.Pick(fMouseX, fMouseY);
-
-			if(nSelection == DialogButton_A)
-			{
-				if(pModel->nSimulationState == SimulationState_Active)
-				{
-					pModel->ResetEnvironment();
-					pModel->ResetBot();
-
-					pModel->nSimulationState = SimulationState_Idle;
-				}
-
-				pModel->nGameState = GameState_LoadMap;
-			}
-			*/
-		}
-	}
-	else
-
-	if(pModel->nGameState == GameState_Over)
-	{
-		// check to see if button was clicked
-		if(pModel->kInputManager.IsButtonDown(0) && !pModel->kInputManager.WasButtonDown(0))
-		{
-			for(uint32 i = 0; i < sizeof(pModel->kDialog1Bounds) / sizeof(pModel->kDialog1Bounds[0]); ++i)
-			{
-				if(pModel->kDialog1Bounds[i].Contains(fMouseX, fMouseY))
-				{
-					if(i == 0)
-					{
-						pModel->nCurrentLevel	= 0;
-						pModel->nGameState		= GameState_LoadMap;
-					}
-				}
-			}
-
-			/*
-			int32 nSelection = kDialog.Pick(fMouseX, fMouseY);
-
-			if(nSelection == DialogButton_A)
-			{
-				pModel->nCurrentLevel	= 0;
-				pModel->nGameState		= GameState_LoadMap;
-			}
-			*/
-		}
-	}
-	else
-
-	if(pModel->nGameState == GameState_Confirm)
-	{
-		// check to see if button was clicked
-		if(pModel->kInputManager.IsButtonDown(0) && !pModel->kInputManager.WasButtonDown(0))
-		{
-			for(uint32 i = 0; i < sizeof(pModel->kDialog2Bounds) / sizeof(pModel->kDialog2Bounds[0]); ++i)
-			{
-				if(pModel->kDialog2Bounds[i].Contains(fMouseX, fMouseY))
-				{
-					if(i == 0)
-					{
-						pModel->nGameState = GameState_Exit;
-					}
-					else
-
-					if(i == 1)
-					{
-						pModel->nGameState = GameState_Active;
-					}
-				}
-			}
-
-			/*
-			int32 nSelection = kDialog.Pick(fMouseX, fMouseY);
-
-			if(nSelection == DialogButton_A)
-			{
-				pModel->nGameState = GameState_Exit;
-			}
-			else
-
-			if(nSelection == DialogButton_B)
-			{
-				pModel->nGameState = GameState_Active;
-			}
-			*/
-		}
-	}
-}
--- a/LightClone/Source/Controller.h	Fri Sep 09 13:21:22 2011 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,56 +0,0 @@
-/*
- * Controller
- */
-
-#ifndef __CONTROLLER_H__
-#define __CONTROLLER_H__
-
-#include "View.h"
-#include "Model.h"
-
-/*
- * Controller
- */
-class Controller
-{
-	/*
-	 * pModel
-	 */
-	Model* pModel;
-
-	/*
-	 * pView
-	 */
-	View* pView;
-
-public:
-
-	/*
-	 * Controller
-	 */
-	Controller(Model* pModel, View* pView);
-
-	/*
-	 * Initialize
-	 */
-	ErrorCode Initialize();
-
-	/*
-	 * Terminate
-	 */
-	void Terminate();
-
-	/*
-	 * Update
-	 */
-	void Update(float fElapsed);
-
-private:
-
-	/*
-	 * ProcessInput
-	 */
-	void ProcessInput(float fElapsed);
-};
-
-#endif //__CONTROLLER_H__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LightClone/Source/GraphicsDevice.cpp	Fri Sep 09 16:18:41 2011 -0700
@@ -0,0 +1,138 @@
+/*
+ * GraphicsDevice
+ */
+
+#include "GraphicsDevice.h"
+#include "VertexTypes.h"
+
+/*
+ * GraphicsDevice
+ */
+GraphicsDevice::GraphicsDevice(IDirect3D9* pContext, IDirect3DDevice9* pInstance, const D3DPRESENT_PARAMETERS& kPresentParameters)
+	: pDirect3D(pContext), pDevice(pInstance), kParameters(kPresentParameters)
+{
+	//ASSERT(pDirect3D);
+	pDirect3D->AddRef();
+
+	//ASSERT(pDevice);
+	pDevice->AddRef();
+}
+
+/*
+ * ~GraphicsDevice
+ */
+GraphicsDevice::~GraphicsDevice()
+{
+	if(pDevice)
+	{
+		pDevice->Release();
+		pDevice = NULL;
+	}
+
+	if(pDirect3D)
+	{
+		pDirect3D->Release();
+		pDirect3D = NULL;
+	}
+}
+
+/*
+ * Create
+ */
+ErrorCode GraphicsDevice::Create(HWND kWindow, uint32 nWidth, uint32 nHeight, GraphicsDevice** pInstance)
+{
+	ErrorCode eCode = Error_Fail;
+
+	if(pInstance)
+	{
+		const uint32 nAdapter	= D3DADAPTER_DEFAULT;
+		const uint32 nFlags		= D3DCREATE_HARDWARE_VERTEXPROCESSING;
+
+		D3DPRESENT_PARAMETERS kParameters = {
+			nWidth,
+			nHeight,
+			D3DFMT_UNKNOWN,
+			1,
+			D3DMULTISAMPLE_4_SAMPLES,
+			1,
+			D3DSWAPEFFECT_DISCARD,
+			kWindow,
+			TRUE,
+			TRUE,
+			D3DFMT_D24S8,
+			0,
+			0,
+			D3DPRESENT_INTERVAL_DEFAULT,
+		};
+
+		IDirect3D9* pContext = Direct3DCreate9(D3D_SDK_VERSION);
+		if(pContext)
+		{
+			IDirect3DDevice9* pDevice = NULL;
+
+			HRESULT hResult = pContext->CreateDevice(nAdapter, D3DDEVTYPE_HAL, kWindow, nFlags, &kParameters, &pDevice);
+			if(SUCCEEDED(hResult))
+			{
+				eCode = InitializeVertexTypes(pDevice);
+				if(eCode == Error_Success)
+				{
+					*pInstance = new GraphicsDevice(pContext, pDevice, kParameters);
+				}
+
+				pDevice->Release();
+			}
+
+			pContext->Release();
+		}
+	}
+
+	return eCode;
+}
+
+/*
+ * Destroy
+ */
+void GraphicsDevice::Destroy(GraphicsDevice*& pDevice)
+{
+	//NOTE: If I ever want to have more than one device this would be an issue
+	TerminateVertexTypes();
+
+	delete pDevice;
+	pDevice = NULL;
+}
+
+/*
+ * GetViewport
+ */
+ErrorCode GraphicsDevice::GetViewport(D3DVIEWPORT9* pViewport)
+{
+	return SUCCEEDED(pDevice->GetViewport(pViewport)) ? Error_Success : Error_Fail;
+}
+
+/*
+ * Begin
+ */
+void GraphicsDevice::Begin(uint32 nColor, float fDepth, uint32 nStencil)
+{
+	pDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, nColor, fDepth, nStencil);
+	pDevice->BeginScene();
+}
+
+/*
+ * End
+ */
+void GraphicsDevice::End()
+{
+	pDevice->EndScene();
+	pDevice->Present(NULL, NULL, NULL, NULL);
+}
+
+/*
+ * DrawTriangles
+ */
+void GraphicsDevice::DrawTriangles(IDirect3DVertexDeclaration9* pDeclaration, IDirect3DVertexBuffer9* pBuffer, uint32 nSize, uint32 nCount)
+{
+	pDevice->SetVertexDeclaration(pDeclaration);
+	pDevice->SetStreamSource(0, pBuffer, 0, nSize);
+	pDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, nCount);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LightClone/Source/GraphicsDevice.h	Fri Sep 09 16:18:41 2011 -0700
@@ -0,0 +1,81 @@
+/*
+ * GraphicsDevice
+ */
+
+#ifndef __GRAPHICSDEVICE_H__
+#define __GRAPHICSDEVICE_H__
+
+#include "Core.h"
+
+/*
+ * GraphicsDevice
+ */
+class GraphicsDevice
+{
+	/*
+	 * pDirect3D
+	 */
+	IDirect3D9* pDirect3D;
+
+	/*
+	 * pDevice
+	 */
+	IDirect3DDevice9* pDevice;
+
+	/*
+	 * kParameters
+	 */
+	D3DPRESENT_PARAMETERS kParameters;
+
+	/*
+	 * GraphicsDevice
+	 */
+	GraphicsDevice(IDirect3D9* pContext, IDirect3DDevice9* pInstance, const D3DPRESENT_PARAMETERS& kPresentParameters);
+
+public:
+
+	/*
+	 * ~GraphicsDevice
+	 */
+	~GraphicsDevice();
+
+	/*
+	 * Create
+	 */
+	static ErrorCode Create(HWND kWindow, uint32 nWidth, uint32 nHeight, GraphicsDevice** pDevice);
+
+	/*
+	 * Destroy
+	 */
+	static void Destroy(GraphicsDevice*& pDevice);
+
+	/*
+	 * GetViewport
+	 */
+	ErrorCode GetViewport(D3DVIEWPORT9* pViewport);
+
+	/*
+	 * Begin
+	 */
+	void Begin(uint32 nColor = 0, float fDepth = 1.0f, uint32 nStencil = 0);
+
+	/*
+	 * End
+	 */
+	void End();
+
+	/*
+	 * DrawTriangles
+	 */
+	void DrawTriangles(IDirect3DVertexDeclaration9* pDeclaration, IDirect3DVertexBuffer9* pBuffer, uint32 nSize, uint32 nCount);
+
+	/*
+	 * operator IDirect3DDevice9*
+	 */
+	operator IDirect3DDevice9*()
+	{
+		return pDevice;
+	}
+};
+
+#endif //__GRAPHICSDEVICE_H__
--- a/LightClone/Source/Interface.cpp	Fri Sep 09 13:21:22 2011 -0700
+++ b/LightClone/Source/Interface.cpp	Fri Sep 09 16:18:41 2011 -0700
@@ -59,30 +59,30 @@
 /*
  * Initialize
  */
-ErrorCode Interface::Initialize(RenderContext& kContext)
+ErrorCode Interface::Initialize(ResourceManager* pManager)
 {
-	ErrorCode eCode = kContext.CreateEffectFromFile("Data\\Shaders\\TexturedQuad.fx", &pEffect);
+	ErrorCode eCode = pManager->CreateEffectFromFile("Data\\Shaders\\TexturedQuad.fx", &pEffect);
 	if(eCode != Error_Success)
 	{
 		Terminate();
 		return Error_Fail;
 	}
 
-	eCode = kContext.CreateFontFromName("Courier New", 18, FW_BOLD, &pFont);
+	eCode = pManager->CreateFontFromName("Courier New", 18, FW_BOLD, &pFont);
 	if(eCode != Error_Success)
 	{
 		Terminate();
 		return Error_Fail;
 	}
 
-	eCode = kContext.CreateVertexBuffer(TrianglesPerFace * VerticesPerTriangle * sizeof(Vertex::Quad), D3DUSAGE_WRITEONLY, D3DPOOL_MANAGED, &pVertexBuffer);
+	eCode = pManager->CreateVertexBuffer(TrianglesPerFace * VerticesPerTriangle * sizeof(Vertex::Quad), D3DUSAGE_WRITEONLY, D3DPOOL_MANAGED, &pVertexBuffer);
 	if(eCode != Error_Success)
 	{
 		Terminate();
 		return Error_Fail;
 	}
 
-	eCode = kContext.CreateTextureFromFile("Data\\Textures\\Background00.tga", &pBackgroundTexture);
+	eCode = pManager->CreateTextureFromFile("Data\\Textures\\Background00.tga", &pBackgroundTexture);
 	if(eCode != Error_Success)
 	{
 		Terminate();
@@ -91,7 +91,7 @@
 
 	for(uint32 i = 0; i < Action_Count; ++i)
 	{
-		eCode = kContext.CreateTextureFromFile(pActionTextureName[i], pActionTexture + i);
+		eCode = pManager->CreateTextureFromFile(pActionTextureName[i], pActionTexture + i);
 		if(eCode != Error_Success)
 		{
 			Terminate();
@@ -101,7 +101,7 @@
 
 	for(uint32 i = 0; i < sizeof(pControlTextureName) / sizeof(pControlTextureName[0]); ++i)
 	{
-		eCode = kContext.CreateTextureFromFile(pControlTextureName[i], pControlTexture + i);
+		eCode = pManager->CreateTextureFromFile(pControlTextureName[i], pControlTexture + i);
 		if(eCode != Error_Success)
 		{
 			Terminate();
@@ -111,7 +111,7 @@
 
 	for(uint32 i = 0; i < sizeof(pArrowTextureName) / sizeof(pArrowTextureName[0]); ++i)
 	{
-		eCode = kContext.CreateTextureFromFile(pArrowTextureName[i], pArrowTexture + i);
+		eCode = pManager->CreateTextureFromFile(pArrowTextureName[i], pArrowTexture + i);
 		if(eCode != Error_Success)
 		{
 			Terminate();
@@ -119,21 +119,21 @@
 		}
 	}
 
-	eCode = kContext.CreateTextureFromFile("Data\\Textures\\Dialog1.tga", &pDialog1Texture);
+	eCode = pManager->CreateTextureFromFile("Data\\Textures\\Dialog1.tga", &pDialog1Texture);
 	if(eCode != Error_Success)
 	{
 		Terminate();
 		return Error_Fail;
 	}
 
-	eCode = kContext.CreateTextureFromFile("Data\\Textures\\Dialog2.tga", &pDialog2Texture);
+	eCode = pManager->CreateTextureFromFile("Data\\Textures\\Dialog2.tga", &pDialog2Texture);
 	if(eCode != Error_Success)
 	{
 		Terminate();
 		return Error_Fail;
 	}
 
-	eCode = kContext.CreateTextureFromFile("Data\\Textures\\Arrow.tga", &pCursorTexture);
+	eCode = pManager->CreateTextureFromFile("Data\\Textures\\Arrow.tga", &pCursorTexture);
 	if(eCode != Error_Success)
 	{
 		Terminate();
@@ -221,8 +221,9 @@
 /*
  * Render
  */
-void Interface::Render(RenderContext& kContext, Model* pModel)
+void Interface::Render(RenderContext& kContext)
 {
+	/*
 	if(pModel->nGameState != GameState_Exit)
 	{
 		kContext.SetupCamera2D(pEffect);
@@ -250,7 +251,6 @@
 			sprintf_s(kMessage, "Congratulations!\nYou have completed level %d", pModel->nCurrentLevel);
 
 			RenderDialog(kContext, pModel, kMessage, "Ok");
-			//RenderLevelDialog(kContext, pModel);
 		}
 		else
 
@@ -259,7 +259,6 @@
 			const char* pMessage = "Congratulations!\nYou have won the game!";
 
 			RenderDialog(kContext, pModel, pMessage, "Ok");
-			//RenderGameOverDialog(kContext, pModel);
 		}
 		else
 
@@ -268,7 +267,6 @@
 			const char* pMessage = "Are you sure you want to quit the game?";
 
 			RenderDialog(kContext, pModel, pMessage, "Yes", "No");
-			//RenderConfirmExitDialog(kContext, pModel);
 		}
 
 		RenderCursor(kContext, pModel);
@@ -276,6 +274,7 @@
 		pEffect->EndPass();
 		pEffect->End();
 	}
+	*/
 }
 
 /*
@@ -306,8 +305,9 @@
 /*
  * RenderBackground
  */
-void Interface::RenderBackground(RenderContext& kContext, Model* pModel)
+void Interface::RenderBackground(RenderContext& kContext)
 {
+	/*
 	const D3DXVECTOR4 kColorVector(1.0f, 1.0f, 1.0f, 1.0f);
 
 	D3DSURFACE_DESC kDescriptor;
@@ -331,13 +331,15 @@
 	pEffect->CommitChanges();
 
 	kContext.DrawTriangles(Vertex::Quad::Declaration, pVertexBuffer, sizeof(Vertex::Quad), TrianglesPerFace);
+	*/
 }
 
 /*
  * RenderToolbar
  */
-void Interface::RenderToolbar(RenderContext& kContext, Model* pModel)
+void Interface::RenderToolbar(RenderContext& kContext)
 {
+	/*
 	const D3DXVECTOR4 kColorVector(1.0f, 1.0f, 1.0f, 1.0f);
 
 	for(uint32 i = 0; i < Action_Count; ++i)
@@ -360,13 +362,15 @@
 
 		kContext.DrawTriangles(Vertex::Quad::Declaration, pVertexBuffer, sizeof(Vertex::Quad), TrianglesPerFace);
 	}
+	*/
 }
 
 /*
  * RenderMain
  */
-void Interface::RenderMain(RenderContext& kContext, Model* pModel)
+void Interface::RenderMain(RenderContext& kContext)
 {
+	/*
 	Code* pCode = pModel->GetFunction(0);
 	if(pCode)
 	{
@@ -406,13 +410,15 @@
 
 		pFont->DrawTextA(NULL, "Main", -1, &kRectangle, 0, D3DCOLOR_XRGB(0, 0, 0));
 	}
+	*/
 }
 
 /*
  * RenderFunctions
  */
-void Interface::RenderFunctions(RenderContext& kContext, Model* pModel)
+void Interface::RenderFunctions(RenderContext& kContext)
 {
+	/*
 	Code* pCode = pModel->GetFunction(pModel->nCurrentFunction + 1);
 	if(pCode)
 	{
@@ -484,13 +490,15 @@
 
 		pFont->DrawTextA(NULL, kBuffer, -1, &kRectangle, 0, D3DCOLOR_XRGB(0, 0, 0));
 	}
+	*/
 }
 
 /*
  * RenderControls
  */
-void Interface::RenderControls(RenderContext& kContext, Model* pModel)
+void Interface::RenderControls(RenderContext& kContext)
 {
+	/*
 	const D3DXVECTOR4 kColorVector(1.0f, 1.0f, 1.0f, 1.0f);
 
 	for(uint32 i = 0; i < pModel->kControls.GetSize(); ++i)
@@ -513,14 +521,15 @@
 
 		kContext.DrawTriangles(Vertex::Quad::Declaration, pVertexBuffer, sizeof(Vertex::Quad), TrianglesPerFace);
 	}
+	*/
 }
 
 /*
  * RenderDialog
  */
-void Interface::RenderDialog(RenderContext& kContext, Model* pModel, const char* pMessage, const char* pChoiceA, const char* pChoiceB)
-//void Interface::RenderDialog(RenderContext& kContext, const Dialog& kDialog)
+void Interface::RenderDialog(RenderContext& kContext, const char* pMessage, const char* pChoiceA, const char* pChoiceB)
 {
+	/*
 	if(pMessage && pChoiceA)
 	{
 		IDirect3DTexture9* pDialogTexture = pChoiceB ? pDialog2Texture : pDialog1Texture;
@@ -549,27 +558,6 @@
 			kContext.DrawTriangles(Vertex::Quad::Declaration, pVertexBuffer, sizeof(Vertex::Quad), TrianglesPerFace);
 		}
 
-		/*
-		for(uint32 i = 0; i < kDialog.nButtonCount; ++i)
-		{
-			D3DXMATRIX kScale;
-			D3DXMatrixScaling(&kScale, fSizeX, fSizeY, 1.0f);
-
-			D3DXMATRIX kTranslate;
-			D3DXMatrixTranslation(&kTranslate, -0.5f * fSizeX, 0.5f * fSizeY, 0.0f);
-
-			D3DXMATRIX kWorldMatrix;
-			D3DXMatrixMultiply(&kWorldMatrix, &kScale, &kTranslate);
-
-			pEffect->SetMatrix(pEffect->GetParameterByName(NULL, "kWorld"), &kWorldMatrix);
-			pEffect->SetTexture(pEffect->GetParameterByName(NULL, "kTexture"), pDialogTexture);
-			pEffect->SetVector(pEffect->GetParameterByName(NULL, "kColor"), &D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f));
-			pEffect->CommitChanges();
-
-			kContext.DrawTriangles(Vertex::Quad::Declaration, pVertexBuffer, sizeof(Vertex::Quad), TrianglesPerFace);
-		}
-		*/
-
 		#if 0
 		{
 			D3DXMATRIX kScale;
@@ -611,13 +599,15 @@
 		{
 		}
 	}
+	*/
 }
 
 /*
  * RenderCursor
  */
-void Interface::RenderCursor(RenderContext& kContext, Model* pModel)
+void Interface::RenderCursor(RenderContext& kContext)
 {
+	/*
 	const D3DXVECTOR4 kColorVector(1.0f, 1.0f, 1.0f, 1.0f);
 
 	if(pCursorTexture)
@@ -671,4 +661,5 @@
 
 		kContext.DrawTriangles(Vertex::Quad::Declaration, pVertexBuffer, sizeof(Vertex::Quad), TrianglesPerFace);
 	}
+	*/
 }
--- a/LightClone/Source/Interface.h	Fri Sep 09 13:21:22 2011 -0700
+++ b/LightClone/Source/Interface.h	Fri Sep 09 16:18:41 2011 -0700
@@ -7,7 +7,7 @@
 
 #include "Core.h"
 #include "RenderContext.h"
-#include "Model.h"
+#include "ResourceManager.h"
 
 /*
  * Interface
@@ -76,7 +76,7 @@
 	/*
 	 * Initialize
 	 */
-	ErrorCode Initialize(RenderContext& kContext);
+	ErrorCode Initialize(ResourceManager* pManager);
 
 	/* 
 	 * Terminate
@@ -86,7 +86,7 @@
 	/*
 	 * Render
 	 */
-	void Render(RenderContext& kContext, Model* pModel);
+	void Render(RenderContext& kContext);
 
 private:
 
@@ -98,52 +98,37 @@
 	/*
 	 * RenderBackground
 	 */
-	void RenderBackground(RenderContext& kContext, Model* pModel);
+	void RenderBackground(RenderContext& kContext);
 
 	/*
 	 * RenderToolbar
 	 */
-	void RenderToolbar(RenderContext& kContext, Model* pModel);
+	void RenderToolbar(RenderContext& kContext);
 
 	/*
 	 * RenderMain
 	 */
-	void RenderMain(RenderContext& kContext, Model* pModel);
+	void RenderMain(RenderContext& kContext);
 
 	/*
 	 * RenderFunctions
 	 */
-	void RenderFunctions(RenderContext& kContext, Model* pModel);
+	void RenderFunctions(RenderContext& kContext);
 
 	/*
 	 * RenderControls
 	 */
-	void RenderControls(RenderContext& kContext, Model* pModel);
-
-	/*
-	 * RenderLevelDialog
-	 */
-	void RenderLevelDialog(RenderContext& kContext, Model* pModel);
-
-	/*
-	 * RenderGameOverDialog
-	 */
-	void RenderGameOverDialog(RenderContext& kContext, Model* pModel);
-
-	/*
-	 * RenderConfirmExitDialog
-	 */
-	void RenderConfirmExitDialog(RenderContext& kContext, Model* pModel);
+	void RenderControls(RenderContext& kContext);
 
 	/*
 	 * RenderDialog
 	 */
-	void RenderDialog(RenderContext& kContext, Model* pModel, const char* pMessage, const char* pChoiceA, const char* pChoiceB = NULL);
+	void RenderDialog(RenderContext& kContext, const char* pMessage, const char* pChoiceA, const char* pChoiceB = NULL);
 
 	/*
 	 * RenderCursor
 	 */
-	void RenderCursor(RenderContext& kContext, Model* pModel);
+	void RenderCursor(RenderContext& kContext);
 };
 
 #endif //__INTERFACE_H__
--- a/LightClone/Source/Mediator.cpp	Fri Sep 09 13:21:22 2011 -0700
+++ b/LightClone/Source/Mediator.cpp	Fri Sep 09 16:18:41 2011 -0700
@@ -3,7 +3,10 @@
  */
 
 #include "Mediator.h"
-#include <windows.h>
+#include "VertexTypes.h"
+//#include <windows.h>
+
+#pragma warning(disable:4355)
 
 /*
  * fUpdatePeriod
@@ -13,8 +16,65 @@
 /*
  * Mediator
  */
-Mediator::Mediator()
+Mediator::Mediator() : kWindow(this), kToolbar(8), kMain(16), kFunction(8), kControls(4)
 {
+	nGameState				= GameState_Active;
+	nSimulationState		= SimulationState_Idle;
+	pFunction				= 0;
+	nCurrentFunction		= 0;
+	nCurrentLevel			= 0;
+
+	kArrowBounds[0]			= Rectangle2(1206.0f + 0 * 16.0f, 473.0f + 0 * 54.0f, 16.0f, 16.0f);
+	kArrowBounds[1]			= Rectangle2(1206.0f + 2 * 16.0f, 473.0f + 0 * 54.0f, 16.0f, 16.0f);
+
+	kDialog1Bounds[0]		= Rectangle2(567.0f, 412.0f, 150.0f, 60.0f);
+
+	kDialog2Bounds[0]		= Rectangle2(1023.0f + 2.5f * 54.0f, 638.0f + 0 * 00.0f, 48.0f, 48.0f);
+	kDialog2Bounds[1]		= Rectangle2(1023.0f + 2.5f * 54.0f, 638.0f + 0 * 00.0f, 48.0f, 48.0f);
+
+	kToolbar.Add(1023.0f + 0 * 54.0f, 85.0f + 0 * 54.0f, 48.0f, 48.0f);
+	kToolbar.Add(1023.0f + 1 * 54.0f, 85.0f + 0 * 54.0f, 48.0f, 48.0f);
+	kToolbar.Add(1023.0f + 2 * 54.0f, 85.0f + 0 * 54.0f, 48.0f, 48.0f);
+	kToolbar.Add(1023.0f + 3 * 54.0f, 85.0f + 0 * 54.0f, 48.0f, 48.0f);
+	kToolbar.Add(1023.0f + 0 * 54.0f, 85.0f + 1 * 54.0f, 48.0f, 48.0f);
+	kToolbar.Add(1023.0f + 1 * 54.0f, 85.0f + 1 * 54.0f, 48.0f, 48.0f);
+	kToolbar.Add(1023.0f + 2 * 54.0f, 85.0f + 1 * 54.0f, 48.0f, 48.0f);
+	kToolbar.Add(1023.0f + 3 * 54.0f, 85.0f + 1 * 54.0f, 48.0f, 48.0f);
+
+	kMain.Add(1023.0f + 0 * 54.0f, 238.0f + 0 * 54.0f, 48.0f, 48.0f);
+	kMain.Add(1023.0f + 1 * 54.0f, 238.0f + 0 * 54.0f, 48.0f, 48.0f);
+	kMain.Add(1023.0f + 2 * 54.0f, 238.0f + 0 * 54.0f, 48.0f, 48.0f);
+	kMain.Add(1023.0f + 3 * 54.0f, 238.0f + 0 * 54.0f, 48.0f, 48.0f);
+	kMain.Add(1023.0f + 0 * 54.0f, 238.0f + 1 * 54.0f, 48.0f, 48.0f);
+	kMain.Add(1023.0f + 1 * 54.0f, 238.0f + 1 * 54.0f, 48.0f, 48.0f);
+	kMain.Add(1023.0f + 2 * 54.0f, 238.0f + 1 * 54.0f, 48.0f, 48.0f);
+	kMain.Add(1023.0f + 3 * 54.0f, 238.0f + 1 * 54.0f, 48.0f, 48.0f);
+	kMain.Add(1023.0f + 0 * 54.0f, 238.0f + 2 * 54.0f, 48.0f, 48.0f);
+	kMain.Add(1023.0f + 1 * 54.0f, 238.0f + 2 * 54.0f, 48.0f, 48.0f);
+	kMain.Add(1023.0f + 2 * 54.0f, 238.0f + 2 * 54.0f, 48.0f, 48.0f);
+	kMain.Add(1023.0f + 3 * 54.0f, 238.0f + 2 * 54.0f, 48.0f, 48.0f);
+	kMain.Add(1023.0f + 0 * 54.0f, 238.0f + 3 * 54.0f, 48.0f, 48.0f);
+	kMain.Add(1023.0f + 1 * 54.0f, 238.0f + 3 * 54.0f, 48.0f, 48.0f);
+	kMain.Add(1023.0f + 2 * 54.0f, 238.0f + 3 * 54.0f, 48.0f, 48.0f);
+	kMain.Add(1023.0f + 3 * 54.0f, 238.0f + 3 * 54.0f, 48.0f, 48.0f);
+
+	kFunction.Add(1023.0f + 0 * 54.0f, 501.0f + 0 * 54.0f, 48.0f, 48.0f);
+	kFunction.Add(1023.0f + 1 * 54.0f, 501.0f + 0 * 54.0f, 48.0f, 48.0f);
+	kFunction.Add(1023.0f + 2 * 54.0f, 501.0f + 0 * 54.0f, 48.0f, 48.0f);
+	kFunction.Add(1023.0f + 3 * 54.0f, 501.0f + 0 * 54.0f, 48.0f, 48.0f);
+	kFunction.Add(1023.0f + 0 * 54.0f, 501.0f + 1 * 54.0f, 48.0f, 48.0f);
+	kFunction.Add(1023.0f + 1 * 54.0f, 501.0f + 1 * 54.0f, 48.0f, 48.0f);
+	kFunction.Add(1023.0f + 2 * 54.0f, 501.0f + 1 * 54.0f, 48.0f, 48.0f);
+	kFunction.Add(1023.0f + 3 * 54.0f, 501.0f + 1 * 54.0f, 48.0f, 48.0f);
+
+	kControls.Add(1023.0f + 0.5f * 54.0f, 638.0f + 0 * 00.0f, 48.0f, 48.0f);
+	kControls.Add(1023.0f + 1.5f * 54.0f, 638.0f + 0 * 00.0f, 48.0f, 48.0f);
+	kControls.Add(1023.0f + 2.5f * 54.0f, 638.0f + 0 * 00.0f, 48.0f, 48.0f);
+
+	pGraphicsDevice			= NULL;
+	pBlockEffect			= NULL;
+	pBlockVertexBuffer		= NULL;
+	pBlockTexture			= NULL;
 }
 
 /*
@@ -29,7 +89,7 @@
 
 		kClock.Reset();
 
-		while(pModel->nGameState != GameState_Exit)
+		while(nGameState != GameState_Exit)
 		//while(pWorld->IsActive())
 		{
 			ProcessMessages();
@@ -54,27 +114,152 @@
 }
 
 /*
+ * 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)
+	{
+		eCode = GraphicsDevice::Create(kWindow, ScreenSizeX, ScreenSizeY, &pGraphicsDevice);
+		if(eCode == Error_Success)
+		{
+			eCode = kRenderContext.Initialize(pGraphicsDevice);
+			if(eCode == Error_Success)
+			{
+				//NOTE: This seems like a big hack.  Why does the resource manager
+				//		need to be aware of the graphics device just to load resources?
+				//		This could be replaced by a "resource loader" which is owned
+				//		by the graphics device and registered to the resource manager
+				eCode = kResourceManager.Initialize(pGraphicsDevice);
+				if(eCode == Error_Success)
+				{
+					eCode = kInputManager.Initialize(kWindow);
+					if(eCode == Error_Success)
+					{
+						eCode = kWorld.Initialize(&kResourceManager);
+						if(eCode == Error_Success)
+						{
+							eCode = kInputManager.Register(&kWorld);
+							if(eCode == Error_Success)
+							{
+								kWorld.Activate();
+							}
+						}
+					}
+				}
+			}
+		}
+	}
+	*/
+
+	/*
 	pModel		= new Model();
 	pView		= new View(pModel);
 	pController = new Controller(pModel, pView);
-	//pWorld = new World();
 
 	ErrorCode eCode = pView->Initialize();
-	//eCode = pWorld->Initialize();
 	if(eCode == Error_Success)
 	{
-		//pWorld->Activate();
-
 		eCode = pController->Initialize();
 		if(eCode == Error_Success)
 		{
-			pModel->nCurrentLevel	= 0;
-			pModel->nGameState		= GameState_LoadMap;
+			nCurrentLevel	= 0;
+			nGameState		= GameState_LoadMap;
+		}
+	}
+	*/
+
+	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 = kResourceManager.CreateEffectFromFile("Data\\Shaders\\Environment.fx", &pBlockEffect);
+		if(eCode != Error_Success)
+		{
+			Terminate();
+			return Error_Fail;
+		}
+
+		eCode = kResourceManager.CreateTextureFromFile("Data\\Textures\\Block02.tga", &pBlockTexture);
+		if(eCode != Error_Success)
+		{
+			Terminate();
+			return Error_Fail;
+		}
+
+		eCode = kResourceManager.CreateVertexBuffer(VerticesPerBlock * sizeof(Vertex::Block), D3DUSAGE_WRITEONLY, D3DPOOL_MANAGED, &pBlockVertexBuffer);
+		if(eCode != Error_Success)
+		{
+			Terminate();
+			return Error_Fail;
+		}
+
+		eCode = SetupVertexBuffer();
+		if(eCode != Error_Success)
+		{
+			Terminate();
+			return Error_Fail;
+		}
+
+		eCode = kInterface.Initialize(&kResourceManager);
+		if(eCode != Error_Success)
+		{
+			Terminate();
+			return Error_Fail;
+		}
+
+		nCurrentLevel	= 0;
+		nGameState		= GameState_LoadMap;
 	}
 	
 	return eCode;
@@ -85,34 +270,31 @@
  */
 void Mediator::Terminate()
 {
-	/*
-	if(pWorld)
-	{
-		pWorld->Terminate();
+	kInterface.Terminate();
 
-		delete pWorld;
-		pWorld = NULL;
-	}
-	*/
-
-	if(pController)
+	if(pBlockVertexBuffer)
 	{
-		pController->Terminate();
-
-		delete pController;
-		pController = 0;
+		pBlockVertexBuffer->Release();
+		pBlockVertexBuffer = NULL;
 	}
 
-	if(pView)
+	if(pBlockTexture)
 	{
-		pView->Terminate();
-
-		delete pView;
-		pView = 0;
+		pBlockTexture->Release();
+		pBlockTexture = NULL;
 	}
 
-	delete pModel;
-	pModel = 0;
+	if(pBlockEffect)
+	{
+		pBlockEffect->Release();
+		pBlockEffect = 0;
+	}
+
+	kContext.Terminate();
+	kInputManager.Terminate();
+	kResourceManager.Terminate();
+
+	GraphicsDevice::Destroy(pGraphicsDevice);
 }
 
 /*
@@ -120,8 +302,35 @@
  */
 void Mediator::Update(float fElapsed)
 {
-	pController->Update(fElapsed);
-	//pWorld->Update(fElapsed);
+	ProcessInput(fElapsed);
+
+	if(nGameState == GameState_LoadMap)
+	{
+		char kBuffer[256];
+		sprintf_s(kBuffer, "Data\\Maps\\Map%02d.map", nCurrentLevel++);
+
+		nGameState = Load(kBuffer) ? GameState_Active : GameState_Over;
+	}
+	else
+
+	if(nGameState == GameState_Active)
+	{
+		if(nSimulationState == SimulationState_Active)
+		{
+			if(kBot.Update(fElapsed))
+			{
+				if(kEnvironment.RequirementsMet())
+				{
+					kDialog.Reset("Some message");
+					kDialog.AddButton(DialogButton_Ok, "Ok", 0.0f, 0.0f, 0.0f, 0.0f);
+
+					nGameState = GameState_Complete;
+				}
+			}
+		}
+	}
+
+	//kWorld.Update(fElapsed);
 }
 
 /*
@@ -129,8 +338,19 @@
  */
 void Mediator::Render()
 {
-	pView->Render();
-	//pWorld->Render();
+	const uint32 nColor = D3DCOLOR_XRGB(32, 32, 32);
+
+	kContext.Begin(nColor);
+
+	if(nGameState >= GameState_Active)
+	{
+		Render3D();
+		Render2D();
+	}
+
+	kContext.End();
+
+	//kWorld.Render(kRenderContext);
 }
 
 /*
@@ -144,12 +364,615 @@
 	{
 		if(kMessage.message == WM_QUIT)
 		{
-			pModel->nGameState = GameState_Exit;
-			//pWorld->Deactivate();
+			nGameState = GameState_Exit;
+			//kWorld.Deactivate();
 			break;
 		}
 
 		TranslateMessage(&kMessage);
 		DispatchMessage(&kMessage);
 	}
-}
\ No newline at end of file
+}
+
+/*
+ * Load
+ */
+bool Mediator::Load(const char* pName)
+{
+	ErrorCode eCode = kLoader.Load(pName);
+	if(eCode == Error_Success)
+	{
+		if(pFunction)
+		{
+			delete[] pFunction;
+			pFunction = NULL;
+		}
+
+		const Size& kSize = kLoader.GetSize();
+
+		eCode = kEnvironment.Initialize(kSize.X, kSize.Y);
+		if(eCode == Error_Success)
+		{
+			for(uint32 nY = 0; nY < kSize.Y; ++nY)
+			{
+				for(uint32 nX = 0; nX < kSize.X; ++nX)
+				{
+					kEnvironment.SetType(nX, nY, kLoader.GetTowerType(nX, nY));
+					kEnvironment.SetAltitude(nX, nY, kLoader.GetTowerHeight(nX, nY));
+				}
+			}
+
+			kBot.Initialize(&kEnvironment);
+			kBot.kPosition	= kLoader.GetInitialPosition();
+			kBot.kDirection	= kLoader.GetInitialDirection();
+
+			const uint32 nCount = kLoader.GetFunctionCount();
+
+			pFunction = new Code[nCount + 1];
+			pFunction[0].Initialize(MainFunctionLength);
+			pFunction[0].Clear();
+
+			for(uint32 i = 0; i < nCount; ++i)
+			{
+				const uint32 nLength = kLoader.GetFunctionLength(i);
+
+				pFunction[i + 1].Initialize(nLength);
+				pFunction[i + 1].Clear();
+			}
+		}
+	}
+
+	return eCode == Error_Success;
+}
+
+/*
+ * ResetEnvironment
+ */
+void Mediator::ResetEnvironment()
+{
+	kEnvironment.Reset();
+}
+
+/*
+ * ResetBot
+ */
+void Mediator::ResetBot()
+{
+	kBot.Reset();
+	kBot.kPosition	= kLoader.GetInitialPosition();
+	kBot.kDirection	= kLoader.GetInitialDirection();
+}
+
+/*
+ * ResetCode
+ */
+void Mediator::ClearCode()
+{
+	for(uint32 i = 0; i < kLoader.GetFunctionCount(); ++i)
+	{
+		pFunction[i].Clear();
+	}
+}
+
+/*
+ * SetupVertexBuffer
+ */
+ErrorCode Mediator::SetupVertexBuffer()
+{
+	Vertex::Block* pVertices = NULL;
+	
+	HRESULT hResult = pBlockVertexBuffer->Lock(0, 0, (void**)&pVertices, D3DLOCK_DISCARD);
+	if(FAILED(hResult))
+	{
+		return Error_Fail;
+	}
+
+	const float fU1	= 0.66f;
+	const float fV1 = 0.66f;
+
+	// front
+	pVertices[0]	= Vertex::Block(-0.5f, 0.0f, -0.5f, 0.0f, 0.0f, -1.0f, 0.00f, 1.00f);
+	pVertices[1]	= Vertex::Block(-0.5f, 1.0f, -0.5f, 0.0f, 0.0f, -1.0f, 0.00f, 0.66f);
+	pVertices[2]	= Vertex::Block(+0.5f, 1.0f, -0.5f, 0.0f, 0.0f, -1.0f, 1.00f, 0.66f);
+	pVertices[3]	= Vertex::Block(-0.5f, 0.0f, -0.5f, 0.0f, 0.0f, -1.0f, 0.00f, 1.00f);
+	pVertices[4]	= Vertex::Block(+0.5f, 1.0f, -0.5f, 0.0f, 0.0f, -1.0f, 1.00f, 0.66f);
+	pVertices[5]	= Vertex::Block(+0.5f, 0.0f, -0.5f, 0.0f, 0.0f, -1.0f, 1.00f, 1.00f);
+	// back
+	pVertices[6]	= Vertex::Block(+0.5f, 0.0f, +0.5f, 0.0f, 0.0f, +1.0f, 0.00f, 1.00f);
+	pVertices[7]	= Vertex::Block(+0.5f, 1.0f, +0.5f, 0.0f, 0.0f, +1.0f, 0.00f, 0.66f);
+	pVertices[8]	= Vertex::Block(-0.5f, 1.0f, +0.5f, 0.0f, 0.0f, +1.0f, 1.00f, 0.66f);
+	pVertices[9]	= Vertex::Block(+0.5f, 0.0f, +0.5f, 0.0f, 0.0f, +1.0f, 0.00f, 1.00f);
+	pVertices[10]	= Vertex::Block(-0.5f, 1.0f, +0.5f, 0.0f, 0.0f, +1.0f, 1.00f, 0.66f);
+	pVertices[11]	= Vertex::Block(-0.5f, 0.0f, +0.5f, 0.0f, 0.0f, +1.0f, 1.00f, 1.00f);
+	// left
+	pVertices[12]	= Vertex::Block(-0.5f, 0.0f, +0.5f, -1.0f, 0.0f, 0.0f, 0.00f, 1.00f);
+	pVertices[13]	= Vertex::Block(-0.5f, 1.0f, +0.5f, -1.0f, 0.0f, 0.0f, 0.00f, 0.66f);
+	pVertices[14]	= Vertex::Block(-0.5f, 1.0f, -0.5f, -1.0f, 0.0f, 0.0f, 1.00f, 0.66f);
+	pVertices[15]	= Vertex::Block(-0.5f, 0.0f, +0.5f, -1.0f, 0.0f, 0.0f, 0.00f, 1.00f);
+	pVertices[16]	= Vertex::Block(-0.5f, 1.0f, -0.5f, -1.0f, 0.0f, 0.0f, 1.00f, 0.66f);
+	pVertices[17]	= Vertex::Block(-0.5f, 0.0f, -0.5f, -1.0f, 0.0f, 0.0f, 1.00f, 1.00f);
+	// right
+	pVertices[18]	= Vertex::Block(+0.5f, 0.0f, -0.5f, +1.0f, 0.0f, 0.0f, 0.00f, 1.00f);
+	pVertices[19]	= Vertex::Block(+0.5f, 1.0f, -0.5f, +1.0f, 0.0f, 0.0f, 0.00f, 0.66f);
+	pVertices[20]	= Vertex::Block(+0.5f, 1.0f, +0.5f, +1.0f, 0.0f, 0.0f, 1.00f, 0.66f);
+	pVertices[21]	= Vertex::Block(+0.5f, 0.0f, -0.5f, +1.0f, 0.0f, 0.0f, 0.00f, 1.00f);
+	pVertices[22]	= Vertex::Block(+0.5f, 1.0f, +0.5f, +1.0f, 0.0f, 0.0f, 1.00f, 0.66f);
+	pVertices[23]	= Vertex::Block(+0.5f, 0.0f, +0.5f, +1.0f, 0.0f, 0.0f, 1.00f, 1.00f);
+	// top
+	pVertices[24]	= Vertex::Block(-0.5f, 1.0f, -0.5f, 0.0f, +1.0f, 0.0f, 0.00f, 0.66f);
+	pVertices[25]	= Vertex::Block(-0.5f, 1.0f, +0.5f, 0.0f, +1.0f, 0.0f, 0.00f, 0.00f);
+	pVertices[26]	= Vertex::Block(+0.5f, 1.0f, +0.5f, 0.0f, +1.0f, 0.0f, 1.00f, 0.00f);
+	pVertices[27]	= Vertex::Block(-0.5f, 1.0f, -0.5f, 0.0f, +1.0f, 0.0f, 0.00f, 0.66f);
+	pVertices[28]	= Vertex::Block(+0.5f, 1.0f, +0.5f, 0.0f, +1.0f, 0.0f, 1.00f, 0.00f);
+	pVertices[29]	= Vertex::Block(+0.5f, 1.0f, -0.5f, 0.0f, +1.0f, 0.0f, 1.00f, 0.66f);
+	// bottom
+	pVertices[30]	= Vertex::Block(-0.5f, 0.0f, +0.5f, 0.0f, -1.0f, 0.0f, 0.00f, 0.66f);
+	pVertices[31]	= Vertex::Block(-0.5f, 0.0f, -0.5f, 0.0f, -1.0f, 0.0f, 0.00f, 0.00f);
+	pVertices[32]	= Vertex::Block(+0.5f, 0.0f, -0.5f, 0.0f, -1.0f, 0.0f, 1.00f, 0.00f);
+	pVertices[33]	= Vertex::Block(-0.5f, 0.0f, +0.5f, 0.0f, -1.0f, 0.0f, 0.00f, 0.66f);
+	pVertices[34]	= Vertex::Block(+0.5f, 0.0f, -0.5f, 0.0f, -1.0f, 0.0f, 1.00f, 0.00f);
+	pVertices[35]	= Vertex::Block(+0.5f, 0.0f, +0.5f, 0.0f, -1.0f, 0.0f, 1.00f, 0.66f);
+
+	pBlockVertexBuffer->Unlock();
+
+	return Error_Success;
+}
+
+/*
+ * Render3D
+ */
+void Mediator::Render3D()
+{
+	const D3DXVECTOR3& kScale	= kEnvironment.GetScale();
+	const uint32 nSizeX			= kEnvironment.GetWidth();
+	const uint32 nSizeY			= kEnvironment.GetHeight();
+
+	//const D3DXVECTOR3 kTarget(0.5f * (nSizeX - 1) * kScale.x, 0.0f, 0.5f * (nSizeY - 1) * kScale.z);
+	const D3DXVECTOR3 kTarget(0.0f, 0.0f, 0.0f);
+
+	kContext.SetupCamera3D(pBlockEffect, kCameraController.GetLocation(kTarget), kTarget);
+
+	RenderEnvironment(&kEnvironment);
+	RenderBot(&kEnvironment, &kBot);
+}
+
+/* 
+ * Render2D
+ */
+void Mediator::Render2D()
+{
+	kInterface.Render(kContext);
+}
+
+/*
+ * 	RenderEnvironment
+ */
+void Mediator::RenderEnvironment(Environment* pEnvironment)
+{
+	const D3DXVECTOR3& kScale = pEnvironment->GetScale();
+
+	uint32 nPasses = 0;
+
+	pBlockEffect->SetTechnique(pBlockEffect->GetTechnique(0));
+	pBlockEffect->Begin(&nPasses, 0);
+	pBlockEffect->BeginPass(0);
+
+	D3DXMATRIX kScaleMatrix;
+	D3DXMatrixScaling(&kScaleMatrix, kScale.x, kScale.y, kScale.z);
+
+	const uint32 nSizeX				= pEnvironment->GetWidth();
+	const uint32 nSizeY				= pEnvironment->GetHeight();
+	
+	const D3DXMATRIX& kProjection	= kContext.GetProjection();
+	const D3DXMATRIX& kView			= kContext.GetView();
+
+	D3DVIEWPORT9 kViewport;
+	kContext.GetViewport(&kViewport);
+
+	D3DXVECTOR3 kCenterWorld(0.0f, 0.0f, 0.0f);
+	const float fMagicOffset = 400.0f;
+	//const D3DXVECTOR3& kCenterWorld = ComputeOrigin(0.5f * (ScreenSizeX - (304.0f + fMagicOffset)), 0.5f * ScreenSizeY, kViewport, kProjection, kView);
+
+	const float fOffsetX	= -0.5f * (nSizeX * kScale.x);
+	const float fOffsetZ	= -0.5f * (nSizeY * kScale.z);
+
+	for(uint32 nZ = 0; nZ < nSizeY; ++nZ)
+	{
+		for(uint32 nX = 0; nX < nSizeX; ++nX)
+		{
+			uint32 nType	= pEnvironment->GetType(nX, nZ);
+			uint32 nHeight	= pEnvironment->GetAltitude(nX, nZ);
+			uint32 nState	= pEnvironment->GetState(nX, nZ);
+			uint32 nColor	= D3DCOLOR_XRGB(0x80, 0x80, 0x80);
+
+			if(nType == 1)
+			{
+				nColor = nState ? D3DCOLOR_XRGB(0, 0, 255) : D3DCOLOR_XRGB(255, 0, 0);
+			}
+
+			for(uint32 i = 0; i < pEnvironment->GetAltitude(nX, nZ); ++i)
+			{
+				D3DXMATRIX kTranslateMatrix;
+				D3DXMatrixTranslation(&kTranslateMatrix, fOffsetX + (kCenterWorld.x + nX) * kScale.x, i * kScale.y, fOffsetZ + (kCenterWorld.z + nZ) * kScale.z);
+
+				D3DXMATRIX kWorldMatrix;
+				D3DXMatrixMultiply(&kWorldMatrix, &kScaleMatrix, &kTranslateMatrix);
+
+				const float fAlpha	= ((nColor >> 24) & 0xFF) / 255.0f;
+				const float fRed	= ((nColor >> 16) & 0xFF) / 255.0f;
+				const float fGreen	= ((nColor >> 8 ) & 0xFF) / 255.0f;
+				const float fBlue	= ((nColor >> 0 ) & 0xFF) / 255.0f;
+
+				const D3DXVECTOR4 kColorVector(fRed, fGreen, fBlue, fAlpha);
+
+				pBlockEffect->SetMatrix(pBlockEffect->GetParameterByName(NULL, "kWorld"), &kWorldMatrix);
+				pBlockEffect->SetVector(pBlockEffect->GetParameterByName(NULL, "kColor"), &kColorVector);
+				pBlockEffect->SetTexture(pBlockEffect->GetParameterByName(NULL, "kTexture"), pBlockTexture);
+				pBlockEffect->CommitChanges();
+
+				kContext.DrawTriangles(Vertex::Block::Declaration, pBlockVertexBuffer, sizeof(Vertex::Block), FacesPerCube * TrianglesPerFace);
+			}
+		}
+	}
+
+	pBlockEffect->EndPass();
+	pBlockEffect->End();
+
+	if(false) // wireframe
+	{
+		pBlockEffect->SetTechnique(pBlockEffect->GetTechniqueByName("Wire"));
+		pBlockEffect->Begin(&nPasses, 0);
+		pBlockEffect->BeginPass(0);
+
+		for(uint32 nZ = 0; nZ < nSizeY; ++nZ)
+		{
+			for(uint32 nX = 0; nX < nSizeX; ++nX)
+			{
+				for(uint32 i = 0; i < pEnvironment->GetAltitude(nX, nZ); ++i)
+				{
+					D3DXMATRIX kTranslateMatrix;
+					D3DXMatrixTranslation(&kTranslateMatrix, nX * kScale.x, i * kScale.y, nZ * kScale.z);
+
+					D3DXMATRIX kWorldMatrix;
+					D3DXMatrixMultiply(&kWorldMatrix, &kScaleMatrix, &kTranslateMatrix);
+
+					const D3DXVECTOR4 kColorVector(0.0f, 0.0f, 0.0f, 1.0f);
+
+					pBlockEffect->SetMatrix(pBlockEffect->GetParameterByName(NULL, "kWorld"), &kWorldMatrix);
+					pBlockEffect->SetVector(pBlockEffect->GetParameterByName(NULL, "kColor"), &kColorVector);
+					pBlockEffect->SetTexture(pBlockEffect->GetParameterByName(NULL, "kTexture"), pBlockTexture);
+					pBlockEffect->CommitChanges();
+
+					kContext.DrawTriangles(Vertex::Block::Declaration, pBlockVertexBuffer, sizeof(Vertex::Block), FacesPerCube * TrianglesPerFace);
+				}
+			}
+		}
+
+		pBlockEffect->EndPass();
+		pBlockEffect->End();
+	}
+}
+
+/*
+ * RenderBot
+ */
+void Mediator::RenderBot(Environment* pEnvironment, Bot* pBot)
+{
+	const D3DXVECTOR3& kPosition	= pBot->GetWorldPosition();
+	const D3DXVECTOR3& kOrientation	= pBot->GetWorldOrientation();
+
+	const float fOffsetX	= -0.5f * (pEnvironment->GetWidth() * pEnvironment->GetScale().x);
+	const float fOffsetZ	= -0.5f * (pEnvironment->GetHeight() * pEnvironment->GetScale().z);
+
+	uint32 nPasses = 0;
+
+	pBlockEffect->SetTechnique(pBlockEffect->GetTechnique(0));
+	pBlockEffect->Begin(&nPasses, 0);
+	pBlockEffect->BeginPass(0);
+
+	D3DXMATRIX kScale;
+	D3DXMatrixScaling(&kScale, pBot->kSize.x, pBot->kSize.y, pBot->kSize.z);
+
+	D3DXMATRIX kTranslate;
+	D3DXMatrixTranslation(&kTranslate, fOffsetX + kPosition.x, kPosition.y, fOffsetZ + kPosition.z);
+
+	D3DXMATRIX kRotate;
+	D3DXMatrixRotationY(&kRotate, kOrientation.y);
+
+	D3DXMATRIX kTempMatrix;
+	D3DXMATRIX kWorldMatrix;
+	D3DXMatrixMultiply(&kWorldMatrix, D3DXMatrixMultiply(&kTempMatrix, &kScale, &kRotate), &kTranslate);
+
+	const float fAlpha	= ((pBot->nColor >> 24) & 0xFF) / 255.0f;
+	const float fRed	= ((pBot->nColor >> 16) & 0xFF) / 255.0f;
+	const float fGreen	= ((pBot->nColor >> 8 ) & 0xFF) / 255.0f;
+	const float fBlue	= ((pBot->nColor >> 0 ) & 0xFF) / 255.0f;
+
+	const D3DXVECTOR4 kColorVector(fRed, fGreen, fBlue, fAlpha);
+
+	pBlockEffect->SetMatrix(pBlockEffect->GetParameterByName(NULL, "kWorld"), &kWorldMatrix);
+	pBlockEffect->SetVector(pBlockEffect->GetParameterByName(NULL, "kColor"), &kColorVector);
+	pBlockEffect->SetTexture(pBlockEffect->GetParameterByName(NULL, "kTexture"), NULL);
+	pBlockEffect->CommitChanges();
+
+	kContext.DrawTriangles(Vertex::Block::Declaration, pBlockVertexBuffer, sizeof(Vertex::Block), FacesPerCube * TrianglesPerFace);
+
+	pBlockEffect->EndPass();
+	pBlockEffect->End();
+}
+
+/*
+ * ProcessInput
+ */
+void Mediator::ProcessInput(float fElapsed)
+{
+	kInputManager.Update(fElapsed);
+
+	#if defined(_DEBUG)
+	if(kInputManager.IsKeyDown(DIK_LEFT))
+	{
+		//pView->UpdateCameraYaw(0.01f);
+		kCameraController.Yaw(0.01f);
+	}
+	else
+
+	if(kInputManager.IsKeyDown(DIK_RIGHT))
+	{
+		//pView->UpdateCameraYaw(-0.01f);
+		kCameraController.Yaw(-0.01f);
+	}
+
+	if(kInputManager.IsKeyDown(DIK_UP))
+	{
+		//pView->UpdateCameraPitch(0.01f);
+		kCameraController.Pitch(0.01f);
+	}
+	else
+
+	if(kInputManager.IsKeyDown(DIK_DOWN))
+	{
+		//pView->UpdateCameraPitch(-0.01f);
+		kCameraController.Pitch(-0.01f);
+	}
+
+	if(kInputManager.IsKeyDown(DIK_NEXT))
+	{
+		//pView->UpdateCameraDistance(0.1f);
+		kCameraController.Move(0.1f);
+	}
+	else
+
+	if(kInputManager.IsKeyDown(DIK_PRIOR))
+	{
+		//pView->UpdateCameraDistance(-0.1f);
+		kCameraController.Move(-0.1f);
+	}
+	#endif
+
+	const float fMouseX = kInputManager.GetMouseX();
+	const float fMouseY = kInputManager.GetMouseY();
+
+	if(nGameState == GameState_Active)
+	{
+		if(kInputManager.IsButtonDown(0) && !kInputManager.WasButtonDown(0))
+		{
+			int32 nSelection = kControls.Pick(fMouseX, fMouseY);
+			if(nSelection >= 0)
+			{
+				if(nSelection == ControlButton_Play)
+				{
+					if(nSimulationState == SimulationState_Idle)
+					{
+						ResetBot();
+						kBot.Upload(pFunction, kLoader.GetFunctionCount() + 1);
+
+						nSimulationState = SimulationState_Active;
+					}
+				}
+				else
+
+				if(nSelection == ControlButton_Stop)
+				{
+					if(nSimulationState == SimulationState_Active)
+					{
+						ResetEnvironment();
+						ResetBot();
+
+						nSimulationState = SimulationState_Idle;
+					}
+				}
+				else
+
+				if(nSelection == ControlButton_Exit)
+				{
+					nGameState = GameState_Exit;
+				}
+			}
+			else
+			{
+				// pick against the toolbar
+				nSelection = kToolbar.Pick(fMouseX, fMouseY);
+				if(nSelection >= 0)
+				{
+					kDragController.Begin(Action_Forward + nSelection);
+				}
+				else
+				{
+					// pick against the main pane
+					nSelection = kMain.Pick(fMouseX, fMouseY);
+					if(nSelection >= 0)
+					{
+						Code* pCode = pFunction;
+						if(!pCode->IsEmptySlot(nSelection))
+						{
+							kDragController.Begin(pCode->GetSlot(nSelection));
+							pCode->ClearSlot(nSelection);
+						}
+					}
+					else
+					{
+						// pick against the function pane
+						nSelection = kFunction.Pick(fMouseX, fMouseY);
+						if(nSelection >= 0)
+						{
+							Code* pCode = pFunction + nCurrentFunction + 1;
+							if(!pCode->IsEmptySlot(nSelection))
+							{
+								kDragController.Begin(pCode->GetSlot(nSelection));
+								pCode->ClearSlot(nSelection);
+							}
+						}
+						else
+						{
+							// pick against the function pane arrows
+							for(uint32 i = 0; i < sizeof(kArrowBounds) / sizeof(kArrowBounds[0]); ++i)
+							{
+								if(kArrowBounds[i].Contains(fMouseX, fMouseY))
+								{
+									const uint32 nCount = kLoader.GetFunctionCount();
+									nCurrentFunction = (nCurrentFunction + 2 * (int32)i - 1 + nCount) % nCount;
+								}
+							}
+						}
+					}
+				}
+			}
+		}
+		else
+
+		if(!kInputManager.IsButtonDown(0) && kInputManager.WasButtonDown(0))
+		{
+			if(kDragController.IsActive())
+			{
+				const uint32 nAction = kDragController.End();
+
+				int32 nSelection = kMain.Pick(fMouseX, fMouseY);
+				if(nSelection >= 0)
+				{
+					pFunction[0].SetSlot(nSelection, nAction);
+				}
+				else
+				{
+					nSelection = kFunction.Pick(fMouseX, fMouseY);
+					if(nSelection >= 0)
+					{
+						pFunction[nCurrentFunction + 1].SetSlot(nSelection, nAction);
+					}
+				}
+			}
+		}
+	}
+	else
+
+	if(nGameState == GameState_Complete)
+	{
+		if(kInputManager.IsButtonDown(0) && !kInputManager.WasButtonDown(0))
+		{
+			for(uint32 i = 0; i < sizeof(kDialog1Bounds) / sizeof(kDialog1Bounds[0]); ++i)
+			{
+				if(kDialog1Bounds[i].Contains(fMouseX, fMouseY))
+				{
+					if(i == 0)
+					{
+						if(nSimulationState == SimulationState_Active)
+						{
+							ResetEnvironment();
+							ResetBot();
+
+							nSimulationState = SimulationState_Idle;
+						}
+
+						nGameState = GameState_LoadMap;
+					}
+				}
+			}
+
+			/*
+			int32 nSelection = kDialog.Pick(fMouseX, fMouseY);
+
+			if(nSelection == DialogButton_A)
+			{
+				if(nSimulationState == SimulationState_Active)
+				{
+					ResetEnvironment();
+					ResetBot();
+
+					nSimulationState = SimulationState_Idle;
+				}
+
+				nGameState = GameState_LoadMap;
+			}
+			*/
+		}
+	}
+	else
+
+	if(nGameState == GameState_Over)
+	{
+		// check to see if button was clicked
+		if(kInputManager.IsButtonDown(0) && !kInputManager.WasButtonDown(0))
+		{
+			for(uint32 i = 0; i < sizeof(kDialog1Bounds) / sizeof(kDialog1Bounds[0]); ++i)
+			{
+				if(kDialog1Bounds[i].Contains(fMouseX, fMouseY))
+				{
+					if(i == 0)
+					{
+						nCurrentLevel	= 0;
+						nGameState		= GameState_LoadMap;
+					}
+				}
+			}
+
+			/*
+			int32 nSelection = kDialog.Pick(fMouseX, fMouseY);
+
+			if(nSelection == DialogButton_A)
+			{
+				nCurrentLevel	= 0;
+				nGameState		= GameState_LoadMap;
+			}
+			*/
+		}
+	}
+	else
+
+	if(nGameState == GameState_Confirm)
+	{
+		// check to see if button was clicked
+		if(kInputManager.IsButtonDown(0) && !kInputManager.WasButtonDown(0))
+		{
+			for(uint32 i = 0; i < sizeof(kDialog2Bounds) / sizeof(kDialog2Bounds[0]); ++i)
+			{
+				if(kDialog2Bounds[i].Contains(fMouseX, fMouseY))
+				{
+					if(i == 0)
+					{
+						nGameState = GameState_Exit;
+					}
+					else
+
+					if(i == 1)
+					{
+						nGameState = GameState_Active;
+					}
+				}
+			}
+
+			/*
+			int32 nSelection = kDialog.Pick(fMouseX, fMouseY);
+
+			if(nSelection == DialogButton_A)
+			{
+				nGameState = GameState_Exit;
+			}
+			else
+
+			if(nSelection == DialogButton_B)
+			{
+				nGameState = GameState_Active;
+			}
+			*/
+		}
+	}
+}
--- a/LightClone/Source/Mediator.h	Fri Sep 09 13:21:22 2011 -0700
+++ b/LightClone/Source/Mediator.h	Fri Sep 09 16:18:41 2011 -0700
@@ -7,9 +7,20 @@
 
 #include "Core.h"
 #include "Clock.h"
-#include "Model.h"
-#include "View.h"
-#include "Controller.h"
+#include "Window.h"
+#include "InputManager.h"
+#include "Loader.h"
+#include "Environment.h"
+#include "Bot.h"
+#include "Code.h"
+#include "DragController.h"
+#include "ButtonPane.h"
+#include "Dialog.h"
+#include "GraphicsDevice.h"
+#include "ResourceManager.h"
+#include "RenderContext.h"
+#include "CameraController.h"
+#include "Interface.h"
 
 /*
  * MediatorState
@@ -24,29 +35,177 @@
 };
 
 /*
+ * GameState
+ */
+enum
+{
+	GameState_Idle,
+	GameState_LoadMap,
+	GameState_Active,
+	GameState_Complete,
+	GameState_Over,
+	GameState_Confirm,
+	GameState_Exit,
+};
+
+/*
+ * SimulationState
+ */
+enum
+{
+	SimulationState_Idle,
+	SimulationState_Active,
+};
+
+/*
  * Mediator
  */
-class Mediator
+class Mediator : public WindowCallback
 {
 	/*
+	 * kWindow
+	 */
+	Window kWindow;
+
+	/*
 	 * kClock
 	 */
 	Clock kClock;
 
 	/*
-	 * pModel
+	 * kLoader
+	 */
+	Loader kLoader;
+
+	/*
+	 * kEnvironment
+	 */
+	Environment kEnvironment;
+
+	/*
+	 * kBot
+	 */
+	Bot kBot;
+
+	/*
+	 * kFunction
+	 */
+	Code* pFunction;
+
+	/*
+	 * nGameState
+	 */
+	uint32 nGameState;
+
+	/*
+	 * nSimulationState
+	 */
+	uint32 nSimulationState;
+
+	/*
+	 * nCurrentLevel
 	 */
-	Model* pModel;
+	uint32 nCurrentLevel;
+
+	/*
+	 * nCurrentFunction
+	 */
+	uint32 nCurrentFunction;
+
+	/*
+	 * kInputManager
+	 */
+	InputManager kInputManager;
+
+	/*
+	 * kDragController
+	 */
+	DragController kDragController;
+
+	/*
+	 * kToolbar
+	 */
+	ButtonPane kToolbar;
+
+	/*
+	 * kMain
+	 */
+	ButtonPane kMain;
+
+	/*
+	 * kFunction
+	 */
+	ButtonPane kFunction;
 
 	/*
-	 * pView
+	 * kControlBounds
+	 */
+	ButtonPane kControls;
+
+	/*
+	 * kArrowBounds
+	 */
+	Rectangle2 kArrowBounds[2];
+
+	/*
+	 * kDialog1Bounds
 	 */
-	View* pView;
+	Rectangle2 kDialog1Bounds[1];
+
+	/*
+	 * kDialog2Bounds
+	 */
+	Rectangle2 kDialog2Bounds[2];
+
+	/*
+	 * kDialog
+	 */
+	Dialog kDialog;
+
+	/*
+	 * pGraphicsDevice
+	 */
+	GraphicsDevice* pGraphicsDevice;
 
 	/*
-	 * pController
+	 * kResourceManager
+	 */
+	ResourceManager kResourceManager;
+
+	/*
+	 * kContext
+	 */
+	RenderContext kContext;
+
+	/*
+	 * kCameraController
+	 *	Move to World
+	 */
+	CameraController kCameraController;
+
+	/*
+	 * pBlockEffect
+	 *	Move to World
 	 */
-	Controller* pController;
+	ID3DXEffect* pBlockEffect;
+
+	/*
+	 * pBlockVertexBuffer
+	 *	Move to World
+	 */
+	IDirect3DVertexBuffer9* pBlockVertexBuffer;
+
+	/*
+	 * pBlockTexture
+	 *	Move to World
+	 */
+	IDirect3DTexture9* pBlockTexture;
+
+	/*
+	 * kInterface
+	 *	Move to World
+	 */
+	Interface kInterface;
 
 public:
 
@@ -60,7 +219,12 @@
 	 */
 	ErrorCode Run();
 
-protected:
+	/*
+	 * OnMessage
+	 */
+	virtual int32 OnMessage(Window* pInstance, uint32 nMessage, WPARAM wParam, LPARAM lParam);
+
+private:
 
 	/*
 	 * Initialize
@@ -86,6 +250,56 @@
 	 * ProcessMessages
 	 */
 	virtual void ProcessMessages();
+
+	/*
+	 * Load
+	 */
+	bool Load(const char* pName);
+
+	/*
+	 * ResetEnvironment
+	 */
+	void ResetEnvironment();
+
+	/*
+	 * ResetBot
+	 */
+	void ResetBot();
+
+	/*
+	 * ResetCode
+	 */
+	void ClearCode();
+
+	/*
+	 * SetupVertexBuffer
+	 */
+	ErrorCode SetupVertexBuffer();
+
+	/*
+	 * Render3D
+	 */
+	void Render3D();
+
+	/* 
+	 * Render2D
+	 */
+	void Render2D();
+
+	/*
+	 * 	RenderEnvironment
+	 */
+	void RenderEnvironment(Environment* pEnvironment);
+
+	/*
+	 * RenderBot
+	 */
+	void RenderBot(Environment* pEnvironment, Bot* pBot);
+
+	/*
+	 * ProcessInput
+	 */
+	void ProcessInput(float fElapsed);
 };
 
 #endif //__MEDIATOR_H__
--- a/LightClone/Source/Model.cpp	Fri Sep 09 13:21:22 2011 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,207 +0,0 @@
-/*
- * Model
- */
-
-#include "Model.h"
-
-/*
-World
-	Update
-		Update WorldObjects
-
-	Render3D
-		Setup 3D Projection
-		Render Environment
-		Render Bot
-
-	Render2D
-		Interface
-
-
-WorldObject Concerns:
-	Initialization & Resource Loading
-	Updating
-	Rendering
-	Termination
-*/
-
-/*
- * Model
- */
-Model::Model() : kToolbar(Action_Count), kMain(MaximumFunctionLength), kFunction(MaximumFunctionLength / 2), kControls(4)
-{
-	nGameState				= GameState_Active;
-	nSimulationState		= SimulationState_Idle;
-	pFunction				= 0;
-	nCurrentFunction		= 0;
-	nCurrentLevel			= 0;
-
-	kArrowBounds[0]			= Rectangle2(1206.0f + 0 * 16.0f, 473.0f + 0 * 54.0f, 16.0f, 16.0f);
-	kArrowBounds[1]			= Rectangle2(1206.0f + 2 * 16.0f, 473.0f + 0 * 54.0f, 16.0f, 16.0f);
-
-	kDialog1Bounds[0]		= Rectangle2(567.0f, 412.0f, 150.0f, 60.0f);
-
-	kDialog2Bounds[0]		= Rectangle2(1023.0f + 2.5f * 54.0f, 638.0f + 0 * 00.0f, 48.0f, 48.0f);
-	kDialog2Bounds[1]		= Rectangle2(1023.0f + 2.5f * 54.0f, 638.0f + 0 * 00.0f, 48.0f, 48.0f);
-
-	kToolbar.Add(1023.0f + 0 * 54.0f, 85.0f + 0 * 54.0f, 48.0f, 48.0f);
-	kToolbar.Add(1023.0f + 1 * 54.0f, 85.0f + 0 * 54.0f, 48.0f, 48.0f);
-	kToolbar.Add(1023.0f + 2 * 54.0f, 85.0f + 0 * 54.0f, 48.0f, 48.0f);
-	kToolbar.Add(1023.0f + 3 * 54.0f, 85.0f + 0 * 54.0f, 48.0f, 48.0f);
-	kToolbar.Add(1023.0f + 0 * 54.0f, 85.0f + 1 * 54.0f, 48.0f, 48.0f);
-	kToolbar.Add(1023.0f + 1 * 54.0f, 85.0f + 1 * 54.0f, 48.0f, 48.0f);
-	kToolbar.Add(1023.0f + 2 * 54.0f, 85.0f + 1 * 54.0f, 48.0f, 48.0f);
-	kToolbar.Add(1023.0f + 3 * 54.0f, 85.0f + 1 * 54.0f, 48.0f, 48.0f);
-
-	kMain.Add(1023.0f + 0 * 54.0f, 238.0f + 0 * 54.0f, 48.0f, 48.0f);
-	kMain.Add(1023.0f + 1 * 54.0f, 238.0f + 0 * 54.0f, 48.0f, 48.0f);
-	kMain.Add(1023.0f + 2 * 54.0f, 238.0f + 0 * 54.0f, 48.0f, 48.0f);
-	kMain.Add(1023.0f + 3 * 54.0f, 238.0f + 0 * 54.0f, 48.0f, 48.0f);
-	kMain.Add(1023.0f + 0 * 54.0f, 238.0f + 1 * 54.0f, 48.0f, 48.0f);
-	kMain.Add(1023.0f + 1 * 54.0f, 238.0f + 1 * 54.0f, 48.0f, 48.0f);
-	kMain.Add(1023.0f + 2 * 54.0f, 238.0f + 1 * 54.0f, 48.0f, 48.0f);
-	kMain.Add(1023.0f + 3 * 54.0f, 238.0f + 1 * 54.0f, 48.0f, 48.0f);
-	kMain.Add(1023.0f + 0 * 54.0f, 238.0f + 2 * 54.0f, 48.0f, 48.0f);
-	kMain.Add(1023.0f + 1 * 54.0f, 238.0f + 2 * 54.0f, 48.0f, 48.0f);
-	kMain.Add(1023.0f + 2 * 54.0f, 238.0f + 2 * 54.0f, 48.0f, 48.0f);
-	kMain.Add(1023.0f + 3 * 54.0f, 238.0f + 2 * 54.0f, 48.0f, 48.0f);
-	kMain.Add(1023.0f + 0 * 54.0f, 238.0f + 3 * 54.0f, 48.0f, 48.0f);
-	kMain.Add(1023.0f + 1 * 54.0f, 238.0f + 3 * 54.0f, 48.0f, 48.0f);
-	kMain.Add(1023.0f + 2 * 54.0f, 238.0f + 3 * 54.0f, 48.0f, 48.0f);
-	kMain.Add(1023.0f + 3 * 54.0f, 238.0f + 3 * 54.0f, 48.0f, 48.0f);
-
-	kFunction.Add(1023.0f + 0 * 54.0f, 501.0f + 0 * 54.0f, 48.0f, 48.0f);
-	kFunction.Add(1023.0f + 1 * 54.0f, 501.0f + 0 * 54.0f, 48.0f, 48.0f);
-	kFunction.Add(1023.0f + 2 * 54.0f, 501.0f + 0 * 54.0f, 48.0f, 48.0f);
-	kFunction.Add(1023.0f + 3 * 54.0f, 501.0f + 0 * 54.0f, 48.0f, 48.0f);
-	kFunction.Add(1023.0f + 0 * 54.0f, 501.0f + 1 * 54.0f, 48.0f, 48.0f);
-	kFunction.Add(1023.0f + 1 * 54.0f, 501.0f + 1 * 54.0f, 48.0f, 48.0f);
-	kFunction.Add(1023.0f + 2 * 54.0f, 501.0f + 1 * 54.0f, 48.0f, 48.0f);
-	kFunction.Add(1023.0f + 3 * 54.0f, 501.0f + 1 * 54.0f, 48.0f, 48.0f);
-
-	kControls.Add(1023.0f + 0.5f * 54.0f, 638.0f + 0 * 00.0f, 48.0f, 48.0f);
-	kControls.Add(1023.0f + 1.5f * 54.0f, 638.0f + 0 * 00.0f, 48.0f, 48.0f);
-	kControls.Add(1023.0f + 2.5f * 54.0f, 638.0f + 0 * 00.0f, 48.0f, 48.0f);
-}
-
-/*
- * Load
- */
-bool Model::Load(const char* pName)
-{
-	ErrorCode eCode = kLoader.Load(pName);
-	if(eCode == Error_Success)
-	{
-		if(pFunction)
-		{
-			delete[] pFunction;
-			pFunction = NULL;
-		}
-
-		const Size& kSize = kLoader.GetSize();
-
-		eCode = kEnvironment.Initialize(kSize.X, kSize.Y);
-		if(eCode == Error_Success)
-		{
-			for(uint32 nY = 0; nY < kSize.Y; ++nY)
-			{
-				for(uint32 nX = 0; nX < kSize.X; ++nX)
-				{
-					kEnvironment.SetType(nX, nY, kLoader.GetTowerType(nX, nY));
-					kEnvironment.SetAltitude(nX, nY, kLoader.GetTowerHeight(nX, nY));
-				}
-			}
-
-			kBot.Initialize(&kEnvironment);
-			kBot.kPosition	= kLoader.GetInitialPosition();
-			kBot.kDirection	= kLoader.GetInitialDirection();
-
-			const uint32 nCount = kLoader.GetFunctionCount();
-
-			pFunction = new Code[nCount + 1];
-			pFunction[0].Initialize(MainFunctionLength);
-			pFunction[0].Clear();
-
-			for(uint32 i = 0; i < nCount; ++i)
-			{
-				const uint32 nLength = kLoader.GetFunctionLength(i);
-
-				pFunction[i + 1].Initialize(nLength);
-				pFunction[i + 1].Clear();
-			}
-		}
-	}
-
-	return eCode == Error_Success;
-}
-
-/*
- * GetBot
- */
-Bot* Model::GetBot()
-{
-	return &kBot;
-}
-
-/*
- * GetFunction
- */
-Code* Model::GetFunction(uint32 nIndex)
-{
-	return &pFunction[nIndex];
-}
-
-/*
- * GetFunctionCount
- */
-uint32 Model::GetFunctionCount()
-{
-	return kLoader.GetFunctionCount() + 1;
-}
-
-/*
- * GetEnvironment
- */
-Environment* Model::GetEnvironment()
-{
-	return &kEnvironment;
-}
-
-/*
- * ResetEnvironment
- */
-void Model::ResetEnvironment()
-{
-	kEnvironment.Reset();
-}
-
-/*
- * ResetBot
- */
-void Model::ResetBot()
-{
-	kBot.Reset();
-	kBot.kPosition	= kLoader.GetInitialPosition();
-	kBot.kDirection	= kLoader.GetInitialDirection();
-}
-
-/*
- * ClearCode
- */
-void Model::ClearCode()
-{
-	for(uint32 i = 0; i < kLoader.GetFunctionCount(); ++i)
-	{
-		pFunction[i].Clear();
-	}
-}
-
-/*
- * SetupDialog
- */
-void Model::SetupDialog(const char* pMessage, const char* pChoiceA, uint32 nResultA, const char* pChoiceB, uint32 nResultB)
-{
-	//kDialog.Reset("Some message");
-	//kDialog.AddButton(DialogButton_Ok, "Ok", 0.0f, 0.0f, 0.0f, 0.0f);
-	//kDialog.AddButton(nResultA, pMessageA, 
-}
--- a/LightClone/Source/Model.h	Fri Sep 09 13:21:22 2011 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,193 +0,0 @@
-/*
- * Model
- */
-
-#ifndef __MODEL_H__
-#define __MODEL_H__
-
-#include "Core.h"
-#include "Bot.h"
-#include "Environment.h"
-#include "Code.h"
-#include "Loader.h"
-#include "InputManager.h"
-#include "DragController.h"
-#include "ButtonPane.h"
-#include "Dialog.h"
-
-/*
- * GameState
- */
-enum
-{
-	GameState_Idle,
-	GameState_LoadMap,
-	GameState_Active,
-	GameState_Complete,
-	GameState_Over,
-	GameState_Confirm,
-	GameState_Exit,
-};
-
-/*
- * SimulationState
- */
-enum
-{
-	SimulationState_Idle,
-	SimulationState_Active,
-};
-
-/*
- * Model
- */
-class Model
-{
-	/*
-	 * kLoader
-	 */
-	Loader kLoader;
-
-	/*
-	 * kEnvironment
-	 */
-	Environment kEnvironment;
-
-	/*
-	 * kBot
-	 */
-	Bot kBot;
-
-	/*
-	 * kFunction
-	 */
-	Code* pFunction;
-
-public:
-
-	/*
-	 * nGameState
-	 */
-	uint32 nGameState;
-
-	/*
-	 * nSimulationState
-	 */
-	uint32 nSimulationState;
-
-	/*
-	 * nCurrentLevel
-	 */
-	uint32 nCurrentLevel;
-
-	/*
-	 * nCurrentFunction
-	 */
-	uint32 nCurrentFunction;
-
-public:
-
-	/*
-	 * kInputManager
-	 */
-	InputManager kInputManager;
-
-	/*
-	 * kDragController
-	 */
-	DragController kDragController;
-
-	/*
-	 * kToolbar
-	 */
-	ButtonPane kToolbar;
-
-	/*
-	 * kMain
-	 */
-	ButtonPane kMain;
-
-	/*
-	 * kFunction
-	 */
-	ButtonPane kFunction;
-
-	/*
-	 * kControlBounds
-	 */
-	ButtonPane kControls;
-
-	/*
-	 * kArrowBounds
-	 */
-	Rectangle2 kArrowBounds[2];
-
-	/*
-	 * kDialog1Bounds
-	 */
-	Rectangle2 kDialog1Bounds[1];
-
-	/*
-	 * kDialog2Bounds
-	 */
-	Rectangle2 kDialog2Bounds[2];
-
-	/*
-	 * kDialog
-	 */
-	Dialog kDialog;
-
-public:
-
-	/*
-	 * Model
-	 */
-	Model();
-
-	/*
-	 * Load
-	 */
-	bool Load(const char* pName);
-
-	/*
-	 * GetEnvironment
-	 */
-	Environment* GetEnvironment();
-
-	/*
-	 * GetBot
-	 */
-	Bot* GetBot();
-
-	/*
-	 * GetFunction
-	 */
-	Code* GetFunction(uint32 nIndex);
-
-	/*
-	 * GetFunctionCount
-	 */
-	uint32 GetFunctionCount();
-
-	/*
-	 * ResetEnvironment
-	 */
-	void ResetEnvironment();
-
-	/*
-	 * ResetBot
-	 */
-	void ResetBot();
-
-	/*
-	 * ResetCode
-	 */
-	void ClearCode();
-
-	/*
-	 * SetupDialog
-	 */
-	void SetupDialog(const char* pMessage, const char* pChoiceA, uint32 nResultA, const char* pChoiceB = NULL, uint32 nResultB = 0);
-};
-
-#endif //__MODEL_H__
--- a/LightClone/Source/RenderContext.cpp	Fri Sep 09 13:21:22 2011 -0700
+++ b/LightClone/Source/RenderContext.cpp	Fri Sep 09 16:18:41 2011 -0700
@@ -8,56 +8,16 @@
 /*
  * RenderContext
  */
-RenderContext::RenderContext()
+RenderContext::RenderContext() : pGraphicsDevice(NULL)
 {
-	pDirect3D				= NULL;
-	pDevice					= NULL;
 }
 
 /*
  * Initialize
  */
-ErrorCode RenderContext::Initialize(HWND kWindow, uint32 nWidth, uint32 nHeight)
+ErrorCode RenderContext::Initialize(GraphicsDevice* pDevice)
 {
-	pDirect3D = Direct3DCreate9(D3D_SDK_VERSION);
-	if(!pDirect3D)
-	{
-		return Error_Fail;
-	}
-
-	uint32 nAdapter = D3DADAPTER_DEFAULT;
-	uint32 nFlags	= D3DCREATE_HARDWARE_VERTEXPROCESSING;
-
-	kParameters.BackBufferCount				= 1;
-	kParameters.BackBufferWidth				= nWidth;
-	kParameters.BackBufferHeight			= nHeight;
-	kParameters.BackBufferFormat			= D3DFMT_UNKNOWN;
-//	kParameters.MultiSampleType				= D3DMULTISAMPLE_NONE;
-//	kParameters.MultiSampleQuality			= 0;
-	kParameters.MultiSampleType				= D3DMULTISAMPLE_4_SAMPLES;
-	kParameters.MultiSampleQuality			= 2;
-	kParameters.SwapEffect					= D3DSWAPEFFECT_DISCARD;
-	kParameters.hDeviceWindow				= kWindow;
-	kParameters.Windowed					= TRUE;
-	kParameters.EnableAutoDepthStencil		= TRUE;
-	kParameters.AutoDepthStencilFormat		= D3DFMT_D24S8;
-	kParameters.Flags						= 0;
-	kParameters.FullScreen_RefreshRateInHz	= 0;
-	kParameters.PresentationInterval		= D3DPRESENT_INTERVAL_DEFAULT;
-
-	HRESULT hResult = pDirect3D->CreateDevice(nAdapter, D3DDEVTYPE_HAL, kWindow, nFlags, &kParameters, &pDevice);
-	if(FAILED(hResult))
-	{
-		Terminate();
-		return Error_Fail;
-	}
-
-	if(InitializeVertexTypes(pDevice) != Error_Success)
-	{
-		return Error_Fail;
-	}
-
-	return Error_Success;
+	return pGraphicsDevice = pDevice, Error_Success;
 }
 
 /* 
@@ -65,19 +25,7 @@
  */
 void RenderContext::Terminate()
 {
-	TerminateVertexTypes();
-
-	if(pDevice)
-	{
-		pDevice->Release();
-		pDevice = 0;
-	}
-
-	if(pDirect3D)
-	{
-		pDirect3D->Release();
-		pDirect3D = 0;
-	}
+	pGraphicsDevice = NULL;
 }
 
 /*
@@ -85,18 +33,23 @@
  */
 ErrorCode RenderContext::CreateTextureFromFile(const char* pName, IDirect3DTexture9** pTexture)
 {
-	const uint32 nSizeX		= D3DX_DEFAULT_NONPOW2;
-	const uint32 nSizeY		= D3DX_DEFAULT_NONPOW2;
-	const uint32 nFilter	= D3DX_DEFAULT;
-	const uint32 nFilterMip	= D3DX_DEFAULT;
+	ErrorCode eCode = Error_Fail;
 
-	HRESULT hResult = D3DXCreateTextureFromFileExA(pDevice, pName, nSizeX, nSizeY, 0, 0, D3DFMT_FROM_FILE, D3DPOOL_MANAGED, nFilter, nFilterMip, 0, NULL, NULL, pTexture);
-	if(FAILED(hResult))
+	if(pGraphicsDevice)
 	{
-		return Error_Fail;
+		const uint32 nSizeX		= D3DX_DEFAULT_NONPOW2;
+		const uint32 nSizeY		= D3DX_DEFAULT_NONPOW2;
+		const uint32 nFilter	= D3DX_DEFAULT;
+		const uint32 nFilterMip	= D3DX_DEFAULT;
+
+		HRESULT hResult = D3DXCreateTextureFromFileExA(*pGraphicsDevice, pName, nSizeX, nSizeY, 0, 0, D3DFMT_FROM_FILE, D3DPOOL_MANAGED, nFilter, nFilterMip, 0, NULL, NULL, pTexture);
+		if(SUCCEEDED(hResult))
+		{
+			eCode = Error_Success;
+		}
 	}
 
-	return Error_Success;
+	return eCode;
 }
 
 /*
@@ -106,7 +59,7 @@
 {
 	ID3DXBuffer* pBuffer = NULL;
 
-	HRESULT hResult = D3DXCreateEffectFromFileA(pDevice, pName, NULL, NULL, 0, NULL, pEffect, &pBuffer);
+	HRESULT hResult = D3DXCreateEffectFromFileA(*pGraphicsDevice, pName, NULL, NULL, 0, NULL, pEffect, &pBuffer);
 	if(FAILED(hResult))
 	{
 		if(pBuffer)
@@ -125,7 +78,7 @@
  */
 ErrorCode RenderContext::CreateVertexBuffer(uint32 nSize, uint32 nUsage, uint32 nPool, IDirect3DVertexBuffer9** pBuffer)
 {
-	HRESULT hResult = pDevice->CreateVertexBuffer(nSize, nUsage, 0, (D3DPOOL)nPool, pBuffer, NULL);
+	HRESULT hResult = ((IDirect3DDevice9*)*pGraphicsDevice)->CreateVertexBuffer(nSize, nUsage, 0, (D3DPOOL)nPool, pBuffer, NULL);
 	if(FAILED(hResult))
 	{
 		return Error_Fail;
@@ -139,7 +92,7 @@
  */
 ErrorCode RenderContext::CreateFontFromName(const char* pName, uint32 nSize, uint32 nWeight, ID3DXFont** pFont)
 {
-	HRESULT hResult = D3DXCreateFontA(pDevice, nSize, 0, nWeight, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, pName, pFont);
+	HRESULT hResult = D3DXCreateFontA(*pGraphicsDevice, nSize, 0, nWeight, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, pName, pFont);
 	if(FAILED(hResult))
 	{
 		return Error_Fail;
@@ -153,8 +106,7 @@
  */
 void RenderContext::Begin(uint32 nColor)
 {
-	pDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL, nColor, 1.0f, 0);
-	pDevice->BeginScene();
+	pGraphicsDevice->Begin(nColor);
 }
 
 /*
@@ -162,8 +114,7 @@
  */
 void RenderContext::End()
 {
-	pDevice->EndScene();
-	pDevice->Present(NULL, NULL, NULL, NULL);
+	pGraphicsDevice->End();
 }
 
 /*
@@ -172,7 +123,7 @@
 void RenderContext::SetupCamera3D(ID3DXEffect* pEffect, const D3DXVECTOR3& kLocation, const D3DXVECTOR3& kTarget, float fViewAngle)
 {
 	D3DVIEWPORT9 kViewport;
-	if(SUCCEEDED(pDevice->GetViewport(&kViewport)))
+	if(SUCCEEDED(pGraphicsDevice->GetViewport(&kViewport)))
 	{
 		D3DXHANDLE kHandleProj	= pEffect->GetParameterByName(NULL, "kProjection");
 		D3DXHANDLE kHandleView	= pEffect->GetParameterByName(NULL, "kView");
@@ -198,7 +149,7 @@
 void RenderContext::SetupCamera2D(ID3DXEffect* pEffect)
 {
 	D3DVIEWPORT9 kViewport;
-	if(SUCCEEDED(pDevice->GetViewport(&kViewport)))
+	if(SUCCEEDED(pGraphicsDevice->GetViewport(&kViewport)))
 	{
 		D3DXHANDLE kHandle = pEffect->GetParameterByName(NULL, "kProjection");
 		if(kHandle)
@@ -219,9 +170,7 @@
  */
 void RenderContext::DrawTriangles(IDirect3DVertexDeclaration9* pDeclaration, IDirect3DVertexBuffer9* pBuffer, uint32 nSize, uint32 nCount)
 {
-	pDevice->SetVertexDeclaration(pDeclaration);
-	pDevice->SetStreamSource(0, pBuffer, 0, nSize);
-	pDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, nCount);
+	pGraphicsDevice->DrawTriangles(pDeclaration, pBuffer, nSize, nCount);
 }
 
 /*
@@ -229,7 +178,7 @@
  */
 ErrorCode RenderContext::GetViewport(D3DVIEWPORT9* pViewport)
 {
-	return SUCCEEDED(pDevice->GetViewport(pViewport)) ? Error_Success : Error_Fail;
+	return pGraphicsDevice->GetViewport(pViewport);
 }
 
 /*
--- a/LightClone/Source/RenderContext.h	Fri Sep 09 13:21:22 2011 -0700
+++ b/LightClone/Source/RenderContext.h	Fri Sep 09 16:18:41 2011 -0700
@@ -6,8 +6,7 @@
 #define __RENDERCONTEXT_H__
 
 #include "Core.h"
-#include <d3d9.h>
-#include <d3dx9.h>
+#include "GraphicsDevice.h"
 
 /*
  * RenderContext
@@ -15,19 +14,19 @@
 class RenderContext
 {
 	/*
-	 * pDirect3D
+	 * pGraphicsDevice
 	 */
-	IDirect3D9* pDirect3D;
+	GraphicsDevice* pGraphicsDevice;
 
 	/*
-	 * pDevice
+	 * kProjectionStack
 	 */
-	IDirect3DDevice9* pDevice;
+	//MatrixStack kProjectionStack;
 
 	/*
-	 * kParameters
+	 * kWorldStack
 	 */
-	D3DPRESENT_PARAMETERS kParameters;
+	//MatrixStack kWorldStack;
 
 	/*
 	 * kProjection
@@ -49,7 +48,7 @@
 	/*
 	 * Initialize
 	 */
-	ErrorCode Initialize(HWND kWindow, uint32 nWidth, uint32 nHeight);
+	ErrorCode Initialize(GraphicsDevice* pDevice);
 
 	/* 
 	 * Terminate
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LightClone/Source/ResourceManager.cpp	Fri Sep 09 16:18:41 2011 -0700
@@ -0,0 +1,112 @@
+/*
+ * ResourceManager
+ */
+
+#include "ResourceManager.h"
+
+/*
+ * ResourceManager
+ */
+ResourceManager::ResourceManager()
+{
+}
+
+/*
+ * Initialize
+ */
+ErrorCode ResourceManager::Initialize(GraphicsDevice* pDevice)
+{
+	return pGraphicsDevice = pDevice, Error_Success;
+}
+
+/* 
+ * Terminate
+ */
+void ResourceManager::Terminate()
+{
+	pGraphicsDevice = NULL;
+}
+
+/*
+ * CreateTextureFromFile
+ */
+ErrorCode ResourceManager::CreateTextureFromFile(const char* pName, IDirect3DTexture9** pTexture)
+{
+	ErrorCode eCode = Error_Fail;
+
+	if(pGraphicsDevice)
+	{
+		const uint32 nSizeX		= D3DX_DEFAULT_NONPOW2;
+		const uint32 nSizeY		= D3DX_DEFAULT_NONPOW2;
+		const uint32 nFilter	= D3DX_DEFAULT;
+		const uint32 nFilterMip	= D3DX_DEFAULT;
+
+		HRESULT hResult = D3DXCreateTextureFromFileExA(*pGraphicsDevice, pName, nSizeX, nSizeY, 0, 0, D3DFMT_FROM_FILE, D3DPOOL_MANAGED, nFilter, nFilterMip, 0, NULL, NULL, pTexture);
+		if(SUCCEEDED(hResult))
+		{
+			eCode = Error_Success;
+		}
+	}
+
+	return eCode;
+}
+
+/*
+ * CreateEffectFromFile
+ */
+ErrorCode ResourceManager::CreateEffectFromFile(const char* pName, ID3DXEffect** pEffect)
+{
+	ErrorCode eCode = Error_Fail;
+
+	if(pGraphicsDevice)
+	{
+		ID3DXBuffer* pBuffer = NULL;
+
+		HRESULT hResult = D3DXCreateEffectFromFileA(*pGraphicsDevice, pName, NULL, NULL, 0, NULL, pEffect, &pBuffer);
+		if(SUCCEEDED(hResult))
+		{
+			eCode = Error_Success;
+		}
+	}
+
+	return eCode;
+}
+
+/*
+ * CreateVertexBuffer
+ */
+ErrorCode ResourceManager::CreateVertexBuffer(uint32 nSize, uint32 nUsage, uint32 nPool, IDirect3DVertexBuffer9** pBuffer)
+{
+	ErrorCode eCode = Error_Fail;
+
+	if(pGraphicsDevice)
+	{
+		//HRESULT hResult = ((IDirect3DDevice9*)*pGraphicsDevice)->CreateVertexBuffer(nSize, nUsage, 0, (D3DPOOL)nPool, pBuffer, NULL);
+		HRESULT hResult = static_cast<IDirect3DDevice9*>(*pGraphicsDevice)->CreateVertexBuffer(nSize, nUsage, 0, (D3DPOOL)nPool, pBuffer, NULL);
+		if(SUCCEEDED(hResult))
+		{
+			eCode = Error_Success;
+		}
+	}
+
+	return eCode;
+}
+
+/*
+ * CreateFontFromName
+ */
+ErrorCode ResourceManager::CreateFontFromName(const char* pName, uint32 nSize, uint32 nWeight, ID3DXFont** pFont)
+{
+	ErrorCode eCode = Error_Fail;
+
+	if(pGraphicsDevice)
+	{
+		HRESULT hResult = D3DXCreateFontA(*pGraphicsDevice, nSize, 0, nWeight, 1, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, pName, pFont);
+		if(SUCCEEDED(hResult))
+		{
+			eCode = Error_Success;
+		}
+	}
+
+	return eCode;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LightClone/Source/ResourceManager.h	Fri Sep 09 16:18:41 2011 -0700
@@ -0,0 +1,59 @@
+/*
+ * ResourceManager
+ */
+
+#ifndef __RESOURCEMANAGER_H__
+#define __RESOURCEMANAGER_H__
+
+#include "Core.h"
+#include "GraphicsDevice.h"
+
+/*
+ * ResourceManager
+ */
+class ResourceManager
+{
+	/*
+	 * pGraphicsDevice
+	 */
+	GraphicsDevice* pGraphicsDevice;
+
+public:
+
+	/*
+	 * ResourceManager
+	 */
+	ResourceManager();
+
+	/*
+	 * Initialize
+	 */
+	ErrorCode Initialize(GraphicsDevice* pDevice);
+
+	/* 
+	 * Terminate
+	 */
+	void Terminate();
+
+	/*
+	 * CreateTextureFromFile
+	 */
+	ErrorCode CreateTextureFromFile(const char* pName, IDirect3DTexture9** pTexture);
+
+	/*
+	 * CreateEffectFromFile
+	 */
+	ErrorCode CreateEffectFromFile(const char* pName, ID3DXEffect** pEffect);
+
+	/*
+	 * CreateVertexBuffer
+	 */
+	ErrorCode CreateVertexBuffer(uint32 nSize, uint32 nUsage, uint32 nPool, IDirect3DVertexBuffer9** pBuffer);
+
+	/*
+	 * CreateFontFromName
+	 */
+	ErrorCode CreateFontFromName(const char* pName, uint32 nSize, uint32 nWeight, ID3DXFont** pFont);
+};
+
+#endif //__RESOURCEMANAGER_H__
--- a/LightClone/Source/View.cpp	Fri Sep 09 13:21:22 2011 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,497 +0,0 @@
-/*
- * View
- */
-
-#include "View.h"
-#include "VertexTypes.h"
-
-/*
- * kClassName
- */
-static const TCHAR* kClassName = L"LightCloneClass";
-
-/*
- * kCaption
- */
-static const TCHAR* kCaption = L"LightClone";
-
-/*
- * pView
- */
-View* View::pView = NULL;
-
-/*
- * View
- */
-View::View(Model* pInstance) : pModel(pInstance)
-{
-	pView					= this;
-
-	kWindow					= NULL;
-	pBlockEffect			= NULL;
-	pBlockVertexBuffer		= NULL;
-	pBlockTexture			= NULL;
-}
-
-/*
- * Initialize
- */
-ErrorCode View::Initialize()
-{
-	HINSTANCE hInstance = GetModuleHandle(NULL);
-
-	WNDCLASSEX kClass;
-	kClass.cbSize			= sizeof(WNDCLASSEX);
-	kClass.cbClsExtra		= 0;
-	kClass.cbWndExtra		= 0;
-	kClass.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
-	kClass.hCursor			= LoadCursor(NULL, IDC_ARROW);
-	kClass.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
-	kClass.hIconSm			= LoadIcon(NULL, IDI_APPLICATION);
-	kClass.hInstance		= hInstance;
-	kClass.lpfnWndProc		= &MessageRouter;
-	kClass.lpszClassName	= kClassName;
-	kClass.lpszMenuName		= NULL;
-	kClass.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
-
-	ATOM kAtom = RegisterClassEx(&kClass);
-	if(kAtom == 0)
-	{
-		return Error_Fail;
-	}
-
-	const uint32 nStyleEx	= WS_EX_OVERLAPPEDWINDOW;
-	const uint32 nStyle		= WS_OVERLAPPEDWINDOW;
-
-	RECT kDesktopRectangle;
-	GetClientRect(GetDesktopWindow(), &kDesktopRectangle);
-
-	RECT kRectangle;
-	kRectangle.left		= 0;
-	kRectangle.top		= 0;
-	kRectangle.right	= ScreenSizeX;
-	kRectangle.bottom	= ScreenSizeY;
-
-	AdjustWindowRectEx(&kRectangle, nStyle, FALSE, nStyleEx);
-
-	uint32 nW			= kRectangle.right - kRectangle.left;
-	uint32 nH			= kRectangle.bottom - kRectangle.top;
-	uint32 nX			= ((kDesktopRectangle.right - kDesktopRectangle.left) - nW) / 2;
-	uint32 nY			= ((kDesktopRectangle.bottom - kDesktopRectangle.top) - nH) / 2;
-
-	kWindow = CreateWindowEx(nStyleEx, kClassName, kCaption, nStyle, nX, nY, nW, nH, NULL, NULL, hInstance, NULL);
-	if(!kWindow)
-	{
-		return Error_Fail;
-	}
-
-	UpdateWindow(kWindow);
-	ShowWindow(kWindow, SW_SHOW);
-
-	ErrorCode eCode = kContext.Initialize(kWindow, ScreenSizeX, ScreenSizeY);
-	if(eCode != Error_Success)
-	{
-		Terminate();
-		return Error_Fail;
-	}
-
-	eCode = kContext.CreateEffectFromFile("Data\\Shaders\\Environment.fx", &pBlockEffect);
-	if(eCode != Error_Success)
-	{
-		Terminate();
-		return Error_Fail;
-	}
-
-	eCode = kContext.CreateTextureFromFile("Data\\Textures\\Block02.tga", &pBlockTexture);
-	if(eCode != Error_Success)
-	{
-		Terminate();
-		return Error_Fail;
-	}
-
-	eCode = kContext.CreateVertexBuffer(VerticesPerBlock * sizeof(Vertex::Block), D3DUSAGE_WRITEONLY, D3DPOOL_MANAGED, &pBlockVertexBuffer);
-	if(eCode != Error_Success)
-	{
-		Terminate();
-		return Error_Fail;
-	}
-
-	eCode = SetupVertexBuffer();
-	if(eCode != Error_Success)
-	{
-		Terminate();
-		return Error_Fail;
-	}
-
-	return kInterface.Initialize(kContext);
-}
-
-/* 
- * Terminate
- */
-void View::Terminate()
-{
-	kInterface.Terminate();
-
-	if(pBlockTexture)
-	{
-		pBlockTexture->Release();
-		pBlockTexture = NULL;
-	}
-
-	if(pBlockVertexBuffer)
-	{
-		pBlockVertexBuffer->Release();
-		pBlockVertexBuffer = NULL;
-	}
-
-	if(pBlockEffect)
-	{
-		pBlockEffect->Release();
-		pBlockEffect = 0;
-	}
-
-	kContext.Terminate();
-}
-
-/*
- * Render
- */
-void View::Render()
-{
-	const uint32 nColor = D3DCOLOR_XRGB(32, 32, 32);
-
-	kContext.Begin(nColor);
-
-	if(pModel)
-	{
-		if(pModel->nGameState >= GameState_Active)
-		{
-			Render3D();
-			Render2D();
-		}
-	}
-
-	kContext.End();
-}
-
-/*
- * GetWindow
- */
-HWND View::GetWindow() const
-{
-	return kWindow;
-}
-
-/*
- * UpdateCameraYaw
- */
-void View::UpdateCameraYaw(float fDelta)
-{
-	kCameraController.Yaw(fDelta);
-}
-
-/*
- * UpdateCameraPitch
- */
-void View::UpdateCameraPitch(float fDelta)
-{
-	kCameraController.Pitch(fDelta);
-}
-
-/*
- * UpdateCameraDistance
- */
-void View::UpdateCameraDistance(float fDelta)
-{
-	kCameraController.Move(fDelta);
-}
-
-/*
- * SetupVertexBuffer
- */
-ErrorCode View::SetupVertexBuffer()
-{
-	Vertex::Block* pVertices = NULL;
-	
-	HRESULT hResult = pBlockVertexBuffer->Lock(0, 0, (void**)&pVertices, D3DLOCK_DISCARD);
-	if(FAILED(hResult))
-	{
-		return Error_Fail;
-	}
-
-	const float fU1	= 0.66f;
-	const float fV1 = 0.66f;
-
-	// front
-	pVertices[0]	= Vertex::Block(-0.5f, 0.0f, -0.5f, 0.0f, 0.0f, -1.0f, 0.00f, 1.00f);
-	pVertices[1]	= Vertex::Block(-0.5f, 1.0f, -0.5f, 0.0f, 0.0f, -1.0f, 0.00f, 0.66f);
-	pVertices[2]	= Vertex::Block(+0.5f, 1.0f, -0.5f, 0.0f, 0.0f, -1.0f, 1.00f, 0.66f);
-	pVertices[3]	= Vertex::Block(-0.5f, 0.0f, -0.5f, 0.0f, 0.0f, -1.0f, 0.00f, 1.00f);
-	pVertices[4]	= Vertex::Block(+0.5f, 1.0f, -0.5f, 0.0f, 0.0f, -1.0f, 1.00f, 0.66f);
-	pVertices[5]	= Vertex::Block(+0.5f, 0.0f, -0.5f, 0.0f, 0.0f, -1.0f, 1.00f, 1.00f);
-	// back
-	pVertices[6]	= Vertex::Block(+0.5f, 0.0f, +0.5f, 0.0f, 0.0f, +1.0f, 0.00f, 1.00f);
-	pVertices[7]	= Vertex::Block(+0.5f, 1.0f, +0.5f, 0.0f, 0.0f, +1.0f, 0.00f, 0.66f);
-	pVertices[8]	= Vertex::Block(-0.5f, 1.0f, +0.5f, 0.0f, 0.0f, +1.0f, 1.00f, 0.66f);
-	pVertices[9]	= Vertex::Block(+0.5f, 0.0f, +0.5f, 0.0f, 0.0f, +1.0f, 0.00f, 1.00f);
-	pVertices[10]	= Vertex::Block(-0.5f, 1.0f, +0.5f, 0.0f, 0.0f, +1.0f, 1.00f, 0.66f);
-	pVertices[11]	= Vertex::Block(-0.5f, 0.0f, +0.5f, 0.0f, 0.0f, +1.0f, 1.00f, 1.00f);
-	// left
-	pVertices[12]	= Vertex::Block(-0.5f, 0.0f, +0.5f, -1.0f, 0.0f, 0.0f, 0.00f, 1.00f);
-	pVertices[13]	= Vertex::Block(-0.5f, 1.0f, +0.5f, -1.0f, 0.0f, 0.0f, 0.00f, 0.66f);
-	pVertices[14]	= Vertex::Block(-0.5f, 1.0f, -0.5f, -1.0f, 0.0f, 0.0f, 1.00f, 0.66f);
-	pVertices[15]	= Vertex::Block(-0.5f, 0.0f, +0.5f, -1.0f, 0.0f, 0.0f, 0.00f, 1.00f);
-	pVertices[16]	= Vertex::Block(-0.5f, 1.0f, -0.5f, -1.0f, 0.0f, 0.0f, 1.00f, 0.66f);
-	pVertices[17]	= Vertex::Block(-0.5f, 0.0f, -0.5f, -1.0f, 0.0f, 0.0f, 1.00f, 1.00f);
-	// right
-	pVertices[18]	= Vertex::Block(+0.5f, 0.0f, -0.5f, +1.0f, 0.0f, 0.0f, 0.00f, 1.00f);
-	pVertices[19]	= Vertex::Block(+0.5f, 1.0f, -0.5f, +1.0f, 0.0f, 0.0f, 0.00f, 0.66f);
-	pVertices[20]	= Vertex::Block(+0.5f, 1.0f, +0.5f, +1.0f, 0.0f, 0.0f, 1.00f, 0.66f);
-	pVertices[21]	= Vertex::Block(+0.5f, 0.0f, -0.5f, +1.0f, 0.0f, 0.0f, 0.00f, 1.00f);
-	pVertices[22]	= Vertex::Block(+0.5f, 1.0f, +0.5f, +1.0f, 0.0f, 0.0f, 1.00f, 0.66f);
-	pVertices[23]	= Vertex::Block(+0.5f, 0.0f, +0.5f, +1.0f, 0.0f, 0.0f, 1.00f, 1.00f);
-	// top
-	pVertices[24]	= Vertex::Block(-0.5f, 1.0f, -0.5f, 0.0f, +1.0f, 0.0f, 0.00f, 0.66f);
-	pVertices[25]	= Vertex::Block(-0.5f, 1.0f, +0.5f, 0.0f, +1.0f, 0.0f, 0.00f, 0.00f);
-	pVertices[26]	= Vertex::Block(+0.5f, 1.0f, +0.5f, 0.0f, +1.0f, 0.0f, 1.00f, 0.00f);
-	pVertices[27]	= Vertex::Block(-0.5f, 1.0f, -0.5f, 0.0f, +1.0f, 0.0f, 0.00f, 0.66f);
-	pVertices[28]	= Vertex::Block(+0.5f, 1.0f, +0.5f, 0.0f, +1.0f, 0.0f, 1.00f, 0.00f);
-	pVertices[29]	= Vertex::Block(+0.5f, 1.0f, -0.5f, 0.0f, +1.0f, 0.0f, 1.00f, 0.66f);
-	// bottom
-	pVertices[30]	= Vertex::Block(-0.5f, 0.0f, +0.5f, 0.0f, -1.0f, 0.0f, 0.00f, 0.66f);
-	pVertices[31]	= Vertex::Block(-0.5f, 0.0f, -0.5f, 0.0f, -1.0f, 0.0f, 0.00f, 0.00f);
-	pVertices[32]	= Vertex::Block(+0.5f, 0.0f, -0.5f, 0.0f, -1.0f, 0.0f, 1.00f, 0.00f);
-	pVertices[33]	= Vertex::Block(-0.5f, 0.0f, +0.5f, 0.0f, -1.0f, 0.0f, 0.00f, 0.66f);
-	pVertices[34]	= Vertex::Block(+0.5f, 0.0f, -0.5f, 0.0f, -1.0f, 0.0f, 1.00f, 0.00f);
-	pVertices[35]	= Vertex::Block(+0.5f, 0.0f, +0.5f, 0.0f, -1.0f, 0.0f, 1.00f, 0.66f);
-
-	pBlockVertexBuffer->Unlock();
-
-	return Error_Success;
-}
-
-/*
- * Render3D
- */
-void View::Render3D()
-{
-	Environment* pEnvironment = pModel->GetEnvironment();
-	if(pEnvironment)
-	{
-		Bot* pBot = pModel->GetBot();
-		if(pBot)
-		{
-			const D3DXVECTOR3& kScale	= pEnvironment->GetScale();
-			const uint32 nSizeX			= pEnvironment->GetWidth();
-			const uint32 nSizeY			= pEnvironment->GetHeight();
-
-			//const D3DXVECTOR3 kTarget(0.5f * (nSizeX - 1) * kScale.x, 0.0f, 0.5f * (nSizeY - 1) * kScale.z);
-			const D3DXVECTOR3 kTarget(0.0f, 0.0f, 0.0f);
-
-			kContext.SetupCamera3D(pBlockEffect, kCameraController.GetLocation(kTarget), kTarget);
-
-			RenderEnvironment(pEnvironment);
-			RenderBot(pEnvironment, pBot);
-		}
-	}
-}
-
-/* 
- * Render2D
- */
-void View::Render2D()
-{
-	kInterface.Render(kContext, pModel);
-}
-
-/*
- * 	RenderEnvironment
- */
-void View::RenderEnvironment(Environment* pEnvironment)
-{
-	const D3DXVECTOR3& kScale = pEnvironment->GetScale();
-
-	uint32 nPasses = 0;
-
-	pBlockEffect->SetTechnique(pBlockEffect->GetTechnique(0));
-	pBlockEffect->Begin(&nPasses, 0);
-	pBlockEffect->BeginPass(0);
-
-	D3DXMATRIX kScaleMatrix;
-	D3DXMatrixScaling(&kScaleMatrix, kScale.x, kScale.y, kScale.z);
-
-	const uint32 nSizeX				= pEnvironment->GetWidth();
-	const uint32 nSizeY				= pEnvironment->GetHeight();
-	
-	const D3DXMATRIX& kProjection	= kContext.GetProjection();
-	const D3DXMATRIX& kView			= kContext.GetView();
-
-	D3DVIEWPORT9 kViewport;
-	kContext.GetViewport(&kViewport);
-
-	D3DXVECTOR3 kCenterWorld(0.0f, 0.0f, 0.0f);
-	const float fMagicOffset = 400.0f;
-	//const D3DXVECTOR3& kCenterWorld = ComputeOrigin(0.5f * (ScreenSizeX - (304.0f + fMagicOffset)), 0.5f * ScreenSizeY, kViewport, kProjection, kView);
-
-	const float fOffsetX	= -0.5f * (nSizeX * kScale.x);
-	const float fOffsetZ	= -0.5f * (nSizeY * kScale.z);
-
-	for(uint32 nZ = 0; nZ < nSizeY; ++nZ)
-	{
-		for(uint32 nX = 0; nX < nSizeX; ++nX)
-		{
-			uint32 nType	= pEnvironment->GetType(nX, nZ);
-			uint32 nHeight	= pEnvironment->GetAltitude(nX, nZ);
-			uint32 nState	= pEnvironment->GetState(nX, nZ);
-			uint32 nColor	= D3DCOLOR_XRGB(0x80, 0x80, 0x80);
-
-			if(nType == 1)
-			{
-				nColor = nState ? D3DCOLOR_XRGB(0, 0, 255) : D3DCOLOR_XRGB(255, 0, 0);
-			}
-
-			for(uint32 i = 0; i < pEnvironment->GetAltitude(nX, nZ); ++i)
-			{
-				D3DXMATRIX kTranslateMatrix;
-				D3DXMatrixTranslation(&kTranslateMatrix, fOffsetX + (kCenterWorld.x + nX) * kScale.x, i * kScale.y, fOffsetZ + (kCenterWorld.z + nZ) * kScale.z);
-
-				D3DXMATRIX kWorldMatrix;
-				D3DXMatrixMultiply(&kWorldMatrix, &kScaleMatrix, &kTranslateMatrix);
-
-				const float fAlpha	= ((nColor >> 24) & 0xFF) / 255.0f;
-				const float fRed	= ((nColor >> 16) & 0xFF) / 255.0f;
-				const float fGreen	= ((nColor >> 8 ) & 0xFF) / 255.0f;
-				const float fBlue	= ((nColor >> 0 ) & 0xFF) / 255.0f;
-
-				const D3DXVECTOR4 kColorVector(fRed, fGreen, fBlue, fAlpha);
-
-				pBlockEffect->SetMatrix(pBlockEffect->GetParameterByName(NULL, "kWorld"), &kWorldMatrix);
-				pBlockEffect->SetVector(pBlockEffect->GetParameterByName(NULL, "kColor"), &kColorVector);
-				pBlockEffect->SetTexture(pBlockEffect->GetParameterByName(NULL, "kTexture"), pBlockTexture);
-				pBlockEffect->CommitChanges();
-
-				kContext.DrawTriangles(Vertex::Block::Declaration, pBlockVertexBuffer, sizeof(Vertex::Block), FacesPerCube * TrianglesPerFace);
-			}
-		}
-	}
-
-	pBlockEffect->EndPass();
-	pBlockEffect->End();
-
-	if(false) // wireframe
-	{
-		pBlockEffect->SetTechnique(pBlockEffect->GetTechniqueByName("Wire"));
-		pBlockEffect->Begin(&nPasses, 0);
-		pBlockEffect->BeginPass(0);
-
-		for(uint32 nZ = 0; nZ < nSizeY; ++nZ)
-		{
-			for(uint32 nX = 0; nX < nSizeX; ++nX)
-			{
-				for(uint32 i = 0; i < pEnvironment->GetAltitude(nX, nZ); ++i)
-				{
-					D3DXMATRIX kTranslateMatrix;
-					D3DXMatrixTranslation(&kTranslateMatrix, nX * kScale.x, i * kScale.y, nZ * kScale.z);
-
-					D3DXMATRIX kWorldMatrix;
-					D3DXMatrixMultiply(&kWorldMatrix, &kScaleMatrix, &kTranslateMatrix);
-
-					const D3DXVECTOR4 kColorVector(0.0f, 0.0f, 0.0f, 1.0f);
-
-					pBlockEffect->SetMatrix(pBlockEffect->GetParameterByName(NULL, "kWorld"), &kWorldMatrix);
-					pBlockEffect->SetVector(pBlockEffect->GetParameterByName(NULL, "kColor"), &kColorVector);
-					pBlockEffect->SetTexture(pBlockEffect->GetParameterByName(NULL, "kTexture"), pBlockTexture);
-					pBlockEffect->CommitChanges();
-
-					kContext.DrawTriangles(Vertex::Block::Declaration, pBlockVertexBuffer, sizeof(Vertex::Block), FacesPerCube * TrianglesPerFace);
-				}
-			}
-		}
-
-		pBlockEffect->EndPass();
-		pBlockEffect->End();
-	}
-}
-
-/*
- * RenderBot
- */
-void View::RenderBot(Environment* pEnvironment, Bot* pBot)
-{
-	const D3DXVECTOR3& kPosition	= pBot->GetWorldPosition();
-	const D3DXVECTOR3& kOrientation	= pBot->GetWorldOrientation();
-
-	const float fOffsetX	= -0.5f * (pEnvironment->GetWidth() * pEnvironment->GetScale().x);
-	const float fOffsetZ	= -0.5f * (pEnvironment->GetHeight() * pEnvironment->GetScale().z);
-
-	uint32 nPasses = 0;
-
-	pBlockEffect->SetTechnique(pBlockEffect->GetTechnique(0));
-	pBlockEffect->Begin(&nPasses, 0);
-	pBlockEffect->BeginPass(0);
-
-	D3DXMATRIX kScale;
-	D3DXMatrixScaling(&kScale, pBot->kSize.x, pBot->kSize.y, pBot->kSize.z);
-
-	D3DXMATRIX kTranslate;
-	D3DXMatrixTranslation(&kTranslate, fOffsetX + kPosition.x, kPosition.y, fOffsetZ + kPosition.z);
-
-	D3DXMATRIX kRotate;
-	D3DXMatrixRotationY(&kRotate, kOrientation.y);
-
-	D3DXMATRIX kTempMatrix;
-	D3DXMATRIX kWorldMatrix;
-	D3DXMatrixMultiply(&kWorldMatrix, D3DXMatrixMultiply(&kTempMatrix, &kScale, &kRotate), &kTranslate);
-
-	const float fAlpha	= ((pBot->nColor >> 24) & 0xFF) / 255.0f;
-	const float fRed	= ((pBot->nColor >> 16) & 0xFF) / 255.0f;
-	const float fGreen	= ((pBot->nColor >> 8 ) & 0xFF) / 255.0f;
-	const float fBlue	= ((pBot->nColor >> 0 ) & 0xFF) / 255.0f;
-
-	const D3DXVECTOR4 kColorVector(fRed, fGreen, fBlue, fAlpha);
-
-	pBlockEffect->SetMatrix(pBlockEffect->GetParameterByName(NULL, "kWorld"), &kWorldMatrix);
-	pBlockEffect->SetVector(pBlockEffect->GetParameterByName(NULL, "kColor"), &kColorVector);
-	pBlockEffect->SetTexture(pBlockEffect->GetParameterByName(NULL, "kTexture"), NULL);
-	pBlockEffect->CommitChanges();
-
-	kContext.DrawTriangles(Vertex::Block::Declaration, pBlockVertexBuffer, sizeof(Vertex::Block), FacesPerCube * TrianglesPerFace);
-
-	pBlockEffect->EndPass();
-	pBlockEffect->End();
-}
-
-/*
- * OnMessage
- */
-LRESULT View::OnMessage(UINT nMessage, WPARAM wParam, LPARAM lParam)
-{
-	if(nMessage == WM_CLOSE)
-	{
-		DestroyWindow(kWindow);
-		return 0;
-	}
-	else
-	
-	if(nMessage == WM_DESTROY)
-	{
-		PostQuitMessage(0);
-		return 0;
-	}
-
-	return DefWindowProc(kWindow, nMessage, wParam, lParam);
-}
-
-/*
- * MessageRouter
- */
-LRESULT CALLBACK View::MessageRouter(HWND hWnd, UINT nMessage, WPARAM wParam, LPARAM lParam)
-{
-	if(pView && pView->kWindow)
-	{
-		return pView->OnMessage(nMessage, wParam, lParam);
-	}
-
-	return DefWindowProc(hWnd, nMessage, wParam, lParam);
-}
--- a/LightClone/Source/View.h	Fri Sep 09 13:21:22 2011 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,199 +0,0 @@
-/*
- * View
- */
-
-#ifndef __VIEW_H__
-#define __VIEW_H__
-
-#include "Core.h"
-#include "Model.h"
-#include "RenderContext.h"
-#include "CameraController.h"
-#include "Interface.h"
-
-/*
- * View
- */
-class View
-{
-	/*
-	 * pView
-	 *	Replace this with the Mediator
-	 */
-	static View* pView;
-
-	/*
-	 * pModel
-	 *	Remove
-	 */
-	Model* pModel;
-
-	/*
-	 * kWindow
-	 *	Move to Mediator; Anything requiring access to the window will need to
-	 *	be owned and initialized by the Mediator.
-	 */
-	HWND kWindow;
-
-	/*
-	 * kContext
-	 *	Move to Mediator
-	 */
-	RenderContext kContext;
-
-	/*
-	 * kCameraController
-	 *	Move to World
-	 */
-	CameraController kCameraController;
-
-	/*
-	 * pBlockEffect
-	 *	Move to World
-	 */
-	ID3DXEffect* pBlockEffect;
-
-	/*
-	 * pBlockVertexBuffer
-	 *	Move to World
-	 */
-	IDirect3DVertexBuffer9* pBlockVertexBuffer;
-
-	/*
-	 * pBlockTexture
-	 *	Move to World
-	 */
-	IDirect3DTexture9* pBlockTexture;
-
-	/*
-	 * kInterface
-	 *	Move to World
-	 */
-	Interface kInterface;
-
-public:
-
-	/*
-	 * View
-	 */
-	View(Model* pModel);
-
-	/*
-	 * Initialize
-	 */
-	ErrorCode Initialize();
-
-	/* 
-	 * Terminate
-	 */
-	void Terminate();
-
-	/*
-	 * Render
-	 */
-	void Render();
-
-	/*
-	 * GetWindow
-	 */
-	HWND GetWindow() const;
-
-	/*
-	 * UpdateCameraYaw
-	 */
-	void UpdateCameraYaw(float fDelta);
-
-	/*
-	 * UpdateCameraPitch
-	 */
-	void UpdateCameraPitch(float fDelta);
-
-	/*
-	 * UpdateCameraDistance
-	 */
-	void UpdateCameraDistance(float fDelta);
-
-private:
-
-	/*
-	 * SetupVertexBuffer
-	 */
-	ErrorCode SetupVertexBuffer();
-
-	/*
-	 * Render3D
-	 */
-	void Render3D();
-
-	/* 
-	 * Render2D
-	 */
-	void Render2D();
-
-	/*
-	 * 	RenderEnvironment
-	 */
-	void RenderEnvironment(Environment* pEnvironment);
-
-	/*
-	 * RenderBot
-	 */
-	void RenderBot(Environment* pEnvironment, Bot* pBot);
-
-	/*
-	 * RenderBlock
-	 */
-	void RenderBlock(const D3DXMATRIX& kWorldMatrix, D3DCOLOR kColor);
-
-	/*
-	 * RenderBackground
-	 */
-	void RenderBackground();
-
-	/*
-	 * RenderToolbar
-	 */
-	void RenderToolbar();
-
-	/*
-	 * RenderMain
-	 */
-	void RenderMain();
-
-	/*
-	 * RenderFunction();
-	 */
-	void RenderFunction();
-
-	/*
-	 * RenderControls
-	 */
-	void RenderControls();
-
-	/*
-	 * RenderLevelDialog
-	 */
-	void RenderLevelDialog();
-
-	/*
-	 * RenderGameOverDialog
-	 */
-	void RenderGameOverDialog();
-
-	/*
-	 * RenderCursor
-	 */
-	void RenderCursor();
-
-	/*
-	 * OnMessage
-	 */
-	LRESULT OnMessage(UINT nMessage, WPARAM wParam, LPARAM lParam);
-
-	/*
-	 * MessageRouter
-	 */
-	static LRESULT CALLBACK MessageRouter(HWND hWnd, UINT nMessage, WPARAM wParam, LPARAM lParam);
-};
-
-#endif //__VIEW_H__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LightClone/Source/Window.cpp	Fri Sep 09 16:18:41 2011 -0700
@@ -0,0 +1,122 @@
+/*
+ * Window
+ */
+
+#include "Window.h"
+#include "VertexTypes.h"
+
+/*
+ * kClassName
+ */
+static const TCHAR* kClassName = L"LightCloneClass";
+
+/*
+ * kCaption
+ */
+static const TCHAR* kCaption = L"LightClone";
+
+/*
+ * pInstance
+ */
+Window* Window::pInstance = NULL;
+
+/*
+ * Window
+ */
+Window::Window(WindowCallback* pHandler) : pCallback(pHandler)
+{
+	pInstance	= this;
+	kWindow		= NULL;
+}
+
+/*
+ * Initialize
+ */
+ErrorCode Window::Initialize()
+{
+	HINSTANCE hInstance = GetModuleHandle(NULL);
+
+	WNDCLASSEX kClass;
+	kClass.cbSize			= sizeof(WNDCLASSEX);
+	kClass.cbClsExtra		= 0;
+	kClass.cbWndExtra		= 0;
+	kClass.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
+	kClass.hCursor			= LoadCursor(NULL, IDC_ARROW);
+	kClass.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
+	kClass.hIconSm			= LoadIcon(NULL, IDI_APPLICATION);
+	kClass.hInstance		= hInstance;
+	kClass.lpfnWndProc		= &MessageRouter;
+	kClass.lpszClassName	= kClassName;
+	kClass.lpszMenuName		= NULL;
+	kClass.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
+
+	ATOM kAtom = RegisterClassEx(&kClass);
+	if(kAtom)
+	{
+		const uint32 nStyleEx	= WS_EX_OVERLAPPEDWINDOW;
+		const uint32 nStyle		= WS_OVERLAPPEDWINDOW | WS_VISIBLE;
+
+		RECT kDesktopRectangle;
+		GetClientRect(GetDesktopWindow(), &kDesktopRectangle);
+
+		RECT kRectangle;
+		kRectangle.left		= 0;
+		kRectangle.top		= 0;
+		kRectangle.right	= ScreenSizeX;
+		kRectangle.bottom	= ScreenSizeY;
+
+		AdjustWindowRectEx(&kRectangle, nStyle, FALSE, nStyleEx);
+
+		uint32 nW			= kRectangle.right - kRectangle.left;
+		uint32 nH			= kRectangle.bottom - kRectangle.top;
+		uint32 nX			= ((kDesktopRectangle.right - kDesktopRectangle.left) - nW) / 2;
+		uint32 nY			= ((kDesktopRectangle.bottom - kDesktopRectangle.top) - nH) / 2;
+
+		kWindow = CreateWindowEx(nStyleEx, kClassName, kCaption, nStyle, nX, nY, nW, nH, NULL, NULL, hInstance, NULL);
+		if(kWindow)
+		{
+			UpdateWindow(kWindow);
+
+			return Error_Success;
+		}
+	}
+
+	return Error_Fail;
+}
+
+/* 
+ * Terminate
+ */
+void Window::Terminate()
+{
+	if(kWindow)
+	{
+		DestroyWindow(kWindow);
+	}
+}
+
+/*
+ * GetHandle
+ */
+HWND Window::GetHandle() const
+{
+	return kWindow;
+}
+
+/*
+ * MessageRouter
+ */
+LRESULT CALLBACK Window::MessageRouter(HWND hWnd, UINT nMessage, WPARAM wParam, LPARAM lParam)
+{
+	//NOTE: This only works for a single, global window instance
+	
+	if(pInstance && pInstance->kWindow)
+	{
+		if(pInstance->pCallback)
+		{
+			return pInstance->pCallback->OnMessage(pInstance, nMessage, wParam, lParam);
+		}
+	}
+
+	return DefWindowProc(hWnd, nMessage, wParam, lParam);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LightClone/Source/Window.h	Fri Sep 09 16:18:41 2011 -0700
@@ -0,0 +1,85 @@
+/*
+ * Window
+ */
+
+#ifndef __WINDOW_H__
+#define __WINDOW_H__
+
+#include "Core.h"
+
+/*
+ * Window
+ */
+class Window;
+
+/*
+ * WindowCallback
+ */
+class WindowCallback
+{
+public:
+
+	/*
+	 * ~WindowCallback
+	 */
+	virtual ~WindowCallback()
+	{
+	}
+
+	/*
+	 * OnMessage
+	 */
+	virtual int32 OnMessage(Window* pWindow, uint32 nMessage, WPARAM wParam, LPARAM lParam) = 0;
+};
+
+/*
+ * Window
+ */
+class Window
+{
+	/*
+	 * pInstance
+	 */
+	static Window* pInstance;
+
+	/*
+	 * kWindow
+	 */
+	HWND kWindow;
+
+	/*
+	 * pCallback
+	 */
+	WindowCallback* pCallback;
+
+public:
+
+	/*
+	 * Window
+	 */
+	Window(WindowCallback* pHandler);
+
+	/*
+	 * Initialize
+	 */
+	ErrorCode Initialize();
+
+	/* 
+	 * Terminate
+	 */
+	void Terminate();
+
+	/*
+	 * GetHandle
+	 */
+	HWND GetHandle() const;
+
+private:
+
+	/*
+	 * MessageRouter
+	 */
+	static LRESULT CALLBACK MessageRouter(HWND hWnd, UINT nMessage, WPARAM wParam, LPARAM lParam);
+};
+
+#endif //__WINDOW_H__
--- a/LightClone/Source/World.cpp	Fri Sep 09 13:21:22 2011 -0700
+++ b/LightClone/Source/World.cpp	Fri Sep 09 16:18:41 2011 -0700
@@ -4,3 +4,67 @@
 
 #include "World.h"
 
+/*
+ * World
+ */
+World::World()
+{
+}
+
+/*
+ * Initialize
+ */
+ErrorCode World::Initialize()
+{
+	ErrorCode eCode = Error_Fail;
+
+	/*
+	eCode = kEnvironment.Initialize();
+	if(eCode == Error_Success)
+	{
+		eCode = kBot.Initialize(&kEnvironment);
+		if(eCode == Error_Success)
+		{
+			eCode = kProgram.Initialize();
+			if(eCode == Error_Success)
+			{
+				eCode = kInterface.Initialize();
+			}
+		}
+	}
+	*/
+
+	return eCode;
+}
+
+/*
+ * Terminate
+ */
+void World::Terminate()
+{
+	/*
+	kInterface.Terminate();
+	kProgram.Terminate();
+	kBot.Terminate();
+	kEnvironment.Terminate();
+	*/
+}
+
+/*
+ * Update
+ */
+void World::Update(float fElapsed)
+{
+	//kBot.Update(fElapsed);
+	//kEnvironment.Update(fElapsed);
+	//kInterface.Update(fElapsed);
+}
+
+/*
+ * Render
+ */
+void World::Render()
+{
+	//Render3D();
+	//Render2D();
+}
--- a/LightClone/Source/World.h	Fri Sep 09 13:21:22 2011 -0700
+++ b/LightClone/Source/World.h	Fri Sep 09 16:18:41 2011 -0700
@@ -12,6 +12,29 @@
  */
 class World
 {
+	/*
+	 * kEnvironment
+	 *	The environment is the grid on which the bot is placed and with which it interacts
+	 */
+	//Environment kEnvironment;
+
+	/*
+	 * kBot
+	 *	Represents the robot programmed to solve the challenges
+	 */
+	//Bot kBot;
+
+	/*
+	 * kProgram
+	 *	Stores the current program being edited by the player
+	 */
+
+	/*
+	 * kInterface
+	 *	Manages the tree of UI elements
+	 */
+	//Interface kInterface;
+
 public:
 	
 	/*