#include "util.h" vector > splitString(string s) { vector > result; vector line; string word; for(int i = 0; i < (int) s.size(); ++i) { if(s[i] == ' ') { line.push_back(word); word = ""; } else if(s[i] == '\n') { line.push_back(word); word = ""; result.push_back(line); line = vector(); } else word += s[i]; } line.push_back(word); result.push_back(line); return result; } void _tospace(string &s) { for(int i = 0; i < (int) s.size(); ++i) if(s[i] == '_') s[i] = ' '; } void spaceto_(string &s) { for(int i = 0; i < (int) s.size(); ++i) if(s[i] == ' ') s[i] = '_'; } double round_beautiful(double d) { if(d == 0) return d; double ten = 1; while(d >= 10) { d/= 10; ten *= 10; } while(d < 1) { d*= 10; ten /= 10; } vector candidates; candidates.push_back((int)d); candidates.push_back(((int)d) + 0.5); if(ten > 1) { candidates.push_back(((int)d)/9.0*10); } int closest = 0; double mindist = 10; for(int i = 0; i < (int)candidates.size(); ++i) { if(mindist > fabs(candidates[i]-d)) { mindist = fabs(candidates[i]-d); closest = i; } } double result = candidates[closest] * ten; if(closest == 2) result = (int) result; return result; } SDL_Event relativate(const SDL_Event &e, SDL_Rect p) { SDL_Event result = e; if(e.type == SDL_MOUSEMOTION) { result.motion.x -= p.x; result.motion.y -= p.y; } if(e.type == SDL_MOUSEBUTTONDOWN || e.type == SDL_MOUSEBUTTONUP) { result.button.x -= p.x; result.button.y -= p.y; } return result; } bool inRect(SDL_Rect r, double x, double y) { if(x < r.x || x >= r.x + r.w || y < r.y || y >= r.y + r.h) return false; return true; } SDL_Surface* copyImage(SDL_Surface* s) { return SDL_ConvertSurface(s,s->format,SDL_SWSURFACE); } //loads a bitmap from file and converts it to screen properties SDL_Surface* loadBMP(std::string filename) { SDL_Surface* loadedImage = NULL; SDL_Surface* convertedImage = NULL; loadedImage = IMG_Load(filename.c_str()); if(loadedImage != NULL) { convertedImage = SDL_DisplayFormat(loadedImage); if(convertedImage) { Uint32 colorkey = SDL_MapRGB(convertedImage->format, 0, 1, 2); SDL_SetColorKey(convertedImage, SDL_SRCCOLORKEY, colorkey); SDL_FreeSurface(loadedImage); return convertedImage; } cout << "Error: Could not convert Image" << filename << '.' << endl; } cout << "Error: Could not load bitmap from file " << filename << endl; return NULL; } string lltostr(const long long& l) { stringstream str; str << l; return str.str(); } string dbltostr(const double& d, int dec) { stringstream str; long long bla = pow(10,dec); str << ((long long) (bla*d))/((double) bla); return str.str(); } //returns true if the line intersects with the circle (or lies in it) bool intersects(double radius, double startx, double starty, double endx, double endy) { double xdir = endx - startx; double ydir = endy - starty; //|dir*lambda+start| 1) return false; return true; }