1 /* --------------------------------------------------------------------
2 EXTREME TUXRACER
3 
4 Copyright (C) 1999-2001 Jasmin F. Patry (Tuxracer)
5 Copyright (C) 2010 Extreme Tux Racer Team
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
11 
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16 ---------------------------------------------------------------------*/
17 
18 #ifdef HAVE_CONFIG_H
19 #include <etr_config.h>
20 #endif
21 
22 #include "common.h"
23 #include "spx.h"
24 #include <sys/stat.h>
25 #include <iostream>
26 #include <cerrno>
27 #include <ctime>
28 
29 // --------------------------------------------------------------------
30 //				color utils
31 // --------------------------------------------------------------------
32 
33 #define TColor(r, g, b, a) sf::Color(static_cast<sf::Uint8>(r*255), static_cast<sf::Uint8>(g*255), static_cast<sf::Uint8>(b*255), static_cast<sf::Uint8>(a*255))
34 const sf::Color colDYell =		TColor(1.0, 0.8, 0.0, 1.0);
35 const sf::Color colDDYell =		TColor(0.8, 0.6, 0.0, 1.0);
36 const sf::Color colLYell =		TColor(1.0, 1.0, 0.4, 1.0);
37 const sf::Color colOrange =		TColor(1.0, 0.5, 0.0, 1.0);
38 const sf::Color colLRed =		TColor(1.0, 0.3, 0.3, 1.0);
39 const sf::Color colDRed =		TColor(0.8, 0.0, 0.0, 1.0);
40 const sf::Color colGrey =		TColor(0.5, 0.5, 0.5, 1.0);
41 const sf::Color colLGrey =		TColor(0.7, 0.7, 0.7, 1.0);
42 const sf::Color colDGrey =		TColor(0.3, 0.3, 0.3, 1.0);
43 const sf::Color colLBlue =		TColor(0.5, 0.7, 1.0, 1.0);
44 const sf::Color colDBlue =		TColor(0.0, 0.0, 0.6, 1.0);
45 const sf::Color colLBackgr =	TColor(0.5, 0.7, 0.9, 1.0);
46 const sf::Color colBackgr =		TColor(0.4, 0.6, 0.8, 1.0);
47 const sf::Color colMBackgr =	TColor(0.35, 0.5, 0.7, 1.0);
48 const sf::Color colDBackgr =	TColor(0.2, 0.3, 0.6, 1.0);
49 const sf::Color colDDBackgr =	TColor(0.13, 0.2, 0.4, 1.0);
50 const sf::Color colTBackr =		TColor(0.4, 0.6, 0.8, 0.0);
51 const sf::Color colMess =		TColor(0.3, 0.3, 0.7, 1.0);
52 const sf::Color colSky =		TColor(0.82, 0.86, 0.88, 1.0);
53 
54 const sf::Color colBronze   = sf::Color(205, 127, 50,  255);
55 const sf::Color colSilver   = sf::Color(192, 192, 192, 255);
56 const sf::Color colGold     = sf::Color(255, 215, 0,   255);
57 const sf::Color colGreen    = sf::Color(0,   128, 0,   255);
58 
59 // --------------------------------------------------------------------
60 //				print utils
61 // --------------------------------------------------------------------
62 
PrintInt(const int val)63 void PrintInt(const int val) {
64 	std::cout << "Integer: " << val << '\n';
65 }
66 
PrintInt(const std::string & s,const int val)67 void PrintInt(const std::string& s, const int val) {
68 	std::cout << s << val << std::endl;
69 }
70 
PrintStr(const char * val)71 void PrintStr(const char *val) {
72 	std::cout << val << '\n';
73 }
74 
PrintString(const std::string & s)75 void PrintString(const std::string& s) {
76 	std::cout << s << std::endl;
77 }
78 
PrintDouble(const double val)79 void PrintDouble(const double val) {
80 	std::cout.precision(4);
81 	std::cout << val << '\n';
82 }
83 
PrintVector4(const TVector4d & v)84 void PrintVector4(const TVector4d& v) {
85 	std::cout.precision(3);
86 	std::cout << v.x << "  " << v.y << "  " << v.z << "  " << v.w << '\n';
87 }
88 
PrintColor(const sf::Color & c)89 void PrintColor(const sf::Color& c) {
90 	std::cout.precision(3);
91 	std::cout << c.r << "  " << c.g << "  " << c.b << '\n';
92 }
93 
PrintVector2(const TVector2d & v)94 void PrintVector2(const TVector2d& v) {
95 	std::cout.precision(3);
96 	std::cout << v.x << "  " << v.y << '\n';
97 }
98 
PrintVector(const TVector3d & v)99 void PrintVector(const TVector3d& v) {
100 	std::cout.precision(5);
101 	std::cout << v.x << "  " << v.y << "  " << v.z << '\n';
102 }
103 
104 template<int x, int y>
PrintMatrix(const TMatrix<x,y> & mat)105 void PrintMatrix(const TMatrix<x, y>& mat) {
106 	std::cout << '\n';
107 	std::cout.precision(3);
108 	for (int i=0; i<x; i++) {
109 		for (int j=0; j<y; j++) {
110 			if (mat[i][j]>=0) std::cout << ' ';
111 			std::cout << "  " << mat[i][j];
112 		}
113 		std::cout << '\n';
114 	}
115 	std::cout << '\n';
116 }
117 template void PrintMatrix<4, 4>(const TMatrix<4, 4>& mat);
118 template void PrintMatrix<3, 3>(const TMatrix<3, 3>& mat);
119 
PrintQuaternion(const TQuaternion & q)120 void PrintQuaternion(const TQuaternion& q) {
121 	std::cout.precision(5);
122 	std::cout << "Quaternion: " << q.x << "  " << q.y << "  " << q.z << "  " << q.w << '\n';
123 }
124 
125 // --------------------------------------------------------------------
126 //				message utils
127 // --------------------------------------------------------------------
128 
129 static CSPList msg_list;
130 
SaveMessages()131 void SaveMessages() {
132 	msg_list.Save(param.config_dir, "messages");
133 }
134 
Message(const char * msg,const char * desc)135 void Message(const char *msg, const char *desc) {
136 	if (*msg == 0 && *desc == 0) {
137 		std::cout << '\n';
138 		return;
139 	}
140 
141 	std::string aa = msg;
142 	std::string bb = desc;
143 	std::cout << aa << "  " << bb << '\n';
144 	msg_list.Add(aa + bb);
145 }
146 
Message(const char * msg)147 void Message(const char *msg) {
148 	std::cout << msg << '\n';
149 	if (*msg != 0)
150 		msg_list.Add(msg);
151 }
152 
Message(const std::string & a,const std::string & b)153 void Message(const std::string& a, const std::string& b) {
154 	std::cout << a << ' ' << b << std::endl;
155 	msg_list.Add(a + b);
156 }
157 
Message(const std::string & msg)158 void Message(const std::string& msg) {
159 	std::cout << msg << std::endl;
160 	msg_list.Add(msg);
161 }
162 
163 // --------------------------------------------------------------------
164 //				file utils
165 // --------------------------------------------------------------------
166 
FileExists(const std::string & filename)167 bool FileExists(const std::string& filename) {
168 	struct stat stat_info;
169 	if (stat(filename.c_str(), &stat_info) != 0) {
170 		if (errno != ENOENT) Message("couldn't stat", filename);
171 		return false;
172 	} else return true;
173 }
174 
FileExists(const std::string & dir,const std::string & filename)175 bool FileExists(const std::string& dir, const std::string& filename) {
176 	return FileExists(MakePathStr(dir, filename));
177 }
178 
179 #ifndef OS_WIN32_MSC
DirExists(const char * dirname)180 bool DirExists(const char *dirname) {
181 	DIR *xdir;
182 	if ((xdir = opendir(dirname)) == 0)
183 		return ((errno != ENOENT) && (errno != ENOTDIR));
184 	if (closedir(xdir) != 0) Message("Couldn't close directory", dirname);
185 	return true;
186 }
187 #else
DirExists(const char * dirname)188 bool DirExists(const char *dirname) {
189 	DWORD typ = GetFileAttributesA(dirname);
190 	if (typ == INVALID_FILE_ATTRIBUTES)
191 		return false; // Doesn't exist
192 
193 	return (typ & FILE_ATTRIBUTE_DIRECTORY) != 0; // Is directory?
194 }
195 #endif
196 
197 // --------------------------------------------------------------------
198 //				date and time
199 // --------------------------------------------------------------------
200 
GetTimeComponents(double time,int * min,int * sec,int * hundr)201 void GetTimeComponents(double time, int *min, int *sec, int *hundr) {
202 	*min = (int)(time / 60);
203 	*sec = ((int) time) % 60;
204 	*hundr = ((int)(time * 100 + 0.5)) % 100;
205 }
206 
GetTimeString()207 std::string GetTimeString() {
208 	std::time_t rawtime;
209 	std::time(&rawtime);
210 	struct std::tm* timeinfo = std::localtime(&rawtime);
211 
212 	std::string line = Int_StrN(timeinfo->tm_mon + 1);
213 	line += '_' + Int_StrN(timeinfo->tm_mday);
214 	line += '_' + Int_StrN(timeinfo->tm_hour);
215 	line += Int_StrN(timeinfo->tm_min);
216 	line += Int_StrN(timeinfo->tm_sec);
217 	return line;
218 }
219