Danbias/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp

207 lines
5.2 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 10:49:52 +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();
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
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 10:49:52 +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 10:49:52 +01:00
float dt = (float)m_data->timer.getElapsedSeconds();
m_data->timer.reset();
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;
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)
{
2014-01-31 14:31:27 +01:00
2014-01-31 10:49:52 +01:00
if(m_data->recieverObj->IsConnected())
m_data->recieverObj->Update();
2014-01-31 09:14:55 +01:00
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)
{
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;
case Client::GameClientState::ClientState_Lobby:
2013-12-16 11:08:10 +01:00
m_data->recieverObj->gameClientState = new Client::LobbyState();
break;
case Client::GameClientState::ClientState_Game:
2013-12-16 11:08:10 +01:00
m_data->recieverObj->gameClientState = new Client::GameState();
break;
default:
return E_FAIL;
break;
}
2014-01-29 10:18:01 +01:00
m_data->recieverObj->gameClientState->Init(m_data->recieverObj); // send game client
}
return S_OK;
}
HRESULT DanBiasGame::Render(float deltaTime)
{
int isPressed = 0;
if(m_data->inputObj->IsKeyPressed(DIK_A))
{
isPressed = 1;
}
wchar_t title[255];
swprintf(title, sizeof(title), L"| Pressing A: %d | \n", (int)(isPressed));
SetWindowText(m_data->window->GetHWND(), title);
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;
Oyster::Graphics::API::Clean();
return S_OK;
}
} //End namespace DanBias