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_SIZE_HPP
18#define HEADER_SUPERTUX_MATH_SIZE_HPP
19
20#include <iosfwd>
21
22class Sizef;
23
24class Size final
25{
26public:
27 Size() :
28 width(0),
29 height(0)
30 {}
31
32 Size(int width_, int height_) :
33 width(width_),
34 height(height_)
35 {}
36
37 Size(const Size& rhs) = default;
38 Size& operator=(const Size& rhs) = default;
39
40 explicit Size(const Sizef& rhs);
41
42 Size& operator*=(int factor)
43 {
44 width *= factor;
45 height *= factor;
46 return *this;
47 }
48
49 Size& operator/=(int divisor)
50 {
51 width /= divisor;
52 height /= divisor;
53 return *this;
54 }
55
56 Size& operator+=(const Size& rhs)
57 {
58 width += rhs.width;
59 height += rhs.height;
60 return *this;
61 }
62
63 Size& operator-=(const Size& rhs)
64 {
65 width -= rhs.width;
66 height -= rhs.height;
67 return *this;
68 }
69
70 bool is_valid() const
71 {
72 return width > 0 && height > 0;
73 }
74
75public:
76 int width;
77 int height;
78};
79
80inline Size operator*(const Size& lhs, int factor)
81{
82 return Size(lhs.width * factor,
83 lhs.height * factor);
84}
85
86inline Size operator*(int factor, const Size& rhs)
87{
88 return Size(rhs.width * factor,
89 rhs.height * factor);
90}
91
92inline Size operator/(const Size& lhs, int divisor)
93{
94 return Size(lhs.width / divisor,
95 lhs.height / divisor);
96}
97
98inline Size operator+(const Size& lhs, const Size& rhs)
99{
100 return Size(lhs.width + rhs.width,
101 lhs.height + rhs.height);
102}
103
104inline Size operator-(const Size& lhs, const Size& rhs)
105{
106 return Size(lhs.width - rhs.width,
107 lhs.height - rhs.height);
108}
109
110inline bool operator==(const Size& lhs, const Size& rhs)
111{
112 return (lhs.width == rhs.width) && (lhs.height == rhs.height);
113}
114
115inline bool operator!=(const Size& lhs, const Size& rhs)
116{
117 return (lhs.width != rhs.width) || (lhs.height != rhs.height);
118}
119
120std::ostream& operator<<(std::ostream& s, const Size& size);
121
122#endif
123
124/* EOF */
125