Explorar o código

write_vec3 and read_vec3 fixed

mikymod %!s(int64=13) %!d(string=hai) anos
pai
achega
9393f6a274
Modificáronse 3 ficheiros con 34 adicións e 74 borrados
  1. 12 52
      src/network/BitMessage.cpp
  2. 4 4
      src/network/BitMessage.h
  3. 18 18
      tests/messages.cpp

+ 12 - 52
src/network/BitMessage.cpp

@@ -391,9 +391,11 @@ void BitMessage::write_real(real f)
 
 //---------------------------------------------------------------------------------------------
 
-void BitMessage::write_vec3(const Vec3& v, int32_t num_bits)
+void BitMessage::write_vec3(const Vec3& v)
 {
-	write_bits(vec3_to_bits(v, num_bits), num_bits);
+	write_real(v.x);
+	write_real(v.y);
+	write_real(v.z);
 }
 
 //---------------------------------------------------------------------------------------------
@@ -609,9 +611,15 @@ real BitMessage::read_real() const
 
 //---------------------------------------------------------------------------------------------
 
-Vec3 BitMessage::read_vec3(int32_t num_bits) const
+Vec3 BitMessage::read_vec3() const
 {
-	return bits_to_vec3(read_bits(num_bits), num_bits);
+	Vec3 v;
+	
+	v.x = read_real();
+	v.y = read_real();
+	v.z = read_real();
+	
+	return v;
 }
 
 //---------------------------------------------------------------------------------------------
@@ -695,53 +703,5 @@ void BitMessage::read_ipv4addr(os::NetAddress* addr) const
 
 //---------------------------------------------------------------------------------------------
 
-int32_t BitMessage::vec3_to_bits(const Vec3& v, int32_t num_bits)
-{
-	assert(num_bits >= 6 && num_bits <= 32);
-	assert(v.squared_length() - 1.0f < 0.01f);
-  
-	int32_t max; 
-	int32_t bits;
-	float bias;
-
-	num_bits /= 3;
-	max = (1 << (num_bits - 1)) - 1;
-	bias = 0.5f / max;
-
-	bits = FLOATSIGNBITSET(v.x) << (num_bits * 3 - 1);
-	bits |= ((int32_t)((math::abs(v.x) + bias) * max)) << (num_bits * 2);
-	bits |= FLOATSIGNBITSET(v.y) << (num_bits * 2 - 1);
-	bits |= ((int32_t)((math::abs(v.y) + bias) * max)) << (num_bits * 1);
-	bits |= FLOATSIGNBITSET(v.z) << (num_bits * 1 - 1);
-	bits |= ((int32_t)((math::abs(v.z) + bias) * max)) << (num_bits * 0);
-	
-	return bits;  
-}
-
-//---------------------------------------------------------------------------------------------
-
-Vec3 BitMessage::bits_to_vec3(int32_t bits, int32_t num_bits)
-{
-	assert(num_bits >= 6 && num_bits <= 32);
-  
-	static float sign[2] = {1.0f, -1.0f};
-	int max;
-	float inv_max;
-	Vec3 v;
-
-	num_bits /= 3;
-	max = (1 << (num_bits - 1)) - 1;
-	inv_max = 1.0f / max;
-
-	v.x = sign[(bits >> (num_bits * 3 - 1)) & 1] * ((bits >> (num_bits * 2)) & max) * inv_max;
-	v.y = sign[(bits >> (num_bits * 2 - 1)) & 1] * ((bits >> (num_bits * 1)) & max) * inv_max;
-	v.z = sign[(bits >> (num_bits * 1 - 1)) & 1] * ((bits >> (num_bits * 0)) & max) * inv_max;
-	v.normalize();
-	
-	return v;
-}
-
-//---------------------------------------------------------------------------------------------
-
 }	//namespace network
 }	//namespace crown

+ 4 - 4
src/network/BitMessage.h

@@ -54,7 +54,7 @@ namespace network
 		void			write_uint16(int32_t c);
 		void			write_int32(int32_t c);
 		void			write_real(real f);
-		void			write_vec3(const Vec3& v, int32_t num_bits);
+		void			write_vec3(const Vec3& v);
 		void			write_string(const char* s, int32_t max_len = -1, bool make_7_bit = true);
 		void			write_data(const void* data, int32_t length);
 		void			write_ipv4addr(const os::NetAddress addr);
@@ -69,13 +69,13 @@ namespace network
 		int32_t			read_uint16() const;
 		int32_t			read_int32() const;
 		real			read_real() const;
