pingus-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Pingus-CVS] r3651 - trunk/pingus/src


From: grumbel at BerliOS
Subject: [Pingus-CVS] r3651 - trunk/pingus/src
Date: Fri, 4 Jul 2008 02:55:33 +0200

Author: grumbel
Date: 2008-07-04 02:55:32 +0200 (Fri, 04 Jul 2008)
New Revision: 3651

Modified:
   trunk/pingus/src/math.cpp
   trunk/pingus/src/math.hpp
   trunk/pingus/src/server_event.cpp
Log:
Moved float2string/string2float into Math

Modified: trunk/pingus/src/math.cpp
===================================================================
--- trunk/pingus/src/math.cpp   2008-07-04 00:41:32 UTC (rev 3650)
+++ trunk/pingus/src/math.cpp   2008-07-04 00:55:32 UTC (rev 3651)
@@ -54,6 +54,45 @@
   return ::atan2(x, y);
 }
 
+static char num2hex[] = "0123456789abcdef";
+
+std::string float2string(float value)
+{
+  std::string str(2*sizeof(float), '0');
+
+  for(size_t i = 0; i < sizeof(float); ++i)
+    {
+      char v = reinterpret_cast<char*>(&value)[i];
+      str[2*i + 0] = num2hex[(v & 0xf0) >> 4];
+      str[2*i + 1] = num2hex[v & 0x0f];
+    }
+  return str;
+}
+
+static char hex2int(char c)
+{
+  if (c >= '0' && c <= '9')
+    return c - '0';
+  else if (c >= 'a' && c <= 'f')
+    return c - 'a' + 0xa;
+  else
+    return 0;    
+}
+
+float string2float(const std::string& str)
+{
+  assert(str.size() == 2*sizeof(float));
+
+  float value;
+  for(size_t i = 0; i < sizeof(float); ++i)
+    {
+      char& v = reinterpret_cast<char*>(&value)[i];
+      v = (hex2int(str[2*i+0]) << 4) | hex2int(str[2*i+1]);
+    }
+
+  return value;
+}
+
 } // namespace Math
 
 /* EOF */

Modified: trunk/pingus/src/math.hpp
===================================================================
--- trunk/pingus/src/math.hpp   2008-07-04 00:41:32 UTC (rev 3650)
+++ trunk/pingus/src/math.hpp   2008-07-04 00:55:32 UTC (rev 3651)
@@ -19,6 +19,7 @@
 
 #include <assert.h>
 #include <stdlib.h>
+#include <string>
 #include "pingus.hpp"
 
 /** A collection of small math helper functions, some of them might be
@@ -92,6 +93,12 @@
 float floor(float x);
 float atan2(float x, float y);
 
+/** Write out the raw bits of a float as hex */
+std::string float2string(float value);
+
+/** Restore the raw bits of a float from a string */
+float string2float(const std::string& str);
+
 } // namespace Math
 
 #endif

Modified: trunk/pingus/src/server_event.cpp
===================================================================
--- trunk/pingus/src/server_event.cpp   2008-07-04 00:41:32 UTC (rev 3650)
+++ trunk/pingus/src/server_event.cpp   2008-07-04 00:55:32 UTC (rev 3651)
@@ -19,6 +19,7 @@
 #include <iostream>
 #include <boost/format.hpp>
 
+#include "math.hpp"
 #include "pingus_error.hpp"
 #include "server.hpp"
 #include "world.hpp"
@@ -26,47 +27,6 @@
 #include "pingu.hpp"
 #include "string_util.hpp"
 
-static char num2hex[] = "0123456789abcdef";
-
-/** Write out the raw bits of a float as hex */
-static std::string float2string(float value)
-{
-  std::string str(2*sizeof(float), '0');
-
-  for(size_t i = 0; i < sizeof(float); ++i)
-    {
-      char v = reinterpret_cast<char*>(&value)[i];
-      str[2*i + 0] = num2hex[(v & 0xf0) >> 4];
-      str[2*i + 1] = num2hex[v & 0x0f];
-    }
-  return str;
-}
-
-static char hex2int(char c)
-{
-  if (c >= '0' && c <= '9')
-    return c - '0';
-  else if (c >= 'a' && c <= 'f')
-    return c - 'a' + 0xa;
-  else
-    return 0;    
-}
-
-/** Restore the raw bits of a float from a string */
-static float string2float(const std::string& str)
-{
-  assert(str.size() == 2*sizeof(float));
-
-  float value;
-  for(size_t i = 0; i < sizeof(float); ++i)
-    {
-      char& v = reinterpret_cast<char*>(&value)[i];
-      v = (hex2int(str[2*i+0]) << 4) | hex2int(str[2*i+1]);
-    }
-
-  return value;
-}
-
 ServerEvent::ServerEvent() 
   : type(PINGU_ACTION_EVENT),
     time_stamp(0),
@@ -102,10 +62,10 @@
       reader.read_int ("id",     pingu_id);
 
       if (reader.read_string("raw-x", raw_x))
-        pos.x = string2float(raw_x);
+        pos.x = Math::string2float(raw_x);
 
       if (reader.read_string("raw-y", raw_y))
-        pos.y = string2float(raw_y);
+        pos.y = Math::string2float(raw_y);
 
       reader.read_enum("action", pingu_action, Actions::action_from_string);
 
@@ -135,8 +95,8 @@
         out << "(pingu-action "
             << "(time " << time_stamp << ") "
             << "(id " << pingu_id << ") "
-            << "(raw-x \"" << float2string(pos.x) << "\") "
-            << "(raw-y \"" << float2string(pos.y) << "\") "
+            << "(raw-x \"" << Math::float2string(pos.x) << "\") "
+            << "(raw-y \"" << Math::float2string(pos.y) << "\") "
             << "(action \"" << Actions::action_to_string(pingu_action) << 
"\"))"
             << std::endl;
         break;





reply via email to

[Prev in Thread] Current Thread [Next in Thread]