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

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

Make client compile on a Mac

  • Property mode set to 100644
File size: 2.8 KB
RevLine 
[edfd1d0]1#include "Common.h"
2
[248e3c1]3#include "Compiler.h"
4
5#if defined WINDOWS
6 #include <winsock2.h>
[e437a19]7#elif defined LINUX
8 #include <fcntl.h>
9 #include <assert.h>
[34bd549]10#elif defined MAC
11 #include <fcntl.h>
12 #include <assert.h>
13 #include <mach/clock.h>
14 #include <mach/mach.h>
[248e3c1]15#endif
16
[d05086b]17#include <sstream>
[b07eeac]18#include <cmath>
[8271c78]19#include <ctime>
[8554263]20#include <cstdlib>
21#include <cstdio>
[8271c78]22
[b07eeac]23using namespace std;
24
[0693e25]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
[8554263]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
[edfd1d0]48void set_nonblock(int sock)
49{
[4c202e0]50 #if defined WINDOWS
[edfd1d0]51 unsigned long mode = 1;
52 ioctlsocket(sock, FIONBIO, &mode);
[4c202e0]53 #elif defined LINUX
[248e3c1]54 int flags = fcntl(sock, F_GETFL,0);
[edfd1d0]55 assert(flags != -1);
56 fcntl(sock, F_SETFL, flags | O_NONBLOCK);
[34bd549]57 #elif defined MAC
58 int flags = fcntl(sock, F_GETFL,0);
59 assert(flags != -1);
60 fcntl(sock, F_SETFL, flags | O_NONBLOCK);
[edfd1d0]61 #endif
62}
[8f85180]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
[34bd549]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
[8f85180]86 numMilliseconds = curTime.tv_sec*(unsigned long long)1000+curTime.tv_nsec/(unsigned long long)1000000;
87 #endif
88
89 return numMilliseconds;
90}
[b07eeac]91
[d05086b]92string getCurrentDateTimeString() {
[8271c78]93 ostringstream timeString;
94
[d05086b]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
[b07eeac]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}
[0693e25]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.