Added back to main menu NOTE MEMORY LEAKS

This commit is contained in:
dean11 2014-02-27 11:41:17 +01:00
parent ca715dbe05
commit a80b1fa96f
9 changed files with 141 additions and 146 deletions

View File

@ -16,7 +16,6 @@
#include "../WindowManager/WindowShell.h"
#include "WinTimer.h"
#include "vld.h"
#include "EventHandler/EventHandler.h"
@ -192,37 +191,42 @@ namespace DanBias
}
DanBias::Client::GameClientState::ClientState state = DanBias::Client::GameClientState::ClientState_Same;
state = data.state->Update( deltaTime );
if( state != Client::GameClientState::ClientState_Same )
{
bool stateChanged = false;
data.state->Release();
switch (state)
{
case Client::GameClientState::ClientState_Main:
data.networkClient.Disconnect();
data.state->Release();
data.state = new Client::MainState();
stateChanged = true;
break;
case Client::GameClientState::ClientState_Lan:
data.state->Release();
data.state = new Client::LanMenuState();
stateChanged = true;
break;
case Client::GameClientState::ClientState_Lobby:
data.state->Release();
data.state = new Client::LobbyState();
stateChanged = true;
break;
case Client::GameClientState::ClientState_LobbyCreate:
data.state->Release();
data.state = new Client::LobbyAdminState();
stateChanged = true;
break;
case Client::GameClientState::ClientState_Game:
data.state->Release();
data.state = new Client::GameState();
stateChanged = true;
break;
case Client::GameClientState::ClientState_NetLoad:
data.state->Release();
data.state = new Client::NetLoadState();
stateChanged = true;
break;

View File

@ -8,6 +8,7 @@ using namespace ::GameLogic;
using namespace ::Utility::Value;
using namespace ::Oyster::Math;
using namespace ::Input;
using namespace ::Input::Enum;
GamingUI::GamingUI() :
GameStateUI()
@ -17,6 +18,10 @@ GamingUI::GamingUI() :
this->camera = nullptr;
this->plane = nullptr;
this->text = nullptr;
this->key_backward = false;
this->key_forward = false;
this->key_strafeLeft = false;
this->key_strafeRight = false;
this->nextState = GameStateUI::UIState_same;
}
@ -25,10 +30,16 @@ GamingUI::GamingUI( SharedStateContent* shared, Camera_FPSV2 *camera ) :
{
this->sharedData = shared;
this->camera = camera;
this->plane = nullptr;
this->text = nullptr;
this->key_backward = false;
this->key_forward = false;
this->key_strafeLeft = false;
this->key_strafeRight = false;
this->nextState = GameStateUI::UIState_same;
}
GamingUI::~GamingUI() { /* Do nothing */ }
GamingUI::~GamingUI() { }
bool GamingUI::Init()
{
// z value should be between 0.5 - 0.9 so that it will be behind other states
@ -40,6 +51,7 @@ bool GamingUI::Init()
// setting input mode to all raw
this->sharedData->keyboardDevice->Activate();
this->sharedData->keyboardDevice->AddKeyboardEvent(this);
this->sharedData->mouseDevice->Activate();
this->sharedData->mouseDevice->AddMouseEvent(this);
@ -73,11 +85,13 @@ void GamingUI::RenderText() const
bool GamingUI::Release()
{
//Release as input event
this->sharedData->keyboardDevice->RemoveKeyboardEvent(this);
this->sharedData->mouseDevice->RemoveMouseEvent(this);
// TODO: Release UI components here.
if(this->plane)
delete this->plane;
if(this->text)
delete this->text;
if(this->plane) delete this->plane;
if(this->text) delete this->text;
this->sharedData = 0;
@ -89,91 +103,10 @@ void GamingUI::SetHPtext( std::wstring hp )
}
void GamingUI::ReadKeyInput()
{
if( this->sharedData->keyboardDevice->IsKeyDown(::Input::Enum::SAKI_W) )
{ // move forward
this->sharedData->network->Send( Protocol_PlayerMovementForward() );
}
if( this->sharedData->keyboardDevice->IsKeyDown(::Input::Enum::SAKI_S) )
{ // move backward
this->sharedData->network->Send( Protocol_PlayerMovementBackward() );
}
if( this->sharedData->keyboardDevice->IsKeyDown(::Input::Enum::SAKI_A) )
{ // strafe left
this->sharedData->network->Send( Protocol_PlayerMovementLeft() );
}
if( this->sharedData->keyboardDevice->IsKeyDown(::Input::Enum::SAKI_D) )
{ // strafe right
this->sharedData->network->Send( Protocol_PlayerMovementRight() );
}
if( this->sharedData->keyboardDevice->IsKeyDown(::Input::Enum::SAKI_Space) )
{ // jump
if(!this->key_Jump)
{
this->sharedData->network->Send( Protocol_PlayerJump() );
this->key_Jump = true;
}
}
else
this->key_Jump = false;
// shoot
//if( this->sharedData->mouseDevice->IsBtnDown(::Input::Enum::SAMI_MouseLeftBtn) )
//{
// if( !this->key_Shoot )
// {
// Protocol_PlayerShot playerShot;
// playerShot.primaryPressed = true;
// playerShot.secondaryPressed = false;
// playerShot.utilityPressed = false;
// this->sharedData->network->Send( playerShot );
// this->key_Shoot = true;
// }
//}
//else
// this->key_Shoot = false;
//
//if( this->sharedData->mouseDevice->IsBtnDown(::Input::Enum::SAMI_MouseRightBtn) )
//{
// if( !this->key_Shoot )
// {
// Protocol_PlayerShot playerShot;
// playerShot.primaryPressed = false;
// playerShot.secondaryPressed = true;
// playerShot.utilityPressed = false;
// this->sharedData->network->Send( playerShot );
// this->key_Shoot = true;
// }
//}
//else
// this->key_Shoot = false;
//
//if( this->sharedData->mouseDevice->IsBtnDown(::Input::Enum::SAMI_MouseMiddleBtn) )
//{
// if( !this->key_Shoot )
// {
// Protocol_PlayerShot playerShot;
// playerShot.primaryPressed = false;
// playerShot.secondaryPressed = false;
// playerShot.utilityPressed = true;
// this->sharedData->network->Send( playerShot );
// this->key_Shoot = true;
// }
//}
//else
// this->key_Shoot = false;
if( this->sharedData->keyboardDevice->IsKeyDown(::Input::Enum::SAKI_Escape) )
{
this->nextState = GameStateUI::UIState_shut_down;
}
if( this->sharedData->keyboardDevice->IsKeyDown(::Input::Enum::SAKI_M) )
{
this->nextState = GameStateUI::UIState_main_menu;
}
if( this->key_forward ) this->sharedData->network->Send( Protocol_PlayerMovementForward() );
if( this->key_backward ) this->sharedData->network->Send( Protocol_PlayerMovementBackward() );
if( this->key_strafeLeft ) this->sharedData->network->Send( Protocol_PlayerMovementLeft() );
if( this->key_strafeRight ) this->sharedData->network->Send( Protocol_PlayerMovementRight() );
}
void GamingUI::OnMousePress ( Input::Enum::SAMI key, Input::Mouse* sender )
@ -209,7 +142,6 @@ void GamingUI::OnMouseRelease ( Input::Enum::SAMI key, Input::Mouse* sender )
void GamingUI::OnMouseMoveVelocity ( Input::Struct::SAIPointInt2D coordinate, Input::Mouse* sender )
{
//send delta mouse movement
{
this->camera->PitchDown( (-coordinate.y) * this->sharedData->mouseSensitivity );
//this->camera->YawLeft( (-coordinate.x) * this->sharedData->mouseSensitivity );
//if( deltaPos.x != 0.0f ) //This made the camera reset to a specific rotation. Why?
@ -217,6 +149,38 @@ void GamingUI::OnMouseMoveVelocity ( Input::Struct::SAIPointInt2D coordinate, In
this->sharedData->network->Send( Protocol_PlayerLeftTurn((coordinate.x) * this->sharedData->mouseSensitivity, this->camera->GetLook()) );
}
}
void GamingUI::OnKeyPress(Enum::SAKI key, Keyboard* sender)
{
switch (key)
{
case SAKI_W: this->key_forward = true;
break;
case SAKI_S: this->key_backward = true;
break;
case SAKI_A: this->key_strafeLeft = true;
break;
case SAKI_D: this->key_strafeRight = true;
break;
case SAKI_Space: this->sharedData->network->Send( Protocol_PlayerJump() );
break;
case SAKI_Escape: this->nextState = GameStateUI::UIState_main_menu;
break;
}
}
void GamingUI::OnKeyRelease(Enum::SAKI key, Keyboard* sender)
{
if(sender != this->sharedData->keyboardDevice) return;
switch (key)
{
case SAKI_W: this->key_forward = false;
break;
case SAKI_S: this->key_backward = false;
break;
case SAKI_A: this->key_strafeLeft = false;
break;
case SAKI_D: this->key_strafeRight = false;
break;
}
}

View File

@ -11,7 +11,7 @@
namespace DanBias { namespace Client
{
class GamingUI : public GameStateUI, Input::Mouse::MouseEvent
class GamingUI : public GameStateUI, Input::Mouse::MouseEvent, Input::Keyboard::KeyboardEvent
{
public:
GamingUI( SharedStateContent* shared, Camera_FPSV2 *camera );
@ -35,6 +35,11 @@ namespace DanBias { namespace Client
void OnMouseMoveVelocity ( Input::Struct::SAIPointInt2D coordinate, Input::Mouse* sender ) override;
void OnMouseScroll ( int delta, Input::Mouse* sender ) override { }
void OnKeyEvent ( const Input::Struct::KeyboardEventData& eventData) override { }
void OnKeyPress ( Input::Enum::SAKI key, Input::Keyboard* sender) override;
void OnKeyDown ( Input::Enum::SAKI key, Input::Keyboard* sender) override { }
void OnKeyRelease ( Input::Enum::SAKI key, Input::Keyboard* sender) override;
private:
SharedStateContent *sharedData;
Camera_FPSV2 *camera;
@ -47,8 +52,6 @@ namespace DanBias { namespace Client
bool key_backward;
bool key_strafeRight;
bool key_strafeLeft;
bool key_Shoot;
bool key_Jump;
GamingUI();
void ReadKeyInput();

View File

@ -14,6 +14,7 @@
#define NOMINMAX
#include <Windows.h>
#include <vld.h>
namespace DanBias
{

View File

@ -18,17 +18,19 @@ GameClient::GameClient(Utility::DynamicMemory::SmartPointer<Oyster::Network::Net
this->failedPackagesCount = 0;
this->client = nwClient;
this->player = 0;
isReady = false;
this->isReady = false;
this->character = L"char_orca.dan";
this->alias = L"Unknown";
this->secondsSinceLastResponse = 0.0f;
}
GameClient::~GameClient()
{
this->client->Disconnect();
if(this->player)
{
this->player->Inactivate();
}
this->isReady = false;
this->character = L"char_orca.dan";
this->alias = L"Unknown";
@ -68,12 +70,17 @@ bool GameClient::IsInvalid()
}
void GameClient::Invalidate()
{
GameLogic::IPlayerData* player;
this->client->Disconnect();
this->isReady = false;
this->isInvalid = true; //TODO: Fix this, should be true
this->secondsSinceLastResponse = 0.0f;
this->failedPackagesCount = 0;
this->character = L"char_orca.dan";
this->alias = L"Unknown";
this->state = ClientState_Invalid;
this->player->Release();
this->player = 0;
this->isInvalid = true;
this->isReady = false;
this->state = ClientState_Invalid;
this->client->Disconnect();
}
int GameClient::IncrementFailedProtocol()
{

View File

@ -79,7 +79,9 @@ using namespace DanBias;
break;
case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToSend:
if(this->gClients[temp]->IncrementFailedProtocol() >= 5/*client->threshold*/)
{
this->gClients[temp]->Invalidate();
}
break;
case NetworkClient::ClientEventArgs::EventType_ProtocolRecieved:
this->ParseProtocol(e.args.data.protocol, cl);
@ -93,6 +95,8 @@ using namespace DanBias;
if(this->gClients[i] && !this->gClients[i]->IsInvalid())
{
this->gClients[i]->UpdateClient();
if(this->gClients[i]->IsInvalid())
this->gClients[i] = 0;
}
}
}
@ -101,7 +105,7 @@ using namespace DanBias;
bool returnValue = false;
for (unsigned int i = 0; i < this->gClients.Size(); i++)
{
if(this->gClients[i] && !this->gClients[i]->IsInvalid())
if(this->gClients[i] && !this->gClients[i]->IsReady())
{
this->gClients[i]->GetClient()->Send(message);
returnValue = true;

View File

@ -99,6 +99,18 @@ bool ExistsInList(std::vector<Keyboard::KeyboardEvent*>& list, Keyboard::Keyboar
}
return false;
}
void RemoveFromList(std::vector<Keyboard::KeyboardEvent*>& list, Keyboard::KeyboardEvent* data)
{
for (unsigned int i = 0; i < list.size(); i++)
{
if(list[i] == data)
{
std::swap(list[i], list[list.size() - 1]);
list.resize(list.size() - 1);
return;
}
}
}
@ -201,12 +213,7 @@ void Keyboard::AddKeyboardEvent(KeyboardEvent* object)
}
void Keyboard::RemoveKeyboardEvent(KeyboardEvent* object)
{
int i = -1;
if((i = ExistsInList(this->keyEventSubscrivers, object)))
{
std::swap(this->keyEventSubscrivers[i], this->keyEventSubscrivers[this->keyEventSubscrivers.size() - 1]);
this->keyEventSubscrivers.resize(this->keyEventSubscrivers.size() - 1);
}
RemoveFromList( this->keyEventSubscrivers, object);
}
void Keyboard::operator+= (KeyboardEvent* object)
{

View File

@ -107,6 +107,18 @@ bool ExistsInList(std::vector<Mouse::MouseEvent*>& list, Mouse::MouseEvent* data
}
return false;
}
void RemoveFromList(std::vector<Mouse::MouseEvent*>& list, Mouse::MouseEvent* data)
{
for (unsigned int i = 0; i < list.size(); i++)
{
if(list[i] == data)
{
std::swap(list[i], list[list.size() - 1]);
list.resize(list.size() - 1);
return;
}
}
}
Mouse::Mouse()
: InputObject(Input::Enum::SAIType_Mouse)
@ -235,12 +247,7 @@ void Mouse::AddMouseEvent(MouseEvent* object)
}
void Mouse::RemoveMouseEvent(MouseEvent* object)
{
int i = -1;
if((i = ExistsInList(this->mouseSubscribers, object)))
{
std::swap(this->mouseSubscribers[i], this->mouseSubscribers[this->mouseSubscribers.size() - 1]);
this->mouseSubscribers.resize(this->mouseSubscribers.size() - 1);
}
RemoveFromList(this->mouseSubscribers, object);
}
void Mouse::operator+= (MouseEvent* object)
{

View File

@ -521,8 +521,7 @@ bool NetworkClient::operator ==(const int& ID)
void NetworkClient::Update()
{
if(!this->privateData) return;
while (!this->privateData->recieveQueue.IsEmpty())
while ( this->privateData && !this->privateData->recieveQueue.IsEmpty())
{
NetEvent<NetworkClient*, ClientEventArgs> temp = this->privateData->recieveQueue.Pop();
@ -532,9 +531,9 @@ void NetworkClient::Update()
bool NetworkClient::Connect(ConnectionInfo& socket)
{
if(this->IsConnected()) return true;
if(this->privateData) return false;
if(!this->privateData) this->privateData = new PrivateData();
if(this->IsConnected()) return true;
int result = this->privateData->connection.Connect(socket, true);
@ -550,12 +549,13 @@ bool NetworkClient::Connect(ConnectionInfo& socket)
bool NetworkClient::Connect(unsigned short port, const char serverIP[])
{
if(!this->privateData)
this->privateData = new PrivateData();
//Return true if you are already connected.
if(this->IsConnected())
return true;
if(!this->privateData)
this->privateData = new PrivateData();
int result = this->privateData->connection.Connect(port, serverIP, true);
@ -582,12 +582,13 @@ bool NetworkClient::Connect(unsigned short port, std::wstring serverIP)
bool NetworkClient::Reconnect()
{
if(!this->privateData)
this->privateData = new PrivateData();
//Return true if you are already connected.
if(this->IsConnected())
return true;
if(!this->privateData) this->privateData = new PrivateData();
int result = this->privateData->connection.Reconnect();
if(result != 0)
@ -605,12 +606,8 @@ void NetworkClient::Disconnect()
{
if(!privateData) return;
SetEvent(privateData->shutdownEvent);
privateData->thread.Wait();
privateData->connection.Disconnect();
this->privateData->sendQueue.Clear();
this->privateData->recieveQueue.Clear();
delete this->privateData;
this->privateData = 0;
}
void NetworkClient::Send(CustomProtocolObject& protocol)
@ -650,6 +647,7 @@ bool NetworkClient::IsConnected()
int NetworkClient::GetID() const
{
if(!this->privateData) return false;
return this->privateData->ID;
}