-		Vec3			read_vec3(int32_t num_bits) const;
+		Vec3			read_vec3() const;
 		int32_t			read_string(char* buffer, int32_t buffer_size) const;
 		int32_t			read_data(void* data, int32_t length) const;
 		void			read_ipv4addr(os::NetAddress* addr) const;
 
- 		static int32_t	vec3_to_bits(const Vec3& v, int32_t num_bits);
- 		static Vec3		bits_to_vec3(int32_t bits, int32_t num_bits);
+ 		static int32_t	vec3_to_bits(const Vec3& v);
+ 		static Vec3		bits_to_vec3(int32_t bits);
 
 	private:
 	  

+ 18 - 18
tests/messages.cpp

@@ -2,7 +2,7 @@
 
 #include "Vec3.h"
 #include "OS.h"
-#include "Message.h"
+#include "BitMessage.h"
 
 using namespace crown;
 
@@ -13,7 +13,7 @@ void test_int8()
 	uint32_t bits_read;
 	uint32_t rem_read_bits; 
 	
-  	network::Message m = network::Message();
+  	network::BitMessage m = network::BitMessage();
 	
 	uint8_t tmp[4];
 	int8_t res;
@@ -48,7 +48,7 @@ void test_uint8()
 	uint32_t bits_read;
 	uint32_t rem_read_bits; 
 	
-  	network::Message m = network::Message();
+  	network::BitMessage m = network::BitMessage();
 
 	uint8_t tmp[4];
 	uint8_t res;
@@ -82,7 +82,7 @@ void test_int16()
 	uint32_t bits_read;
 	uint32_t rem_read_bits; 
 	
-  	network::Message m = network::Message();  
+  	network::BitMessage m = network::BitMessage();  
 	
 	uint8_t tmp[4];
 	int16_t res;
@@ -114,7 +114,7 @@ void test_uint16()
 	uint32_t bits_read;
 	uint32_t rem_read_bits; 
 	
-  	network::Message m = network::Message();
+  	network::BitMessage m = network::BitMessage();
 
 	uint8_t tmp[4];
 	uint16_t res;
@@ -146,7 +146,7 @@ void test_int32()
 	uint32_t bits_read;
 	uint32_t rem_read_bits; 
 	
-  	network::Message m = network::Message();
+  	network::BitMessage m = network::BitMessage();
 	
 	uint8_t tmp[4];
 	int32_t res;
@@ -179,7 +179,7 @@ void test_real()
 	uint32_t bits_read;
 	uint32_t rem_read_bits; 
 	
-  	network::Message m = network::Message();
+  	network::BitMessage m = network::BitMessage();
 
 	uint8_t tmp[4];
 	real res;
@@ -213,19 +213,19 @@ void test_vec3()
 	uint32_t bits_read;
 	uint32_t rem_read_bits; 
 	
-  	network::Message m = network::Message();
+  	network::BitMessage m = network::BitMessage();
 	
 	
-	uint8_t tmp[4];
-	Vec3 v(0.0f, 0.0f, 1.0f);
+	uint8_t tmp[12];
+	Vec3 v(0.525f, 0.432f, 0.234f);
 	Vec3 res;
 	
-	m.init(tmp, 4);
-	m.write_vec3(v, 32);
+	m.init(tmp, 12);
+	m.write_vec3(v);
 	bits_written = m.get_num_bits_written();
 	rem_write_bits = m.get_remaining_write_bits();
 	
-	res = m.read_vec3(32);
+	res = m.read_vec3();
 	bits_read = m.get_num_bits_read();
 	rem_read_bits = m.get_remaining_read_bits();
 	
@@ -247,7 +247,7 @@ void test_string()
 	uint32_t bits_read;
 	uint32_t rem_read_bits; 
 	
-  	network::Message m = network::Message();
+  	network::BitMessage m = network::BitMessage();
 	
 	uint8_t tmp[16];
 	char res[16];
@@ -283,7 +283,7 @@ void test_data()
 	uint32_t bits_read;
 	uint32_t rem_read_bits; 
 	
-  	network::Message m = network::Message();
+  	network::BitMessage m = network::BitMessage();
 	
 	uint8_t tmp[] = "test generic";
 	uint8_t res[16];
@@ -317,13 +317,13 @@ void test_net_address()
 	uint32_t bits_read;
 	uint32_t rem_read_bits; 
 	
-	network::Message m = network::Message();
+	network::BitMessage m = network::BitMessage();
 
 	uint8_t tmp[16];
 	
 	
-	os::IPv4Address addr;
-	os::IPv4Address res;
+	os::NetAddress addr;
+	os::NetAddress res;
 	
 	addr.set(192, 168, 0, 1, 80);