source: network-game/common/Common.cpp@ d05086b

Last change on this file since d05086b was d05086b, checked in by dportnoy <dmp1488@…>, 11 years ago

Support for logging to a textfile

  • Property mode set to 100644
File size: 1.3 KB
Line 
1#include "Common.h"
2
3#include <sstream>
4#include <cmath>
5
6#if defined WINDOWS
7 #include <Windows.h>
8#elif defined LINUX
9 #include <ctime>
10#endif
11
12using namespace std;
13
14void set_nonblock(int sock)
15{
16 #if defined WINDOWS
17 unsigned long mode = 1;
18 ioctlsocket(sock, FIONBIO, &mode);
19 #elif defined LINUX
20 int flags;
21 flags = fcntl(sock, F_GETFL,0);
22 assert(flags != -1);
23 fcntl(sock, F_SETFL, flags | O_NONBLOCK);
24 #endif
25}
26
27unsigned long long getCurrentMillis()
28{
29 unsigned long long numMilliseconds;
30
31 #if defined WINDOWS
32 numMilliseconds = GetTickCount();
33 #elif defined LINUX
34 timespec curTime;
35 clock_gettime(CLOCK_REALTIME, &curTime);
36
37 numMilliseconds = curTime.tv_sec*(unsigned long long)1000+curTime.tv_nsec/(unsigned long long)1000000;
38 #endif
39
40 return numMilliseconds;
41}
42
43string getCurrentDateTimeString() {
44 time_t millis = time(NULL);
45 struct tm *time = localtime(&millis);
46
47 ostringstream timeString;
48 timeString << time->tm_hour << ":" << time->tm_min << ":"<< time->tm_sec << " " << (time->tm_mon+1) << "/" << time->tm_mday << "/" << (time->tm_year+1900);
49
50 return timeString.str();
51}
52
53float posDistance(FLOAT_POSITION pos1, FLOAT_POSITION pos2) {
54 float xDiff = pos2.x - pos1.x;
55 float yDiff = pos2.y - pos1.y;
56
57 return sqrt( pow(xDiff,2) + pow(yDiff,2) );
58}
Note: See TracBrowser for help on using the repository browser.