Fixed jittery lag and client closing issues

This commit is contained in:
Kayne Ruse
2013-12-07 03:24:39 +11:00
parent 49c9abe91b
commit de1a6a050f
2 changed files with 31 additions and 16 deletions
+28 -16
View File
@@ -114,6 +114,11 @@ void InWorld::Render(SDL_Surface* const screen) {
//Event handlers //Event handlers
//------------------------- //-------------------------
void InWorld::QuitEvent() {
//exit the game AND the server
RequestDisconnect();
}
void InWorld::MouseMotion(SDL_MouseMotionEvent const& motion) { void InWorld::MouseMotion(SDL_MouseMotionEvent const& motion) {
disconnectButton.MouseMotion(motion); disconnectButton.MouseMotion(motion);
shutDownButton.MouseMotion(motion); shutDownButton.MouseMotion(motion);
@@ -126,28 +131,16 @@ void InWorld::MouseButtonDown(SDL_MouseButtonEvent const& button) {
void InWorld::MouseButtonUp(SDL_MouseButtonEvent const& button) { void InWorld::MouseButtonUp(SDL_MouseButtonEvent const& button) {
if (disconnectButton.MouseButtonUp(button) == Button::State::HOVER) { if (disconnectButton.MouseButtonUp(button) == Button::State::HOVER) {
//send a disconnect request RequestDisconnect();
NetworkPacket packet;
packet.meta.type = NetworkPacket::Type::DISCONNECT;
packet.clientInfo.index = clientIndex;
network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket));
} }
if (shutDownButton.MouseButtonUp(button) == Button::State::HOVER) { if (shutDownButton.MouseButtonUp(button) == Button::State::HOVER) {
//send a shutdown request RequestShutDown();
NetworkPacket packet;
packet.meta.type = NetworkPacket::Type::SHUTDOWN;
network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket));
} }
} }
void InWorld::KeyDown(SDL_KeyboardEvent const& key) { void InWorld::KeyDown(SDL_KeyboardEvent const& key) {
switch(key.keysym.sym) { switch(key.keysym.sym) {
case SDLK_ESCAPE: { case SDLK_ESCAPE: {
//send a disconnect request
NetworkPacket packet;
packet.meta.type = NetworkPacket::Type::DISCONNECT;
packet.clientInfo.index = clientIndex;
network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket));
QuitEvent(); QuitEvent();
} }
break; break;
@@ -284,8 +277,11 @@ void InWorld::HandlePlayerUpdate(NetworkPacket packet) {
return; return;
} }
playerCharacters[packet.playerInfo.playerIndex].SetPosition(packet.playerInfo.position); //update only if the message didn't originate from here
playerCharacters[packet.playerInfo.playerIndex].SetMotion(packet.playerInfo.motion); if (packet.playerInfo.clientIndex != clientIndex) {
playerCharacters[packet.playerInfo.playerIndex].SetPosition(packet.playerInfo.position);
playerCharacters[packet.playerInfo.playerIndex].SetMotion(packet.playerInfo.motion);
}
playerCharacters[packet.playerInfo.playerIndex].ResetDirection(); playerCharacters[packet.playerInfo.playerIndex].ResetDirection();
} }
@@ -301,3 +297,19 @@ void InWorld::SendState() {
network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket)); network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket));
} }
void InWorld::RequestDisconnect() {
//send a disconnect request
NetworkPacket packet;
packet.meta.type = NetworkPacket::Type::DISCONNECT;
packet.clientInfo.index = clientIndex;
network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket));
}
void InWorld::RequestShutDown() {
//send a shutdown request
NetworkPacket packet;
packet.meta.type = NetworkPacket::Type::SHUTDOWN;
packet.clientInfo.index = clientIndex;
network.Send(Channels::SERVER, &packet, sizeof(NetworkPacket));
}
+3
View File
@@ -48,6 +48,7 @@ protected:
void Render(SDL_Surface* const); void Render(SDL_Surface* const);
//Event handlers //Event handlers
void QuitEvent();
void MouseMotion(SDL_MouseMotionEvent const&); void MouseMotion(SDL_MouseMotionEvent const&);
void MouseButtonDown(SDL_MouseButtonEvent const&); void MouseButtonDown(SDL_MouseButtonEvent const&);
void MouseButtonUp(SDL_MouseButtonEvent const&); void MouseButtonUp(SDL_MouseButtonEvent const&);
@@ -61,6 +62,8 @@ protected:
void HandlePlayerUpdate(NetworkPacket); void HandlePlayerUpdate(NetworkPacket);
void SendState(); void SendState();
void RequestDisconnect();
void RequestShutDown();
//global //global
ConfigUtility& config; ConfigUtility& config;