Danbias/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp

219 lines
5.4 KiB
C++
Raw Normal View History

#define NOMINMAX
#include <Windows.h>
#include "Include\DanBiasGame.h"
#include "DllInterfaces/GFXAPI.h"
#include "GameClientState/GameClientState.h"
#include "GameClientState\GameState.h"
#include "GameClientState\LobbyState.h"
2014-01-30 11:58:44 +01:00
#include "GameClientState\LoginState.h"
2014-01-30 23:23:37 +01:00
#include "GameClientState\LanMenuState.h"
#include <Protocols.h>
#include "NetworkClient.h"
#include <GameServerAPI.h>
#include "../WindowManager/WindowShell.h"
#include "L_inputClass.h"
2013-12-18 12:18:01 +01:00
#include "WinTimer.h"
#include "vld.h"
#include "GameClientRecieverFunc.h"
2014-01-31 14:31:27 +01:00
#include "../Misc/EventHandler/EventHandler.h"
using namespace Oyster::Event;
namespace DanBias
{
#pragma region Game Data
class DanBiasGamePrivateData
{
public:
DanBiasGamePrivateData()
{
}
~DanBiasGamePrivateData()
{
}
2013-12-13 12:02:49 +01:00
public:
WindowShell* window;
2013-12-13 12:02:49 +01:00
InputClass* inputObj;
2014-01-31 14:14:20 +01:00
Utility::WinTimer timer;
GameRecieverObject* recieverObj;
bool serverOwner;
} data;
#pragma endregion
DanBiasGamePrivateData* DanBiasGame::m_data = new DanBiasGamePrivateData();
float DanBiasGame::capFrame = 0;
2013-12-13 12:02:49 +01:00
//--------------------------------------------------------------------------------------
// Interface API functions
//--------------------------------------------------------------------------------------
DanBiasClientReturn DanBiasGame::Initiate(DanBiasGameDesc& desc)
{
WindowShell::CreateConsoleWindow();
2014-01-31 22:52:52 +01:00
//if(! m_data->window->CreateWin(WindowShell::WINDOW_INIT_DESC(L"Window", cPOINT(1600, 900), cPOINT())))
if(! m_data->window->CreateWin(WindowShell::WINDOW_INIT_DESC()))
return DanBiasClientReturn_Error;
if( FAILED( InitDirect3D() ) )
return DanBiasClientReturn_Error;
if( FAILED( InitInput() ) )
return DanBiasClientReturn_Error;
m_data->recieverObj = new GameRecieverObject;
m_data->serverOwner = false;
2013-12-17 13:39:10 +01:00
// Start in lobby state
2014-01-31 14:14:20 +01:00
m_data->recieverObj->gameClientState = new Client::LoginState();
2014-01-29 10:18:01 +01:00
if(!m_data->recieverObj->gameClientState->Init(m_data->recieverObj))
2013-12-18 12:18:01 +01:00
return DanBiasClientReturn_Error;
2013-12-13 12:02:49 +01:00
2014-01-31 14:14:20 +01:00
m_data->timer.reset();
return DanBiasClientReturn_Sucess;
}
DanBiasClientReturn DanBiasGame::Run()
{
// Main message loop
2013-12-18 12:18:01 +01:00
while(m_data->window->Frame())
{
2014-01-31 14:14:20 +01:00
float dt = (float)m_data->timer.getElapsedSeconds();
m_data->timer.reset();
2014-02-04 10:29:02 +01:00
if(m_data->recieverObj->IsConnected())
m_data->recieverObj->Update();
capFrame += dt;
if(capFrame > 0.03)
{
if(Update(dt) != S_OK)
return DanBiasClientReturn_Error;
if(Render(dt) != S_OK)
return DanBiasClientReturn_Error;
capFrame = 0;
}
}
return DanBiasClientReturn_Sucess;
}
void DanBiasGame::Release()
{
CleanUp();
}
//--------------------------------------------------------------------------------------
// Create Direct3D with Oyster Graphics
//--------------------------------------------------------------------------------------
HRESULT DanBiasGame::InitDirect3D()
{
if(Oyster::Graphics::API::Init(m_data->window->GetHWND(), false, false, Oyster::Math::Float2( 1024, 768)) != Oyster::Graphics::API::Sucsess)
return E_FAIL;
Oyster::Graphics::API::Option p;
p.modelPath = L"..\\Content\\Models\\";
p.texturePath = L"..\\Content\\Textures\\";
Oyster::Graphics::API::SetOptions(p);
return S_OK;
}
//--------------------------------------------------------------------------------------
// Init the input
//-------------------------------------------------------------------------------------
HRESULT DanBiasGame::InitInput()
{
m_data->inputObj = new InputClass;
if(!m_data->inputObj->Initialize(m_data->window->GetHINSTANCE(), m_data->window->GetHWND(), m_data->window->GetHeight(), m_data->window->GetWidth()))
{
MessageBox(0, L"Could not initialize the input object.", L"Error", MB_OK);
return E_FAIL;
}
return S_OK;
}
HRESULT DanBiasGame::Update(float deltaTime)
{
//Update menu buttons
EventHandler::Instance().Update(m_data->inputObj);
m_data->inputObj->Update();
if(m_data->serverOwner)
{
DanBias::GameServerAPI::ServerUpdate();
}
2014-01-30 11:58:44 +01:00
DanBias::Client::GameClientState::ClientState state = DanBias::Client::GameClientState::ClientState_Same;
2013-12-16 11:08:10 +01:00
state = m_data->recieverObj->gameClientState->Update(deltaTime, m_data->inputObj);
if(state != Client::GameClientState::ClientState_Same)
{
bool stateVal = false;
2013-12-16 11:08:10 +01:00
m_data->recieverObj->gameClientState->Release();
delete m_data->recieverObj->gameClientState;
m_data->recieverObj->gameClientState = NULL;
switch (state)
{
case Client::GameClientState::ClientState_LobbyCreated:
m_data->serverOwner = true;
stateVal = true;
case Client::GameClientState::ClientState_Lobby:
2013-12-16 11:08:10 +01:00
m_data->recieverObj->gameClientState = new Client::LobbyState();
stateVal = true;
break;
case Client::GameClientState::ClientState_Game:
break;
default:
return E_FAIL;
break;
}
if(stateVal)
{
m_data->recieverObj->gameClientState->Init(m_data->recieverObj); // send game client
}
else
{
}
}
return S_OK;
}
HRESULT DanBiasGame::Render(float deltaTime)
{
2013-12-16 11:08:10 +01:00
m_data->recieverObj->gameClientState->Render();
return S_OK;
}
HRESULT DanBiasGame::CleanUp()
{
2013-12-16 11:08:10 +01:00
m_data->recieverObj->gameClientState->Release();
delete m_data->recieverObj->gameClientState;
2014-01-29 10:18:01 +01:00
m_data->recieverObj->Disconnect();
2013-12-16 11:08:10 +01:00
delete m_data->recieverObj;
delete m_data->inputObj;
delete m_data;
2014-01-31 22:52:52 +01:00
Oyster::Graphics::API::Clean();
2014-01-31 22:52:52 +01:00
GameServerAPI::ServerStop();
return S_OK;
}
} //End namespace DanBias