source: network-game/common/Common.cpp@ 35f6097

Last change on this file since 35f6097 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
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>
[248e3c1]10#endif
11
[d05086b]12#include <sstream>
[b07eeac]13#include <cmath>
[8271c78]14#include <ctime>
15
[b07eeac]16using namespace std;
17
[0693e25]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
[edfd1d0]34void set_nonblock(int sock)
35{
[4c202e0]36 #if defined WINDOWS
[edfd1d0]37 unsigned long mode = 1;
38 ioctlsocket(sock, FIONBIO, &mode);
[4c202e0]39 #elif defined LINUX
[248e3c1]40 int flags = fcntl(sock, F_GETFL,0);
[edfd1d0]41 assert(flags != -1);
42 fcntl(sock, F_SETFL, flags | O_NONBLOCK);
43 #endif
44}
[8f85180]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}
[b07eeac]61
[d05086b]62string getCurrentDateTimeString() {
[8271c78]63 ostringstream timeString;
64
[d05086b]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
[b07eeac]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}
[0693e25]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.