From a80b1fa96fde89ef3da1d888e51437e0cd01284c Mon Sep 17 00:00:00 2001 From: dean11 Date: Thu, 27 Feb 2014 11:41:17 +0100 Subject: [PATCH] Added back to main menu NOTE MEMORY LEAKS --- Code/Game/GameClient/DanBiasGame_Impl.cpp | 12 +- .../GameClient/GameClientState/GamingUI.cpp | 170 +++++++----------- .../GameClient/GameClientState/GamingUI.h | 17 +- Code/Game/GameClient/Include/DanBiasGame.h | 1 + .../GameServer/Implementation/GameClient.cpp | 17 +- .../Implementation/GameSession_Gameplay.cpp | 6 +- Code/Misc/Input/Source/Keyboard.cpp | 19 +- Code/Misc/Input/Source/Mouse.cpp | 19 +- Code/Network/NetworkAPI/NetworkClient.cpp | 26 ++- 9 files changed, 141 insertions(+), 146 deletions(-) diff --git a/Code/Game/GameClient/DanBiasGame_Impl.cpp b/Code/Game/GameClient/DanBiasGame_Impl.cpp index 85ea810a..91f56a1a 100644 --- a/Code/Game/GameClient/DanBiasGame_Impl.cpp +++ b/Code/Game/GameClient/DanBiasGame_Impl.cpp @@ -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; diff --git a/Code/Game/GameClient/GameClientState/GamingUI.cpp b/Code/Game/GameClient/GameClientState/GamingUI.cpp index f6b9aa32..d676d25e 100644 --- a/Code/Game/GameClient/GameClientState/GamingUI.cpp +++ b/Code/Game/GameClient/GameClientState/GamingUI.cpp @@ -8,27 +8,38 @@ using namespace ::GameLogic; using namespace ::Utility::Value; using namespace ::Oyster::Math; using namespace ::Input; +using namespace ::Input::Enum; GamingUI::GamingUI() : GameStateUI() { /* Should never be called! */ - this->sharedData = nullptr; - this->camera = nullptr; - this->plane = nullptr; - this->text = nullptr; + this->sharedData = nullptr; + 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; } GamingUI::GamingUI( SharedStateContent* shared, Camera_FPSV2 *camera ) : GameStateUI() { - this->sharedData = shared; - this->camera = camera; - this->nextState = GameStateUI::UIState_same; + 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,14 +142,45 @@ 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? { - 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? - { - this->sharedData->network->Send( Protocol_PlayerLeftTurn((coordinate.x) * this->sharedData->mouseSensitivity, this->camera->GetLook()) ); - } + 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; + } +} diff --git a/Code/Game/GameClient/GameClientState/GamingUI.h b/Code/Game/GameClient/GameClientState/GamingUI.h index d21ae0bd..a8f3bc7a 100644 --- a/Code/Game/GameClient/GameClientState/GamingUI.h +++ b/Code/Game/GameClient/GameClientState/GamingUI.h @@ -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 ); @@ -27,13 +27,18 @@ namespace DanBias { namespace Client void SetHPtext( std::wstring hp ); private: /* Overidden mouse methods */ - void OnMouse ( const Input::Struct::MouseEventData& eventData ) override { } + void OnMouse ( const Input::Struct::MouseEventData& eventData ) override { } void OnMousePress ( Input::Enum::SAMI key, Input::Mouse* sender ) override; - void OnMouseDown ( Input::Enum::SAMI key, Input::Mouse* sender ) override { } + void OnMouseDown ( Input::Enum::SAMI key, Input::Mouse* sender ) override { } void OnMouseRelease ( Input::Enum::SAMI key, Input::Mouse* sender ) override; - void OnMouseMovePixelPos ( Input::Struct::SAIPointInt2D coordinate, Input::Mouse* sender ) override { } + void OnMouseMovePixelPos ( Input::Struct::SAIPointInt2D coordinate, Input::Mouse* sender ) override { } void OnMouseMoveVelocity ( Input::Struct::SAIPointInt2D coordinate, Input::Mouse* sender ) override; - void OnMouseScroll ( int delta, 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; @@ -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(); diff --git a/Code/Game/GameClient/Include/DanBiasGame.h b/Code/Game/GameClient/Include/DanBiasGame.h index f6f0cdee..90487491 100644 --- a/Code/Game/GameClient/Include/DanBiasGame.h +++ b/Code/Game/GameClient/Include/DanBiasGame.h @@ -14,6 +14,7 @@ #define NOMINMAX #include +#include namespace DanBias { diff --git a/Code/Game/GameServer/Implementation/GameClient.cpp b/Code/Game/GameServer/Implementation/GameClient.cpp index 0c33ba89..ce2b1395 100644 --- a/Code/Game/GameServer/Implementation/GameClient.cpp +++ b/Code/Game/GameServer/Implementation/GameClient.cpp @@ -18,17 +18,19 @@ GameClient::GameClient(Utility::DynamicMemory::SmartPointerfailedPackagesCount = 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() { diff --git a/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp b/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp index 18e2b224..676e6515 100644 --- a/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp +++ b/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp @@ -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; diff --git a/Code/Misc/Input/Source/Keyboard.cpp b/Code/Misc/Input/Source/Keyboard.cpp index c08bd60c..230e2607 100644 --- a/Code/Misc/Input/Source/Keyboard.cpp +++ b/Code/Misc/Input/Source/Keyboard.cpp @@ -99,6 +99,18 @@ bool ExistsInList(std::vector& list, Keyboard::Keyboar } return false; } +void RemoveFromList(std::vector& 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) { diff --git a/Code/Misc/Input/Source/Mouse.cpp b/Code/Misc/Input/Source/Mouse.cpp index 2d309ddf..8c22f2ce 100644 --- a/Code/Misc/Input/Source/Mouse.cpp +++ b/Code/Misc/Input/Source/Mouse.cpp @@ -107,6 +107,18 @@ bool ExistsInList(std::vector& list, Mouse::MouseEvent* data } return false; } +void RemoveFromList(std::vector& 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) { diff --git a/Code/Network/NetworkAPI/NetworkClient.cpp b/Code/Network/NetworkAPI/NetworkClient.cpp index 57155ef7..ee9598ca 100644 --- a/Code/Network/NetworkAPI/NetworkClient.cpp +++ b/Code/Network/NetworkAPI/NetworkClient.cpp @@ -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 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; }