GameLogic - Merged with GL

This commit is contained in:
Dennis Andersen 2014-01-31 09:24:16 +01:00
commit 34b6b29fc4
13 changed files with 336 additions and 18 deletions

View File

@ -204,6 +204,7 @@
<ClCompile Include="DLLMain.cpp" /> <ClCompile Include="DLLMain.cpp" />
<ClCompile Include="GameClientState\GameClientState.cpp" /> <ClCompile Include="GameClientState\GameClientState.cpp" />
<ClCompile Include="GameClientState\GameState.cpp" /> <ClCompile Include="GameClientState\GameState.cpp" />
<ClCompile Include="GameClientState\LanMenuState.cpp" />
<ClCompile Include="GameClientState\LobbyState.cpp" /> <ClCompile Include="GameClientState\LobbyState.cpp" />
<ClCompile Include="GameClientState\C_Object.cpp" /> <ClCompile Include="GameClientState\C_Object.cpp" />
<ClCompile Include="GameClientState\LoginState.cpp" /> <ClCompile Include="GameClientState\LoginState.cpp" />
@ -217,6 +218,7 @@
<ClInclude Include="GameClientState\C_obj\C_UIobject.h" /> <ClInclude Include="GameClientState\C_obj\C_UIobject.h" />
<ClInclude Include="GameClientState\GameClientState.h" /> <ClInclude Include="GameClientState\GameClientState.h" />
<ClInclude Include="GameClientState\GameState.h" /> <ClInclude Include="GameClientState\GameState.h" />
<ClInclude Include="GameClientState\LanMenuState.h" />
<ClInclude Include="GameClientState\LoginState.h" /> <ClInclude Include="GameClientState\LoginState.h" />
<ClInclude Include="Include\DanBiasGame.h" /> <ClInclude Include="Include\DanBiasGame.h" />
<ClInclude Include="GameClientState\LobbyState.h" /> <ClInclude Include="GameClientState\LobbyState.h" />

View File

