Danbias/Code/Game/GameClient/DanBiasGame_Impl.cpp

294 lines
7.6 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"
2014-02-14 14:31:01 +01:00
#include "GameClientState\NetLoadState.h"
2014-02-25 16:42:51 +01:00
#include "Utilities.h"
#include <Protocols.h>
#include "NetworkClient.h"
#include <GameServerAPI.h>
#include "../WindowManager/WindowShell.h"
2013-12-18 12:18:01 +01:00
#include "WinTimer.h"
#include "vld.h"
2014-02-17 11:50:51 +01:00
#include "EventHandler/EventHandler.h"
2014-02-17 11:27:43 +01:00
2014-02-20 16:15:47 +01:00
#include "GameClientState/SharedStateContent.h"
2014-02-21 09:36:43 +01:00
#include "Utilities.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-02-17 11:27:43 +01:00
using namespace ::DanBias::Client;
2014-02-21 09:36:43 +01:00
using namespace ::Utility::DynamicMemory;
2014-01-31 14:31:27 +01:00
2014-02-20 14:58:42 +01:00
LRESULT CALLBACK WindowCallBack(HWND handle, UINT message, WPARAM wParam, LPARAM lParam );
void ClientEventFunction( NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e );
2014-02-21 09:36:43 +01:00
#pragma region Game Data
namespace DanBias
{
class DanBiasGamePrivateData
{
2013-12-13 12:02:49 +01:00
public:
WindowShell* window;
2014-02-27 10:49:22 +01:00
Utility::WinTimer timer, graphicalTimer;
2014-02-17 11:27:43 +01:00
2014-02-12 10:43:06 +01:00
UniquePointer<Client::GameClientState> state;
NetworkClient networkClient;
2014-02-20 16:15:47 +01:00
SharedStateContent sharedStateContent;
2014-02-17 11:27:43 +01:00
bool serverOwner;
float capFrame;
DanBiasGamePrivateData()
{
2014-02-25 16:42:51 +01:00
this->sharedStateContent.network = nullptr;
this->sharedStateContent.mouseDevice = nullptr;
this->sharedStateContent.keyboardDevice = nullptr;
2014-02-26 14:07:46 +01:00
this->sharedStateContent.mouseSensitivity = Utility::Value::Radian( 0.6f );
2014-02-25 16:42:51 +01:00
this->serverOwner = false;
this->capFrame = 0;
}
2014-02-12 17:20:42 +01:00
2014-02-21 09:36:43 +01:00
~DanBiasGamePrivateData()
{
}
} data;
}
#pragma endregion
2014-02-21 09:36:43 +01:00
namespace DanBias
{
//--------------------------------------------------------------------------------------
// Interface API functions
//--------------------------------------------------------------------------------------
2014-02-20 16:15:47 +01:00
DanBiasClientReturn DanBiasGame::Initiate( DanBiasGameDesc& desc )
{
WindowShell::CreateConsoleWindow();
//if(! data.window->CreateWin(WindowShell::WINDOW_INIT_DESC(L"Window", cPOINT(1600, 900), cPOINT())))
2014-02-18 10:28:46 +01:00
WindowShell::WINDOW_INIT_DESC winDesc;
2014-02-26 16:10:04 +01:00
winDesc.windowSize.x = 1280;
winDesc.windowSize.y = 720;
2014-02-20 14:58:42 +01:00
winDesc.windowProcCallback = WindowCallBack;
2014-02-18 10:28:46 +01:00
if(! data.window->CreateWin(winDesc) )
return DanBiasClientReturn_Error;
2014-02-21 09:36:43 +01:00
if( FAILED( InitInput(data.window->GetHWND()) ) )
return DanBiasClientReturn_Error;
2014-02-21 09:36:43 +01:00
if( FAILED( InitDirect3D() ) )
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
2014-02-17 11:27:43 +01:00
data.sharedStateContent.network = &data.networkClient;
// Start in main menu state
data.state = new Client::MainState();
2014-02-17 11:27:43 +01:00
if( !data.state->Init( data.sharedStateContent ) )
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-04 10:29:02 +01:00
data.capFrame += dt;
2014-02-19 15:57:52 +01:00
if(data.capFrame > 0.03f)
{
2014-02-19 13:38:36 +01:00
switch( Update(data.capFrame) )
2014-02-12 17:20:42 +01:00
{
case Result_continue: break;
case Result_quit: return DanBiasClientReturn_Success;
case Result_error: return DanBiasClientReturn_Error;
default: break;
}
2014-02-21 12:38:25 +01:00
Graphics::API::Update( data.capFrame );
2014-02-12 14:33:56 +01:00
if(Render() != S_OK)
return DanBiasClientReturn_Error;
2014-02-27 09:04:12 +01:00
data.capFrame = 0.0f;
}
2014-02-17 16:16:27 +01:00
if(data.networkClient.IsConnected())
data.networkClient.Update();
}
2014-02-12 17:20:42 +01:00
return DanBiasClientReturn_Success;
}
void DanBiasGame::Release()
{
CleanUp();
}
//--------------------------------------------------------------------------------------
// Create Direct3D with Oyster Graphics
//--------------------------------------------------------------------------------------
HRESULT DanBiasGame::InitDirect3D()
{
Oyster::Graphics::API::Option p;
p.modelPath = L"..\\Content\\Models\\";
p.texturePath = L"..\\Content\\Textures\\";
2014-02-27 10:49:22 +01:00
p.resolution = Oyster::Math::Float2( 1920.0f, 1080.0f );
//! @todo fix proper amb value
2014-02-27 10:49:22 +01:00
p.ambientValue = 0.3f;
p.globalGlowTint = Math::Float3(1);
p.globalTint = Math::Float3(1);
p.fullscreen = false;
2014-02-12 15:02:29 +01:00
2014-02-27 10:49:22 +01:00
if(Oyster::Graphics::API::Init(data.window->GetHWND(), false, p) != Oyster::Graphics::API::Sucsess)
2014-02-12 15:02:29 +01:00
return E_FAIL;
return S_OK;
}
//--------------------------------------------------------------------------------------
// Init the input
//-------------------------------------------------------------------------------------
2014-02-21 09:36:43 +01:00
HRESULT DanBiasGame::InitInput( HWND handle )
{
2014-02-21 09:36:43 +01:00
data.sharedStateContent.mouseDevice = dynamic_cast<Input::Mouse*>( ::Input::InputManager::Instance()->CreateDevice(Input::Enum::SAIType_Mouse, handle) );
2014-02-20 16:15:47 +01:00
if( !data.sharedStateContent.mouseDevice )
{
2014-02-20 16:15:47 +01:00
MessageBox( 0, L"Could not initialize the mouseDevice.", L"Error", MB_OK );
return E_FAIL;
}
2014-02-20 16:15:47 +01:00
2014-02-21 12:09:38 +01:00
data.sharedStateContent.keyboardDevice= dynamic_cast<Input::Keyboard*>( ::Input::InputManager::Instance()->CreateDevice(Input::Enum::SAIType_Keyboard, handle) );
if( !data.sharedStateContent.keyboardDevice )
2014-02-20 16:15:47 +01:00
{
MessageBox( 0, L"Could not initialize the raw keyboard device.", L"Error", MB_OK );
return E_FAIL;
}
return S_OK;
}
DanBiasGame::Result DanBiasGame::Update(float deltaTime)
{
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;
2014-02-17 11:27:43 +01:00
state = data.state->Update( deltaTime );
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-14 14:31:01 +01:00
case Client::GameClientState::ClientState_Main:
data.state = new Client::MainState();
2014-02-13 13:30:14 +01:00
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;
2014-02-14 14:31:01 +01:00
case Client::GameClientState::ClientState_LobbyCreate:
data.state = new Client::LobbyAdminState();
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-14 14:31:01 +01:00
case Client::GameClientState::ClientState_NetLoad:
data.state = new Client::NetLoadState();
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 )
{
2014-02-17 11:27:43 +01:00
data.state->Init( data.sharedStateContent ); // 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();
2014-02-13 09:45:45 +01:00
data.state = nullptr;
2014-02-20 16:15:47 +01:00
Input::InputManager::DestroyInputManager();
2014-02-13 09:45:45 +01:00
EventHandler::Instance().Clean();
Graphics::API::Clean();
2014-01-31 22:52:52 +01:00
GameServerAPI::ServerStop();
return S_OK;
}
} //End namespace DanBias
2014-02-20 14:58:42 +01:00
LRESULT CALLBACK WindowCallBack(HWND handle, UINT message, WPARAM wParam, LPARAM lParam )
{
2014-02-21 09:36:43 +01:00
switch ( message )
{
case WM_DESTROY:
PostQuitMessage( 0 );
break;
2014-02-21 21:04:30 +01:00
case WM_INPUT:
message = 0;
break;
2014-02-21 12:09:38 +01:00
default: break;
2014-02-21 09:36:43 +01:00
}
2014-02-20 14:58:42 +01:00
return DefWindowProc( handle, message, wParam, lParam );
}
void ClientEventFunction( NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e )
{
if( DanBias::data.state )
DanBias::data.state->DataRecieved( e );
}