The Game Engine  1
fns.cpp
Go to the documentation of this file.
1 #include "fns.h"
2 #include <SDL/SDL_image.h>
3 #include <math.h>
4 
5 #ifndef RELEASE
6 #include <iostream>
7 using std::cout;
8 using std::endl;
9 #endif
10 
11 SDL_Surface* LoadImage( std::string filename )
12 {
13  SDL_Surface* loaded_image = NULL;
14  SDL_Surface* compatible_image = NULL;
15 
16  filename="ArtAssets/"+filename;
17  if(filename.c_str() == NULL)
18  {
19  printf("No Filename %s\n\n",filename.c_str());
20  return NULL;
21  }
22 
23 
25  loaded_image = IMG_Load( filename.c_str() );
26 
27  if(!loaded_image)
28  {
29  printf("Error opening %s\n\n",filename.c_str());
30  return NULL;
31  }
32 
33 
35  compatible_image = SDL_DisplayFormat( loaded_image );
36 
38  SDL_FreeSurface( loaded_image );
39 
41  return compatible_image;
42 }
43 
44 Point2D::Point2D (const Size2D& copy) {
45  x = copy.w;
46  y = copy.h;
47 }
48 
49 double Point2D::length() {
50  return sqrt(x*x + y*y);
51 }
52 
53 double Point2D::lengthSquared() const {
54  return x*x + y*y;
55 }
56 
58  x=0;
59  y=0;
60 }
61 
63  double len = length();
64  return len == 0 ? Point2D(x, y) : Point2D(x, y)/len;
65 }
66 
68  return this->div(length());
69 }
70 
72  x += pt.x;
73  y += pt.y;
74  return *this;
75 }
77  x -= pt.x;
78  y -= pt.y;
79  return *this;
80 }
82  x *= d;
83  y *= d;
84  return *this;
85 }
86 Point2D Point2D::div(double d) {
87  x /= d;
88  y /= d;
89  return *this;
90 }
91 
92 double Point2D::dot(Point2D p1, Point2D p2){
93  return p1.x * p2.x + p1.y * p2.y;
94 }
95 
96 unsigned int nextPow2(unsigned int i) {
97  i--;
98  i |= i>>1; //handle 2 bit numbers
99  i |= i>>2; //handle 4 bit numbers
100  i |= i>>4; //handle 8 bit numbers
101  i |= i>>8; //handle 16 bit numbers
102  i |= i>>16; //handle 32 bit numbers
103  i++;
104  return i;
105 }
106 
107 Point2D getVector(double angle, double speed)
108 {
109  double y = sin(angle);
110  double x = cos(angle);
111  return Point2D(x, y) * speed;
112 }
113 
114 int randInt(int max)
115 {
116  return rand() % max; // >= 0 and < max
117 }
118 
119 int randInt(int min, int max)
120 {
121  return rand() % max + min; // >= min and < max
122 }
123 
124 float randFloat(float min, float max)
125 {
126  float f = (float)rand()/RAND_MAX;
127  return min + f * ( max - min );
128 }
129 
130 vector<string> splitString(string s, char delim, int rep)
131 {
132  vector<string> lines = vector<string>();
133  //the pos of the next space
134  int n=0;
135  //pos of previous space
136  int p=0;
137  string temp(s);
138 
139  while(n!=-1){
140  string substr;
141  //Find the next " "
142  n = temp.find(delim, p + 1 );
143  if( n != -1 ){
144  //sets strSub to the of the current line
145  substr = temp.substr( p, n );
146  p = n;
147  //Puts strSub into the lines vector
148  if(substr!="")
149  lines.push_back( substr );
150  temp = temp.substr( p+1, string::npos );
151  }
152  }
153  if(temp!="")
154  lines.push_back(temp);
155  return lines;
156 }
157 /*
158 template<class T> string to_string(const T& t)
159 {
160 ostringstream os;
161 os << t;
162 return os.str();
163 }//*/