nomlib
Toggle main menu visibility
Loading...
Searching...
No Matches
include
nomlib
math
Size2.hpp
1
/******************************************************************************
2
3
nomlib - C++11 cross-platform game engine
4
5
Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6
All rights reserved.
7
8
Redistribution and use in source and binary forms, with or without
9
modification, are permitted provided that the following conditions are met:
10
11
1. Redistributions of source code must retain the above copyright notice, this
12
list of conditions and the following disclaimer.
13
2. 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
17
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24
ON 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
26
SOFTWARE, 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
#include "export.hpp"
37
38
// FIXME: The following declaration is necessary in order to avoid a very
39
// nasty compiling conflict that can happen under Windows anytime the
40
// windef.h header file is included (commonly from windows.h), due to min and
41
// max macros being declared there. This is why macros are evil.
42
//
43
// http://support.microsoft.com/kb/143208
44
// http://stackoverflow.com/questions/5004858/stdmin-gives-error
45
#if defined( NOM_PLATFORM_WINDOWS )
46
#undef min
47
#undef max
48
#endif
49
50
namespace
nom {
51
53
const
std::string SIZE2_DELIMITER =
", "
;
54
56
template
<
typename
T>
57
struct
NOM_EXPORT
Size2
58
{
60
Size2
(
void
) :
61
w
( -1 ),
62
h
( -1 )
63
{
64
//NOM_LOG_TRACE(NOM);
65
}
66
68
~Size2
(
void
)
69
{
70
//NOM_LOG_TRACE(NOM);
71
}
72
74
Size2
( T
w
, T
h
) :
75
w
(
w
),
76
h
(
h
)
77
{
78
//NOM_LOG_TRACE(NOM);
79
}
80
86
template
<
typename
U>
87
explicit
Size2
(
const
Size2<U>
& copy ) :
88
w
{ static_cast<T> ( copy.
w
) },
89
h
{ static_cast<T> ( copy.
h
) }
90
{
91
this->
w
=
static_cast<
T
>
( copy.
w
);
92
this->
h
=
static_cast<
T
>
( copy.
h
);
93
}
94
97
template
<
typename
U>
98
Size2
max
(
const
Size2<U>
& rhs )
99
{
100
return
Size2<T>
( std::max( this->
w
, rhs.
w
), std::max( this->h, rhs.
h
) );
101
}
102
105
template
<
typename
U>
106
Size2
min
(
const
Size2<U>
& rhs )
107
{
108
return
Size2<T>
( std::min( this->
w
, rhs.
w
), std::min( this->h, rhs.
h
) );
109
}
110
112
void
swap
(
void
)
113
{
114
std::swap( this->
w
, this->
h
);
115
}
116
120
static
const
Size2
null
;
121
123
static
const
Size2
zero
;
124
126
T
w
;
127
129
T
h
;
130
};
131
139
template
<
typename
T>
140
NOM_EXPORT
141
inline
std::ostream& operator <<( std::ostream& os,
const
Size2<T>
& pos )
142
{
143
os
144
<< pos.
w
145
<< SIZE2_DELIMITER
146
<< pos.
h
;
147
148
return
os;
149
}
150
151
template
<
typename
T>
152
NOM_EXPORT
153
inline
bool
operator ==(
const
Size2<T>& lhs,
const
Size2<T>& rhs )
154
{
155
return
( lhs.w == rhs.w ) && ( lhs.h == rhs.h );
156
}
157
158
template
<
typename
T>
159
NOM_EXPORT
160
inline
bool
operator !=(
const
Size2<T>
& lhs,
const
Size2<T>
& rhs )
161
{
162
return
! ( lhs == rhs );
163
}
164
171
template
<
typename
T>
172
NOM_EXPORT
173
inline
Size2<T>
operator +(
const
Size2<T>
& lhs,
const
Size2<T>
& rhs )
174
{
175
return
Size2<T>
(
176
lhs.w + rhs.w,
177
lhs.h + rhs.h
178
);
179
}
180
187
template
<
typename
T>
188
NOM_EXPORT
inline
Size2<T>
operator +(
int
lhs,
const
Size2<T>
& rhs)
189
{
190
return
Size2<T>
(lhs + rhs.w, lhs + rhs.h);
191
}
192
199
template
<
typename
T>
200
NOM_EXPORT
inline
Size2<T>
operator +(
const
Size2<T>
& lhs,
int
rhs)
201
{
202
return
Size2<T>
(lhs.w + rhs, lhs.h + rhs);
203
}
204
210
template
<
typename
T>
211
NOM_EXPORT
inline
Size2<T>
operator ++(
const
Size2<T>
& rhs )
212
{
213
return
Size2<T>
(
214
++rhs.w,
215
++rhs.h
216
);
217
}
218
224
template
<
typename
T>
225
NOM_EXPORT
inline
Size2<T>
operator -(
const
Size2<T>
& rhs )
226
{
227
return
Size2<T>
(
228
-rhs.w
229
-rhs.h
230
);
231
}
232
239
template
<
typename
T>
240
NOM_EXPORT
241
inline
Size2<T>
operator -(
const
Size2<T>
& lhs,
const
Size2<T>
& rhs )
242
{
243
return
Size2<T>
(
244
lhs.w - rhs.w,
245
lhs.h - rhs.h
246
);
247
}
248
255
template
<
typename
T>
256
NOM_EXPORT
257
inline
Size2<T>
operator -(
int
lhs,
const
Size2<T>
& rhs)
258
{
259
return
Size2<T>
(lhs - rhs.w, lhs - rhs.h);
260
}
261
268
template
<
typename
T>
269
NOM_EXPORT
inline
Size2<T>
operator -(
const
Size2<T>
& lhs,
int
rhs)
270
{
271
return
Size2<T>
(lhs.w - rhs, lhs.h - rhs);
272
}
273
279
template
<
typename
T>
280
NOM_EXPORT
inline
Size2<T>
operator --(
const
Size2<T>
& rhs )
281
{
282
return
Size2<T>
(
283
--rhs.w,
284
--rhs.h
285
);
286
}
287
294
template
<
typename
T>
295
NOM_EXPORT
296
inline
Size2<T>
operator *(
const
Size2<T>
& lhs,
const
Size2<T>
& rhs )
297
{
298
return
Size2<T>
( lhs.w * rhs.w,
299
lhs.h * rhs.h
300
);
301
}
302
309
template
<
typename
T>
310
NOM_EXPORT
inline
Size2<T>
operator *(
int
lhs,
const
Size2<T>
& rhs)
311
{
312
return
Size2<T>
(lhs * rhs.w, lhs * rhs.h);
313
}
314
321
template
<
typename
T>
322
NOM_EXPORT
inline
Size2<T>
operator *(
const
Size2<T>
& lhs,
int
rhs)
323
{
324
return
Size2<T>
(lhs.w * rhs, lhs.h * rhs);
325
}
326
336
template
<
typename
T>
337
NOM_EXPORT
338
inline
Size2<T>
& operator +=(
Size2<T>
& lhs,
const
Size2<T>
& rhs )
339
{
340
lhs.
w
+= rhs.w;
341
lhs.h += rhs.h;
342
343
return
lhs;
344
}
345
355
template
<
typename
T>
356
NOM_EXPORT
inline
Size2<T>
& operator +=(
int
lhs,
const
Size2<T>
& rhs)
357
{
358
lhs += rhs.
w
;
359
lhs += rhs.h;
360
361
return
lhs;
362
}
363
373
template
<
typename
T>
374
NOM_EXPORT
inline
Size2<T>
& operator +=(
Size2<T>
& lhs,
int
rhs)
375
{
376
lhs.
w
+= rhs;
377
lhs.h += rhs;
378
379
return
lhs;
380
}
381
391
template
<
typename
T>
392
NOM_EXPORT
393
inline
Size2<T>
& operator -=(
Size2<T>
& lhs,
const
Size2<T>
& rhs )
394
{
395
lhs.
w
-= rhs.w;
396
lhs.h -= rhs.h;
397
398
return
lhs;
399
}
400
410
template
<
typename
T>
411
NOM_EXPORT
inline
Size2<T>
& operator -=(
int
lhs,
const
Size2<T>
& rhs)
412
{
413
lhs -= rhs.
w
;
414
lhs -= rhs.h;
415
416
return
lhs;
417
}
418
428
template
<
typename
T>
429
NOM_EXPORT
inline
Size2<T>
& operator -=(
Size2<T>
& lhs,
int
rhs)
430
{
431
lhs.
w
-= rhs;
432
lhs.h -= rhs;
433
434
return
lhs;
435
}
436
446
template
<
typename
T>
447
NOM_EXPORT
448
inline
Size2<T>
& operator *=(
Size2<T>
& lhs,
const
Size2<T>
& rhs )
449
{
450
lhs.
w
*= rhs.w;
451
lhs.h *= rhs.h;
452
453
return
lhs;
454
}
455
465
template
<
typename
T>
466
NOM_EXPORT
467
inline
Size2<T>
& operator /=(
Size2<T>
& lhs,
const
Size2<T>
& rhs )
468
{
469
lhs.
w
/= rhs.w;
470
lhs.h /= rhs.h;
471
472
return
lhs;
473
}
474
484
template
<
typename
T>
485
NOM_EXPORT
inline
Size2<T>
& operator /=(
int
lhs,
const
Size2<T>
& rhs)
486
{
487
lhs /= rhs.
w
;
488
lhs /= rhs.h;
489
490
return
lhs;
491
}
492
502
template
<
typename
T>
503
NOM_EXPORT
inline
Size2<T>
& operator /=(
Size2<T>
& lhs,
int
rhs)
504
{
505
lhs.
w
/= rhs;
506
lhs.h /= rhs;
507
508
return
lhs;
509
}
510
517
template
<
typename
T>
518
NOM_EXPORT
519
inline
Size2<T>
operator /(
const
Size2<T>
& lhs,
const
Size2<T>
& rhs )
520
{
521
Size2<T>
ret;
522
ret.
w
= lhs.w / rhs.w;
523
ret.h = lhs.h / rhs.h;
524
525
return
ret;
526
}
527
534
template
<
typename
T>
535
NOM_EXPORT
inline
Size2<T>
operator /(
int
lhs,
const
Size2<T>
& rhs)
536
{
537
Size2<T>
ret;
538
ret.
w
= lhs / rhs.w;
539
ret.h = lhs / rhs.h;
540
541
return
ret;
542
}
543
550
template
<
typename
T>
551
NOM_EXPORT
inline
Size2<T>
operator /(
const
Size2<T>
& lhs,
int
rhs)
552
{
553
Size2<T>
ret;
554
ret.
w
= lhs.w / rhs;
555
ret.h = lhs.h / rhs;
556
557
return
ret;
558
}
559
564
template
<
typename
T>
565
NOM_EXPORT
566
inline
bool
operator <(
const
Size2<T>
lhs,
const
Size2<T>
& rhs )
567
{
568
return
( lhs.w < rhs.w ) && ( lhs.h < rhs.h );
569
}
570
575
template
<
typename
T>
576
NOM_EXPORT
577
inline
bool
operator >(
const
Size2<T>
& lhs,
const
Size2<T>
& rhs )
578
{
579
return
( rhs.w < lhs.w ) && ( rhs.h < lhs.h );
580
}
581
586
template
<
typename
T>
587
NOM_EXPORT
588
inline
bool
operator <=(
const
Size2<T>
& lhs,
const
Size2<T>
& rhs )
589
{
590
return
( lhs.w <= rhs.w ) && ( lhs.h <= rhs.h );
591
}
592
597
template
<
typename
T>
598
NOM_EXPORT
599
inline
bool
operator >=(
const
Size2<T>
& lhs,
const
Size2<T>
& rhs )
600
{
601
return
( rhs.w <= lhs.w ) && ( rhs.h <= lhs.h );
602
}
603
605
typedef
Size2<int>
Size2i;
606
608
typedef
Size2<float>
Size2f;
609
611
typedef
Size2<double>
Size2d;
612
614
template
<> NOM_EXPORT
const
Size2i
Size2i::null
;
615
617
template
<> NOM_EXPORT
const
Size2f
Size2f::null
;
618
620
template
<> NOM_EXPORT
const
Size2d
Size2d::null
;
621
623
template
<>
const
Size2i
Size2i::zero
;
624
626
template
<>
const
Size2f
Size2f::zero
;
627
630
template
<>
const
Size2d
Size2d::zero
;
631
632
}
// namespace nom
633
634
#endif
// include guard defined
nom::Size2
Size coordinates (width & height) container.
Definition
Size2.hpp:58
nom::Size2< int >::w
int w
Definition
Size2.hpp:126
nom::Size2::Size2
Size2(void)
Default constructor; initialize values to Size2<T>::null.
Definition
Size2.hpp:60
nom::Size2::min
Size2 min(const Size2< U > &rhs)
Compare two Size2 objects and return the smaller width and height of the two objects.
Definition
Size2.hpp:106
nom::Size2::max
Size2 max(const Size2< U > &rhs)
Compare two Size2 objects and return the larger width and height of the two objects.
Definition
Size2.hpp:98
nom::Size2::swap
void swap(void)
Transpose width and height dimensions.
Definition
Size2.hpp:112
nom::Size2::Size2
Size2(const Size2< U > ©)
Copy constructor.
Definition
Size2.hpp:87
nom::Size2::~Size2
~Size2(void)
Destructor.
Definition
Size2.hpp:68
nom::Size2::Size2
Size2(T w, T h)
Constructor variant for initializing width & height at construction.
Definition
Size2.hpp:74
nom::Size2< int >::zero
static const Size2 zero
Definition
Size2.hpp:123
nom::Size2< int >::null
static const Size2 null
Definition
Size2.hpp:120
nom::Size2< int >::h
int h
Definition
Size2.hpp:129
Generated on
for nomlib by
1.18.0