From: Daniel Karbach <daniel.karbach@localhorst.tv>
Date: Mon, 14 Sep 2015 10:49:39 +0000 (+0200)
Subject: move client update throttling to state
X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=d2eb51ad9759eeee743b04aee6f1ae69132fc706;p=blank.git

move client update throttling to state
---

diff --git a/src/client/InteractiveState.hpp b/src/client/InteractiveState.hpp
index d36472d..9b1ce52 100644
--- a/src/client/InteractiveState.hpp
+++ b/src/client/InteractiveState.hpp
@@ -1,6 +1,7 @@
 #ifndef BLANK_CLIENT_INTERACTIVESTATE_HPP_
 #define BLANK_CLIENT_INTERACTIVESTATE_HPP_
 
+#include "../app/IntervalTimer.hpp"
 #include "../app/State.hpp"
 #include "../io/WorldSave.hpp"
 #include "../model/Skeletons.hpp"
@@ -42,6 +43,7 @@ private:
 	Interface interface;
 	ChunkRenderer chunk_renderer;
 	Skeletons skeletons;
+	IntervalTimer update_timer;
 
 };
 
diff --git a/src/client/client.cpp b/src/client/client.cpp
index 757bc3a..bd43635 100644
--- a/src/client/client.cpp
+++ b/src/client/client.cpp
@@ -55,7 +55,8 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
 	world.AddPlayer(master.GetInterfaceConf().player_name, player_id)
 )
 , chunk_renderer(*interface.GetPlayer().chunks)
-, skeletons() {
+, skeletons()
+, update_timer(16) {
 	TextureIndex tex_index;
 	master.GetEnv().loader.LoadBlockTypes("default", block_types, tex_index);
 	chunk_renderer.LoadTextures(master.GetEnv().loader, tex_index);
@@ -63,6 +64,7 @@ InteractiveState::InteractiveState(MasterState &master, uint32_t player_id)
 	skeletons.Load();
 	// TODO: better solution for initializing HUD
 	interface.SelectNext();
+	update_timer.Start();
 }
 
 void InteractiveState::OnEnter() {
@@ -104,9 +106,13 @@ void InteractiveState::Update(int dt) {
 	world.Update(dt);
 	chunk_renderer.Update(dt);
 
+	update_timer.Update(dt);
+
 	Entity &player = *interface.GetPlayer().entity;
 
-	master.GetClient().SendPlayerUpdate(player);
+	if (update_timer.Hit()) {
+		master.GetClient().SendPlayerUpdate(player);
+	}
 
 	glm::mat4 trans = player.Transform(player.ChunkCoords());
 	glm::vec3 dir(trans * glm::vec4(0.0f, 0.0f, -1.0f, 0.0f));
diff --git a/src/net/Client.hpp b/src/net/Client.hpp
index ee848c0..2848aed 100644
--- a/src/net/Client.hpp
+++ b/src/net/Client.hpp
@@ -2,7 +2,6 @@
 #define BLANK_NET_CLIENT_HPP_
 
 #include "Connection.hpp"
-#include "../app/IntervalTimer.hpp"
 
 #include <string>
 #include <SDL_net.h>
@@ -34,8 +33,7 @@ public:
 	std::uint16_t SendPing();
 	std::uint16_t SendLogin(const std::string &);
 	std::uint16_t SendPart();
-	// this may not send the update at all, in which case it returns -1
-	int SendPlayerUpdate(const Entity &);
+	std::uint16_t SendPlayerUpdate(const Entity &);
 
 private:
 	void HandlePacket(const UDPpacket &);
@@ -44,7 +42,6 @@ private:
 	Connection conn;
 	UDPsocket client_sock;
 	UDPpacket client_pack;
-	IntervalTimer update_timer;
 
 };
 
diff --git a/src/net/net.cpp b/src/net/net.cpp
index 4e6a63a..18abb92 100644
--- a/src/net/net.cpp
+++ b/src/net/net.cpp
@@ -51,13 +51,11 @@ IPaddress client_resolve(const char *host, Uint16 port) {
 Client::Client(const Config &conf)
 : conn(client_resolve(conf.host.c_str(), conf.port))
 , client_sock(client_bind(0))
-, client_pack{ -1, nullptr, 0 }
-, update_timer(16) {
+, client_pack{ -1, nullptr, 0 } {
 	client_pack.data = new Uint8[sizeof(Packet)];
 	client_pack.maxlen = sizeof(Packet);
 	// establish connection
 	SendPing();
-	update_timer.Start();
 }
 
 Client::~Client() {
@@ -93,7 +91,6 @@ void Client::HandlePacket(const UDPpacket &udp_pack) {
 }
 
 void Client::Update(int dt) {
-	update_timer.Update(dt);
 	conn.Update(dt);
 	if (conn.ShouldPing()) {
 		SendPing();
@@ -110,9 +107,7 @@ uint16_t Client::SendLogin(const string &name) {
 	return conn.Send(client_pack, client_sock);
 }
 
-int Client::SendPlayerUpdate(const Entity &player) {
-	// don't send all too many updates
-	if (!update_timer.Hit()) return -1;
+uint16_t Client::SendPlayerUpdate(const Entity &player) {
 	auto pack = Packet::Make<Packet::PlayerUpdate>(client_pack);
 	pack.WritePlayer(player);
 	return conn.Send(client_pack, client_sock);