e7d4167e5249428fb39ff8accfe793a24f2689e9
[supertux.git] / src / math / size.hpp
1 /*
2 **  ClanLib SDK
3 **  Copyright (c) 1997-2005 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 **    (if your name is missing here, please add it)
28 */
29
30 //! clanCore="Math"
31 //! header=core.h
32
33 #ifndef HEADER_WINDSTILLE_MATH_SIZE_HPP
34 #define HEADER_WINDSTILLE_MATH_SIZE_HPP
35
36 #if _MSC_VER > 1000
37 #pragma once
38 #endif
39
40 #include <iostream>
41
42 class Sizef;
43
44 //: 2D (width,height) size structure.
45 //- !group=Core/Math!
46 //- !header=core.h!
47 class Size
48 {
49   //! Construction:
50 public:
51   //: Constructs a size structure.
52   //param width: Initial width of size structure.
53   //param height: Initial height of size structure.
54   //param size: Size structure to construct this one from.
55   Size() 
56     : width(0), height(0)
57   {}
58
59   Size(int width_, int height_)
60     : width(width_), height(height_) 
61   {}
62
63   Size(const Size &s)
64     : width(s.width),
65       height(s.height)
66   {}
67
68   explicit Size(const Sizef& s);
69
70   //! Attributes:
71 public:
72   //: Size width.
73   int width;
74
75   //: Size height.
76   int height;
77
78   //! Operations:
79 public:
80   //: Size += Size operator.
81   Size &operator+=(const Size &s)
82   { width += s.width; height += s.height; return *this; }
83
84   //: Size -= Size operator.
85   Size &operator-=(const Size &s)
86   { width -= s.width; height -= s.height; return *this; }
87         
88   //: Size + Size operator.
89   Size operator+(const Size &s) const
90   { return Size(width + s.width, height + s.height); }
91
92   //: Size - Size operator.
93   Size operator-(const Size &s) const
94   { return Size(width - s.width, height - s.height); }
95
96   //: Size == Size operator (deep compare).
97   bool operator==(const Size &s) const
98   { return (width == s.width) && (height == s.height); }
99
100   //: Size != Size operator (deep compare).
101   bool operator!=(const Size &s) const
102   { return (width != s.width) || (height != s.height); }
103 };
104
105 //: 2D (width,height) floating point size structure.
106 class Sizef
107 {
108   //! Construction:
109 public:
110   //: Constructs a size structure.
111   //param width: Initial width of size structure.
112   //param height: Initial height of size structure.
113   //param size: Size structure to construct this one from.
114   Sizef() 
115     : width(0.0f),
116       height(0.0f)
117   {}
118
119   Sizef(const Size& s) 
120     : width(static_cast<float>(s.width)),
121       height(static_cast<float>(s.height))
122   {}
123
124   Sizef(float width_, float height_)
125     : width(width_), 
126       height(height_) 
127   {}
128
129   Sizef(const Sizef &s)
130     : width(s.width),
131       height(s.height)
132   {}
133
134   //! Attributes:
135 public:
136   //: Size width.
137   float width;
138
139   //: Size height.
140   float height;
141
142   //! Operations:
143 public:
144   //: Size += Size operator.
145   Sizef &operator+=(const Sizef &s)
146   { width += s.width; height += s.height; return *this; }
147
148   //: Size -= Size operator.
149   Sizef &operator-=(const Sizef &s)
150   { width -= s.width; height -= s.height; return *this; }
151         
152   //: Size + Size operator.
153   Sizef operator+(const Sizef &s) const
154   { return Sizef(width + s.width, height + s.height); }
155
156   //: Size - Size operator.
157   Sizef operator-(const Sizef &s) const
158   { return Sizef(width - s.width, height - s.height); }
159
160   //: Size == Size operator (deep compare).
161   bool operator==(const Sizef &s) const
162   { return (width == s.width) && (height == s.height); }
163
164   //: Size != Size operator (deep compare).
165   bool operator!=(const Size &s) const
166   { return (width != s.width) || (height != s.height); }
167 };
168
169 inline Size::Size(const Sizef& s)
170   : width(static_cast<int>(s.width)),
171     height(static_cast<int>(s.height))
172 {}
173
174 inline std::ostream& operator<<(std::ostream& s, const Size& size) 
175 {
176   return s << "Size(" << size.width << ", " << size.height << ")";
177 }
178
179 #endif