LPCGame
A Simple 2d Game Engine
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
vectors.h
1 #ifndef VECTORS_H
2 #define VECTORS_H
3 
4 #include <string>
5 #include <sstream>
6 #include <SDL.h>
7 #include "external/json/json.h"
8 
10 
13 template<class T>
14 class Vector2 {
15 public:
16  Vector2() : x(0), y(0) {
17  }
23  Vector2(T pX, T pY) : x(pX), y(pY) {
24  }
30  void Set(T pX, T pY){
31  x = pX;
32  y = pY;
33  }
38  void Set(Vector2<T> &v){
39  x = v.x;
40  y = v.y;
41  }
47  Json::Value val;
48  val["x"] = x;
49  val["y"] = y;
50  return val;
51  }
56  void Load(Json::Value val){
57  Set(val["x"].asFloat(), val["y"].asFloat());
58  }
61  return Vector2<T>(x + val.x, y + val.y);
62  }
63  Vector2<T> operator - (Vector2<T> val) const {
64  return Vector2<T>(x - val.x, y - val.y);
65  }
66  Vector2<T>& operator += (Vector2<T> val){
67  x += val.x;
68  y += val.y;
69  return *this;
70  }
71  bool operator == (Vector2<T> val) const {
72  return (x == val.x && y == val.y);
73  }
74  Vector2<T> operator * (Vector2<T> val) const {
75  Vector2<T> a(x * val.x, y * val.y);
76  return a;
77  }
78  Vector2<T> operator * (float val) const {
79  Vector2<T> a(x * val, y * val);
80  return a;
81  }
82  Vector2<T> operator / (Vector2<T> val) const {
83  Vector2<T> a(x / val.x, y / val.y);
84  return a;
85  }
86  Vector2<T> operator / (float val) const {
87  Vector2<T> a(x / val, y / val);
88  return a;
89  }
91  operator Vector2<int>() const {
92  Vector2<int> vect(x, y);
93  return vect;
94  }
95  operator Vector2<float>() const {
96  Vector2<float> vect(x, y);
97  return vect;
98  }
99  operator Vector2<double>() const {
100  Vector2<double> vect(x, y);
101  return vect;
102  }
103  operator SDL_Point() const {
104  SDL_Point p;
105  p.x = x;
106  p.y = y;
107  return p;
108  }
109  operator std::string() const {
110  std::stringstream s;
111  s << "Vector2: (x: " << x << ", y: "
112  << y << ")";
113  return s.str();
114  }
115 
116 public:
117  T x, y;
118 };
119 
120 typedef Vector2<int> Vector2i;
121 typedef Vector2<float> Vector2f;
122 typedef Vector2<double> Vector2d;
123 
124 #endif