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

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

New network headers in Common.cpp

  • Property mode set to 100644
File size: 1.9 KB
Line 
1#include "Common.h"
2
3#include "Compiler.h"
4
5#if defined WINDOWS
6 #include <winsock2.h>
7#elif defined LINUX
8 #include <fcntl.h>
9 #include <assert.h>
10#endif
11
12#include <sstream>
13#include <cmath>
14#include <ctime>
15
16using namespace std;
17
18FLOAT_POSITION POSITION::toFloat() {
19 FLOAT_POSITION floatPosition;
20 floatPosition.x = x;
21 floatPosition.y = y;
22
23 return floatPosition;
24}
25
26POSITION FLOAT_POSITION::toInt() {
27 POSITION position;
28 position.x = x;
29 position.y = y;
30
31 return position;
32}
33
34void set_nonblock(int sock)
35{
36 #if defined WINDOWS
37 unsigned long mode = 1;
38 ioctlsocket(sock, FIONBIO, &mode);
39 #elif defined LINUX
40 int flags = fcntl(sock, F_GETFL,0);
41 assert(flags != -1);
42 fcntl(sock, F_SETFL, flags | O_NONBLOCK);
43 #endif
44}
45
46unsigned long long getCurrentMillis()
47{
48 unsigned long long numMilliseconds;
49
50 #if defined WINDOWS
51 numMilliseconds = GetTickCount();
52 #elif defined LINUX
53 timespec curTime;
54 clock_gettime(CLOCK_REALTIME, &curTime);
55
56 numMilliseconds = curTime.tv_sec*(unsigned long long)1000+curTime.tv_nsec/(unsigned long long)1000000;
57 #endif
58
59 return numMilliseconds;
60}
61
62string getCurrentDateTimeString() {
63 ostringstream timeString;
64
65 time_t millis = time(NULL);
66 struct tm *time = localtime(&millis);
67
68 timeString << time->tm_hour << ":" << time->tm_min << ":"<< time->tm_sec << " " << (time->tm_mon+1) << "/" << time->tm_mday << "/" << (time->tm_year+1900);
69
70 return timeString.str();
71}
72
73float posDistance(FLOAT_POSITION pos1, FLOAT_POSITION pos2) {
74 float xDiff = pos2.x - pos1.x;
75 float yDiff = pos2.y - pos1.y;
76
77 return sqrt( pow(xDiff,2) + pow(yDiff,2) );
78}
79
80POSITION screenToMap(POSITION pos)
81{
82 pos.x = pos.x-300;
83 pos.y = pos.y-100;
84
85 if (pos.x < 0 || pos.y < 0)
86 {
87 pos.x = -1;
88 pos.y = -1;
89 }
90
91 return pos;
92}
93
94POSITION mapToScreen(POSITION pos)
95{
96 pos.x = pos.x+300;
97 pos.y = pos.y+100;
98
99 return pos;
100}
Note: See TracBrowser for help on using the repository browser.