The Game Engine  1
fns.h
Go to the documentation of this file.
1 #ifndef __FNS_H__
2 #define __FNS_H__
3 #include <SDL/SDL.h>
4 #include <string>
5 #include <iostream>
6 #include <sstream>
7 #if defined(_WIN32)
8 #include <Windows.h>
9 #endif
10 #include <GL/gl.h>
11 //#include <GL/glu.h>
12 #include <vector>
13 #include "Texture.h"
14 #include <math.h>
15 
16 using namespace std;
17 //Fixes for windows stupidity
18 #ifndef GL_BGR
19 #define GL_BGR GL_BGR_EXT
20 #endif
21 #ifndef GL_BGRA
22 #define GL_BGRA GL_BGRA_EXT
23 #endif
24 #ifndef APIENTRY
25 #define APIENTRY __stdcall
26 #endif
27 #ifndef WINGDIAPI
28 #define WINGDIAPI __declspec(dllimport)
29 #endif
30 
32 typedef void (*Behavior) ();
34 typedef bool (*Condition) ();
36 static bool NeverEnd() {return false;}
38 static const double PI = 3.14159265;
40 const double DEG_IN_RAD = (PI/180.);
41 
47 #define foritr(a,b) for(auto a=b.begin(),end=b.end();a!=end;++a)
48 
54 #define foritrNoInc(a,b) for(auto a=b.begin(),end=b.end();a!=end;)
55 
56 SDL_Surface* LoadImage( std::string filename );
59 enum FontType{
63 };
64 
65 //these are so that we can implement things we need
66 class Size2D;
67 
78 class Point2D
79 {
80  public:
81  Point2D ():x(0),y(0) {}
82  Point2D (double X, double Y):x(X),y(Y) {}
83  Point2D (const Point2D& copy)
84  {
85  x = copy.x;
86  y = copy.y;
87  }
88  Point2D (const Size2D& copy);
89 
90  double x;
91  double y;
92  double length();
93  double lengthSquared() const;
94  void zero();
95  Point2D unitVec();
96  Point2D normalize();
97  Point2D add(Point2D pt);
98  Point2D sub(Point2D pt);
99  Point2D mult(double d);
100  Point2D div(double d);
101  Point2D operator+ (const Point2D& pt) const { return Point2D(x + pt.x, y + pt.y); }
102  Point2D operator- (const Point2D& pt) const { return Point2D(x - pt.x, y - pt.y); }
103  Point2D operator* (const double scale) const { return Point2D(x * scale, y * scale); }
104  Point2D operator/ (const double scale) const { return Point2D(x / scale, y / scale); }
105  Point2D operator+= (const Point2D& pt) { return add(pt); }
106  Point2D operator-= (const Point2D& pt) { return sub(pt); }
107  Point2D operator*= (const double scale) { return mult(scale); }
108  Point2D operator/= (const double scale) { return div(scale); }
109  bool operator< (const Point2D& vec) { return lengthSquared() < vec.lengthSquared(); }
110  double dot(Point2D p1, Point2D p2);
112  friend ostream &operator<<(ostream &out, Point2D p) //output
113  {
114  out<<"("<<p.x<<","<<p.y<<")";
115  return out;
116  }
117 };
118 
125 class Size2D
126 {
127  public:
128  friend class Point2D;
129  Size2D ():w(0),h(0) {}
130  Size2D (double width, double height):w(width),h(height) {}
131  Size2D (const Size2D& copy)
132  {
133  w = copy.w;
134  h = copy.h;
135  }
136  Size2D (const Point2D& copy)
137  {
138  w = copy.x;
139  h = copy.y;
140  }
141  double w;
142  double h;
143  Point2D add(Point2D pt);
144  Point2D sub(Point2D pt);
145  Point2D mult(double d);
146  Point2D div(double d);
147  Point2D operator+ (const Point2D& pt) const { return Point2D(w,h).operator+(pt); }
148  Point2D operator- (const Point2D& pt) const { return Point2D(w,h).operator-(pt); }
149  Point2D operator* (const double scale) const { return Point2D(w,h).operator*(scale); }
150  Point2D operator/ (const double scale) const { return Point2D(w,h).operator/(scale); }
151 };
152 //template<class T> string to_string(const T& t);
153 
155 unsigned int nextPow2(unsigned int i);
156 
162 Point2D getVector(double angle, double speed);
163 
174 int randInt(int max = RAND_MAX);
175 
183 int randInt(int min, int max);
184 
192 float randFloat(float min=0, float max=1);
193 
195 vector<string> splitString(string s, char delim='\n', int rep=0);
196 
197 template<typename NumTy> string ToString(const NumTy& Num)
198 {
199  stringstream StrStream;
200  StrStream << Num;
201  return StrStream.str();
202 }
203 #endif