893d0d4ea8ecb8d0ba24cbb5e883812ae00694b6
[supertux.git] / src / math / sizef.hpp
1 //  SuperTux
2 //  Copyright (C) 2009 Ingo Ruhnke <grumbel@gmail.com>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #ifndef HEADER_SUPERTUX_MATH_SIZEF_HPP
18 #define HEADER_SUPERTUX_MATH_SIZEF_HPP
19
20 #include <iosfwd>
21
22 #include "math/vector.hpp"
23
24 class Size;
25
26 class Sizef
27 {
28 public:
29   Sizef() :
30     width(0.0f),
31     height(0.0f)
32   {}
33
34   Sizef(const Vector& v) :
35     width(v.x),
36     height(v.y)
37   {}
38
39   Sizef(float width_, float height_) :
40     width(width_),
41     height(height_)
42   {}
43
44   Sizef(const Sizef& rhs) :
45     width(rhs.width),
46     height(rhs.height)
47   {}
48
49   Sizef(const Size& rhs);
50
51   Sizef& operator*=(float factor)
52   {
53     width  *= factor;
54     height *= factor;
55     return *this;
56   }
57
58   Sizef& operator/=(float divisor)
59   {
60     width  /= divisor;
61     height /= divisor;
62     return *this;
63   }
64
65   Sizef& operator+=(const Sizef& rhs)
66   {
67     width  += rhs.width;
68     height += rhs.height;
69     return *this;
70   }
71
72   Sizef& operator-=(const Sizef& rhs)
73   {
74     width  -= rhs.width;
75     height -= rhs.height;
76     return *this;
77   }
78
79   Vector as_vector() const
80   {
81     return Vector(width, height);
82   }
83
84 public:
85   float width;
86   float height;
87 };
88
89 inline Sizef operator*(const Sizef& lhs, float factor)
90 {
91   return Sizef(lhs.width  * factor,
92                lhs.height * factor);
93 }
94
95 inline Sizef operator*(float factor, const Sizef& rhs)
96 {
97   return Sizef(rhs.width  * factor,
98                rhs.height * factor);
99 }
100
101 inline Sizef operator/(const Sizef& lhs, float divisor)
102 {
103   return Sizef(lhs.width  / divisor,
104                lhs.height / divisor);
105 }
106
107 inline Sizef operator+(const Sizef& lhs, const Sizef& rhs)
108 {
109   return Sizef(lhs.width  + rhs.width,
110                lhs.height + rhs.height);
111 }
112
113 inline Sizef operator-(const Sizef& lhs, const Sizef& rhs)
114 {
115   return Sizef(lhs.width  - rhs.width,
116                lhs.height - rhs.height);
117 }
118
119 inline bool operator==(const Sizef& lhs, const Sizef& rhs)
120 {
121   return (lhs.width == rhs.width) && (rhs.height == rhs.height);
122 }
123
124 inline bool operator!=(const Sizef& lhs, const Sizef& rhs)
125 {
126   return (lhs.width != rhs.width) || (lhs.height != rhs.height);
127 }
128
129 std::ostream& operator<<(std::ostream& s, const Sizef& size);
130
131 #endif
132
133 /* EOF */