Danbias/Code/Game/GameClient/DanBiasGame_Impl.cpp

300 lines
7.5 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"
#include <Protocols.h>
#include "NetworkClient.h"
#include <GameServerAPI.h>
#include "../WindowManager/WindowShell.h"
2014-02-20 14:58:42 +01:00
#include "Win32\Win32Input.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
#include "GameClientState\SharedStateContent.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-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 );
namespace DanBias
{
#pragma region Game Data
class DanBiasGamePrivateData
{
2013-12-13 12:02:49 +01:00
public:
WindowShell* window;
2014-02-17 11:27:43 +01:00
InputClass inputObj;
2014-01-31 14:14:20 +01:00
Utility::WinTimer timer;
2014-02-17 11:27:43 +01:00
2014-02-12 10:43:06 +01:00
UniquePointer<Client::GameClientState> state;
2014-02-17 11:27:43 +01:00
SharedStateContent sharedStateContent;
NetworkClient networkClient;
2014-02-17 11:27:43 +01:00
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())))
2014-02-18 10:28:46 +01:00
WindowShell::WINDOW_INIT_DESC winDesc;
2014-02-20 14:58:42 +01:00
winDesc.windowSize.x = 1280;
winDesc.windowSize.y = 720;
winDesc.windowProcCallback = WindowCallBack;
2014-02-18 10:28:46 +01:00
if(! data.window->CreateWin(winDesc) )
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
2014-02-17 11:27:43 +01:00
data.sharedStateContent.network = &data.networkClient;
data.sharedStateContent.input = &data.inputObj;
// 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-12 16:31:15 +01:00
Graphics::API::Update( dt );
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-12 14:33:56 +01:00
if(Render() != S_OK)
return DanBiasClientReturn_Error;
2014-02-19 15:57:52 +01:00
data.capFrame -= 0.03f;
}
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()
{
2014-02-12 15:02:29 +01:00
Oyster::Graphics::API::Option p;
p.modelPath = L"..\\Content\\Models\\";
p.texturePath = L"..\\Content\\Textures\\";
2014-02-18 10:28:46 +01:00
p.Resolution = Oyster::Math::Float2( 1280.0f, 720.0f );
//! @todo fix proper amb value
2014-02-19 13:38:36 +01:00
p.AmbientValue = 1.3f;
2014-02-12 15:02:29 +01:00
if(Oyster::Graphics::API::Init(data.window->GetHWND(), false, false, p) != Oyster::Graphics::API::Sucsess)
2014-02-12 15:02:29 +01:00
return E_FAIL;
return S_OK;
}
//--------------------------------------------------------------------------------------
// Init the input
//-------------------------------------------------------------------------------------
HRESULT DanBiasGame::InitInput()
{
2014-02-17 11:27:43 +01:00
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)
{
2014-02-14 11:26:50 +01:00
{ // updating mouse input
2014-02-20 11:12:53 +01:00
// TODO: Is obosolete when Dennis's input system is wired in
2014-02-14 11:26:50 +01:00
POINT mousePos;
GetCursorPos( &mousePos );
RECT windowVertex;
GetWindowRect( data.window->GetHWND(), &windowVertex );
float mouseNormalisedX = (float)(mousePos.x - windowVertex.left);
mouseNormalisedX /= (float)(windowVertex.right - windowVertex.left);
float mouseNormalisedY = (float)(mousePos.y - windowVertex.top);
mouseNormalisedY /= (float)(windowVertex.bottom - windowVertex.top);
2014-02-17 11:27:43 +01:00
data.inputObj.Update( mouseNormalisedX, mouseNormalisedY );
2014-02-20 11:12:53 +01:00
}
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;
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 )
{
PAINTSTRUCT ps;
HDC hdc;
switch ( message )
{
case WM_PAINT:
hdc = BeginPaint( handle, &ps );
EndPaint( handle, &ps );
break;
case WM_DESTROY:
PostQuitMessage( 0 );
break;
case WM_KEYDOWN:
switch( wParam )
{
case VK_ESCAPE:
PostQuitMessage( 0 );
break;
}
break;
}
return DefWindowProc( handle, message, wParam, lParam );
}
void ClientEventFunction( NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e )
{
if( DanBias::data.state )
DanBias::data.state->DataRecieved( e );
}