source: network-game/common/Common.cpp@ 0693e25

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

The client draws the map and players in individual games

  • Property mode set to 100644
File size: 1.8 KB
Line 
1#include "Common.h"
2
3#include <sstream>
4#include <cmath>
5
6#if defined WINDOWS
7 #include <Windows.h>
8#endif
9
10#include <ctime>
11
12using namespace std;
13
14FLOAT_POSITION POSITION::toFloat() {
15 FLOAT_POSITION floatPosition;
16 floatPosition.x = x;
17 floatPosition.y = y;
18
19 return floatPosition;
20}
21
22POSITION FLOAT_POSITION::toInt() {
23 POSITION position;
24 position.x = x;
25 position.y = y;
26
27 return position;
28}
29
30void set_nonblock(int sock)
31{
32 #if defined WINDOWS
33 unsigned long mode = 1;
34 ioctlsocket(sock, FIONBIO, &mode);
35 #elif defined LINUX
36 int flags;
37 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.