source: network-game/common/Common.cpp@ 248e3c1

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

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