nomlib
Loading...
Searching...
No Matches
Size2.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_MATH_SIZE2_HEADERS
30#define NOMLIB_MATH_SIZE2_HEADERS
31
32#include <iostream>
33#include <algorithm>
34
35#include "nomlib/config.hpp"
36
37// FIXME: The following declaration is necessary in order to avoid a very
38// nasty compiling conflict that can happen under Windows anytime the
39// windef.h header file is included (commonly from windows.h), due to min and
40// max macros being declared there. This is why macros are evil.
41//
42// http://support.microsoft.com/kb/143208
43// http://stackoverflow.com/questions/5004858/stdmin-gives-error
44#if defined( NOM_PLATFORM_WINDOWS )
45 #undef min
46 #undef max
47#endif
48
49namespace nom {
50
52const std::string SIZE2_DELIMITER = ", ";
53
55template <typename T>
56struct Size2
57{
59 Size2 ( void ) :
60 w ( -1 ),
61 h ( -1 )
62 {
63 //NOM_LOG_TRACE(NOM);
64 }
65
67 ~Size2 ( void )
68 {
69 //NOM_LOG_TRACE(NOM);
70 }
71
73 Size2 ( T w, T h ) :
74 w ( w ),
75 h ( h )
76 {
77 //NOM_LOG_TRACE(NOM);
78 }
79
85 template <typename U>
86 explicit Size2 ( const Size2<U>& copy ) :
87 w { static_cast<T> ( copy.w ) },
88 h { static_cast<T> ( copy.h ) }
89 {
90 this->w = static_cast<T> ( copy.w );
91 this->h = static_cast<T> ( copy.h );
92 }
93
96 template <typename U>
97 Size2 max( const Size2<U>& rhs )
98 {
99 return Size2<T>( std::max( this->w, rhs.w ), std::max( this->h, rhs.h ) );
100 }
101
104 template <typename U>
105 Size2 min( const Size2<U>& rhs )
106 {
107 return Size2<T>( std::min( this->w, rhs.w ), std::min( this->h, rhs.h ) );
108 }
109
111 void swap( void )
112 {
113 std::swap( this->w, this->h );
114 }
115
119 static const Size2 null;
120
122 static const Size2 zero;
123
125 T w;
126
128 T h;
129};
130
138template <typename T>
139inline std::ostream& operator <<( std::ostream& os, const Size2<T>& pos )
140{
141 os
142 << pos.w
143 << SIZE2_DELIMITER
144 << pos.h;
145
146 return os;
147}
148
149template <typename T>
150inline bool operator ==( const Size2<T>& lhs, const Size2<T>& rhs )
151{
152 return ( lhs.w == rhs.w ) && ( lhs.h == rhs.h );
153}
154
155template <typename T>
156inline bool operator !=( const Size2<T>& lhs, const Size2<T>& rhs )
157{
158 return ! ( lhs == rhs );
159}
160
167template <typename T>
168inline Size2<T> operator +( const Size2<T>& lhs, const Size2<T>& rhs )
169{
170 return Size2<T> (
171 lhs.w + rhs.w,
172 lhs.h + rhs.h
173 );
174}
175
182template <typename T>
183inline Size2<T> operator +(int lhs, const Size2<T>& rhs)
184{
185 return Size2<T>(lhs + rhs.w, lhs + rhs.h);
186}
187
194template <typename T>
195inline Size2<T> operator +(const Size2<T>& lhs, int rhs)
196{
197 return Size2<T>(lhs.w + rhs, lhs.h + rhs);
198}
199
205template <typename T>
206inline Size2<T> operator ++( const Size2<T>& rhs )
207{
208 return Size2<T> (
209 ++rhs.w,
210 ++rhs.h
211 );
212}
213
219template <typename T>
220inline Size2<T> operator -( const Size2<T>& rhs )
221{
222 return Size2<T> (
223 -rhs.w
224 -rhs.h
225 );
226}
227
234template <typename T>
235inline Size2<T> operator -( const Size2<T>& lhs, const Size2<T>& rhs )
236{
237 return Size2<T> (
238 lhs.w - rhs.w,
239 lhs.h - rhs.h
240 );
241}
242
249template <typename T>
250inline Size2<T> operator -(int lhs, const Size2<T>& rhs)
251{
252 return Size2<T>(lhs - rhs.w, lhs - rhs.h);
253}
254
261template <typename T>
262inline Size2<T> operator -(const Size2<T>& lhs, int rhs)
263{
264 return Size2<T>(lhs.w - rhs, lhs.h - rhs);
265}
266
272template <typename T>
273inline Size2<T> operator --( const Size2<T>& rhs )
274{
275 return Size2<T> (
276 --rhs.w,
277 --rhs.h
278 );
279}
280
287template <typename T>
288inline Size2<T> operator *( const Size2<T>& lhs, const Size2<T>& rhs )
289{
290 return Size2<T> ( lhs.w * rhs.w,
291 lhs.h * rhs.h
292 );
293}
294
301template <typename T>
302inline Size2<T> operator *(int lhs, const Size2<T>& rhs)
303{
304 return Size2<T>(lhs * rhs.w, lhs * rhs.h);
305}
306
313template <typename T>
314inline Size2<T> operator *(const Size2<T>& lhs, int rhs)
315{
316 return Size2<T>(lhs.w * rhs, lhs.h * rhs);
317}
318
328template <typename T>
329inline Size2<T>& operator +=( Size2<T>& lhs, const Size2<T>& rhs )
330{
331 lhs.w += rhs.w;
332 lhs.h += rhs.h;
333
334 return lhs;
335}
336
346template <typename T>
347inline Size2<T>& operator +=(int lhs, const Size2<T>& rhs)
348{
349 lhs += rhs.w;
350 lhs += rhs.h;
351
352 return lhs;
353}
354
364template <typename T>
365inline Size2<T>& operator +=(Size2<T>& lhs, int rhs)
366{
367 lhs.w += rhs;
368 lhs.h += rhs;
369
370 return lhs;
371}
372
382template <typename T>
383inline Size2<T>& operator -=( Size2<T>& lhs, const Size2<T>& rhs )
384{
385 lhs.w -= rhs.w;
386 lhs.h -= rhs.h;
387
388 return lhs;
389}
390
400template <typename T>
401inline Size2<T>& operator -=(int lhs, const Size2<T>& rhs)
402{
403 lhs -= rhs.w;
404 lhs -= rhs.h;
405
406 return lhs;
407}
408
418template <typename T>
419inline Size2<T>& operator -=(Size2<T>& lhs, int rhs)
420{
421 lhs.w -= rhs;
422 lhs.h -= rhs;
423
424 return lhs;
425}
426
436template <typename T>
437inline Size2<T>& operator *=( Size2<T>& lhs, const Size2<T>& rhs )
438{
439 lhs.w *= rhs.w;
440 lhs.h *= rhs.h;
441
442 return lhs;
443}
444
454template <typename T>
455inline Size2<T>& operator /=( Size2<T>& lhs, const Size2<T>& rhs )
456{
457 lhs.w /= rhs.w;
458 lhs.h /= rhs.h;
459
460 return lhs;
461}
462
472template <typename T>
473inline Size2<T>& operator /=(int lhs, const Size2<T>& rhs)
474{
475 lhs /= rhs.w;
476 lhs /= rhs.h;
477
478 return lhs;
479}
480
490template <typename T>
491inline Size2<T>& operator /=(Size2<T>& lhs, int rhs)
492{
493 lhs.w /= rhs;
494 lhs.h /= rhs;
495
496 return lhs;
497}
498
505template <typename T>
506inline Size2<T> operator /( const Size2<T>& lhs, const Size2<T>& rhs )
507{
508 Size2<T> ret;
509 ret.w = lhs.w / rhs.w;
510 ret.h = lhs.h / rhs.h;
511
512 return ret;
513}
514
521template <typename T>
522inline Size2<T> operator /(int lhs, const Size2<T>& rhs)
523{
524 Size2<T> ret;
525 ret.w = lhs / rhs.w;
526 ret.h = lhs / rhs.h;
527
528 return ret;
529}
530
537template <typename T>
538inline Size2<T> operator /(const Size2<T>& lhs, int rhs)
539{
540 Size2<T> ret;
541 ret.w = lhs.w / rhs;
542 ret.h = lhs.h / rhs;
543
544 return ret;
545}
546
551template <typename T>
552inline bool operator <( const Size2<T> lhs, const Size2<T>& rhs )
553{
554 return ( lhs.w < rhs.w ) && ( lhs.h < rhs.h );
555}
556
561template <typename T>
562inline bool operator >( const Size2<T>& lhs, const Size2<T>& rhs )
563{
564 return ( rhs.w < lhs.w ) && ( rhs.h < lhs.h );
565}
566
571template <typename T>
572inline bool operator <=( const Size2<T>& lhs, const Size2<T>& rhs )
573{
574 return ( lhs.w <= rhs.w ) && ( lhs.h <= rhs.h );
575}
576
581template <typename T>
582inline bool operator >=( const Size2<T>& lhs, const Size2<T>& rhs )
583{
584 return ( rhs.w <= lhs.w ) && ( rhs.h <= lhs.h );
585}
586
588typedef Size2<int> Size2i;
589
591typedef Size2<float> Size2f;
592
594typedef Size2<double> Size2d;
595
596} // namespace nom
597
598#endif // include guard defined
Size coordinates (width & height) container.
Definition Size2.hpp:57
Size2(void)
Default constructor; initialize values to Size2<T>::null.
Definition Size2.hpp:59
Size2 min(const Size2< U > &rhs)
Compare two Size2 objects and return the smaller width and height of the two objects.
Definition Size2.hpp:105
Size2 max(const Size2< U > &rhs)
Compare two Size2 objects and return the larger width and height of the two objects.
Definition Size2.hpp:97
void swap(void)
Transpose width and height dimensions.
Definition Size2.hpp:111
Size2(const Size2< U > &copy)
Copy constructor.
Definition Size2.hpp:86
~Size2(void)
Destructor.
Definition Size2.hpp:67
Size2(T w, T h)
Constructor variant for initializing width & height at construction.
Definition Size2.hpp:73
static const Size2 zero
Definition Size2.hpp:122
static const Size2 null
Definition Size2.hpp:119