Danbias/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp

234 lines
5.8 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-02-13 13:30:14 +01:00
#include "GameClientState\LobbyAdminState.h"
2014-02-12 10:43:06 +01:00
#include "GameClientState\MainState.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"
2014-01-31 14:31:27 +01:00
#include "../Misc/EventHandler/EventHandler.h"
2014-02-12 10:43:06 +01:00
2014-02-12 16:31:15 +01:00
using namespace ::Oyster;
2014-02-12 17:20:42 +01:00
using namespace ::Oyster::Event;
using namespace ::Oyster::Network;
2014-02-12 10:43:06 +01:00
using namespace ::Utility::DynamicMemory;
2014-01-31 14:31:27 +01:00
void ClientEventFunction( NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e );
namespace DanBias
{
#pragma region Game Data
class 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;
2014-02-12 10:43:06 +01:00
UniquePointer<Client::GameClientState> state;
NetworkClient networkClient;
bool serverOwner;
float capFrame;
DanBiasGamePrivateData()
{
this->capFrame = 0;
}
} data;
2014-02-12 17:20:42 +01:00
#pragma endregion
//--------------------------------------------------------------------------------------
// Interface API functions
//--------------------------------------------------------------------------------------
DanBiasClientReturn DanBiasGame::Initiate(DanBiasGameDesc& desc)
{
WindowShell::CreateConsoleWindow();
//if(! data.window->CreateWin(WindowShell::WINDOW_INIT_DESC(L"Window", cPOINT(1600, 900), cPOINT())))
if(! data.window->CreateWin(WindowShell::WINDOW_INIT_DESC()))
return DanBiasClientReturn_Error;
if( FAILED( InitDirect3D() ) )
return DanBiasClientReturn_Error;
if( FAILED( InitInput() ) )
return DanBiasClientReturn_Error;
data.serverOwner = false;
2013-12-17 13:39:10 +01:00
data.networkClient.SetMessagePump( ClientEventFunction );
2014-02-12 10:43:06 +01:00
// Start in main menu state
data.state = new Client::MainState();
if( !data.state->Init( &data.networkClient ) )
2013-12-18 12:18:01 +01:00
return DanBiasClientReturn_Error;
2013-12-13 12:02:49 +01:00
data.timer.reset();
2014-02-12 17:20:42 +01:00
return DanBiasClientReturn_Success;
}
DanBiasClientReturn DanBiasGame::Run()
{
// Main message loop
while(data.window->Frame())
{
float dt = (float)data.timer.getElapsedSeconds();
data.timer.reset();
2014-02-12 16:31:15 +01:00
Graphics::API::Update( dt );
2014-02-12 14:33:56 +01:00
if(data.networkClient.IsConnected())
data.networkClient.Update();
2014-02-04 10:29:02 +01:00
data.capFrame += dt;
if(data.capFrame > 0.03)
{
2014-02-12 17:20:42 +01:00
switch( Update(dt) )
{
case Result_continue: break;
case Result_quit: return DanBiasClientReturn_Success;
case Result_error: return DanBiasClientReturn_Error;
default: break;
}
2014-02-12 14:33:56 +01:00
if(Render() != S_OK)
return DanBiasClientReturn_Error;
data.capFrame = 0;
}
}
2014-02-12 17:20:42 +01:00
return DanBiasClientReturn_Success;
}
void DanBiasGame::Release()
{
CleanUp();
}
//--------------------------------------------------------------------------------------
// Create Direct3D with Oyster Graphics
//--------------------------------------------------------------------------------------
HRESULT DanBiasGame::InitDirect3D()
{
2014-02-12 15:02:29 +01:00
Oyster::Graphics::API::Option p;
p.modelPath = L"..\\Content\\Models\\";
p.texturePath = L"..\\Content\\Textures\\";
Oyster::Graphics::API::SetOptions(p);
2014-02-12 15:02:29 +01:00
if(Oyster::Graphics::API::Init(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()
{
data.inputObj = new InputClass;
if(!data.inputObj->Initialize(data.window->GetHINSTANCE(), data.window->GetHWND(), data.window->GetHeight(), data.window->GetWidth()))
{
MessageBox(0, L"Could not initialize the input object.", L"Error", MB_OK);
return E_FAIL;
}
return S_OK;
}
2014-02-12 17:20:42 +01:00
DanBiasGame::Result DanBiasGame::Update(float deltaTime)
{
data.inputObj->Update();
2014-02-12 17:20:42 +01:00
if( data.serverOwner )
{
DanBias::GameServerAPI::ServerUpdate();
}
2014-01-30 11:58:44 +01:00
DanBias::Client::GameClientState::ClientState state = DanBias::Client::GameClientState::ClientState_Same;
state = data.state->Update( deltaTime, data.inputObj );
2014-02-12 17:20:42 +01:00
if( state != Client::GameClientState::ClientState_Same )
{
2014-02-12 17:20:42 +01:00
bool stateChanged = false;
data.state->Release();
switch (state)
{
2014-02-12 16:31:15 +01:00
case Client::GameClientState::ClientState_LobbyCreate:
2014-02-13 13:30:14 +01:00
data.state = new Client::LobbyAdminState();
stateChanged = true;
break;
2014-02-12 17:20:42 +01:00
case Client::GameClientState::ClientState_Lan:
data.state = new Client::LanMenuState();
stateChanged = true;
break;
case Client::GameClientState::ClientState_Lobby:
data.state = new Client::LobbyState();
2014-02-12 17:20:42 +01:00
stateChanged = true;
break;
case Client::GameClientState::ClientState_Game:
2014-02-12 17:20:42 +01:00
data.state = new Client::GameState();
stateChanged = true;
break;
2014-02-12 17:20:42 +01:00
case Client::GameClientState::ClientState_Quit:
data.state->Release();
return Result_quit;
default:
2014-02-12 17:20:42 +01:00
data.state->Release();
return Result_error;
}
2014-02-12 17:20:42 +01:00
if( stateChanged )
{
data.state->Init( &data.networkClient ); // send game client
2014-02-12 17:20:42 +01:00
}
}
2014-02-12 17:20:42 +01:00
return Result_continue;
}
2014-02-12 14:33:56 +01:00
HRESULT DanBiasGame::Render( )
{
data.state->Render();
return S_OK;
}
HRESULT DanBiasGame::CleanUp()
{
2014-02-12 17:20:42 +01:00
if( data.networkClient.IsConnected() )
data.networkClient.Disconnect();
delete data.inputObj;
2014-01-31 22:52:52 +01:00
2014-02-13 09:45:45 +01:00
data.state = nullptr;
EventHandler::Instance().Clean();
Graphics::API::Clean();
2014-01-31 22:52:52 +01:00
GameServerAPI::ServerStop();
return S_OK;
}
} //End namespace DanBias
void ClientEventFunction( NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e )
{
if( DanBias::data.state )
DanBias::data.state->DataRecieved( e );
}