@ -6,8 +6,10 @@
#include "GameClientState\GameState.h" #include "GameClientState\GameState.h"
#include "GameClientState\LobbyState.h" #include "GameClientState\LobbyState.h"
#include "GameClientState\LoginState.h" #include "GameClientState\LoginState.h"
#include "GameClientState\LanMenuState.h"
#include <Protocols.h> #include <Protocols.h>
#include "NetworkClient.h" #include "NetworkClient.h"
#include <GameServerAPI.h>
#include "../WindowManager/WindowShell.h" #include "../WindowManager/WindowShell.h"
#include "L_inputClass.h" #include "L_inputClass.h"
@ -38,6 +40,7 @@ namespace DanBias
InputClass* inputObj; InputClass* inputObj;
Utility::WinTimer* timer; Utility::WinTimer* timer;
GameRecieverObject* recieverObj; GameRecieverObject* recieverObj;
bool serverOwner;
} data; } data;
#pragma endregion #pragma endregion
@ -63,13 +66,8 @@ namespace DanBias
return DanBiasClientReturn_Error; return DanBiasClientReturn_Error;
m_data->recieverObj = new GameRecieverObject; m_data->recieverObj = new GameRecieverObject;
/*m_data->recieverObj->Connect(desc.port, desc.IP); m_data->serverOwner = false;
if (!m_data->recieverObj->IsConnected())
{
// failed to connect
return DanBiasClientReturn_Error;
}*/
// Start in lobby state // Start in lobby state
m_data->recieverObj->gameClientState = new Client::LoginState(); m_data->recieverObj->gameClientState = new Client::LoginState();
if(!m_data->recieverObj->gameClientState->Init(m_data->recieverObj)) if(!m_data->recieverObj->gameClientState->Init(m_data->recieverObj))
@ -133,9 +131,14 @@ namespace DanBias
HRESULT DanBiasGame::Update(float deltaTime) HRESULT DanBiasGame::Update(float deltaTime)
{ {
m_data->recieverObj->Update();
m_data->recieverObj->Update(); m_data->recieverObj->Update();
m_data->inputObj->Update(); m_data->inputObj->Update();
if(m_data->serverOwner)
{
DanBias::GameServerAPI::ServerUpdate();
}
DanBias::Client::GameClientState::ClientState state = DanBias::Client::GameClientState::ClientState_Same; DanBias::Client::GameClientState::ClientState state = DanBias::Client::GameClientState::ClientState_Same;
state = m_data->recieverObj->gameClientState->Update(deltaTime, m_data->inputObj); state = m_data->recieverObj->gameClientState->Update(deltaTime, m_data->inputObj);
@ -148,6 +151,8 @@ namespace DanBias
switch (state) switch (state)
{ {
case Client::GameClientState::ClientState_LobbyCreated:
m_data->serverOwner = true;
case Client::GameClientState::ClientState_Lobby: case Client::GameClientState::ClientState_Lobby:
m_data->recieverObj->gameClientState = new Client::LobbyState(); m_data->recieverObj->gameClientState = new Client::LobbyState();
break; break;

View File

@ -53,6 +53,7 @@ public:
{ {
ClientState_Login, ClientState_Login,
ClientState_Lobby, ClientState_Lobby,
ClientState_LobbyCreated,
ClientState_Game, ClientState_Game,
ClientState_Same, ClientState_Same,
}; };

View File

@ -144,7 +144,6 @@ bool GameState::InitCamera(Oyster::Math::Float3 startPos)
GameClientState::ClientState GameState::Update(float deltaTime, InputClass* KeyInput) GameClientState::ClientState GameState::Update(float deltaTime, InputClass* KeyInput)
{ {
DanBias::GameServerAPI::ServerUpdate();
switch (privData->state) switch (privData->state)
{ {
case gameStateState_loading: case gameStateState_loading:

View File

@ -0,0 +1,164 @@
#include "LanMenuState.h"
#include "C_obj/C_Player.h"
#include "C_obj/C_StaticObj.h"
#include "C_obj/C_DynamicObj.h"
#include "DllInterfaces/GFXAPI.h"
#include <GameServerAPI.h>
using namespace DanBias::Client;
struct LanMenuState::myData
{
myData(){}
Oyster::Math3D::Float4x4 view;
Oyster::Math3D::Float4x4 proj;
C_Object* object[2];
int modelCount;
// UI object
// game client*
}privData;
LanMenuState::LanMenuState()
{
}
LanMenuState::~LanMenuState()
{
}
bool LanMenuState::Init(Oyster::Network::NetworkClient* nwClient)
{
privData = new myData();
this->nwClient = nwClient;
// load models
LoadModels(L"UImodels.txt");
InitCamera(Oyster::Math::Float3(0,0,5.4f));
return true;
}
bool LanMenuState::LoadModels(std::wstring file)
{
Oyster::Graphics::Definitions::Pointlight plight;
plight.Pos = Oyster::Math::Float3(-2,3,0);
plight.Color = Oyster::Math::Float3(0,1,0);
plight.Radius = 10;
plight.Bright = 1;
Oyster::Graphics::API::AddLight(plight);
// open file
// read file
// init models
privData->modelCount = 2;
ModelInitData modelData;
modelData.world = Oyster::Math3D::Float4x4::identity;
modelData.visible = true;
modelData.modelPath = L"..\\Content\\Models\\box_2.dan";
// load models
privData->object[0] = new C_StaticObj();
privData->object[0]->Init(modelData);
Oyster::Math3D::Float4x4 translate = Oyster::Math3D::TranslationMatrix(Oyster::Math::Float3(-2,-2,-2));
modelData.world = modelData.world * translate;
privData->object[1] = new C_DynamicObj();
privData->object[1]->Init(modelData);
return true;
}
bool LanMenuState::InitCamera(Oyster::Math::Float3 startPos)
{
privData->proj = Oyster::Math3D::ProjectionMatrix_Perspective(Oyster::Math::pi/2,1024.0f/768.0f,.1f,1000);
//privData->proj = Oyster::Math3D::ProjectionMatrix_Orthographic(1024, 768, 1, 1000);
Oyster::Graphics::API::SetProjection(privData->proj);
privData->view = Oyster::Math3D::OrientationMatrix_LookAtDirection(Oyster::Math::Float3(0,0,-1),Oyster::Math::Float3(0,1,0),startPos);
privData->view = Oyster::Math3D::InverseOrientationMatrix(privData->view);
return true;
}
GameClientState::ClientState LanMenuState::Update(float deltaTime, InputClass* KeyInput)
{
// create game
if( KeyInput->IsKeyPressed(DIK_C))
{
DanBias::GameServerAPI::ServerInitDesc desc;
DanBias::GameServerAPI::ServerInitiate(desc);
DanBias::GameServerAPI::ServerStart();
// my ip
nwClient->Connect(15151, "127.0.0.1");
if (!nwClient->IsConnected())
{
// failed to connect
return ClientState_Same;
}
return ClientState_Lobby;
}
// join game
if( KeyInput->IsKeyPressed(DIK_J))
{
// game ip
nwClient->Connect(15151, "194.47.150.56");
if (!nwClient->IsConnected())
{
// failed to connect
return ClientState_Same;
}
return ClientState_Lobby;
}
return ClientState_Same;
}
bool LanMenuState::Render()
{
Oyster::Graphics::API::SetView(privData->view);
Oyster::Graphics::API::SetProjection( privData->proj);
Oyster::Graphics::API::NewFrame();
// render objects
for (int i = 0; i < privData->modelCount; i++)
{
privData->object[i]->Render();
}
// render effects
// render lights
Oyster::Graphics::API::EndFrame();
return true;
}
bool LanMenuState::Release()
{
for (int i = 0; i < privData->modelCount; i++)
{
privData->object[i]->Release();
delete privData->object[i];
privData->object[i] = NULL;
}
delete privData;
privData = NULL;
return true;
}
void LanMenuState::Protocol(ProtocolStruct* protocolStruct)
{
if((PlayerName*)protocolStruct)
PlayerJoinProtocol((PlayerName*)protocolStruct);
}
void LanMenuState::PlayerJoinProtocol(PlayerName* name)
{
}

View File

@ -0,0 +1,37 @@
#ifndef LAN_MENU_STATE_H
#define LAN_MENU_STATE_H
#include "GameClientState.h"
#include "OysterMath.h"
namespace DanBias
{
namespace Client
{
class LanMenuState : public GameClientState
{
public:
LanMenuState();
virtual ~LanMenuState();
virtual bool Init(Oyster::Network::NetworkClient* nwClient);
virtual ClientState Update(float deltaTime, InputClass* KeyInput);
bool LoadModels(std::wstring file);
bool InitCamera(Oyster::Math::Float3 startPos);
virtual bool Render();
virtual bool Release();
virtual void Protocol(ProtocolStruct* protocolStruct);
void PlayerJoinProtocol(PlayerName* name);
private:
Oyster::Network::NetworkClient* nwClient;
struct myData;
myData* privData;
};
}
}
#endif

View File

@ -87,7 +87,7 @@ GameClientState::ClientState LobbyState::Update(float deltaTime, InputClass* Key
// update animation // update animation
// send data to server // send data to server
// check data from server // check data from server
DanBias::GameServerAPI::ServerUpdate();
if( KeyInput->IsKeyPressed(DIK_G)) if( KeyInput->IsKeyPressed(DIK_G))
{ {

View File

@ -103,7 +103,7 @@ GameClientState::ClientState LoginState::Update(float deltaTime, InputClass* Key
// failed to connect // failed to connect
return ClientState_Same; return ClientState_Same;
} }
return ClientState_Lobby; return ClientState_LobbyCreated;
} }
// join game // join game
if( KeyInput->IsKeyPressed(DIK_J)) if( KeyInput->IsKeyPressed(DIK_J))

View File

@ -57,7 +57,7 @@ int WINAPI WinMain( HINSTANCE hinst, HINSTANCE prevInst, PSTR cmdLine, int cmdSh
return cmdShow; return cmdShow;
} }
serverThread = std::thread(ServerFnc); //serverThread = std::thread(ServerFnc);
Sleep(200); Sleep(200);

View File

@ -16,11 +16,15 @@
#include "../../Misc/Utilities.h" #include "../../Misc/Utilities.h"
#include "../../Misc/Thread/IThreadObject.h" #include "../../Misc/Thread/IThreadObject.h"
#include "../../Misc/Thread/OysterThread.h" #include "../../Misc/Thread/OysterThread.h"
#include "../../Misc/Packing/Packing.h"
#include <queue>
using namespace Oyster::Network; using namespace Oyster::Network;
using namespace Oyster::Thread; using namespace Oyster::Thread;
using namespace Utility::DynamicMemory; using namespace Utility::DynamicMemory;
using namespace Utility::Container; using namespace Utility::Container;
using namespace std;
/************************************* /*************************************
PrivateData PrivateData
@ -35,6 +39,7 @@ struct NetworkClient::PrivateData : public IThreadObject
Translator translator; Translator translator;
OysterThread thread; OysterThread thread;
OysterByte recieveBuffer;
//Message queue for sending and recieving //Message queue for sending and recieving
ThreadSafeQueue<CustomNetProtocol> sendQueue; ThreadSafeQueue<CustomNetProtocol> sendQueue;
@ -82,6 +87,7 @@ struct NetworkClient::PrivateData : public IThreadObject
CustomNetProtocol p = this->sendQueue.Pop(); CustomNetProtocol p = this->sendQueue.Pop();
this->translator.Pack(temp, p); this->translator.Pack(temp, p);
errorCode = this->connection.Send(temp); errorCode = this->connection.Send(temp);
if(errorCode != 0) if(errorCode != 0)
{ {
CEA parg; CEA parg;
@ -103,6 +109,10 @@ struct NetworkClient::PrivateData : public IThreadObject
if(errorCode == 0 && temp.GetSize()) if(errorCode == 0 && temp.GetSize())
{ {
HandleRecievedData(temp);
/* Replaced with EmptyOutbufferedQueue() and HandleRecievedData(OysterByte)
CustomNetProtocol protocol; CustomNetProtocol protocol;
bool ok = this->translator.Unpack(protocol, temp); bool ok = this->translator.Unpack(protocol, temp);
@ -114,7 +124,8 @@ struct NetworkClient::PrivateData : public IThreadObject
parg.data.protocol = protocol; parg.data.protocol = protocol;
NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e = { this->parent, parg }; NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e = { this->parent, parg };
this->recieveQueue.Push(e); this->recieveQueue.Push(e);
} }*/
} }
//else //else
//{ //{
@ -127,6 +138,76 @@ struct NetworkClient::PrivateData : public IThreadObject
return errorCode; return errorCode;
} }
void HandleRecievedData(OysterByte& data)
{
//Loop through all packages that was recieved and add them to the queue.
unsigned int size = 0;
Oyster::Network::OysterByte msg;
//If there is part of a message in the buffer.
if(recieveBuffer.GetSize() > 0)
{
//cout << "the buffer size: " << recvBuffer.GetSize() <<endl;
unsigned int temp = recieveBuffer.GetSize();
size = Oyster::Packing::Unpacki(recieveBuffer);
if(temp + data.GetSize() > size)
{
msg = recieveBuffer;
recieveBuffer.Clear();
size -= msg.GetSize();
msg.AppendPartOfArray(data, 0, size);
UnpackMessageAndAddToQueue(msg);
}
else if(temp + data.GetSize() == size)
{
msg = recieveBuffer;
recieveBuffer.Clear();
size -= msg.GetSize();
msg += data;
UnpackMessageAndAddToQueue(msg);
return;
}
else
{
recieveBuffer += data;
size = data.GetSize();
return;
}
}
for(unsigned int i = size; i < data.GetSize(); i += size)
{
size = Oyster::Packing::Unpacki(&data.GetByteArray()[i]);
if(i+size > data.GetSize())
{
//Add it to the recvBuffer instead.
recieveBuffer.AppendPartOfArray(data, i, data.GetSize());
break;
}
msg.Clear();
msg.AppendPartOfArray(data, i, i+size);
UnpackMessageAndAddToQueue(msg);
}
}
void UnpackMessageAndAddToQueue(OysterByte& msg)
{
CustomNetProtocol protocol;
bool ok = this->translator.Unpack(protocol, msg);
//Check if the protocol was unpacked correctly
if(ok)
{
CEA parg;
parg.type = CEA::EventType_ProtocolRecieved;
parg.data.protocol = protocol;
NetEvent<NetworkClient*, NetworkClient::ClientEventArgs> e = { this->parent, parg };
this->recieveQueue.Push(e);
}
}
}; };
unsigned int NetworkClient::PrivateData::currID = 0; unsigned int NetworkClient::PrivateData::currID = 0;

View File

@ -13,7 +13,7 @@
#include "NetworkAPI_Preprocessor.h" #include "NetworkAPI_Preprocessor.h"
#include "NetworkServerEventStruct.h" #include "NetworkServerEventStruct.h"
#include "NetworkClient.h" #include "NetworkClient.h"
#include "Utilities.h" #include "..\Misc\Utilities.h"
#include "DynamicArray.h" #include "DynamicArray.h"
namespace Oyster namespace Oyster

View File

@ -48,7 +48,7 @@ void OysterByte::Resize(unsigned int cap)
} }
} }
int OysterByte::GetSize() unsigned int OysterByte::GetSize()
{ {
return size; return size;
} }
@ -60,7 +60,7 @@ unsigned char* OysterByte::GetByteArray()
void OysterByte::AddSize(unsigned int size) void OysterByte::AddSize(unsigned int size)
{ {
int newCapacity = this->size + size; unsigned int newCapacity = this->size + size;
if(newCapacity >= capacity) if(newCapacity >= capacity)
{ {
@ -81,6 +81,32 @@ void OysterByte::SetSize(unsigned int size)
this->size = size; this->size = size;
} }
void OysterByte::AppendPartOfArray(OysterByte& source, unsigned int startIndex, unsigned int endIndex)
{
if(startIndex < 0 && startIndex >= endIndex)
return;
if(endIndex > source.size)
return;
unsigned int totalSize = endIndex - startIndex;
totalSize += size;
//Make sure the new data can fit in the array.
if(totalSize > capacity)
{
IncreaseCapacity(totalSize);
}
//Copy over new data.
for(unsigned int i = size; i < totalSize; i++)
{
byteArray[i] = source.byteArray[startIndex++];
}
//Set the new size
size = totalSize;
}
OysterByte& OysterByte::operator =(const OysterByte& obj) OysterByte& OysterByte::operator =(const OysterByte& obj)
{ {
delete[] this->byteArray; delete[] this->byteArray;

View File

@ -25,7 +25,7 @@ namespace Oyster
//Resizes the array with, it does not keep anything in it. //Resizes the array with, it does not keep anything in it.
void Resize(unsigned int cap); void Resize(unsigned int cap);
int GetSize(); unsigned int GetSize();
unsigned char* GetByteArray(); unsigned char* GetByteArray();
void AddSize(unsigned int size); void AddSize(unsigned int size);
@ -34,6 +34,9 @@ namespace Oyster
//Only sets the private variable 'size' //Only sets the private variable 'size'
void SetSize(unsigned int size); void SetSize(unsigned int size);
//Copies over a part of the addFrom array and adds it to the end of this array.
void AppendPartOfArray(OysterByte& source, unsigned int startIndex, unsigned int endIndex);
OysterByte& operator =(const OysterByte& obj); OysterByte& operator =(const OysterByte& obj);
operator char*(); operator char*();