]> git.lizzy.rs Git - irrlicht.git/blob - include/dimension2d.h
Use non-static member vars for SDL clipboard / primary selection buffers
[irrlicht.git] / include / dimension2d.h
1 // Copyright (C) 2002-2012 Nikolaus Gebhardt\r
2 // This file is part of the "Irrlicht Engine".\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h\r
4 \r
5 #ifndef __IRR_DIMENSION2D_H_INCLUDED__\r
6 #define __IRR_DIMENSION2D_H_INCLUDED__\r
7 \r
8 #include "irrTypes.h"\r
9 #include "irrMath.h" // for irr::core::equals()\r
10 \r
11 namespace irr\r
12 {\r
13 namespace core\r
14 {\r
15         template <class T>\r
16         class vector2d;\r
17 \r
18         //! Specifies a 2 dimensional size.\r
19         template <class T>\r
20         class dimension2d\r
21         {\r
22                 public:\r
23                         //! Default constructor for empty dimension\r
24                         dimension2d() : Width(0), Height(0) {}\r
25                         //! Constructor with width and height\r
26                         dimension2d(const T& width, const T& height)\r
27                                 : Width(width), Height(height) {}\r
28 \r
29                         dimension2d(const vector2d<T>& other); // Defined in vector2d.h\r
30 \r
31                         //! Use this constructor only where you are sure that the conversion is valid.\r
32                         template <class U>\r
33                         explicit dimension2d(const dimension2d<U>& other) :\r
34                                 Width((T)other.Width), Height((T)other.Height) { }\r
35 \r
36                         template <class U>\r
37                         dimension2d<T>& operator=(const dimension2d<U>& other)\r
38                         {\r
39                                 Width = (T) other.Width;\r
40                                 Height = (T) other.Height;\r
41                                 return *this;\r
42                         }\r
43 \r
44 \r
45                         //! Equality operator\r
46                         bool operator==(const dimension2d<T>& other) const\r
47                         {\r
48                                 return core::equals(Width, other.Width) &&\r
49                                                 core::equals(Height, other.Height);\r
50                         }\r
51 \r
52                         //! Inequality operator\r
53                         bool operator!=(const dimension2d<T>& other) const\r
54                         {\r
55                                 return ! (*this == other);\r
56                         }\r
57 \r
58                         bool operator==(const vector2d<T>& other) const;  // Defined in vector2d.h\r
59 \r
60                         bool operator!=(const vector2d<T>& other) const\r
61                         {\r
62                                 return !(*this == other);\r
63                         }\r
64 \r
65                         //! Set to new values\r
66                         dimension2d<T>& set(const T& width, const T& height)\r
67                         {\r
68                                 Width = width;\r
69                                 Height = height;\r
70                                 return *this;\r
71                         }\r
72 \r
73                         //! Divide width and height by scalar\r
74                         dimension2d<T>& operator/=(const T& scale)\r
75                         {\r
76                                 Width /= scale;\r
77                                 Height /= scale;\r
78                                 return *this;\r
79                         }\r
80 \r
81                         //! Divide width and height by scalar\r
82                         dimension2d<T> operator/(const T& scale) const\r
83                         {\r
84                                 return dimension2d<T>(Width/scale, Height/scale);\r
85                         }\r
86 \r
87                         //! Multiply width and height by scalar\r
88                         dimension2d<T>& operator*=(const T& scale)\r
89                         {\r
90                                 Width *= scale;\r
91                                 Height *= scale;\r
92                                 return *this;\r
93                         }\r
94 \r
95                         //! Multiply width and height by scalar\r
96                         dimension2d<T> operator*(const T& scale) const\r
97                         {\r
98                                 return dimension2d<T>(Width*scale, Height*scale);\r
99                         }\r
100 \r
101                         //! Add another dimension to this one.\r
102                         dimension2d<T>& operator+=(const dimension2d<T>& other)\r
103                         {\r
104                                 Width += other.Width;\r
105                                 Height += other.Height;\r
106                                 return *this;\r
107                         }\r
108 \r
109                         //! Add two dimensions\r
110                         dimension2d<T> operator+(const dimension2d<T>& other) const\r
111                         {\r
112                                 return dimension2d<T>(Width+other.Width, Height+other.Height);\r
113                         }\r
114 \r
115                         //! Subtract a dimension from this one\r
116                         dimension2d<T>& operator-=(const dimension2d<T>& other)\r
117                         {\r
118                                 Width -= other.Width;\r
119                                 Height -= other.Height;\r
120                                 return *this;\r
121                         }\r
122 \r
123                         //! Subtract one dimension from another\r
124                         dimension2d<T> operator-(const dimension2d<T>& other) const\r
125                         {\r
126                                 return dimension2d<T>(Width-other.Width, Height-other.Height);\r
127                         }\r
128 \r
129                         //! Get area\r
130                         T getArea() const\r
131                         {\r
132                                 return Width*Height;\r
133                         }\r
134 \r
135                         //! Get the optimal size according to some properties\r
136                         /** This is a function often used for texture dimension\r
137                         calculations. The function returns the next larger or\r
138                         smaller dimension which is a power-of-two dimension\r
139                         (2^n,2^m) and/or square (Width=Height).\r
140                         \param requirePowerOfTwo Forces the result to use only\r
141                         powers of two as values.\r
142                         \param requireSquare Makes width==height in the result\r
143                         \param larger Choose whether the result is larger or\r
144                         smaller than the current dimension. If one dimension\r
145                         need not be changed it is kept with any value of larger.\r
146                         \param maxValue Maximum texturesize. if value > 0 size is\r
147                         clamped to maxValue\r
148                         \return The optimal dimension under the given\r
149                         constraints. */\r
150                         dimension2d<T> getOptimalSize(\r
151                                         bool requirePowerOfTwo=true,\r
152                                         bool requireSquare=false,\r
153                                         bool larger=true,\r
154                                         u32 maxValue = 0) const\r
155                         {\r
156                                 u32 i=1;\r
157                                 u32 j=1;\r
158                                 if (requirePowerOfTwo)\r
159                                 {\r
160                                         while (i<(u32)Width)\r
161                                                 i<<=1;\r
162                                         if (!larger && i!=1 && i!=(u32)Width)\r
163                                                 i>>=1;\r
164                                         while (j<(u32)Height)\r
165                                                 j<<=1;\r
166                                         if (!larger && j!=1 && j!=(u32)Height)\r
167                                                 j>>=1;\r
168                                 }\r
169                                 else\r
170                                 {\r
171                                         i=(u32)Width;\r
172                                         j=(u32)Height;\r
173                                 }\r
174 \r
175                                 if (requireSquare)\r
176                                 {\r
177                                         if ((larger && (i>j)) || (!larger && (i<j)))\r
178                                                 j=i;\r
179                                         else\r
180                                                 i=j;\r
181                                 }\r
182 \r
183                                 if ( maxValue > 0 && i > maxValue)\r
184                                         i = maxValue;\r
185 \r
186                                 if ( maxValue > 0 && j > maxValue)\r
187                                         j = maxValue;\r
188 \r
189                                 return dimension2d<T>((T)i,(T)j);\r
190                         }\r
191 \r
192                         //! Get the interpolated dimension\r
193                         /** \param other Other dimension to interpolate with.\r
194                         \param d Value between 0.0f and 1.0f. d=0 returns other, d=1 returns this, values between interpolate.\r
195                         \return Interpolated dimension. */\r
196                         dimension2d<T> getInterpolated(const dimension2d<T>& other, f32 d) const\r
197                         {\r
198                                 f32 inv = (1.0f - d);\r
199                                 return dimension2d<T>( (T)(other.Width*inv + Width*d), (T)(other.Height*inv + Height*d));\r
200                         }\r
201 \r
202 \r
203                         //! Width of the dimension.\r
204                         T Width;\r
205                         //! Height of the dimension.\r
206                         T Height;\r
207         };\r
208 \r
209         //! Typedef for an f32 dimension.\r
210         typedef dimension2d<f32> dimension2df;\r
211         //! Typedef for an unsigned integer dimension.\r
212         typedef dimension2d<u32> dimension2du;\r
213 \r
214         //! Typedef for an integer dimension.\r
215         /** There are few cases where negative dimensions make sense. Please consider using\r
216                 dimension2du instead. */\r
217         typedef dimension2d<s32> dimension2di;\r
218 \r
219 \r
220 } // end namespace core\r
221 } // end namespace irr\r
222 \r
223 #endif\r
224 \r