Mercurial > LightClone
diff LightClone/Source/Mediator.cpp @ 9:e494c4295dba
Conversion to World architecture
author | koryspansel |
---|---|
date | Fri, 09 Sep 2011 18:36:18 -0700 |
parents | 968341ab1fb2 |
children | 7081e8e6008c |
line wrap: on
line diff
--- a/LightClone/Source/Mediator.cpp Fri Sep 09 16:18:41 2011 -0700 +++ b/LightClone/Source/Mediator.cpp Fri Sep 09 18:36:18 2011 -0700 @@ -4,7 +4,6 @@ #include "Mediator.h" #include "VertexTypes.h" -//#include <windows.h> #pragma warning(disable:4355) @@ -16,65 +15,9 @@ /* * Mediator */ -Mediator::Mediator() : kWindow(this), kToolbar(8), kMain(16), kFunction(8), kControls(4) +Mediator::Mediator() : kWindow(this) { - 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; + pGraphicsDevice = NULL; } /* @@ -89,8 +32,7 @@ kClock.Reset(); - while(nGameState != GameState_Exit) - //while(pWorld->IsActive()) + while(kWorld.IsActive()) { ProcessMessages(); @@ -139,59 +81,6 @@ */ 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); - - ErrorCode eCode = pView->Initialize(); - if(eCode == Error_Success) - { - eCode = pController->Initialize(); - if(eCode == Error_Success) - { - nCurrentLevel = 0; - nGameState = GameState_LoadMap; - } - } - */ - ErrorCode eCode = kWindow.Initialize(); if(eCode == Error_Success) { @@ -223,43 +112,12 @@ 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); + eCode = kWorld.Initialize(&kResourceManager, &kInputManager); 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; @@ -270,29 +128,10 @@ */ void Mediator::Terminate() { - kInterface.Terminate(); - - if(pBlockVertexBuffer) - { - pBlockVertexBuffer->Release(); - pBlockVertexBuffer = NULL; - } - - if(pBlockTexture) - { - pBlockTexture->Release(); - pBlockTexture = NULL; - } - - if(pBlockEffect) - { - pBlockEffect->Release(); - pBlockEffect = 0; - } - - kContext.Terminate(); + kWorld.Terminate(); kInputManager.Terminate(); kResourceManager.Terminate(); + kContext.Terminate(); GraphicsDevice::Destroy(pGraphicsDevice); } @@ -302,35 +141,7 @@ */ void Mediator::Update(float 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); + kWorld.Update(fElapsed); } /* @@ -338,19 +149,7 @@ */ void Mediator::Render() { - const uint32 nColor = D3DCOLOR_XRGB(32, 32, 32); - - kContext.Begin(nColor); - - if(nGameState >= GameState_Active) - { - Render3D(); - Render2D(); - } - - kContext.End(); - - //kWorld.Render(kRenderContext); + kWorld.Render(kContext); } /* @@ -364,8 +163,7 @@ { if(kMessage.message == WM_QUIT) { - nGameState = GameState_Exit; - //kWorld.Deactivate(); + kWorld.Deactivate(); break; } @@ -373,606 +171,3 @@ DispatchMessage(&kMessage); } } - -/* - * 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; - } - */ - } - } -}