1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
/*
* Geometries.h, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
#pragma once
#include <SDL_video.h>
#include "../../lib/int3.h"
struct SDL_MouseMotionEvent;
// A point with x/y coordinate, used mostly for graphic rendering
struct Point
{
int x, y;
//constructors
Point()
{
x = y = 0;
};
Point(int X, int Y)
:x(X),y(Y)
{};
Point(const int3 &a)
:x(a.x),y(a.y)
{}
Point(const SDL_MouseMotionEvent &a);
template<typename T>
Point operator+(const T &b) const
{
return Point(x+b.x,y+b.y);
}
template<typename T>
Point operator/(const T &div) const
{
return Point(x/div, y/div);
}
template<typename T>
Point operator*(const T &mul) const
{
return Point(x*mul, y*mul);
}
template<typename T>
Point& operator+=(const T &b)
{
x += b.x;
y += b.y;
return *this;
}
template<typename T>
Point operator-(const T &b) const
{
return Point(x - b.x, y - b.y);
}
template<typename T>
Point& operator-=(const T &b)
{
x -= b.x;
y -= b.y;
return *this;
}
bool operator<(const Point &b) const //product order
{
return x < b.x && y < b.y;
}
template<typename T> Point& operator=(const T &t)
{
x = t.x;
y = t.y;
return *this;
}
template<typename T> bool operator==(const T &t) const
{
return x == t.x && y == t.y;
}
template<typename T> bool operator!=(const T &t) const
{
return !(*this == t);
}
};
/// Rectangle class, which have a position and a size
struct Rect : public SDL_Rect
{
Rect()//default c-tor
{
x = y = w = h = -1;
}
Rect(int X, int Y, int W, int H)
{
x = X;
y = Y;
w = W;
h = H;
}
Rect(const Point & position, const Point & size)
{
x = position.x;
y = position.y;
w = size.x;
h = size.y;
}
Rect(const SDL_Rect & r)
{
x = r.x;
y = r.y;
w = r.w;
h = r.h;
}
explicit Rect(const SDL_Surface * const &surf)
{
x = y = 0;
w = surf->w;
h = surf->h;
}
Rect centerIn(const Rect &r);
static Rect createCentered(int w, int h);
static Rect around(const Rect &r, int width = 1); //creates rect around another
bool isIn(int qx, int qy) const //determines if given point lies inside rect
{
if (qx > x && qx<x+w && qy>y && qy<y+h)
return true;
return false;
}
bool isIn(const Point & q) const //determines if given point lies inside rect
{
return isIn(q.x,q.y);
}
Point topLeft() const //top left corner of this rect
{
return Point(x,y);
}
Point topRight() const //top right corner of this rect
{
return Point(x+w,y);
}
Point bottomLeft() const //bottom left corner of this rect
{
return Point(x,y+h);
}
Point bottomRight() const //bottom right corner of this rect
{
return Point(x+w,y+h);
}
Rect operator+(const Rect &p) const //moves this rect by p's rect position
{
return Rect(x+p.x,y+p.y,w,h);
}
Rect operator+(const Point &p) const //moves this rect by p's point position
{
return Rect(x+p.x,y+p.y,w,h);
}
Rect& operator=(const Point &p) //assignment operator
{
x = p.x;
y = p.y;
return *this;
}
Rect& operator=(const Rect &p) //assignment operator
{
x = p.x;
y = p.y;
w = p.w;
h = p.h;
return *this;
}
Rect& operator+=(const Rect &p) //works as operator+
{
x += p.x;
y += p.y;
return *this;
}
Rect& operator+=(const Point &p) //works as operator+
{
x += p.x;
y += p.y;
return *this;
}
Rect& operator-=(const Rect &p) //works as operator+
{
x -= p.x;
y -= p.y;
return *this;
}
Rect& operator-=(const Point &p) //works as operator+
{
x -= p.x;
y -= p.y;
return *this;
}
template<typename T> Rect operator-(const T &t)
{
return Rect(x - t.x, y - t.y, w, h);
}
Rect operator&(const Rect &p) const //rect intersection
{
bool intersect = true;
if(p.topLeft().y < y && p.bottomLeft().y < y) //rect p is above *this
{
intersect = false;
}
else if(p.topLeft().y > y+h && p.bottomLeft().y > y+h) //rect p is below *this
{
intersect = false;
}
else if(p.topLeft().x > x+w && p.topRight().x > x+w) //rect p is on the right hand side of this
{
intersect = false;
}
else if(p.topLeft().x < x && p.topRight().x < x) //rect p is on the left hand side of this
{
intersect = false;
}
if(intersect)
{
Rect ret;
ret.x = std::max(this->x, p.x);
ret.y = std::max(this->y, p.y);
Point bR; //bottomRight point of returned rect
bR.x = std::min(this->w+this->x, p.w+p.x);
bR.y = std::min(this->h+this->y, p.h+p.y);
ret.w = bR.x - ret.x;
ret.h = bR.y - ret.y;
return ret;
}
else
{
return Rect();
}
}
Rect operator|(const Rect &p) const //union of two rects
{
Rect ret;
ret.x = std::min(p.x, this->x);
ret.y = std::min(p.y, this->y);
int x2 = std::max(p.x+p.w, this->x+this->w);
int y2 = std::max(p.y+p.h, this->y+this->h);
ret.w = x2 -ret.x;
ret.h = y2 -ret.y;
return ret;
}
};
|