Mercurial > LightClone
changeset 9:e494c4295dba
Conversion to World architecture
author | koryspansel |
---|---|
date | Fri, 09 Sep 2011 18:36:18 -0700 |
parents | 968341ab1fb2 |
children | 292e534f00c2 |
files | LightClone/LightClone.vcproj LightClone/Source/Bot.cpp LightClone/Source/Bot.h LightClone/Source/Camera.cpp LightClone/Source/Camera.h LightClone/Source/CameraController.cpp LightClone/Source/CameraController.h LightClone/Source/Core.h LightClone/Source/Interface.cpp LightClone/Source/Interface.h LightClone/Source/Mediator.cpp LightClone/Source/Mediator.h LightClone/Source/RenderContext.cpp LightClone/Source/RenderContext.h LightClone/Source/World.cpp LightClone/Source/World.h LightClone/ToDo.txt |
diffstat | 17 files changed, 1365 insertions(+), 1099 deletions(-) [+] |
line wrap: on
line diff
--- a/LightClone/LightClone.vcproj Fri Sep 09 16:18:41 2011 -0700 +++ b/LightClone/LightClone.vcproj Fri Sep 09 18:36:18 2011 -0700 @@ -185,6 +185,10 @@ > </File> <File + RelativePath=".\Source\Camera.cpp" + > + </File> + <File RelativePath=".\Source\CameraController.cpp" > </File> @@ -283,6 +287,10 @@ > </File> <File + RelativePath=".\Source\Camera.h" + > + </File> + <File RelativePath=".\Source\CameraController.h" > </File>
--- a/LightClone/Source/Bot.cpp Fri Sep 09 16:18:41 2011 -0700 +++ b/LightClone/Source/Bot.cpp Fri Sep 09 18:36:18 2011 -0700 @@ -4,8 +4,7 @@ #include "Bot.h" #include "Clock.h" -#include <d3d9.h> -#include <d3dx9.h> +#include "VertexTypes.h" /* * DirectionAngle @@ -30,12 +29,62 @@ pEnvironment = 0; nState = BotState_Idle; kSize = D3DXVECTOR3(0.55f, 1.00f, 0.55f); + + pEffect = NULL; + pVertexBuffer = NULL; + pTexture = NULL; } /* * Initialize */ -void Bot::Initialize(Environment* pInstance) +ErrorCode Bot::Initialize(ResourceManager* pResourceManager) +{ + ErrorCode eCode = pResourceManager->CreateEffectFromFile("Data\\Shaders\\Environment.fx", &pEffect); + if(eCode == Error_Success) + { + //eCode = pResourceManager->CreateTextureFromFile("Data\\Textures\\Block02.tga", &pTexture); + if(eCode == Error_Success) + { + eCode = pResourceManager->CreateVertexBuffer(VerticesPerBlock * sizeof(Vertex::Block), D3DUSAGE_WRITEONLY, D3DPOOL_MANAGED, &pVertexBuffer); + if(eCode == Error_Success) + { + eCode = SetupVertexBuffer(); + } + } + } + + return eCode; +} + +/* + * Terminate + */ +void Bot::Terminate() +{ + if(pVertexBuffer) + { + pVertexBuffer->Release(); + pVertexBuffer = NULL; + } + + if(pTexture) + { + pTexture->Release(); + pTexture = NULL; + } + + if(pEffect) + { + pEffect->Release(); + pEffect = NULL; + } +} + +/* + * Setup + */ +void Bot::Setup(Environment* pInstance) { pEnvironment = pInstance; nState = BotState_Idle; @@ -159,44 +208,6 @@ } /* - * Compile - */ -ErrorCode Bot::Compile(Code* pInstance, uint8* pData, uint32 nSize) -{ - const uint32 nLength = pInstance->GetLength(); - - for(uint32 i = 0; i < nLength; ++i) - { - const uint32 nAction = pInstance->GetSlot(i); - - if(Action_Forward <= nAction && nAction <= Action_Light) - { - if(nSize < 2) - return Error_Fail; - - *pData++ = Instruction_Action; - *pData++ = nAction; - - nSize -= 2; - } - else - - if(Action_FunctionA <= nAction && nAction <= Action_FunctionB) - { - if(nSize < 2) - return Error_Fail; - - *pData++ = Instruction_Call; - *pData++ = nAction - Action_FunctionA + 1; - - nSize -= 2; - } - } - - return Error_Success; -} - -/* * Update */ bool Bot::Update(float fElapsed) @@ -343,3 +354,155 @@ return false; } + +/* + * Render + */ +void Bot::Render(RenderContext& kContext, Camera& kCamera) +{ + const D3DXVECTOR3& kPosition = GetWorldPosition(); + const D3DXVECTOR3& kOrientation = GetWorldOrientation(); + + const float fOffsetX = -0.5f * (pEnvironment->GetWidth() * pEnvironment->GetScale().x); + const float fOffsetZ = -0.5f * (pEnvironment->GetHeight() * pEnvironment->GetScale().z); + + uint32 nPasses = 0; + + kContext.Apply(kCamera, pEffect); + + pEffect->SetTechnique(pEffect->GetTechnique(0)); + pEffect->Begin(&nPasses, 0); + pEffect->BeginPass(0); + + D3DXMATRIX kScale; + D3DXMatrixScaling(&kScale, kSize.x, kSize.y, 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 = ((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); + + pEffect->SetMatrix(pEffect->GetParameterByName(NULL, "kWorld"), &kWorldMatrix); + pEffect->SetVector(pEffect->GetParameterByName(NULL, "kColor"), &kColorVector); + pEffect->SetTexture(pEffect->GetParameterByName(NULL, "kTexture"), pTexture); + pEffect->CommitChanges(); + + kContext.DrawTriangles(Vertex::Block::Declaration, pVertexBuffer, sizeof(Vertex::Block), FacesPerCube * TrianglesPerFace); + + pEffect->EndPass(); + pEffect->End(); +} + +/* + * Compile + */ +ErrorCode Bot::Compile(Code* pInstance, uint8* pData, uint32 nSize) +{ + const uint32 nLength = pInstance->GetLength(); + + for(uint32 i = 0; i < nLength; ++i) + { + const uint32 nAction = pInstance->GetSlot(i); + + if(Action_Forward <= nAction && nAction <= Action_Light) + { + if(nSize < 2) + return Error_Fail; + + *pData++ = Instruction_Action; + *pData++ = nAction; + + nSize -= 2; + } + else + + if(Action_FunctionA <= nAction && nAction <= Action_FunctionB) + { + if(nSize < 2) + return Error_Fail; + + *pData++ = Instruction_Call; + *pData++ = nAction - Action_FunctionA + 1; + + nSize -= 2; + } + } + + return Error_Success; +} + +/* + * SetupVertexBuffer + */ +ErrorCode Bot::SetupVertexBuffer() +{ + Vertex::Block* pVertices = NULL; + + HRESULT hResult = pVertexBuffer->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); + + pVertexBuffer->Unlock(); + + return Error_Success; +}
--- a/LightClone/Source/Bot.h Fri Sep 09 16:18:41 2011 -0700 +++ b/LightClone/Source/Bot.h Fri Sep 09 18:36:18 2011 -0700 @@ -6,6 +6,8 @@ #define __BOT_H__ #include "Core.h" +#include "RenderContext.h" +#include "ResourceManager.h" #include "VirtualMachine.h" #include "Environment.h" #include "Code.h" @@ -78,6 +80,21 @@ private: /* + * pEffect + */ + ID3DXEffect* pEffect; + + /* + * pVertexBuffer + */ + IDirect3DVertexBuffer9* pVertexBuffer; + + /* + * pTexture + */ + IDirect3DTexture9* pTexture; + + /* * pEnvironment */ Environment* pEnvironment; @@ -135,7 +152,17 @@ /* * Reset */ - void Initialize(Environment* pInstance); + ErrorCode Initialize(ResourceManager* pResourceManager); + + /* + * Terminate + */ + void Terminate(); + + /* + * Setup + */ + void Setup(Environment* pInstance); /* * Reset @@ -167,12 +194,22 @@ */ bool Update(float fElapsed); + /* + * Render + */ + void Render(RenderContext& kContext, Camera& kCamera); + private: /* * Compile */ ErrorCode Compile(Code* pCode, uint8* pData, uint32 nSize); + + /* + * SetupVertexBuffer + */ + ErrorCode SetupVertexBuffer(); }; #endif //__BOT_H__
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LightClone/Source/Camera.cpp Fri Sep 09 18:36:18 2011 -0700 @@ -0,0 +1,12 @@ +/* + * Camera + */ + +#include "Camera.h" + +/* + * ~Camera + */ +Camera::~Camera() +{ +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LightClone/Source/Camera.h Fri Sep 09 18:36:18 2011 -0700 @@ -0,0 +1,43 @@ +/* + * Camera + */ + +#ifndef __CAMERA_H__ +#define __CAMERA_H__ + +#include "Core.h" + +/* + * Camera + */ +class Camera +{ +public: + + /* + * ~Camera + */ + virtual ~Camera(); + + /* + * GetLocation + */ + virtual const D3DXVECTOR3 GetLocation() const = 0; + + /* + * GetDirection + */ + virtual const D3DXVECTOR3 GetDirection() const = 0; + + /* + * GetRight + */ + virtual const D3DXVECTOR3 GetRight() const = 0; + + /* + * GetUp + */ + virtual const D3DXVECTOR3 GetUp() const = 0; +}; + +#endif //__CAMERA_H__
--- a/LightClone/Source/CameraController.cpp Fri Sep 09 16:18:41 2011 -0700 +++ b/LightClone/Source/CameraController.cpp Fri Sep 09 18:36:18 2011 -0700 @@ -48,11 +48,35 @@ /* * GetLocation */ -const D3DXVECTOR3 CameraController::GetLocation(const D3DXVECTOR3& kTarget) const +const D3DXVECTOR3 CameraController::GetLocation() const { const float fOffsetX = fCameraDistance * sinf(0.5f * D3DX_PI - fCameraPitch) * cosf(fCameraYaw); const float fOffsetY = fCameraDistance * cosf(0.5f * D3DX_PI - fCameraPitch); const float fOffsetZ = fCameraDistance * sinf(0.5f * D3DX_PI - fCameraPitch) * sinf(fCameraYaw); - return D3DXVECTOR3(kTarget.x + fOffsetX, kTarget.y + fOffsetY, kTarget.z + fOffsetZ); + return D3DXVECTOR3(fOffsetX, fOffsetY, fOffsetZ); +} + +/* + * GetDirection + */ +const D3DXVECTOR3 CameraController::GetDirection() const +{ + return D3DXVECTOR3(0.0f, 0.0f, 1.0f); } + +/* + * GetRight + */ +const D3DXVECTOR3 CameraController::GetRight() const +{ + return D3DXVECTOR3(1.0f, 0.0f, 0.0f); +} + +/* + * GetUp + */ +const D3DXVECTOR3 CameraController::GetUp() const +{ + return D3DXVECTOR3(0.0f, 1.0f, 0.0f); +}
--- a/LightClone/Source/CameraController.h Fri Sep 09 16:18:41 2011 -0700 +++ b/LightClone/Source/CameraController.h Fri Sep 09 18:36:18 2011 -0700 @@ -6,11 +6,12 @@ #define __CAMERACONTROLLER_H__ #include "Core.h" +#include "Camera.h" /* * CameraController */ -class CameraController +class CameraController : public Camera { public: @@ -34,7 +35,6 @@ /* * CameraController */ - //CameraController(float fInitialDistance = 16.0f, float fInitialYaw = D3DX_PI / 6.0f, float fInitialPitch = D3DX_PI / 4.0); CameraController(float fInitialDistance = 16.0f, float fInitialYaw = 0.306f, float fInitialPitch = 0.875f); /* @@ -60,7 +60,22 @@ /* * GetLocation */ - const D3DXVECTOR3 GetLocation(const D3DXVECTOR3& kTarget) const; + virtual const D3DXVECTOR3 GetLocation() const; + + /* + * GetDirection + */ + virtual const D3DXVECTOR3 GetDirection() const; + + /* + * GetRight + */ + virtual const D3DXVECTOR3 GetRight() const; + + /* + * GetUp + */ + virtual const D3DXVECTOR3 GetUp() const; }; #endif //__CAMERACONTROLLER_H__
--- a/LightClone/Source/Core.h Fri Sep 09 16:18:41 2011 -0700 +++ b/LightClone/Source/Core.h Fri Sep 09 18:36:18 2011 -0700 @@ -60,6 +60,29 @@ }; /* + * GameState + */ +enum +{ + GameState_Idle, + GameState_LoadMap, + GameState_Active, + GameState_Complete, + GameState_Over, + GameState_Confirm, + GameState_Exit, +}; + +/* + * SimulationState + */ +enum +{ + SimulationState_Idle, + SimulationState_Active, +}; + +/* * Action */ enum
--- a/LightClone/Source/Interface.cpp Fri Sep 09 16:18:41 2011 -0700 +++ b/LightClone/Source/Interface.cpp Fri Sep 09 18:36:18 2011 -0700 @@ -221,7 +221,7 @@ /* * Render */ -void Interface::Render(RenderContext& kContext) +void Interface::Render(RenderContext& kContext, Camera& kCamera) { /* if(pModel->nGameState != GameState_Exit) @@ -663,3 +663,9 @@ } */ } + + +/* +Drag & Drop Handling: + 1. Separate into 2 stages: drag detection and drop detection +*/ \ No newline at end of file
--- a/LightClone/Source/Interface.h Fri Sep 09 16:18:41 2011 -0700 +++ b/LightClone/Source/Interface.h Fri Sep 09 18:36:18 2011 -0700 @@ -86,7 +86,7 @@ /* * Render */ - void Render(RenderContext& kContext); + void Render(RenderContext& kContext, Camera& kCamera); private:
--- 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; - } - */ - } - } -}
--- a/LightClone/Source/Mediator.h Fri Sep 09 16:18:41 2011 -0700 +++ b/LightClone/Source/Mediator.h Fri Sep 09 18:36:18 2011 -0700 @@ -9,10 +9,7 @@ #include "Clock.h" #include "Window.h" #include "InputManager.h" -#include "Loader.h" -#include "Environment.h" -#include "Bot.h" -#include "Code.h" +#include "World.h" #include "DragController.h" #include "ButtonPane.h" #include "Dialog.h" @@ -23,41 +20,6 @@ #include "Interface.h" /* - * MediatorState - */ -enum -{ - MediatorState_Initialize, - MediatorState_Splash, - MediatorState_Game, - MediatorState_Complete, - MediatorState_Exit, -}; - -/* - * 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 : public WindowCallback @@ -73,96 +35,6 @@ Clock kClock; /* - * kLoader - */ - Loader kLoader; - - /* - * kEnvironment - */ - Environment kEnvironment; - - /* - * kBot - */ - Bot kBot; - - /* - * kFunction - */ - Code* pFunction; - - /* - * nGameState - */ - uint32 nGameState; - - /* - * nSimulationState - */ - uint32 nSimulationState; - - /* - * nCurrentLevel - */ - uint32 nCurrentLevel; - - /* - * nCurrentFunction - */ - uint32 nCurrentFunction; - - /* - * 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; - - /* * pGraphicsDevice */ GraphicsDevice* pGraphicsDevice; @@ -178,34 +50,14 @@ RenderContext kContext; /* - * kCameraController - * Move to World + * kInputManager */ - CameraController kCameraController; - - /* - * pBlockEffect - * Move to World - */ - ID3DXEffect* pBlockEffect; + InputManager kInputManager; /* - * pBlockVertexBuffer - * Move to World + * kWorld */ - IDirect3DVertexBuffer9* pBlockVertexBuffer; - - /* - * pBlockTexture - * Move to World - */ - IDirect3DTexture9* pBlockTexture; - - /* - * kInterface - * Move to World - */ - Interface kInterface; + World kWorld; public: @@ -250,56 +102,6 @@ * 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/RenderContext.cpp Fri Sep 09 16:18:41 2011 -0700 +++ b/LightClone/Source/RenderContext.cpp Fri Sep 09 18:36:18 2011 -0700 @@ -118,6 +118,14 @@ } /* + * Apply + */ +void RenderContext::Apply(Camera& kCamera, ID3DXEffect* pEffect) +{ + //TODO: +} + +/* * SetupCamera3D */ void RenderContext::SetupCamera3D(ID3DXEffect* pEffect, const D3DXVECTOR3& kLocation, const D3DXVECTOR3& kTarget, float fViewAngle)
--- a/LightClone/Source/RenderContext.h Fri Sep 09 16:18:41 2011 -0700 +++ b/LightClone/Source/RenderContext.h Fri Sep 09 18:36:18 2011 -0700 @@ -7,6 +7,7 @@ #include "Core.h" #include "GraphicsDevice.h" +#include "Camera.h" /* * RenderContext @@ -86,6 +87,11 @@ void End(); /* + * Apply + */ + void Apply(Camera& kCamera, ID3DXEffect* pEffect); + + /* * SetupCamera3D */ void SetupCamera3D(ID3DXEffect* pEffect, const D3DXVECTOR3& kLocation, const D3DXVECTOR3& kTarget, float fViewAngle = D3DX_PI / 4.0f);
--- a/LightClone/Source/World.cpp Fri Sep 09 16:18:41 2011 -0700 +++ b/LightClone/Source/World.cpp Fri Sep 09 18:36:18 2011 -0700 @@ -3,36 +3,109 @@ */ #include "World.h" +#include "VertexTypes.h" /* * World */ -World::World() +World::World() : kToolbar(8), kMain(16), kFunction(8), kControls(4) { + pBlockEffect = NULL; + pBlockVertexBuffer = NULL; + pBlockTexture = NULL; + + 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); } /* * Initialize */ -ErrorCode World::Initialize() +ErrorCode World::Initialize(ResourceManager* pResourceManager, InputManager* pInput) { ErrorCode eCode = Error_Fail; - /* - eCode = kEnvironment.Initialize(); - if(eCode == Error_Success) + if(pResourceManager && pInput) { - eCode = kBot.Initialize(&kEnvironment); + pInputManager = pInput; + + eCode = pResourceManager->CreateEffectFromFile("Data\\Shaders\\Environment.fx", &pBlockEffect); if(eCode == Error_Success) { - eCode = kProgram.Initialize(); + eCode = pResourceManager->CreateTextureFromFile("Data\\Textures\\Block02.tga", &pBlockTexture); if(eCode == Error_Success) { - eCode = kInterface.Initialize(); + eCode = pResourceManager->CreateVertexBuffer(VerticesPerBlock * sizeof(Vertex::Block), D3DUSAGE_WRITEONLY, D3DPOOL_MANAGED, &pBlockVertexBuffer); + if(eCode == Error_Success) + { + eCode = SetupVertexBuffer(); + if(eCode == Error_Success) + { + eCode = kInterface.Initialize(pResourceManager); + if(eCode == Error_Success) + { + eCode = kBot.Initialize(pResourceManager); + if(eCode == Error_Success) + { + nCurrentLevel = 0; + nGameState = GameState_LoadMap; + } + } + } + } } } } - */ return eCode; } @@ -42,29 +115,705 @@ */ void World::Terminate() { + kInterface.Terminate(); + kBot.Terminate(); + + if(pBlockVertexBuffer) + { + pBlockVertexBuffer->Release(); + pBlockVertexBuffer = NULL; + } + + if(pBlockTexture) + { + pBlockTexture->Release(); + pBlockTexture = NULL; + } + + if(pBlockEffect) + { + pBlockEffect->Release(); + pBlockEffect = 0; + } + /* - kInterface.Terminate(); kProgram.Terminate(); - kBot.Terminate(); kEnvironment.Terminate(); */ } /* + * Activate + */ +void World::Activate() +{ + nGameState = GameState_LoadMap; +} + +/* + * Deactivate + */ +void World::Deactivate() +{ + nGameState = GameState_Exit; +} + +/* + * IsActive + */ +bool World::IsActive() +{ + return nGameState != GameState_Exit; +} + +/* + * Load + */ +bool World::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.Setup(&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 World::ResetEnvironment() +{ + kEnvironment.Reset(); +} + +/* + * ResetBot + */ +void World::ResetBot() +{ + kBot.Reset(); + kBot.kPosition = kLoader.GetInitialPosition(); + kBot.kDirection = kLoader.GetInitialDirection(); +} + +/* + * ResetCode + */ +void World::ClearCode() +{ + for(uint32 i = 0; i < kLoader.GetFunctionCount(); ++i) + { + pFunction[i].Clear(); + } +} + +/* * Update */ void World::Update(float fElapsed) { - //kBot.Update(fElapsed); - //kEnvironment.Update(fElapsed); - //kInterface.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; + } + } + } + } } /* * Render */ -void World::Render() +void World::Render(RenderContext& kContext) +{ + const uint32 nColor = D3DCOLOR_XRGB(32, 32, 32); + + kContext.Begin(nColor); + + if(nGameState >= GameState_Active) + { + Render3D(kContext); + Render2D(kContext); + } + + kContext.End(); +} + +/* + * SetupVertexBuffer + */ +ErrorCode World::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 World::Render3D(RenderContext& kContext) +{ + 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, kTarget + kCameraController.GetLocation(), kTarget); + + RenderEnvironment(kContext, &kEnvironment); + RenderBot(kContext, &kEnvironment, &kBot); +} + +/* + * Render2D + */ +void World::Render2D(RenderContext& kContext) +{ + kInterface.Render(kContext, kCameraController); +} + +/* + * RenderEnvironment + */ +void World::RenderEnvironment(RenderContext& kContext, 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 World::RenderBot(RenderContext& kContext, Environment* pEnvironment, Bot* pBot) { - //Render3D(); - //Render2D(); + //* + 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(); + //*/ + //kBot.Render(kContext, kCameraController); } + +/* + * ProcessInput + */ +void World::ProcessInput(float fElapsed) +{ + pInputManager->Update(fElapsed); + + #if defined(_DEBUG) + if(pInputManager->IsKeyDown(DIK_LEFT)) + { + kCameraController.Yaw(0.01f); + } + else + + if(pInputManager->IsKeyDown(DIK_RIGHT)) + { + kCameraController.Yaw(-0.01f); + } + + if(pInputManager->IsKeyDown(DIK_UP)) + { + kCameraController.Pitch(0.01f); + } + else + + if(pInputManager->IsKeyDown(DIK_DOWN)) + { + kCameraController.Pitch(-0.01f); + } + + if(pInputManager->IsKeyDown(DIK_NEXT)) + { + kCameraController.Move(0.1f); + } + else + + if(pInputManager->IsKeyDown(DIK_PRIOR)) + { + kCameraController.Move(-0.1f); + } + #endif + + const float fMouseX = pInputManager->GetMouseX(); + const float fMouseY = pInputManager->GetMouseY(); + + if(nGameState == GameState_Active) + { + if(pInputManager->IsButtonDown(0) && !pInputManager->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(!pInputManager->IsButtonDown(0) && pInputManager->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(pInputManager->IsButtonDown(0) && !pInputManager->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(pInputManager->IsButtonDown(0) && !pInputManager->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(pInputManager->IsButtonDown(0) && !pInputManager->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/World.h Fri Sep 09 16:18:41 2011 -0700 +++ b/LightClone/Source/World.h Fri Sep 09 18:36:18 2011 -0700 @@ -6,6 +6,18 @@ #define __WORLD_H__ #include "Core.h" +#include "RenderContext.h" +#include "Environment.h" +#include "Bot.h" +#include "Code.h" +#include "Loader.h" +#include "CameraController.h" +#include "DragController.h" +#include "ResourceManager.h" +#include "InputManager.h" +#include "ButtonPane.h" +#include "Dialog.h" +#include "Interface.h" /* * World @@ -13,27 +25,123 @@ class World { /* + * pInputManager + */ + InputManager* pInputManager; + + /* + * kLoader + */ + Loader kLoader; + + /* * kEnvironment - * The environment is the grid on which the bot is placed and with which it interacts */ - //Environment kEnvironment; + Environment kEnvironment; /* * kBot - * Represents the robot programmed to solve the challenges + */ + Bot kBot; + + /* + * kFunction + */ + Code* pFunction; + + /* + * nGameState + */ + uint32 nGameState; + + /* + * nSimulationState + */ + uint32 nSimulationState; + + /* + * nCurrentLevel */ - //Bot kBot; + uint32 nCurrentLevel; + + /* + * nCurrentFunction + */ + uint32 nCurrentFunction; + + /* + * kCameraController + */ + CameraController kCameraController; + + /* + * kDragController + */ + DragController kDragController; + + /* + * kToolbar + */ + ButtonPane kToolbar; /* - * kProgram - * Stores the current program being edited by the player + * kMain + */ + ButtonPane kMain; + + /* + * kFunction + */ + ButtonPane kFunction; + + /* + * kControlBounds + */ + ButtonPane kControls; + + /* + * kArrowBounds + */ + Rectangle2 kArrowBounds[2]; + + /* + * kDialog1Bounds */ + Rectangle2 kDialog1Bounds[1]; + + /* + * kDialog2Bounds + */ + Rectangle2 kDialog2Bounds[2]; + + /* + * pBlockEffect + * Move to World + */ + ID3DXEffect* pBlockEffect; + + /* + * pBlockVertexBuffer + * Move to World + */ + IDirect3DVertexBuffer9* pBlockVertexBuffer; + + /* + * pBlockTexture + * Move to World + */ + IDirect3DTexture9* pBlockTexture; /* * kInterface - * Manages the tree of UI elements + * Move to World */ - //Interface kInterface; + Interface kInterface; + + /* + * kDialog + */ + Dialog kDialog; public: @@ -45,7 +153,7 @@ /* * Initialize */ - ErrorCode Initialize(); + ErrorCode Initialize(ResourceManager* pResource, InputManager* pInput); /* * Terminate @@ -53,6 +161,41 @@ void Terminate(); /* + * Activate + */ + void Activate(); + + /* + * Deactivate + */ + void Deactivate(); + + /* + * IsActive + */ + bool IsActive(); + + /* + * Load + */ + bool Load(const char* pName); + + /* + * ResetEnvironment + */ + void ResetEnvironment(); + + /* + * ResetBot + */ + void ResetBot(); + + /* + * ResetCode + */ + void ClearCode(); + + /* * Update */ void Update(float fElapsed); @@ -60,7 +203,39 @@ /* * Render */ - void Render(); + void Render(RenderContext& kContext); + +private: + + /* + * SetupVertexBuffer + */ + ErrorCode SetupVertexBuffer(); + + /* + * Render3D + */ + void Render3D(RenderContext& kContext); + + /* + * Render2D + */ + void Render2D(RenderContext& kContext); + + /* + * RenderEnvironment + */ + void RenderEnvironment(RenderContext& kContext, Environment* pEnvironment); + + /* + * RenderBot + */ + void RenderBot(RenderContext& kContext, Environment* pEnvironment, Bot* pBot); + + /* + * ProcessInput + */ + void ProcessInput(float fElapsed); }; #endif //__WORLD_H__
--- a/LightClone/ToDo.txt Fri Sep 09 16:18:41 2011 -0700 +++ b/LightClone/ToDo.txt Fri Sep 09 18:36:18 2011 -0700 @@ -5,9 +5,9 @@ 5. Pause menu 6. Robot model & texture 7. Separate button images from dialog box -8. Finish level completion dialog -9. Implement game over dialog -10. Implement confirm exit dialog -11. Create textures for each button state -12. Help interface -13. Add button to clear code panes \ No newline at end of file +8. Implement confirm exit dialog +9. Create textures for each button state +10. Help interface +11. Add button to clear code panes +12. Get interface working again +13. Finish conversion to world model \ No newline at end of file