source: network-game/common/Common.cpp@ 53ba300

Last change on this file since 53ba300 was 6319311, checked in by Dmitry Portnoy <dportnoy@…>, 11 years ago

Some redfinition issues related to winsock2 are fixed and a few allegro rendering functions are now in GameRender

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