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
24class Size;
25
26class Sizef final
27{
28public:
29 Sizef() :
30 width(0.0f),
31 height(0.0f)
32 {}
33
34 explicit 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) = default;
45 Sizef& operator=(const Sizef& rhs) = default;
46
47 Sizef(const Size& rhs);
48
49 Sizef& operator*=(float factor)
50 {
51 width *= factor;
52 height *= factor;
53 return *this;
54 }
55
56 Sizef& operator/=(float divisor)
57 {
58 width /= divisor;
59 height /= divisor;
60 return *this;
61 }
62
63 Sizef& operator+=(const Sizef& rhs)
64 {
65 width += rhs.width;
66 height += rhs.height;
67 return *this;
68 }
69
70 Sizef& operator-=(const Sizef& rhs)
71 {
72 width -= rhs.width;
73 height -= rhs.height;
74 return *this;
75 }
76
77 Vector as_vector() const
78 {
79 return Vector(width, height);
80 }
81
82 bool is_valid() const
83 {
84 return width > 0 && height > 0;
85 }
86
87public:
88 float width;
89 float height;
90};
91
92inline Sizef operator*(const Sizef& lhs, float factor)
93{
94 return Sizef(lhs.width * factor,
95 lhs.height * factor);
96}
97
98inline Sizef operator*(float factor, const Sizef& rhs)
99{
100 return Sizef(rhs.width * factor,
101 rhs.height * factor);
102}
103
104inline Sizef operator/(const Sizef& lhs, float divisor)
105{
106 return Sizef(lhs.width / divisor,
107 lhs.height / divisor);
108}
109
110inline Sizef operator+(const Sizef& lhs, const Sizef& rhs)
111{
112 return Sizef(lhs.width + rhs.width,
113 lhs.height + rhs.height);
114}
115
116inline Sizef operator-(const Sizef& lhs, const Sizef& rhs)
117{
118 return Sizef(lhs.width - rhs.width,
119 lhs.height - rhs.height);
120}
121
122inline bool operator==(const Sizef& lhs, const Sizef& rhs)
123{
124 return (lhs.width == rhs.width) && (rhs.height == rhs.height);
125}
126
127inline bool operator!=(const Sizef& lhs, const Sizef& rhs)
128{
129 return (lhs.width != rhs.width) || (lhs.height != rhs.height);
130}
131
132std::ostream& operator<<(std::ostream& s, const Sizef& size);
133
134#endif
135
136/* EOF */
137