summaryrefslogtreecommitdiff
path: root/util.cpp
diff options
context:
space:
mode:
authorReimar <Reimar@Leike.name>2015-12-08 11:31:35 +0100
committerReimar <Reimar@Leike.name>2015-12-08 11:31:35 +0100
commite3a66514d57ff4acf2f02df7d86bd2cf8be0e730 (patch)
tree686eabd8aac2f2eb9d0b3429fe0feee3f031904a /util.cpp
downloadRCade-e3a66514d57ff4acf2f02df7d86bd2cf8be0e730.tar
RCade-e3a66514d57ff4acf2f02df7d86bd2cf8be0e730.tar.gz
RCade-e3a66514d57ff4acf2f02df7d86bd2cf8be0e730.tar.bz2
RCade-e3a66514d57ff4acf2f02df7d86bd2cf8be0e730.tar.xz
RCade-e3a66514d57ff4acf2f02df7d86bd2cf8be0e730.zip
initial commit
Diffstat (limited to 'util.cpp')
-rw-r--r--util.cpp173
1 files changed, 173 insertions, 0 deletions
diff --git a/util.cpp b/util.cpp
new file mode 100644
index 0000000..a1cf8e9
--- /dev/null
+++ b/util.cpp
@@ -0,0 +1,173 @@
1#include "util.h"
2
3
4vector<vector<string> > splitString(string s)
5{
6 vector<vector<string> > result;
7 vector<string> line;
8 string word;
9 for(int i = 0; i < (int) s.size(); ++i)
10 {
11 if(s[i] == ' ')
12 {
13 line.push_back(word);
14 word = "";
15 }
16 else if(s[i] == '\n')
17 {
18 line.push_back(word);
19 word = "";
20 result.push_back(line);
21 line = vector<string>();
22 }
23 else
24 word += s[i];
25 }
26 line.push_back(word);
27 result.push_back(line);
28 return result;
29}
30
31void _tospace(string &s)
32{
33 for(int i = 0; i < (int) s.size(); ++i)
34 if(s[i] == '_')
35 s[i] = ' ';
36}
37
38void spaceto_(string &s)
39{
40 for(int i = 0; i < (int) s.size(); ++i)
41 if(s[i] == ' ')
42 s[i] = '_';
43}
44
45double round_beautiful(double d)
46{
47 if(d == 0)
48 return d;
49 double ten = 1;
50 while(d >= 10)
51 {
52 d/= 10;
53 ten *= 10;
54 }
55 while(d < 1)
56 {
57 d*= 10;
58 ten /= 10;
59 }
60 vector<double> candidates;
61 candidates.push_back((int)d);
62 candidates.push_back(((int)d) + 0.5);
63 if(ten > 1)
64 {
65 candidates.push_back(((int)d)/9.0*10);
66 }
67 int closest = 0;
68 double mindist = 10;
69 for(int i = 0; i < (int)candidates.size(); ++i)
70 {
71 if(mindist > fabs(candidates[i]-d))
72 {
73 mindist = fabs(candidates[i]-d);
74 closest = i;
75 }
76 }
77 double result = candidates[closest] * ten;
78 if(closest == 2)
79 result = (int) result;
80 return result;
81}
82
83SDL_Event relativate(const SDL_Event &e, SDL_Rect p)
84{
85 SDL_Event result = e;
86 if(e.type == SDL_MOUSEMOTION)
87 {
88 result.motion.x -= p.x;
89 result.motion.y -= p.y;
90 }
91 if(e.type == SDL_MOUSEBUTTONDOWN || e.type == SDL_MOUSEBUTTONUP)
92 {
93 result.button.x -= p.x;
94 result.button.y -= p.y;
95 }
96 return result;
97}
98
99bool inRect(SDL_Rect r, double x, double y)
100{
101 if(x < r.x || x >= r.x + r.w || y < r.y || y >= r.y + r.h)
102 return false;
103 return true;
104}
105
106SDL_Surface* copyImage(SDL_Surface* s)
107{
108 return SDL_ConvertSurface(s,s->format,SDL_SWSURFACE);
109}
110
111
112//loads a bitmap from file and converts it to screen properties
113SDL_Surface* loadBMP(std::string filename)
114{
115 SDL_Surface* loadedImage = NULL;
116
117 SDL_Surface* convertedImage = NULL;
118
119 loadedImage = IMG_Load(filename.c_str());
120 if(loadedImage != NULL)
121 {
122 convertedImage = SDL_DisplayFormat(loadedImage);
123 if(convertedImage)
124 {
125 Uint32 colorkey = SDL_MapRGB(convertedImage->format, 0, 1, 2);
126 SDL_SetColorKey(convertedImage, SDL_SRCCOLORKEY, colorkey);
127 SDL_FreeSurface(loadedImage);
128 return convertedImage;
129 }
130 cout << "Error: Could not convert Image" << filename << '.' << endl;
131 }
132 cout << "Error: Could not load bitmap from file " << filename << endl;
133 return NULL;
134}
135
136
137string lltostr(const long long& l)
138{
139 stringstream str;
140 str << l;
141 return str.str();
142}
143
144string dbltostr(const double& d, int dec)
145{
146 stringstream str;
147 long long bla = pow(10,dec);
148 str << ((long long) (bla*d))/((double) bla);
149 return str.str();
150}
151
152//returns true if the line intersects with the circle (or lies in it)
153bool intersects(double radius, double startx, double starty, double endx, double endy)
154{
155 double xdir = endx - startx;
156 double ydir = endy - starty;
157 //|dir*lambda+start|<radius
158 //(xdir^2+ydir^2)*lamda^2 + (xdir*startx + ydir*starty)*lamda + startx^2 + starty^2 - radius^2 = 0;
159 double a = xdir*xdir + ydir*ydir;
160 double b = xdir*startx + ydir*starty;
161 double c = startx*startx + starty*starty - radius*radius;
162 double dis = b*b - 4 * a * c;
163 if(dis < 0)
164 return false;
165 double sol1 = (-b + sqrt(dis))/(2*a);
166 if(sol1 < 0)
167 return false;
168 double sol2 = (-b - sqrt(dis))/(2*a);
169 if(sol2 > 1)
170 return false;
171 return true;
172
173}