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

Last change on this file since e708305 was 34bd549, checked in by Dmitry Portnoy <dmp1488@…>, 11 years ago

Make client compile on a Mac

  • Property mode set to 100644
File size: 2.8 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#elif defined MAC
11 #include <fcntl.h>
12 #include <assert.h>
13 #include <mach/clock.h>
14 #include <mach/mach.h>
15#endif
16
17#include <sstream>
18#include <cmath>
19#include <ctime>
20#include <cstdlib>
21#include <cstdio>
22
23using namespace std;
24
25FLOAT_POSITION POSITION::toFloat() {
26 FLOAT_POSITION floatPosition;
27 floatPosition.x = x;
28 floatPosition.y = y;
29
30 return floatPosition;
31}
32
33POSITION FLOAT_POSITION::toInt() {
34 POSITION position;
35 position.x = x;
36 position.y = y;
37
38 return position;
39}
40
41// This might not be cross-platform. Verify that this works correctly or fix it.
42void error(const char *msg)
43{
44 perror(msg);
45 exit(0);
46}
47
48void set_nonblock(int sock)
49{
50 #if defined WINDOWS
51 unsigned long mode = 1;
52 ioctlsocket(sock, FIONBIO, &mode);
53 #elif defined LINUX
54 int flags = fcntl(sock, F_GETFL,0);
55 assert(flags != -1);
56 fcntl(sock, F_SETFL, flags | O_NONBLOCK);
57 #elif defined MAC
58 int flags = fcntl(sock, F_GETFL,0);
59 assert(flags != -1);
60 fcntl(sock, F_SETFL, flags | O_NONBLOCK);
61 #endif
62}
63
64unsigned long long getCurrentMillis()
65{
66 unsigned long long numMilliseconds;
67
68 #if defined WINDOWS
69 numMilliseconds = GetTickCount();
70 #elif defined LINUX
71 timespec curTime;
72 clock_gettime(CLOCK_REALTIME, &curTime);
73
74 numMilliseconds = curTime.tv_sec*(unsigned long long)1000+curTime.tv_nsec/(unsigned long long)1000000;
75 # elif defined MAC
76 timespec curTime;
77
78 clock_serv_t cclock;
79 mach_timespec_t mts;
80 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
81 clock_get_time(cclock, &mts);
82 mach_port_deallocate(mach_task_self(), cclock);
83 curTime.tv_sec = mts.tv_sec;
84 curTime.tv_nsec = mts.tv_nsec;
85
86 numMilliseconds = curTime.tv_sec*(unsigned long long)1000+curTime.tv_nsec/(unsigned long long)1000000;
87 #endif
88
89 return numMilliseconds;
90}
91
92string getCurrentDateTimeString() {
93 ostringstream timeString;
94
95 time_t millis = time(NULL);
96 struct tm *time = localtime(&millis);
97
98 timeString << time->tm_hour << ":" << time->tm_min << ":"<< time->tm_sec << " " << (time->tm_mon+1) << "/" << time->tm_mday << "/" << (time->tm_year+1900);
99
100 return timeString.str();
101}
102
103float posDistance(FLOAT_POSITION pos1, FLOAT_POSITION pos2) {
104 float xDiff = pos2.x - pos1.x;
105 float yDiff = pos2.y - pos1.y;
106
107 return sqrt( pow(xDiff,2) + pow(yDiff,2) );
108}
109
110POSITION screenToMap(POSITION pos)
111{
112 pos.x = pos.x-300;
113 pos.y = pos.y-100;
114
115 if (pos.x < 0 || pos.y < 0)
116 {
117 pos.x = -1;
118 pos.y = -1;
119 }
120
121 return pos;
122}
123
124POSITION mapToScreen(POSITION pos)
125{
126 pos.x = pos.x+300;
127 pos.y = pos.y+100;
128
129 return pos;
130}
Note: See TracBrowser for help on using the repository browser.