diff --git a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp index 51082518..8264f34b 100644 --- a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp +++ b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp @@ -7,6 +7,7 @@ #include "GameClientState\LobbyState.h" #include "PlayerProtocols.h" #include "ControlProtocols.h" +#include "GameProtocols.h" #include "NetworkClient.h" #include "../WindowManager/WindowShell.h" diff --git a/Code/Game/DanBiasGame/GameClientState/GameState.cpp b/Code/Game/DanBiasGame/GameClientState/GameState.cpp index cfbc13cf..1ee9ac8b 100644 --- a/Code/Game/DanBiasGame/GameClientState/GameState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/GameState.cpp @@ -2,9 +2,11 @@ #include "DllInterfaces/GFXAPI.h" #include "C_obj/C_Player.h" #include "C_obj/C_DynamicObj.h" -#include "NetworkClient.h" #include "PlayerProtocols.h" #include "ControlProtocols.h" +#include "GameProtocols.h" +#include "NetworkClient.h" + using namespace DanBias::Client; diff --git a/Code/Game/DanBiasLauncher/Launcher.cpp b/Code/Game/DanBiasLauncher/Launcher.cpp index b126e5e7..6b82da2a 100644 --- a/Code/Game/DanBiasLauncher/Launcher.cpp +++ b/Code/Game/DanBiasLauncher/Launcher.cpp @@ -5,8 +5,8 @@ #include #include -//#include "DanBiasServerAPI.h" -#include "DanBiasGame.h" +#include "DanBiasServerAPI.h" +//#include "DanBiasGame.h" int WINAPI WinMain( HINSTANCE hinst, HINSTANCE prevInst, PSTR cmdLine, int cmdShow) diff --git a/Code/Game/DanBiasServer/DanBiasServer.vcxproj b/Code/Game/DanBiasServer/DanBiasServer.vcxproj index af26345b..5793ef44 100644 --- a/Code/Game/DanBiasServer/DanBiasServer.vcxproj +++ b/Code/Game/DanBiasServer/DanBiasServer.vcxproj @@ -177,6 +177,7 @@ + @@ -189,6 +190,7 @@ + diff --git a/Code/Game/DanBiasServer/GameServer.cpp b/Code/Game/DanBiasServer/GameServer.cpp index cb5a7ae3..4f4d1c0d 100644 --- a/Code/Game/DanBiasServer/GameServer.cpp +++ b/Code/Game/DanBiasServer/GameServer.cpp @@ -54,6 +54,7 @@ namespace DanBias if(!this->server->Init(serverDesc)) return DanBiasServerReturn_Error; if(!WindowShell::CreateConsoleWindow()) return DanBiasServerReturn_Error; + if(!WindowShell::CreateWin(WindowShell::WINDOW_INIT_DESC())) return DanBiasServerReturn_Error; this->initiated = true; return DanBiasServerReturn_Sucess; diff --git a/Code/Game/DanBiasServer/GameServer.h b/Code/Game/DanBiasServer/GameServer.h index 251aab7a..bb066b69 100644 --- a/Code/Game/DanBiasServer/GameServer.h +++ b/Code/Game/DanBiasServer/GameServer.h @@ -15,7 +15,7 @@ namespace DanBias { public: GameServer(); - ~GameServer(); + virtual~GameServer(); DanBiasServerReturn Create(); DanBiasServerReturn Run(); diff --git a/Code/Game/DanBiasServer/GameSessionManager.cpp b/Code/Game/DanBiasServer/GameSessionManager.cpp new file mode 100644 index 00000000..9a78cae7 --- /dev/null +++ b/Code/Game/DanBiasServer/GameSessionManager.cpp @@ -0,0 +1,57 @@ +#include "GameSessionManager.h" + +#include "ServerObjects\GameSession.h" +#include "DynamicArray.h" + +struct GameSessionData +{ + Utility::DynamicMemory::DynamicArray sessions; + int freeSpot; + + int Existst(DanBias::GameSession* s) + { + for (unsigned int i = 0; i < sessions.Size(); i++) + { + if(!sessions[i] && freeSpot == -1) freeSpot = i; + if(sessions[i] == s) return i; + } + return -1; + } + int GetFree() + { + for (unsigned int i = 0; i < sessions.Size(); i++) + if(!sessions[i]) + return i; + return -1; + } + +} __gameSessionData; + + +void GameSessionManager::AddSession(DanBias::GameSession* session) +{ + if(__gameSessionData.Existst(session) == -1) + { + int k = __gameSessionData.freeSpot; + + if( k == -1) k = __gameSessionData.GetFree(); + + if(k == -1) __gameSessionData.sessions.Push(session); + else __gameSessionData.sessions[k] = session; + } + +} +void GameSessionManager::CloseSession(DanBias::GameSession* session) +{ + int i = __gameSessionData.Existst(session); + + //Moron check... + if(i == -1) return; + + //__gameSessionData.sessions[i]->Close(); +} + + + + + diff --git a/Code/Game/DanBiasServer/GameSessionManager.h b/Code/Game/DanBiasServer/GameSessionManager.h new file mode 100644 index 00000000..eccda1bc --- /dev/null +++ b/Code/Game/DanBiasServer/GameSessionManager.h @@ -0,0 +1,16 @@ +#ifndef DANBIASSERVER_GAME_SEESION_MANAGER_H +#define DANBIASSERVER_GAME_SEESION_MANAGER_H + +namespace DanBias +{ + class GameSession; +} + +class GameSessionManager +{ +public: + static void AddSession(DanBias::GameSession* session); + static void CloseSession(DanBias::GameSession* session); +}; + +#endif // !DANBIASSERVER_GAME_SEESION_MANAGER_H diff --git a/Code/Game/DanBiasServer/ServerObjects/ClientObject.cpp b/Code/Game/DanBiasServer/ServerObjects/ClientObject.cpp index c8a259ad..a06bde05 100644 --- a/Code/Game/DanBiasServer/ServerObjects/ClientObject.cpp +++ b/Code/Game/DanBiasServer/ServerObjects/ClientObject.cpp @@ -13,7 +13,7 @@ ClientObject::~ClientObject() this->client->Disconnect(); } -void ClientObject::SetPostbox(Oyster::PostBox* box) +void ClientObject::SetPostbox(Oyster::IPostBox* box) { this->box = box; } diff --git a/Code/Game/DanBiasServer/ServerObjects/ClientObject.h b/Code/Game/DanBiasServer/ServerObjects/ClientObject.h index f44e70d0..3ee8c710 100644 --- a/Code/Game/DanBiasServer/ServerObjects/ClientObject.h +++ b/Code/Game/DanBiasServer/ServerObjects/ClientObject.h @@ -14,13 +14,14 @@ namespace DanBias { public: ClientObject(Oyster::Network::NetworkClient* client); - ~ClientObject(); + virtual~ClientObject(); - void SetPostbox(Oyster::PostBox* box); + void SetPostbox(Oyster::IPostBox* box); GameLogic::Player* Logic_Object(); Oyster::Network::NetworkClient* NetClient_Object(); + private: /** This method is NOT threadsafe. */ virtual void ProtocolRecievedCallback(Oyster::Network::CustomNetProtocol& protocol) override; diff --git a/Code/Game/DanBiasServer/ServerObjects/GameSession.cpp b/Code/Game/DanBiasServer/ServerObjects/GameSession.cpp index fced4e45..28aacadd 100644 --- a/Code/Game/DanBiasServer/ServerObjects/GameSession.cpp +++ b/Code/Game/DanBiasServer/ServerObjects/GameSession.cpp @@ -1,9 +1,148 @@ +#include +#include "GameSession.h" +#include +#include "ClientObject.h" + +#define ERIK +using namespace Utility::DynamicMemory; +using namespace Oyster::Network; +using namespace Oyster; namespace DanBias { + GameSession::GameSession() + { + + } + GameSession::~GameSession() + { + + } + + void GameSession::Run(const GameSessionDescription& desc) + { + + } + + void GameSession::AttachClient(SmartPointer client, Oyster::IPostBox *box ) + { + this->Init(); + } + +////private: //overriden NetworkSession functions + void GameSession::Close() + { + + } + SmartPointer GameSession::DetachClient(NetworkClient* client) + { + return SmartPointer(); + } + SmartPointer GameSession::DetachClient(ClientObject* client) + { + return SmartPointer(); + } + SmartPointer GameSession::DetachClient(short ID) + { + return SmartPointer(); + } + void GameSession::Send(::CustomNetProtocol& protocol) + { + + } + void GameSession::Send(CustomNetProtocol& protocol, int ID) + { + + } + void GameSession::SetPostbox(IPostBox *box) + { + + } + void GameSession::CloseSession(NetworkSession* clientDestination) + { + + } + +////private: //overriden Threading functions + void GameSession::ThreadEntry() + { + + } + void GameSession::ThreadExit() + { + + } + bool GameSession::DoWork ( ) + { + this->ParseEvents(); + this->Frame(); + + return true; + } + +////private: + void GameSession::Init() + { +#ifdef ERIK + EricLogicInitFunc(); +#else + +#endif + } + void GameSession::Frame() + { +#ifdef ERIK + EricLogicFrameFunc(); +#else + +#endif + } + void GameSession::ParseEvents() + { + if(this->box && !this->box->IsEmpty()) + { + NetEvent &e = this->box->Fetch(); + +#ifdef ERIK + EricsLogicTestingProtocalRecieved(e.reciever, e.protocol); +#else + if(e.protocol[0].type != Oyster::Network::NetAttributeType_Short) return; + + short f = e.protocol[0].value.netShort; + + switch (f) + { + default: + + break; + } +#endif + } + } -}//End namespace DanBias \ No newline at end of file +#pragma region TESTING + + //VARIABLES GOES HERE + int i = 0; + GameLogic::Player erik; + + void GameSession::EricLogicInitFunc() + { + + } + void GameSession::EricLogicFrameFunc() + { + + } + void GameSession::EricsLogicTestingProtocalRecieved(ClientObject* reciever, CustomNetProtocol& protocol) + { + + } +#pragma endregion + +}//End namespace DanBias + diff --git a/Code/Game/DanBiasServer/ServerObjects/GameSession.h b/Code/Game/DanBiasServer/ServerObjects/GameSession.h index 26ad5ec2..33d57b67 100644 --- a/Code/Game/DanBiasServer/ServerObjects/GameSession.h +++ b/Code/Game/DanBiasServer/ServerObjects/GameSession.h @@ -2,16 +2,59 @@ #define DANBIASSERVER_GAME_SESSION_H #include "NetworkSession.h" +#include +#include namespace DanBias { - class GameSession :public NetworkSession + class ClientObject; + class GameSession :private NetworkSession, public Oyster::Thread::IThreadObject { + public: + struct GameSessionDescription + { + NetworkSession* owner; + + }; + public: GameSession(); - ~GameSession(); + virtual~GameSession(); + + void Run(const GameSessionDescription& desc); + + void AttachClient(Utility::DynamicMemory::SmartPointer client, Oyster::IPostBox *box = 0) override; + + private: //overriden NetworkSession functions + void Close(); + Utility::DynamicMemory::SmartPointer DetachClient(Oyster::Network::NetworkClient* client) override; + Utility::DynamicMemory::SmartPointer DetachClient(ClientObject* client) override; + Utility::DynamicMemory::SmartPointer DetachClient(short ID) override; + void Send(Oyster::Network::CustomNetProtocol& protocol) override; + void Send(Oyster::Network::CustomNetProtocol& protocol, int ID) override; + void SetPostbox(Oyster::IPostBox *box) override; + void CloseSession(NetworkSession* clientDestination) override; + + private: //overriden Threading functions + void ThreadEntry() override; + void ThreadExit() override; + bool DoWork ( ) override; + + private: + void Init(); + void Frame(); + void ParseEvents(); private: + NetworkSession* owner; + Oyster::IPostBox *box; + Oyster::Thread::OysterThread worker; + +#pragma region TESTING + void EricLogicInitFunc(); + void EricLogicFrameFunc(); + void EricsLogicTestingProtocalRecieved(ClientObject* reciever, Oyster::Network::CustomNetProtocol& protocol); +#pragma endregion };//End GameSession diff --git a/Code/Game/DanBiasServer/ServerObjects/Lobby/GameLobby.h b/Code/Game/DanBiasServer/ServerObjects/Lobby/GameLobby.h index c4a0718c..a328271e 100644 --- a/Code/Game/DanBiasServer/ServerObjects/Lobby/GameLobby.h +++ b/Code/Game/DanBiasServer/ServerObjects/Lobby/GameLobby.h @@ -9,7 +9,8 @@ namespace DanBias { public: GameLobby(); - ~GameLobby(); + virtual~GameLobby(); + void Release(); private: diff --git a/Code/Game/DanBiasServer/ServerObjects/Lobby/MainLobby.cpp b/Code/Game/DanBiasServer/ServerObjects/Lobby/MainLobby.cpp index 97033790..39e5dad1 100644 --- a/Code/Game/DanBiasServer/ServerObjects/Lobby/MainLobby.cpp +++ b/Code/Game/DanBiasServer/ServerObjects/Lobby/MainLobby.cpp @@ -1,19 +1,21 @@ #include "MainLobby.h" #include +#include namespace DanBias { MainLobby::MainLobby() { - + this->box = new Oyster::PostBox(); } MainLobby::~MainLobby() { - + delete this->box; + this->box = 0; } void MainLobby::Release() { - this->DetachClient(); + this->CloseSession(0); } void MainLobby::Frame() @@ -24,9 +26,9 @@ namespace DanBias //////// Private void MainLobby::ParseEvents() { - if(!this->box.IsEmpty()) + if(this->box && !this->box->IsEmpty()) { - NetEvent &e = this->box.Fetch(); + NetEvent &e = this->box->Fetch(); if(e.protocol[0].type != Oyster::Network::NetAttributeType_Short) return; diff --git a/Code/Game/DanBiasServer/ServerObjects/Lobby/MainLobby.h b/Code/Game/DanBiasServer/ServerObjects/Lobby/MainLobby.h index c78f70e2..7269f66d 100644 --- a/Code/Game/DanBiasServer/ServerObjects/Lobby/MainLobby.h +++ b/Code/Game/DanBiasServer/ServerObjects/Lobby/MainLobby.h @@ -2,6 +2,7 @@ #define DANBIASSERVER_MAINLOBBY_H #include "..\NetworkSession.h" +#include namespace DanBias { @@ -9,7 +10,7 @@ namespace DanBias { public: MainLobby(); - ~MainLobby(); + virtual~MainLobby(); void Release(); void Frame(); @@ -17,6 +18,9 @@ namespace DanBias private: void ParseEvents(); + private: + Oyster::IPostBox *box; + }; }//End namespace DanBias #endif // !DANBIASGAME_GAMELOBBY_H diff --git a/Code/Game/DanBiasServer/ServerObjects/NetworkSession.cpp b/Code/Game/DanBiasServer/ServerObjects/NetworkSession.cpp index 1e64642d..56e019c0 100644 --- a/Code/Game/DanBiasServer/ServerObjects/NetworkSession.cpp +++ b/Code/Game/DanBiasServer/ServerObjects/NetworkSession.cpp @@ -8,79 +8,154 @@ static std::mutex ClientListLock; namespace DanBias { NetworkSession::NetworkSession() + { } + NetworkSession::NetworkSession(const NetworkSession& orig) { - + this->clients = orig.clients; + } + const NetworkSession& NetworkSession::operator=(const NetworkSession& orig) + { + this->clients = orig.clients; + return *this; } NetworkSession::~NetworkSession() { - + this->clients.Clear(); } - void NetworkSession::AttachClient(Utility::DynamicMemory::SmartPointer client) + void NetworkSession::AttachClient(Utility::DynamicMemory::SmartPointer client, Oyster::IPostBox *box) { - while (!ClientListLock.try_lock()); //Possible Deadlock + ClientListLock.lock(); - int k = -1; - for (unsigned int i = 0; (k == -1) && i < this->clients.size(); i++) - { - if(!this->clients[i]) - k = i; - } + int k = -1; + for (unsigned int i = 0; (k == -1) && i < this->clients.Size(); i++) + { + if(!this->clients[i]) + k = i; + } - if(k == -1) - { - this->clients.push_back(client); - this->clients[this->clients.size() - 1]->SetPostbox(&this->box); - } - else - { - this->clients[k]->SetPostbox(&this->box); - } + if(k == -1) + { + this->clients.Push(client); + this->clients[this->clients.Size() - 1]->SetPostbox(box); + } + else + { + this->clients[k] = client; + this->clients[k]->SetPostbox(box); + } ClientListLock.unlock(); } - void NetworkSession::DetachClient(Oyster::Network::NetworkClient* client) + Utility::DynamicMemory::SmartPointer NetworkSession::DetachClient(Oyster::Network::NetworkClient* client) { - for (unsigned int i = 0; i < this->clients.size(); i++) - { - if(this->clients[0]->NetClient_Object()->Id() == client->Id()) - this->clients[i] = 0; - } + Utility::DynamicMemory::SmartPointer val; + + ClientListLock.lock(); + + for (unsigned int i = 0; i < this->clients.Size(); i++) + { + if(this->clients[0]->NetClient_Object()->Id() == client->Id()) + { + val = this->clients[i]; + this->clients[i] = 0; + } + } + + ClientListLock.unlock(); + + return val; } - void NetworkSession::DetachClient(ClientObject* client) + Utility::DynamicMemory::SmartPointer NetworkSession::DetachClient(ClientObject* client) { - for (unsigned int i = 0; i < this->clients.size(); i++) - { - if(this->clients[0]->NetClient_Object()->Id() == client->NetClient_Object()->Id()) - this->clients[i] = 0; - } + Utility::DynamicMemory::SmartPointer val; + + ClientListLock.lock(); + + for (unsigned int i = 0; i < this->clients.Size(); i++) + { + if(this->clients[0]->NetClient_Object()->Id() == client->NetClient_Object()->Id()) + { + val = this->clients[i]; + this->clients[i] = 0; + } + } + ClientListLock.unlock(); + + return val; } - void NetworkSession::DetachClient(short ID) + Utility::DynamicMemory::SmartPointer NetworkSession::DetachClient(short ID) { - for (unsigned int i = 0; i < this->clients.size(); i++) - { - if(this->clients[0]->NetClient_Object()->Id() == ID) - this->clients[i] = 0; - } - - } - void NetworkSession::DetachClient() - { - for (unsigned int i = 0; i < this->clients.size(); i++) - { - this->clients[i] = 0; - } + Utility::DynamicMemory::SmartPointer val; + + ClientListLock.lock(); + + for (unsigned int i = 0; i < this->clients.Size(); i++) + { + if(this->clients[0]->NetClient_Object()->Id() == ID) + { + val = this->clients[i]; + this->clients[i] = 0; + } + } + + ClientListLock.unlock(); + + return val; } - void NetworkSession::Kick() + void NetworkSession::Send(Oyster::Network::CustomNetProtocol& protocol) { - for (unsigned int i = 0; i < this->clients.size(); i++) + for (unsigned int i = 0; i < this->clients.Size(); i++) { - this->clients[i]->NetClient_Object()->Disconnect(); - this->clients[i] = 0; + this->clients[i]->NetClient_Object()->Send(&protocol); } } + void NetworkSession::Send(Oyster::Network::CustomNetProtocol& protocol, int ID) + { + for (unsigned int i = 0; i < this->clients.Size(); i++) + { + if(this->clients[i]->NetClient_Object()->Id() == ID) + { + this->clients[i]->NetClient_Object()->Send(&protocol); + break; + } + } + } + + void NetworkSession::SetPostbox(Oyster::IPostBox *box) + { + for (unsigned int i = 0; i < this->clients.Size(); i++) + { + this->clients[i]->SetPostbox(box); + } + } + + void NetworkSession::CloseSession(NetworkSession * owner) + { + ClientListLock.lock(); + + if(!owner) + { + for (unsigned int i = 0; i < this->clients.Size(); i++) + { + this->clients[i]->NetClient_Object()->Disconnect(); + } + } + else + { + for (unsigned int i = 0; i < this->clients.Size(); i++) + { + owner->AttachClient(this->clients[i]); + } + } + + this->clients.Clear(); + + ClientListLock.unlock(); + } + }//End namespace DanBias \ No newline at end of file diff --git a/Code/Game/DanBiasServer/ServerObjects/NetworkSession.h b/Code/Game/DanBiasServer/ServerObjects/NetworkSession.h index 9ff9b016..dd87652a 100644 --- a/Code/Game/DanBiasServer/ServerObjects/NetworkSession.h +++ b/Code/Game/DanBiasServer/ServerObjects/NetworkSession.h @@ -4,13 +4,17 @@ #ifndef DANBIASSERVER_NETWORK_SESSION_H #define DANBIASSERVER_NETWORK_SESSION_H +#pragma warning(disable: 4150) + #define NOMINMAX #include "Utilities.h" -#include +#include +#include #include #include #include + namespace DanBias { class ClientObject; @@ -25,26 +29,26 @@ namespace DanBias public: NetworkSession(); - ~NetworkSession(); + NetworkSession(const NetworkSession& orig); + const NetworkSession& operator=(const NetworkSession& orig); + virtual~NetworkSession(); - void AttachClient(Utility::DynamicMemory::SmartPointer client); + virtual void AttachClient(Utility::DynamicMemory::SmartPointer client, Oyster::IPostBox *box = 0); - void DetachClient(Oyster::Network::NetworkClient* client); - void DetachClient(ClientObject* client); - void DetachClient(short ID); - void DetachClient(); + virtual Utility::DynamicMemory::SmartPointer DetachClient(Oyster::Network::NetworkClient* client); + virtual Utility::DynamicMemory::SmartPointer DetachClient(ClientObject* client); + virtual Utility::DynamicMemory::SmartPointer DetachClient(short ID); - void Kick(); - - void Send(Oyster::Network::CustomNetProtocol& protocol); - void Send(Oyster::Network::CustomNetProtocol& protocol, int ID); + virtual void Send(Oyster::Network::CustomNetProtocol& protocol); + virtual void Send(Oyster::Network::CustomNetProtocol& protocol, int ID); //TODO: Do more lobby features - //virtual void + virtual void SetPostbox(Oyster::IPostBox *box); + + virtual void CloseSession(NetworkSession* clientDestination); //> clients; - Oyster::PostBox box; + Utility::DynamicMemory::DynamicArray> clients; }; }//End namespace DanBias #endif // !DANBIASSERVER_NETWORK_SESSION_H diff --git a/Code/Game/GameLogic/Camera.cpp b/Code/Game/GameLogic/Camera.cpp deleted file mode 100644 index a969ced5..00000000 --- a/Code/Game/GameLogic/Camera.cpp +++ /dev/null @@ -1,185 +0,0 @@ -#include "Camera.h" - -Camera::Camera() -{ - this->m_position = Oyster::Math::Float3(0, 50, 0); - this->mRight = Oyster::Math::Float3(1, 0, 0); - this->mUp = Oyster::Math::Float3(0, 1, 0); - this->mLook = Oyster::Math::Float3(0, 0, 1); -} - -Camera::~Camera() -{ -} - -void Camera::SetPosition(const Oyster::Math::Float3& v) -{ - this->m_position = v; -} - -Oyster::Math::Float3 Camera::GetPosition()const -{ - return this->m_position; -} - -Oyster::Math::Float3 Camera::GetRight()const -{ - return this->mRight; -} - -Oyster::Math::Float3 Camera::GetUp()const -{ - return this->mUp; -} - -Oyster::Math::Float3 Camera::GetLook()const -{ - return this->mLook; -} - -float Camera::GetNearZ()const -{ - return this->mNearZ; -} - -float Camera::GetFarZ()const -{ - return this->mFarZ; -} - -float Camera::GetAspect()const -{ - return this->mAspect; -} - -Oyster::Math::Float3 Camera::CrossMatrix(const Oyster::Math::Float3& vector, const Oyster::Math::Float4x4& matrix) -{ - Oyster::Math::Float3 vec; - vec.x = matrix.m11*vector.x + matrix.m12*vector.y + matrix.m13*vector.z; - vec.y = matrix.m21*vector.x + matrix.m22*vector.y + matrix.m23*vector.z; - vec.z = matrix.m31*vector.x + matrix.m32*vector.y + matrix.m33*vector.z; - return vec; -} - -void Camera::SetLens(float fovY, float aspect, float zn, float zf) -{ - this->mFovY = fovY; - this->mAspect = aspect; - this->mNearZ = zn; - this->mFarZ = zf; - - float yScale = tan((Oyster::Math::pi*0.5f) - (mFovY*0.5f)); - float xScale = yScale/this->mAspect; - - mProj = Oyster::Math::Float4x4(xScale, 0, 0, 0, - 0, yScale, 0, 0, - 0, 0, zf/(zf-zn), 1, - 0, 0, -zn*zf/(zf-zn), 0); - mProj.Transpose(); -} - -void Camera::LookAt(Oyster::Math::Float3 pos, Oyster::Math::Float3 target, Oyster::Math::Float3 worldUp) -{ - Oyster::Math::Float3 L; - - L = target - pos; - L.Normalize(); - - Oyster::Math::Float3 R; - R = worldUp.Cross(L); - R.Normalize(); - - Oyster::Math::Float3 U; - U = L.Cross(R); - - this->m_position = pos; - this->mLook = L; - this->mRight = R; - this->mUp = U; -} - -Oyster::Math::Float4x4 Camera::View()const -{ - return this->mView; -} - -Oyster::Math::Float4x4 Camera::Proj()const -{ - return this->mProj; -} - -Oyster::Math::Float4x4 Camera::ViewsProj()const -{ - Oyster::Math::Float4x4 M; - M = mView * mProj; - return M; -} - -void Camera::Walk(float dist) -{ - this->m_position += dist*this->mLook; -} - -void Camera::Strafe(float dist) -{ - this->m_position += dist*this->mRight; -} - -void Camera::Pitch(float angle) -{ - float radians = angle * 0.0174532925f; - - Oyster::Math::Float4x4 R; - - Oyster::Math3D::RotationMatrix(radians,-mRight,R); - this->mUp = CrossMatrix(this->mUp, R); - this->mLook = CrossMatrix(this->mLook, R); -} - -void Camera::Yaw(float angle) -{ - float radians = angle * 0.0174532925f; - - Oyster::Math::Float4x4 R; - - Oyster::Math::Float3 up(0,1,0); - Oyster::Math3D::RotationMatrix(radians,-up,R); - - this->mRight = CrossMatrix(this->mRight, R); - this->mUp = CrossMatrix(mUp, R); - this->mLook = CrossMatrix(this->mLook, R); -} - -void Camera::UpdateViewMatrix() -{ - mLook.Normalize(); - mUp = mLook.Cross(mRight); - mUp.Normalize(); - mRight = mUp.Cross(mLook); - - float x = -m_position.Dot(mRight); - float y = -m_position.Dot(mUp); - float z = -m_position.Dot(mLook); - - mView.m11 = mRight.x; - mView.m21 = mRight.y; - mView.m31 = mRight.z; - mView.m41 = x; - - mView.m12 = mUp.x; - mView.m22 = mUp.y; - mView.m32 = mUp.z; - mView.m42 = y; - - mView.m13 = mLook.x; - mView.m23 = mLook.y; - mView.m33 = mLook.z; - mView.m43 = z; - - mView.m14 = 0.0f; - mView.m24 = 0.0f; - mView.m34 = 0.0f; - mView.m44 = 1.0f; - - mView.Transpose(); -} \ No newline at end of file diff --git a/Code/Game/GameLogic/Camera.h b/Code/Game/GameLogic/Camera.h deleted file mode 100644 index deb9df62..00000000 --- a/Code/Game/GameLogic/Camera.h +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef CAMERA__H -#define CAMERA__H - -#include "OysterMath.h" - -class Camera -{ -private: - - Oyster::Math::Float3 m_position; - Oyster::Math::Float3 mRight; - Oyster::Math::Float3 mUp; - Oyster::Math::Float3 mLook; - - - - float mNearZ; - float mFarZ; - float mAspect; - float mFovY; - - Oyster::Math::Float4x4 mView; - Oyster::Math::Float4x4 mProj; - -public: - Camera(); - virtual ~Camera(); - - void SetPosition(const Oyster::Math::Float3& v); - - Oyster::Math::Float3 GetPosition()const; - - Oyster::Math::Float3 GetRight()const; - Oyster::Math::Float3 GetUp()const; - Oyster::Math::Float3 GetLook()const; - - float GetNearZ()const; - float GetFarZ()const; - float GetAspect()const; - - Oyster::Math::Float3 CrossMatrix(const Oyster::Math::Float3& v, const Oyster::Math::Float4x4& m); - - void SetLens(float fovY, float aspect, float zn, float zf); - - void LookAt(Oyster::Math::Float3 pos, Oyster::Math::Float3 target, Oyster::Math::Float3 worldUp); - - void setLook(Oyster::Math::Float3 look) { mLook = look; } - void setUp(Oyster::Math::Float3 up) { mUp = up; } - void setRight(Oyster::Math::Float3 right) { mRight = right; } - - Oyster::Math::Float4x4 View()const; - Oyster::Math::Float4x4 Proj()const; - Oyster::Math::Float4x4 ViewsProj()const; - - void Walk(float dist); - void Strafe(float dist); - - void Pitch(float angle); - void Yaw(float angle); - - void UpdateViewMatrix(); -}; -#endif \ No newline at end of file diff --git a/Code/Game/GameLogic/Game.cpp b/Code/Game/GameLogic/Game.cpp deleted file mode 100644 index 636b152c..00000000 --- a/Code/Game/GameLogic/Game.cpp +++ /dev/null @@ -1,79 +0,0 @@ -#include "Game.h" - -using namespace GameLogic; - -Game::Game(void) -{ - player = NULL; - level = NULL; - camera = NULL; -} - - -Game::~Game(void) -{ - //SAFE_DELETE(player); - if(player) - { - delete player; - player = NULL; - } - if(camera) - { - delete camera; - camera = NULL; - } -} - -void Game::Init() -{ - //Oyster::Physics::API::SetSubscription("remove object"); - - player = new Player(L"worldDummy"); - - box = new DynamicObject(L"crate"); - //poi - //box = new physcTestObj("box"); - camera = new Camera(); -} -void Game::StartGame() -{ - Oyster::Math::Float3 dir = Oyster::Math::Float3(0,0,-1); - Oyster::Math::Float3 up =Oyster::Math::Float3(0,1,0); - Oyster::Math::Float3 pos = Oyster::Math::Float3(0, 0, 100); - - camera->LookAt(pos, dir, up); - camera->SetLens(3.14f/2, 1024/768, 1, 1000); -} -void Game::Update(keyInput keyPressed, float pitch, float yaw) -{ - //player->Update(keyPressed); - camera->Yaw(yaw); - camera->Pitch(pitch); - if(keyPressed == keyInput_A) - { - camera->Strafe(-0.1); - } - if(keyPressed == keyInput_D) - { - camera->Strafe(0.1); - } - if(keyPressed == keyInput_S) - { - camera->Walk(-0.1); - } - if(keyPressed == keyInput_W) - { - camera->Walk(0.1); - } - camera->UpdateViewMatrix(); - //poi Oyster::Physics::API::Update(); -} -void Game::Render() -{ - Oyster::Graphics::API::SetView(camera->View()); - Oyster::Graphics::API::SetProjection(camera->Proj()); - Oyster::Graphics::API::NewFrame(); - player->Render(); - box->Render(); -} \ No newline at end of file diff --git a/Code/Game/GameLogic/Game.h b/Code/Game/GameLogic/Game.h deleted file mode 100644 index 708c01e3..00000000 --- a/Code/Game/GameLogic/Game.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef GAME_H -#define GAME_H - -#include "Level.h" -#include "Player.h" -#include "IGame.h" -#include "Camera.h" -#include "DynamicObject.h" - -namespace GameLogic -{ - class Game - { - public: - Game(); - ~Game(); - - void Init(); - void StartGame(); - void Update(keyInput keyPressed, float pitch, float yaw); - void Render(); - - private: - Level* level; - DynamicObject* box; - Player* player; - Camera* camera; - }; -} -#endif \ No newline at end of file diff --git a/Code/Game/GameLogic/GameMode.h b/Code/Game/GameLogic/GameMode.h index 865f07c3..06ba6681 100644 --- a/Code/Game/GameLogic/GameMode.h +++ b/Code/Game/GameLogic/GameMode.h @@ -8,7 +8,6 @@ namespace GameLogic { - class GameMode { public: diff --git a/Code/Game/GameLogic/IGame.cpp b/Code/Game/GameLogic/IGame.cpp deleted file mode 100644 index dd07c9f8..00000000 --- a/Code/Game/GameLogic/IGame.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "IGame.h" -#include "Game.h" -#include - -BOOL WINAPI DllMain( - _In_ HINSTANCE hinstDLL, - _In_ DWORD fdwReason, - _In_ LPVOID lpvReserved - ) -{ - return TRUE; -} -using namespace GameLogic; - -IGame::IGame() -{ - gameModule = new Game(); -} -IGame::~IGame() -{ - delete gameModule; -} - -void IGame::Init() -{ - gameModule->Init(); -} -void IGame::StartGame() -{ - gameModule->StartGame(); -} -void IGame::Update(keyInput keyPressed, float pitch, float yaw) -{ - gameModule->Update(keyPressed, pitch, yaw); -} -void IGame::Render() -{ - gameModule->Render(); -} -Game* IGame::getGameModule() -{ - return gameModule; -} \ No newline at end of file diff --git a/Code/Game/GameLogic/IGame.h b/Code/Game/GameLogic/IGame.h deleted file mode 100644 index 41b2e0d5..00000000 --- a/Code/Game/GameLogic/IGame.h +++ /dev/null @@ -1,47 +0,0 @@ -////////////////////////////////////////////////// -//Created by Erik and Linda of the GameLogic team -////////////////////////////////////////////////// - -#ifndef IGAME_H -#define IGAME_H - -#if defined GAME_DLL_EXPORT -#define GAME_DLL_USAGE __declspec(dllexport) -#else -#define GAME_DLL_USAGE __declspec(dllimport) -#endif - -namespace GameLogic -{ - class Game; - - enum keyInput - { - keyInput_W, - keyInput_A, - keyInput_S, - keyInput_D, - keyInput_none - }; - - class GAME_DLL_USAGE IGame - { - private: - Game* gameModule; - public: - IGame(); - ~IGame(); - - - void Init(); - void StartGame(); - /************************************************************************/ - /* Get key input to update the player */ - /************************************************************************/ - void Update(keyInput keyPressed, float pitch, float yaw); - void Render(); - Game* getGameModule(); - private: - }; -} -#endif \ No newline at end of file diff --git a/Code/Game/GameLogic/Object.cpp b/Code/Game/GameLogic/Object.cpp index dcd32bf1..c9d1e476 100644 --- a/Code/Game/GameLogic/Object.cpp +++ b/Code/Game/GameLogic/Object.cpp @@ -1,6 +1,7 @@ #include "Object.h" #include "OysterMath.h" #include "CollisionManager.h" +#include "GID.h" using namespace GameLogic; @@ -18,6 +19,7 @@ Object::Object() rigidBody->gameObjectRef = this; + this->objectID = GID(); this->type = OBJECT_TYPE_UNKNOWN; } @@ -34,6 +36,8 @@ Object::Object(void* collisionFunc, OBJECT_TYPE type) rigidBody->gameObjectRef = this; + this->objectID = GID(); + this->type = type; } @@ -48,6 +52,10 @@ OBJECT_TYPE Object::GetType() { return this->type; } +int Object::GetID() +{ + return this->objectID; +} Oyster::Physics::ICustomBody* Object::GetRigidBody() { diff --git a/Code/Game/GameLogic/Object.h b/Code/Game/GameLogic/Object.h index 01864515..a6ba2b27 100644 --- a/Code/Game/GameLogic/Object.h +++ b/Code/Game/GameLogic/Object.h @@ -21,11 +21,13 @@ namespace GameLogic ~Object(void); OBJECT_TYPE GetType(); + int GetID(); Oyster::Physics::ICustomBody* GetRigidBody(); private: OBJECT_TYPE type; + int objectID; protected: Oyster::Physics::ICustomBody *rigidBody; }; diff --git a/Code/Game/GameLogic/RefManager.cpp b/Code/Game/GameLogic/RefManager.cpp deleted file mode 100644 index cb3d099f..00000000 --- a/Code/Game/GameLogic/RefManager.cpp +++ /dev/null @@ -1,47 +0,0 @@ -#include "RefManager.h" - -using namespace GameLogic; - -typedef std::pair mapData; - -RefManager* RefManager::instance = 0; - -RefManager::RefManager(void) -{ -} - - -RefManager::~RefManager(void) -{ -} - -void RefManager::Release() -{ - if (instance) - { - delete instance; - instance = NULL; - } - -} - -RefManager* RefManager::getInstance( ) -{ - if (!instance) - { - instance = new RefManager(); - }; - return instance; -} - -Object* RefManager::GetMap(const Oyster::Physics::ICustomBody &body) -{ - return mapper[&body]; -} - -void RefManager::AddMapping( const Oyster::Physics::ICustomBody &body, Object &obj) -{ - mapper.insert(mapData(&body,&obj)); -} - - diff --git a/Code/Game/GameLogic/RefManager.h b/Code/Game/GameLogic/RefManager.h deleted file mode 100644 index fa4080c9..00000000 --- a/Code/Game/GameLogic/RefManager.h +++ /dev/null @@ -1,36 +0,0 @@ -////////////////////////////////////////////////// -//Created by Erik and Linda of the GameLogic team -////////////////////////////////////////////////// - - -#ifndef REFMANAGER_H -#define REFMANAGER_H - -#include -#include "Object.h" - -namespace GameLogic -{ - - class RefManager - { - public: - RefManager(void); - ~RefManager(void); - - static RefManager* getInstance( ); - void Release(); - - - Object* GetMap(const Oyster::Physics::ICustomBody &body); //returns the object of an rigidBody, mainly used for CollisionHandler - void AddMapping(const Oyster::Physics::ICustomBody &body, Object &obj); //adds a mapping with body as key and the object as a value - - - private: - static RefManager* instance; - std::map mapper; //mapper points a rigidBody to an actual game object - - - }; -} -#endif \ No newline at end of file diff --git a/Code/Game/GameLogic/Team.cpp b/Code/Game/GameLogic/Team.cpp index b0c28785..27a19bcd 100644 --- a/Code/Game/GameLogic/Team.cpp +++ b/Code/Game/GameLogic/Team.cpp @@ -56,4 +56,6 @@ bool Team::AddPlayer(Player *player) myData->players[myData->nrOfPlayers] = player; myData->nrOfPlayers++; } + + return true; } diff --git a/Code/Game/GameLogic/TestGLMain.cpp b/Code/Game/GameLogic/TestGLMain.cpp deleted file mode 100644 index 57392224..00000000 --- a/Code/Game/GameLogic/TestGLMain.cpp +++ /dev/null @@ -1,304 +0,0 @@ -//-------------------------------------------------------------------------------------- -// File: TemplateMain.cpp -// -// BTH-D3D-Template -// -// Copyright (c) Stefan Petersson 2011. All rights reserved. -//-------------------------------------------------------------------------------------- - -////////////////////////////////////////////////////////////////////////// -// Test main function for game logic when .exe -// Doesn't run when Game logic is compiled as a .dll -////////////////////////////////////////////////////////////////////////// - -#define NOMINMAX -#include -#include "Core/Core.h" -#include "DllInterfaces/GFXAPI.h" -#include "IGame.h" - -#include "L_inputClass.h" - -// debug window include -#include -#include -#include -#include - - - - -//-------------------------------------------------------------------------------------- -// Global Variables -//-------------------------------------------------------------------------------------- -HINSTANCE g_hInst = NULL; -HWND g_hWnd = NULL; - -GameLogic::IGame *game; -InputClass *inputObj; - - -//-------------------------------------------------------------------------------------- -// Forward declarations -//-------------------------------------------------------------------------------------- -HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow ); -LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); -HRESULT Render(float deltaTime); -HRESULT Update(float deltaTime); -HRESULT InitDirect3D(); -HRESULT InitGame(); -HRESULT CleanUp(); - - - -//-------------------------------------------------------------------------------------- -// Entry point to the program. Initializes everything and goes into a message processing -// loop. Idle time is used to render the scene. -//-------------------------------------------------------------------------------------- - -void SetStdOutToNewConsole() -{ - // allocate a console for this app - AllocConsole(); - - // redirect unbuffered STDOUT to the console - HANDLE consoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); - int fileDescriptor = _open_osfhandle((intptr_t)consoleHandle, _O_TEXT); - FILE *fp = _fdopen( fileDescriptor, "w" ); - *stdout = *fp; - setvbuf( stdout, NULL, _IONBF, 0 ); - - // give the console window a nicer title - - SetConsoleTitle(L"Debug Output"); - - // give the console window a bigger buffer size - CONSOLE_SCREEN_BUFFER_INFO csbi; - if ( GetConsoleScreenBufferInfo(consoleHandle, &csbi) ) - { - COORD bufferSize; - bufferSize.X = csbi.dwSize.X; - bufferSize.Y = 50; - SetConsoleScreenBufferSize(consoleHandle, bufferSize); - } -} - -int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow ) -{ - if( FAILED( InitWindow( hInstance, nCmdShow ) ) ) - return 0; - - if( FAILED( InitDirect3D() ) ) - return 0; - - if( FAILED( InitGame() ) ) - return 0; - - __int64 cntsPerSec = 0; - QueryPerformanceFrequency((LARGE_INTEGER*)&cntsPerSec); - float secsPerCnt = 1.0f / (float)cntsPerSec; - - __int64 prevTimeStamp = 0; - QueryPerformanceCounter((LARGE_INTEGER*)&prevTimeStamp); - - //Init debug window - //SetStdOutToNewConsole(); - - // Main message loop - MSG msg = {0}; - while(WM_QUIT != msg.message) - { - if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE) ) - { - TranslateMessage( &msg ); - DispatchMessage( &msg ); - } - else - { - __int64 currTimeStamp = 0; - QueryPerformanceCounter((LARGE_INTEGER*)&currTimeStamp); - float dt = (currTimeStamp - prevTimeStamp) * secsPerCnt; - - //render - Update(dt); - Render(dt); - - prevTimeStamp = currTimeStamp; - } - } - CleanUp(); - return (int) msg.wParam; -} - -//-------------------------------------------------------------------------------------- -// Register class and create window -//-------------------------------------------------------------------------------------- -HRESULT InitWindow( HINSTANCE hInstance, int nCmdShow ) -{ - // Register class - WNDCLASSEX wcex; - wcex.cbSize = sizeof(WNDCLASSEX); - wcex.style = CS_HREDRAW | CS_VREDRAW; - wcex.lpfnWndProc = WndProc; - wcex.cbClsExtra = 0; - wcex.cbWndExtra = 0; - wcex.hInstance = hInstance; - wcex.hIcon = 0; - wcex.hCursor = LoadCursor(NULL, IDC_ARROW); - wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); - wcex.lpszMenuName = NULL; - wcex.lpszClassName = L"BTH_D3D_Template"; - wcex.hIconSm = 0; - if( !RegisterClassEx(&wcex) ) - return E_FAIL; - - // Adjust and create window - g_hInst = hInstance; - RECT rc = { 0, 0, 1024, 768 }; - AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE ); - - if(!(g_hWnd = CreateWindow( - L"BTH_D3D_Template", - L"BTH - Direct3D 11.0 Template", - WS_OVERLAPPEDWINDOW, - CW_USEDEFAULT, - CW_USEDEFAULT, - rc.right - rc.left, - rc.bottom - rc.top, - NULL, - NULL, - hInstance, - NULL))) - { - return E_FAIL; - } - - ShowWindow( g_hWnd, nCmdShow ); - - return S_OK; -} - -//-------------------------------------------------------------------------------------- -// Create Direct3D with Oyster Graphics -//-------------------------------------------------------------------------------------- -HRESULT InitDirect3D() -{ - if(Oyster::Graphics::API::Init(g_hWnd, false, false, Oyster::Math::Float2( 1024, 768)) != Oyster::Graphics::API::Sucsess) - return E_FAIL; - return S_OK; -} - -//-------------------------------------------------------------------------------------- -// Init the input and the game -//------------------------------------------------------------------------------------- -HRESULT InitGame() -{ - inputObj = new InputClass; - if(!inputObj->Initialize(g_hInst, g_hWnd, 1024, 768)) - { - MessageBox(0, L"Could not initialize the input object.", L"Error", MB_OK); - return false; - } - game = new GameLogic::IGame(); - game->Init(); - game->StartGame(); - - return S_OK; -} - -HRESULT Update(float deltaTime) -{ - inputObj->Update(); - GameLogic::keyInput key = GameLogic::keyInput_none; - - if(inputObj->IsKeyPressed(DIK_W)) - { - key = GameLogic::keyInput_W; - } - else if(inputObj->IsKeyPressed(DIK_A)) - { - key = GameLogic::keyInput_A; - } - else if(inputObj->IsKeyPressed(DIK_S)) - { - key = GameLogic::keyInput_S; - } - else if(inputObj->IsKeyPressed(DIK_D)) - { - key = GameLogic::keyInput_D; - } - - float pitch = 0; - float yaw = 0; - - // move only when mouse is pressed - //if(inputObj->IsMousePressed()) - //{ - pitch = inputObj->GetPitch(); - yaw = inputObj->GetYaw(); - //} - - game->Update(key, pitch, yaw); - return S_OK; -} - -HRESULT Render(float deltaTime) -{ - int isPressed = 0; - if(inputObj->IsKeyPressed(DIK_A)) - { - isPressed = 1; - //std::cout<<"test"; - } - - game->Render(); - wchar_t title[255]; - swprintf(title, sizeof(title), L"| Pressing A: %d | \n", (int)(isPressed)); - SetWindowText(g_hWnd, title); - - Oyster::Graphics::API::EndFrame(); - - return S_OK; -} - -HRESULT CleanUp() -{ - SAFE_DELETE(game); - return S_OK; -} -//-------------------------------------------------------------------------------------- -// Called every time the application receives a message -//-------------------------------------------------------------------------------------- -LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ) -{ - PAINTSTRUCT ps; - HDC hdc; - - switch (message) - { - case WM_PAINT: - hdc = BeginPaint(hWnd, &ps); - EndPaint(hWnd, &ps); - break; - - case WM_DESTROY: - PostQuitMessage(0); - break; - - case WM_KEYDOWN: - - switch(wParam) - { - case VK_ESCAPE: - PostQuitMessage(0); - break; - } - break; - - default: - return DefWindowProc(hWnd, message, wParam, lParam); - } - - return 0; -} - diff --git a/Code/Game/GameProtocols/GameProtocols.h b/Code/Game/GameProtocols/GameProtocols.h new file mode 100644 index 00000000..ca248fd0 --- /dev/null +++ b/Code/Game/GameProtocols/GameProtocols.h @@ -0,0 +1,10 @@ +#ifndef GAMEPROTOCOLS_GAMEPROTOCOLS_H +#define GAMEPROTOCOLS_GAMEPROTOCOLS_H + +#include "ObjectProtocols.h" +#include "PlayerProtocols.h" +#include "ControlProtocols.h" +#include "TEST_PROTOCOLS.h" + + +#endif // !GAMEPROTOCOLS_GAMEPROTOCOLS_H diff --git a/Code/Game/GameProtocols/GameProtocols.vcxproj b/Code/Game/GameProtocols/GameProtocols.vcxproj index da83ab6c..fd98e71b 100644 --- a/Code/Game/GameProtocols/GameProtocols.vcxproj +++ b/Code/Game/GameProtocols/GameProtocols.vcxproj @@ -155,6 +155,7 @@ + diff --git a/Code/Game/GameProtocols/ObjectProtocols.h b/Code/Game/GameProtocols/ObjectProtocols.h index 38ab11a2..9cac85be 100644 --- a/Code/Game/GameProtocols/ObjectProtocols.h +++ b/Code/Game/GameProtocols/ObjectProtocols.h @@ -1,5 +1,5 @@ -#ifndef GAMELOGIC_PLAYER_PROTOCOLS_H -#define GAMELOGIC_PLAYER_PROTOCOLS_H +#ifndef GAMELOGIC_OBJECT_PROTOCOLS_H +#define GAMELOGIC_OBJECT_PROTOCOLS_H #include #include "ProtocolIdentificationID.h" diff --git a/Code/Misc/DynamicArray.h b/Code/Misc/DynamicArray.h new file mode 100644 index 00000000..ffc790e6 --- /dev/null +++ b/Code/Misc/DynamicArray.h @@ -0,0 +1,215 @@ +////////////////////////////// +// Dennis Andersen 2013 // +////////////////////////////// +#ifndef MISC_DYNAMIC_ARRAY_H +#define MISC_DYNAMIC_ARRAY_H + +namespace Utility +{ + namespace DynamicMemory + { + template + class DynamicArray + { + public: + DynamicArray(); + DynamicArray(unsigned int capacity); + DynamicArray(const DynamicArray& orig); + const DynamicArray& operator=(const DynamicArray& orig); + virtual~DynamicArray(); + + T& operator[](unsigned int index); + const T& operator[](unsigned int index) const; + + void Push(const T& value); + void Push(unsigned int index, const T& value); + + T& PopFront(); + T& PopBack(); + T& Pop(unsigned int index); + + void Remove(unsigned int index); + void Remove(unsigned int first, unsigned int last); + + void Clear(); + + void Expand(int elements = 0); + + unsigned int Size() const; + unsigned int Capacity() const; + + private: + T* data; + int size; + int capacity; + + }; + + #pragma region Implementation + template DynamicArray::DynamicArray() + : data(0) + , size(0) + , capacity(0) + { } + + template DynamicArray::DynamicArray(unsigned int capacity) + : size(capacity) + , capacity(capacity) + , data(new T[capacity]) + { } + + template DynamicArray::DynamicArray(const DynamicArray& orig) + : capacity(orig.capacity) + , size(orig.size) + { + this->data = new T[orig.capacity]; + for (int i = 0; i < orig.size; i++) + { + this->data[i] = orig.data[i]; + } + } + + template const DynamicArray& DynamicArray::operator=(const DynamicArray& orig) + { + if(this->data) + { + delete [] this->data; + } + + this->capacity = orig.capacity; + this->size = orig.size; + if(orig.capacity > 0) + { + this->data = new T[orig.capacity]; + for (int i = 0; i < orig.size; i++) + { + this->data[i] = orig.data[i]; + } + } + return *this; + } + + template DynamicArray::~DynamicArray() + { + Clear(); + } + + template T& DynamicArray::operator[](unsigned int index) + { + assert((int)index < this->size); + + return this->data[index]; + } + + template const T& DynamicArray::operator[](unsigned int index) const + { + assert(index < this->size); + + return this->data[index]; + } + + template void DynamicArray::Push(const T& value) + { + Expand(1); + + this->data[this->size] = value; + this->size ++; + } + + template void DynamicArray::Push(unsigned int index, const T& value) + { + int newElem = 1; + if((int)index >= this->size) + newElem = (index + 1) - this->size; + + Expand(newElem); + + this->data[index] = value; + this->size += newElem; + } + + template T& DynamicArray::PopFront() + { + return Pop(0); + } + + template T& DynamicArray::PopBack() + { + return Pop(this->size-1); + } + + template T& DynamicArray::Pop(unsigned int index) + { + assert((int)index < this->size); + + T* temp = new T[this->capacity]; + + for (int i = 0; i < this->size; i++) + { + if(i != index) temp[i] = this->data[i]; + } + delete [] this->data; + this->data = temp; + this->size--; + return this->data[index]; + } + + template void DynamicArray::Remove(unsigned int index) + { + assert(index > this->size); + + T* temp = new T[this->capacity - 1]; + + for (int i = 0; i < this->size; i++) + { + if(i != index) temp[i] = this->data[i]; + } + + delete [] this->data; + this->data = temp; + this->size--; + } + + template void DynamicArray::Clear() + { + delete [] this->data; + + this->data = 0; + this->size = 0; + this->capacity = 0; + } + + template void DynamicArray::Expand(int elements) + { + int newSize = this->size + elements; + + if(newSize >= this->capacity) + { + T* temp = new T[newSize]; + + for (int i = 0; i < this->size; i++) + { + temp[i] = this->data[i]; + } + this->capacity = newSize; + + delete [] this->data; + this->data = temp; + } + } + + template unsigned int DynamicArray::Size() const + { + return (unsigned int)this->size; + } + + template unsigned int DynamicArray::Capacity() const + { + return (unsigned int)this->capacity; + } + + #pragma endregion + } +} + +#endif \ No newline at end of file diff --git a/Code/Misc/GID.h b/Code/Misc/GID.h new file mode 100644 index 00000000..9e96a0a8 --- /dev/null +++ b/Code/Misc/GID.h @@ -0,0 +1,29 @@ +#ifndef GID_H +#define GID_H + +#include + +/** +* This class only purpos is to generate a uniqe global id, nothing else.. +*/ +class GID +{ + private: + int id; + int usft() { static int ID = 0; return ID++; } + + public: + GID::GID() { this->id = usft(); } + GID::~GID() { } + GID(const GID& o) { this->id = usft(); } + const GID& operator=(const GID& o) { this->id = usft(); return *this; } + + operator int() const { return this->id; } + bool operator == (const GID& object) const { return (this->id == object.id); } + bool operator == (const int& id) const { return (this->id == id); } + int get() const { return this->id; } +}; + + +#endif + diff --git a/Code/Misc/Misc.vcxproj b/Code/Misc/Misc.vcxproj index 7d3074d3..c0ff5de7 100644 --- a/Code/Misc/Misc.vcxproj +++ b/Code/Misc/Misc.vcxproj @@ -156,7 +156,10 @@ + + + diff --git a/Code/Misc/Misc.vcxproj.filters b/Code/Misc/Misc.vcxproj.filters index d6fea6e9..7f094027 100644 --- a/Code/Misc/Misc.vcxproj.filters +++ b/Code/Misc/Misc.vcxproj.filters @@ -92,5 +92,14 @@ Header Files + + Header Files + + + Header Files + + + Header Files + \ No newline at end of file diff --git a/Code/Misc/OysterCallback.h b/Code/Misc/OysterCallback.h new file mode 100644 index 00000000..64ff9a8f --- /dev/null +++ b/Code/Misc/OysterCallback.h @@ -0,0 +1,47 @@ +////////////////////////////// +// Dennis Andersen 2013 // +////////////////////////////// +#ifndef MISC_OYSTER_CALLBACK_H +#define MISC_OYSTER_CALLBACK_H + +#include "PostBox\IPostBox.h" + +namespace Oyster +{ + namespace Callback + { + template + struct CallbackFunction + { + typedef ReturnVal (*FNC)(ParamVal); + }; + + template + struct CallbackObject + { + virtual ReturnVal ObjectCallback(ParamVal) = 0; + }; + + enum CallbackType + { + CallbackType_PostBox, + CallbackType_Function, + CallbackType_Object, + }; + + template + union OysterCallback + { + IPostBox* callbackPostBox; + CallbackObject *callbackObject; + typename CallbackFunction::FNC callbackFunction; + + OysterCallback() { memset(this, 0, sizeof(OysterCallback)); } + OysterCallback(IPostBox* postbox) { callbackPostBox = postbox; } + OysterCallback(CallbackObject* obj) { callbackObject = obj; } + OysterCallback(typename CallbackFunction::FNC function) { callbackFunction = function; } + }; + } +} + +#endif \ No newline at end of file