1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
#include "Game.h"
#include "Entity.h"
#include "dxManager.h"
#include "diManager.h"
/* Init the one and only ProjectManager */
static dxManager& dxMgr = dxManager::getInstance();
/* Get a reference to the DirectInput Manager */
static diManager& diMgr = diManager::getInstance();
CGame::CGame(IDirect3DDevice9 *device) : m_d3ddev(device)
{
m_entity = NULL;
curX = (SCREEN_WIDTH/2) - (IMAGE_WIDTH/2);
curY = (SCREEN_HEIGHT/2) - (IMAGE_HEIGHT/2);
arrows = dxMgr.getSurfaceFromBitmap(L"Cross Hairs.bmp");
mouseRect.top = 0;
mouseRect.bottom = mouseRect.top + 48;
mouseRect.left = 0;
mouseRect.right = mouseRect.left + 48;
if (NULL == arrows) {MessageBox(NULL, L"Unable to 'Load Cursor'", L"ERROR", MB_OK);}
mouseOldZ = diMgr.getCurMouseZ();
}
CGame::~CGame(void)
{
if (m_entity)
{
delete m_entity;
m_entity=0;
}
if (m_d3ddev)
{
m_d3ddev->Release();
m_d3ddev=0;
}
}
/********************************************************************
* init
* Initializes the game object. This involves setting the initial
* game state and preparing the timer
********************************************************************/
bool CGame::init(HWND wndHandle)
{
QueryPerformanceFrequency(&timerFreq);
// Initialize the in-game objects
/*if (!initGameObjects())
{
MessageBox(wndHandle, L"Unable to 'Init GameObjects'", L"ERROR", MB_OK);
return false;
}*/
// setup the camera for the game
//dxMgr.createCamera(1.0f, 450.0f); // near clip plane, far clip plane
//dxMgr.moveCamera(D3DXVECTOR3(0.0f, 2.0f, 10.0f));
//dxMgr.pointCamera(D3DXVECTOR3(0.0f, 0.0f, -20.0f));
//pCam = new CCamera(D3DXVECTOR3(0,0,-10.0));
return true;
}
/********************************************************************
* shutdown
* This function is used to release any memory that the game
* uses
********************************************************************/
void CGame::shutdown(void)
{
dxMgr.shutdown();
}
bool CGame::LoadXFile(LPCTSTR filename, unsigned int firstAnimSet)
{
if(m_entity)
{
MessageBox(NULL, L"Could not 'Create Entity'", L"ERROR", MB_OK);
delete m_entity;
m_entity = 0;
}
m_entity = new CEntity(m_d3ddev);
if(!m_entity->Load(filename))
{
MessageBox(NULL, L"Could not 'Create Entity'", L"ERROR", MB_OK);
delete m_entity;
m_entity = 0;
return false;
}
m_entity->SetAnimationSet(firstAnimSet);
return true;
}
/********************************************************************
* update
* This function is called once per frame. It's used to get the user
* input and move the objects in the game accordingly.
********************************************************************/
int CGame::update(void)
{
// Get the current user input
diMgr.getInput();
// set the destination rectangle
mouseDestRect.top = curY;
mouseDestRect.bottom = mouseDestRect.top + 48;
mouseDestRect.left = curX;
mouseDestRect.right = mouseDestRect.left + 48;
// If the mouse is moved to the left, rotate camera
if (diMgr.getCurMouseX() < 0)
{
//pMan->move(CMan::LEFT, anim_rate);
dxMgr.pCam->Yaw( sin(D3DXToRadian( (FLOAT)diMgr.getCurMouseX() ) )/ 8 );
}
// If the mouse is moved to the right, rotate camera
if (diMgr.getCurMouseX() > 0)
{
//pMan->move(CMan::RIGHT, anim_rate);
dxMgr.pCam->Yaw( sin(D3DXToRadian( (FLOAT)diMgr.getCurMouseX() ) ) / 8 );
}
if (diMgr.getCurMouseY() < 0)
{
dxMgr.pCam->Pitch(sin(D3DXToRadian( (FLOAT)diMgr.getCurMouseY() ) )/ 8 );
}
if (diMgr.getCurMouseY() > 0)
{
dxMgr.pCam->Pitch(sin(D3DXToRadian( (FLOAT)diMgr.getCurMouseY() ) )/ 8 );
}
if (KEYDOWN(diMgr.buffer, DIK_W) || (diMgr.getCurMouseZ() - mouseOldZ > 0))
{
dxMgr.pCam->MoveForward((FLOAT)CAMERA_MOVEMENT_INCREMENT);
}
if (KEYDOWN(diMgr.buffer, DIK_S) || (diMgr.getCurMouseZ() - mouseOldZ < 0))
{
dxMgr.pCam->MoveForward((FLOAT)-CAMERA_MOVEMENT_INCREMENT);
}
if (KEYDOWN(diMgr.buffer, DIK_A))
{
dxMgr.pCam->MoveRight((FLOAT)-CAMERA_MOVEMENT_INCREMENT);
}
if (KEYDOWN(diMgr.buffer, DIK_D))
{
dxMgr.pCam->MoveRight((FLOAT)CAMERA_MOVEMENT_INCREMENT);
}
if (KEYDOWN(diMgr.buffer, DIK_Q))
{
dxMgr.pCam->Roll((FLOAT)-CAMERA_ROTATION_INCREMENT);
}
if (KEYDOWN(diMgr.buffer, DIK_E))
{
dxMgr.pCam->Roll((FLOAT)CAMERA_ROTATION_INCREMENT);
}
// Move Cursor
if(diMgr.getCurMouseX()<=0 && curX>0 )
curX += diMgr.getCurMouseX();
if(diMgr.getCurMouseX()<=0 && curX<0)
curX = 0;
if(diMgr.getCurMouseX()>0 && curX<SCREEN_WIDTH)
curX += diMgr.getCurMouseX();
if(diMgr.getCurMouseX()>0 && curX>SCREEN_WIDTH - IMAGE_WIDTH)
curX = SCREEN_WIDTH - IMAGE_WIDTH;
if(diMgr.getCurMouseY()<=0 && curY>0)
curY += diMgr.getCurMouseY();
if(diMgr.getCurMouseY()<=0 && curY<0)
curY = 0;
if(diMgr.getCurMouseY()>0 && curY<SCREEN_HEIGHT)
curY += diMgr.getCurMouseY();
if(diMgr.getCurMouseY()>0 && curY>SCREEN_HEIGHT)
{
curY = SCREEN_HEIGHT;
}
return 1;
}
/********************************************************************
* render
* Renders the in-game objects based on a timer
********************************************************************/
void CGame::render(void)
{
// get the time before rendering
QueryPerformanceCounter(&timeStart);
static DWORD lastTime=timeGetTime();
// call our render function
dxMgr.beginRender();
dxMgr.blitToSurface(arrows, &mouseRect, &mouseDestRect);
if (m_entity)
{
float timeElapsed=0.001f*(timeGetTime()-lastTime);
lastTime=timeGetTime();
// Need to pass the high level matrix used to position and orientate the model
// Create this from current position and rotations:
D3DXMATRIX matRotX,matRotY,matRotZ,matTrans;
// Calculate rotation matrix
D3DXMatrixRotationX( &matRotX, 0 );
D3DXMatrixRotationY( &matRotY, 0 );
D3DXMatrixRotationZ( &matRotZ, 0 );
// Calculate a translation matrix
D3DXMatrixTranslation(&matTrans,0,0,0);
// Calculate our world matrix by multiplying the above (in the correct order)
D3DXMATRIX matWorld=(matRotX*matRotY*matRotZ)*matTrans;
// FrameMove carries out the animation while Render just draws
m_entity->AdjustFrame(timeElapsed,&matWorld);
m_entity->Render();
}
dxMgr.endRender();
// get the updated time
QueryPerformanceCounter(&timeEnd);
// determine the animation rate
anim_rate = ( (float)timeEnd.QuadPart - (float)timeStart.QuadPart ) / timerFreq.QuadPart;
}
|