size.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2020 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 ** Kenneth Gangstoe
28 */
29 
30 
31 #pragma once
32 
33 #include "vec2.h"
34 
35 namespace clan
36 {
39 
40  template<typename Type>
41  class Vec2;
42 
43  template<typename Type>
44  class Vec3;
45 
46  template<typename Type>
47  class Vec4;
48 
53  template<typename Type>
54  class Sizex
55  {
56  public:
58  Sizex() : width(0), height(0) { }
59 
64  Sizex(Type width, Type height)
65  : width(width), height(height) { }
66 
70  Sizex(const Sizex<Type> &s) = default;
71 
72  template<typename OtherType, typename std::enable_if_t<std::is_integral<Type>::value && !std::is_integral<OtherType>::value, int> = 0>
73  Sizex(const Sizex<OtherType>& copy) : width(static_cast<Type>(std::floor(copy.width + 0.5f))), height(static_cast<Type>(std::floor(copy.height + 0.5f))) {}
74 
75  template<typename OtherType, typename std::enable_if_t<!std::is_integral<Type>::value || std::is_integral<OtherType>::value, int> = 0>
76  Sizex(const Sizex<OtherType>& copy) : width(static_cast<Type>(copy.width)), height(static_cast<Type>(copy.height)) {}
77 
79  Type width;
80 
82  Type height;
83 
84  operator Vec2<Type>() const
85  {
86  return Vec2<Type>(width, height);
87  }
88 
91  {
92  width += s.width; height += s.height; return *this;
93  }
94 
97  {
98  width -= s.width; height -= s.height; return *this;
99  }
100 
103  {
104  return Sizex<Type>(width + s.width, height + s.height);
105  }
106 
109  {
110  return Sizex<Type>(width - s.width, height - s.height);
111  }
112 
114  Sizex<Type> &operator+=(const Type &s)
115  {
116  width += s; height += s; return *this;
117  }
118 
120  Sizex<Type> &operator-=(const Type &s)
121  {
122  width -= s; height -= s; return *this;
123  }
124 
126  Sizex<Type> &operator*=(const Type &s)
127  {
128  width *= s; height *= s; return *this;
129  }
130 
132  Sizex<Type> &operator/=(const Type &s)
133  {
134  width /= s; height /= s; return *this;
135  }
136 
138  Sizex<Type> operator+(const Type &s) const
139  {
140  return Sizex<Type>(width + s, height + s);
141  }
142 
144  Sizex<Type> operator-(const Type &s) const
145  {
146  return Sizex<Type>(width - s, height - s);
147  }
148 
150  Sizex<Type> operator*(const Type &s) const
151  {
152  return Sizex<Type>(width * s, height * s);
153  }
154 
156  Sizex<Type> operator/(const Type &s) const
157  {
158  return Sizex<Type>(width / s, height / s);
159  }
160 
162  bool operator==(const Sizex<Type> &s) const
163  {
164  return (width == s.width) && (height == s.height);
165  }
166 
168  bool operator!=(const Sizex<Type> &s) const
169  {
170  return (width != s.width) || (height != s.height);
171  }
172  };
173 
175  class Size : public Sizex<int>
176  {
177  public:
178  Size() : Sizex<int>() {}
179  Size(int width, int height) : Sizex<int>(width, height) {}
180  Size(const Sizex<int>& s) : Sizex<int>(s) {}
181  Size(const Vec2<int>& s) : Sizex<int>(s.x, s.y) {}
182 
183  explicit Size(const Sizex<float>& copy) { width = (int)(copy.width + 0.5f); height = (int)(copy.height + 0.5f); }
184  explicit Size(const Sizex<double>& copy) { width = (int)(copy.width + 0.5); height = (int)(copy.height + 0.5); }
185  };
186 
188  class Sizef : public Sizex<float>
189  {
190  public:
191  Sizef() : Sizex<float>() {}
192  Sizef(float width, float height) : Sizex<float>(width, height) {}
193  Sizef(const Sizex<float>& s) : Sizex<float>(s) {}
194  Sizef(const Vec2<float>& s) : Sizex<float>(s.x, s.y) {}
195 
196  Sizef(const Sizex<int>& copy) { width = (float)copy.width; height = (float)copy.height; }
197  explicit Sizef(const Sizex<double>& copy) { width = (float)copy.width; height = (float)copy.height; }
198  };
199 
201  class Sized : public Sizex<double>
202  {
203  public:
204  Sized() : Sizex<double>() {}
205  Sized(double width, double height) : Sizex<double>(width, height) {}
206  Sized(const Sizex<double>& s) : Sizex<double>(s) {}
207  Sized(const Vec2<double>& s) : Sizex<double>(s.x, s.y) {}
208 
209  Sized(const Sizex<int>& copy) { width = (double)copy.width; height = (double)copy.height; }
210  Sized(const Sizex<float>& copy) { width = (double)copy.width; height = (double)copy.height; }
211  };
212 
214 }
Size(const Vec2< int > &s)
Definition: size.h:181
Definition: clanapp.h:35
Sizef(const Sizex< int > &copy)
Definition: size.h:196
Sizef(const Vec2< float > &s)
Definition: size.h:194
Sizex< Type > operator/(const Type &s) const
Size / operator.
Definition: size.h:156
Sizex< Type > & operator/=(const Type &s)
Size /= operator.
Definition: size.h:132
Sizex< Type > operator-(const Type &s) const
Size - operator.
Definition: size.h:144
bool operator==(const Sizex< Type > &s) const
Size == Size operator (deep compare).
Definition: size.h:162
Sizex(Type width, Type height)
Constructs a size structure.
Definition: size.h:64
Sizex< Type > & operator-=(const Sizex< Type > &s)
Size -= Size operator.
Definition: size.h:96
Sized(const Sizex< float > &copy)
Definition: size.h:210
STL namespace.
Sizex< Type > operator*(const Type &s) const
Size * operator.
Definition: size.h:150
Sizex()
Constructs a size structure.
Definition: size.h:58
Sizef()
Definition: size.h:191
Sizex< Type > operator+(const Sizex< Type > &s) const
Size + Size operator.
Definition: size.h:102
Sized()
Definition: size.h:204
Sized(const Sizex< double > &s)
Definition: size.h:206
Sizex< Type > & operator+=(const Type &s)
Size += operator.
Definition: size.h:114
bool operator!=(const Sizex< Type > &s) const
Size != Size operator (deep compare).
Definition: size.h:168
Sizex< Type > operator+(const Type &s) const
Size + operator.
Definition: size.h:138
Sizef(float width, float height)
Definition: size.h:192
Sizef(const Sizex< double > &copy)
Definition: size.h:197
2D (width,height) size structure - Double
Definition: size.h:201
Sizex< Type > operator-(const Sizex< Type > &s) const
Size - Size operator.
Definition: size.h:108
Sizef(const Sizex< float > &s)
Definition: size.h:193
2D vector
Definition: line.h:46
Size(const Sizex< double > &copy)
Definition: size.h:184
Size(const Sizex< float > &copy)
Definition: size.h:183
Sized(const Sizex< int > &copy)
Definition: size.h:209
Size()
Definition: size.h:178
Sizex(const Sizex< OtherType > &copy)
Definition: size.h:73
Size(const Sizex< int > &s)
Definition: size.h:180
Type height
Size height.
Definition: size.h:82
Sizex< Type > & operator*=(const Type &s)
Size *= operator.
Definition: size.h:126
2D (width,height) size structure - Integer
Definition: size.h:175
Sizex< Type > & operator+=(const Sizex< Type > &s)
Size += Size operator.
Definition: size.h:90
Sizex< Type > & operator-=(const Type &s)
Size -= operator.
Definition: size.h:120
4D vector
Definition: size.h:47
Sized(double width, double height)
Definition: size.h:205
Size(int width, int height)
Definition: size.h:179
2D (width,height) size structure.
Definition: size.h:54
Sized(const Vec2< double > &s)
Definition: size.h:207
Type width
Size width.
Definition: size.h:79
2D (width,height) size structure - Float
Definition: size.h:188