2013-12-04 11:32:43 +01:00
|
|
|
#define NOMINMAX
|
|
|
|
#include <Windows.h>
|
|
|
|
#include "Include\DanBiasGame.h"
|
2013-12-06 10:38:43 +01:00
|
|
|
#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"
|
2014-01-14 09:25:22 +01:00
|
|
|
#include <Protocols.h>
|
2013-12-11 12:14:00 +01:00
|
|
|
#include "NetworkClient.h"
|
2014-01-30 15:20:35 +01:00
|
|
|
#include <GameServerAPI.h>
|
2013-12-11 09:00:36 +01:00
|
|
|
|
2013-12-18 10:47:30 +01:00
|
|
|
#include "../WindowManager/WindowShell.h"
|
2013-12-11 09:00:36 +01:00
|
|
|
#include "L_inputClass.h"
|
2013-12-18 12:18:01 +01:00
|
|
|
#include "WinTimer.h"
|
2013-12-18 10:47:30 +01:00
|
|
|
#include "vld.h"
|
2014-01-14 10:25:49 +01:00
|
|
|
#include "GameClientRecieverFunc.h"
|
2013-12-04 11:32:43 +01:00
|
|
|
|
2014-01-31 14:31:27 +01:00
|
|
|
#include "../Misc/EventHandler/EventHandler.h"
|
|
|
|
using namespace Oyster::Event;
|
|
|
|
|
2013-12-04 11:32:43 +01:00
|
|
|
namespace DanBias
|
|
|
|
{
|
|
|
|
|
|
|
|
#pragma region Game Data
|
2013-12-11 12:14:00 +01:00
|
|
|
|
2013-12-04 11:32:43 +01:00
|
|
|
class DanBiasGamePrivateData
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
DanBiasGamePrivateData()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
~DanBiasGamePrivateData()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-12-13 12:02:49 +01:00
|
|
|
public:
|
2013-12-18 10:47:30 +01:00
|
|
|
WindowShell* window;
|
2013-12-13 12:02:49 +01:00
|
|
|
InputClass* inputObj;
|
2014-01-31 10:49:52 +01:00
|
|
|
Utility::WinTimer timer;
|
2014-01-14 10:25:49 +01:00
|
|
|
GameRecieverObject* recieverObj;
|
2014-01-30 15:20:35 +01:00
|
|
|
bool serverOwner;
|
2013-12-04 11:32:43 +01:00
|
|
|
|
|
|
|
} data;
|
|
|
|
#pragma endregion
|
|
|
|
|
2013-12-11 12:14:00 +01:00
|
|
|
|
|
|
|
DanBiasGamePrivateData* DanBiasGame::m_data = new DanBiasGamePrivateData();
|
2014-01-13 12:11:45 +01:00
|
|
|
float DanBiasGame::capFrame = 0;
|
2013-12-13 12:02:49 +01:00
|
|
|
|
2013-12-04 11:32:43 +01:00
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// Interface API functions
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
DanBiasClientReturn DanBiasGame::Initiate(DanBiasGameDesc& desc)
|
|
|
|
{
|
2013-12-18 10:47:30 +01:00
|
|
|
|
2014-01-31 08:41:08 +01:00
|
|
|
WindowShell::CreateConsoleWindow();
|
2013-12-18 10:47:30 +01:00
|
|
|
if(! m_data->window->CreateWin(WindowShell::WINDOW_INIT_DESC()))
|
2013-12-04 11:32:43 +01:00
|
|
|
return DanBiasClientReturn_Error;
|
|
|
|
|
|
|
|
if( FAILED( InitDirect3D() ) )
|
|
|
|
return DanBiasClientReturn_Error;
|
|
|
|
|
2013-12-06 10:38:43 +01:00
|
|
|
if( FAILED( InitInput() ) )
|
2013-12-04 11:32:43 +01:00
|
|
|
return DanBiasClientReturn_Error;
|
|
|
|
|
2014-01-14 10:25:49 +01:00
|
|
|
m_data->recieverObj = new GameRecieverObject;
|
2014-01-30 15:20:35 +01:00
|
|
|
m_data->serverOwner = false;
|
2013-12-17 13:39:10 +01:00
|
|
|
|
2013-12-16 09:45:12 +01:00
|
|
|
// Start in lobby state
|
2014-01-31 11:10:35 +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 10:49:52 +01:00
|
|
|
m_data->timer.reset();
|
2013-12-04 11:32:43 +01:00
|
|
|
return DanBiasClientReturn_Sucess;
|
|
|
|
}
|
|
|
|
|
|
|
|
DanBiasClientReturn DanBiasGame::Run()
|
|
|
|
{
|
|
|
|
// Main message loop
|
2013-12-18 12:18:01 +01:00
|
|
|
while(m_data->window->Frame())
|
2013-12-04 11:32:43 +01:00
|
|
|
{
|
2014-01-31 10:49:52 +01:00
|
|
|
float dt = (float)m_data->timer.getElapsedSeconds();
|
|
|
|
m_data->timer.reset();
|
2013-12-04 11:32:43 +01:00
|
|
|
|
2014-01-13 12:11:45 +01:00
|
|
|
capFrame += dt;
|
|
|
|
if(capFrame > 0.03)
|
|
|
|
{
|
|
|
|
if(Update(dt) != S_OK)
|
|
|
|
return DanBiasClientReturn_Error;
|
|
|
|
if(Render(dt) != S_OK)
|
|
|
|
return DanBiasClientReturn_Error;
|
|
|
|
capFrame = 0;
|
|
|
|
}
|
|
|
|
|
2013-12-04 11:32:43 +01:00
|
|
|
}
|
|
|
|
return DanBiasClientReturn_Sucess;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DanBiasGame::Release()
|
|
|
|
{
|
|
|
|
CleanUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
// Create Direct3D with Oyster Graphics
|
|
|
|
//--------------------------------------------------------------------------------------
|
|
|
|
HRESULT DanBiasGame::InitDirect3D()
|
|
|
|
{
|
2013-12-18 10:47:30 +01:00
|
|
|
if(Oyster::Graphics::API::Init(m_data->window->GetHWND(), false, false, Oyster::Math::Float2( 1024, 768)) != Oyster::Graphics::API::Sucsess)
|
2013-12-04 11:32:43 +01:00
|
|
|
return E_FAIL;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------
|
2013-12-06 10:38:43 +01:00
|
|
|
// Init the input
|
2013-12-04 11:32:43 +01:00
|
|
|
//-------------------------------------------------------------------------------------
|
2013-12-18 10:47:30 +01:00
|
|
|
HRESULT DanBiasGame::InitInput()
|
2013-12-04 11:32:43 +01:00
|
|
|
{
|
2013-12-11 09:00:36 +01:00
|
|
|
m_data->inputObj = new InputClass;
|
2013-12-18 10:47:30 +01:00
|
|
|
if(!m_data->inputObj->Initialize(m_data->window->GetHINSTANCE(), m_data->window->GetHWND(), m_data->window->GetHeight(), m_data->window->GetWidth()))
|
2013-12-04 11:32:43 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2013-12-11 09:00:36 +01:00
|
|
|
m_data->inputObj->Update();
|
2013-12-10 11:26:18 +01:00
|
|
|
|
2014-01-30 15:20:35 +01:00
|
|
|
if(m_data->serverOwner)
|
|
|
|
{
|
|
|
|
DanBias::GameServerAPI::ServerUpdate();
|
|
|
|
}
|
2014-01-30 11:58:44 +01:00
|
|
|
|
2013-12-06 10:38:43 +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);
|
2013-12-04 11:32:43 +01:00
|
|
|
|
2013-12-06 10:38:43 +01:00
|
|
|
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;
|
2013-12-09 11:05:47 +01:00
|
|
|
|
2013-12-06 10:38:43 +01:00
|
|
|
switch (state)
|
|
|
|
{
|
2014-01-30 15:20:35 +01:00
|
|
|
case Client::GameClientState::ClientState_LobbyCreated:
|
|
|
|
m_data->serverOwner = true;
|
2013-12-06 10:38:43 +01:00
|
|
|
case Client::GameClientState::ClientState_Lobby:
|
2013-12-16 11:08:10 +01:00
|
|
|
m_data->recieverObj->gameClientState = new Client::LobbyState();
|
2013-12-06 10:38:43 +01:00
|
|
|
break;
|
|
|
|
case Client::GameClientState::ClientState_Game:
|
2013-12-16 11:08:10 +01:00
|
|
|
m_data->recieverObj->gameClientState = new Client::GameState();
|
2013-12-06 10:38:43 +01:00
|
|
|
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
|
2013-12-09 11:05:47 +01:00
|
|
|
|
2013-12-06 10:38:43 +01:00
|
|
|
}
|
2013-12-04 11:32:43 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT DanBiasGame::Render(float deltaTime)
|
|
|
|
{
|
|
|
|
int isPressed = 0;
|
2013-12-11 09:00:36 +01:00
|
|
|
if(m_data->inputObj->IsKeyPressed(DIK_A))
|
2013-12-04 11:32:43 +01:00
|
|
|
{
|
|
|
|
isPressed = 1;
|
|
|
|
}
|
2013-12-06 10:38:43 +01:00
|
|
|
|
2013-12-04 11:32:43 +01:00
|
|
|
wchar_t title[255];
|
|
|
|
swprintf(title, sizeof(title), L"| Pressing A: %d | \n", (int)(isPressed));
|
2013-12-18 10:47:30 +01:00
|
|
|
SetWindowText(m_data->window->GetHWND(), title);
|
2013-12-06 10:38:43 +01:00
|
|
|
|
2013-12-16 11:08:10 +01:00
|
|
|
m_data->recieverObj->gameClientState->Render();
|
2013-12-04 11:32:43 +01:00
|
|
|
|
|
|
|
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;
|
2013-12-11 09:00:36 +01:00
|
|
|
delete m_data->inputObj;
|
2013-12-09 09:23:39 +01:00
|
|
|
delete m_data;
|
2013-12-11 09:00:36 +01:00
|
|
|
|
2013-12-09 11:05:47 +01:00
|
|
|
|
|
|
|
Oyster::Graphics::API::Clean();
|
2013-12-04 11:32:43 +01:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} //End namespace DanBias
|