From: grumbel Date: Fri, 20 Nov 2009 19:47:42 +0000 (+0000) Subject: Rewrote Size/Sizef classes X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=246b8530cda002dcaf9135c1604a68fd5b52ad6f;p=supertux.git Rewrote Size/Sizef classes git-svn-id: http://supertux.lethargik.org/svn/supertux/trunk/supertux@6071 837edb03-e0f3-0310-88ca-d4d4e8b29345 --- diff --git a/src/math/size.cpp b/src/math/size.cpp new file mode 100644 index 000000000..6c1c96310 --- /dev/null +++ b/src/math/size.cpp @@ -0,0 +1,34 @@ +// SuperTux +// Copyright (C) 2009 Ingo Ruhnke +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#include "math/size.hpp" + +#include + +#include "math/sizef.hpp" + +Size::Size(const Sizef& rhs) : + width(static_cast(rhs.width)), + height(static_cast(rhs.height)) +{ +} + +std::ostream& operator<<(std::ostream& s, const Size& size) +{ + return s << "Size(" << size.width << ", " << size.height << ")"; +} + +/* EOF */ diff --git a/src/math/size.hpp b/src/math/size.hpp index e7d4167e5..81983d6f5 100644 --- a/src/math/size.hpp +++ b/src/math/size.hpp @@ -1,179 +1,119 @@ -/* -** ClanLib SDK -** Copyright (c) 1997-2005 The ClanLib Team -** -** This software is provided 'as-is', without any express or implied -** warranty. In no event will the authors be held liable for any damages -** arising from the use of this software. -** -** Permission is granted to anyone to use this software for any purpose, -** including commercial applications, and to alter it and redistribute it -** freely, subject to the following restrictions: -** -** 1. The origin of this software must not be misrepresented; you must not -** claim that you wrote the original software. If you use this software -** in a product, an acknowledgment in the product documentation would be -** appreciated but is not required. -** 2. Altered source versions must be plainly marked as such, and must not be -** misrepresented as being the original software. -** 3. This notice may not be removed or altered from any source distribution. -** -** Note: Some of the libraries ClanLib may link to may have additional -** requirements or restrictions. -** -** File Author(s): -** -** Magnus Norddahl -** (if your name is missing here, please add it) -*/ - -//! clanCore="Math" -//! header=core.h - -#ifndef HEADER_WINDSTILLE_MATH_SIZE_HPP -#define HEADER_WINDSTILLE_MATH_SIZE_HPP - -#if _MSC_VER > 1000 -#pragma once -#endif - -#include +// SuperTux +// Copyright (C) 2009 Ingo Ruhnke +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#ifndef HEADER_SUPERTUX_MATH_SIZE_HPP +#define HEADER_SUPERTUX_MATH_SIZE_HPP + +#include class Sizef; -//: 2D (width,height) size structure. -//- !group=Core/Math! -//- !header=core.h! class Size { - //! Construction: public: - //: Constructs a size structure. - //param width: Initial width of size structure. - //param height: Initial height of size structure. - //param size: Size structure to construct this one from. - Size() - : width(0), height(0) + Size() : + width(0), + height(0) {} - Size(int width_, int height_) - : width(width_), height(height_) + Size(int width_, int height_) : + width(width_), + height(height_) {} - Size(const Size &s) - : width(s.width), - height(s.height) + Size(const Size& rhs) : + width(rhs.width), + height(rhs.height) {} - explicit Size(const Sizef& s); + explicit Size(const Sizef& rhs); + + Size& operator*=(int factor) + { + width *= factor; + height *= factor; + return *this; + } + + Size& operator/=(int divisor) + { + width /= divisor; + height /= divisor; + return *this; + } + + Size& operator+=(const Size& rhs) + { + width += rhs.width; + height += rhs.height; + return *this; + } + + Size& operator-=(const Size& rhs) + { + width -= rhs.width; + height -= rhs.height; + return *this; + } - //! Attributes: public: - //: Size width. int width; - - //: Size height. int height; - - //! Operations: -public: - //: Size += Size operator. - Size &operator+=(const Size &s) - { width += s.width; height += s.height; return *this; } - - //: Size -= Size operator. - Size &operator-=(const Size &s) - { width -= s.width; height -= s.height; return *this; } - - //: Size + Size operator. - Size operator+(const Size &s) const - { return Size(width + s.width, height + s.height); } - - //: Size - Size operator. - Size operator-(const Size &s) const - { return Size(width - s.width, height - s.height); } - - //: Size == Size operator (deep compare). - bool operator==(const Size &s) const - { return (width == s.width) && (height == s.height); } - - //: Size != Size operator (deep compare). - bool operator!=(const Size &s) const - { return (width != s.width) || (height != s.height); } }; -//: 2D (width,height) floating point size structure. -class Sizef -{ - //! Construction: -public: - //: Constructs a size structure. - //param width: Initial width of size structure. - //param height: Initial height of size structure. - //param size: Size structure to construct this one from. - Sizef() - : width(0.0f), - height(0.0f) - {} - - Sizef(const Size& s) - : width(static_cast(s.width)), - height(static_cast(s.height)) - {} - - Sizef(float width_, float height_) - : width(width_), - height(height_) - {} - - Sizef(const Sizef &s) - : width(s.width), - height(s.height) - {} +inline Size operator*(const Size& lhs, int factor) +{ + return Size(lhs.width * factor, + lhs.height * factor); +} - //! Attributes: -public: - //: Size width. - float width; +inline Size operator*(int factor, const Size& rhs) +{ + return Size(rhs.width * factor, + rhs.height * factor); +} - //: Size height. - float height; +inline Size operator/(const Size& lhs, int divisor) +{ + return Size(lhs.width / divisor, + lhs.height / divisor); +} - //! Operations: -public: - //: Size += Size operator. - Sizef &operator+=(const Sizef &s) - { width += s.width; height += s.height; return *this; } - - //: Size -= Size operator. - Sizef &operator-=(const Sizef &s) - { width -= s.width; height -= s.height; return *this; } - - //: Size + Size operator. - Sizef operator+(const Sizef &s) const - { return Sizef(width + s.width, height + s.height); } - - //: Size - Size operator. - Sizef operator-(const Sizef &s) const - { return Sizef(width - s.width, height - s.height); } - - //: Size == Size operator (deep compare). - bool operator==(const Sizef &s) const - { return (width == s.width) && (height == s.height); } - - //: Size != Size operator (deep compare). - bool operator!=(const Size &s) const - { return (width != s.width) || (height != s.height); } -}; +inline Size operator+(const Size& lhs, const Size& rhs) +{ + return Size(lhs.width + rhs.width, + lhs.height + rhs.height); +} -inline Size::Size(const Sizef& s) - : width(static_cast(s.width)), - height(static_cast(s.height)) -{} +inline Size operator-(const Size& lhs, const Size& rhs) +{ + return Size(lhs.width - rhs.width, + lhs.height - rhs.height); +} -inline std::ostream& operator<<(std::ostream& s, const Size& size) +inline bool operator==(const Size& lhs, const Size& rhs) { - return s << "Size(" << size.width << ", " << size.height << ")"; + return (lhs.width == rhs.width) && (lhs.height == rhs.height); } +inline bool operator!=(const Size& lhs, const Size& rhs) +{ + return (lhs.width != rhs.width) || (lhs.height != rhs.height); +} + +std::ostream& operator<<(std::ostream& s, const Size& size); + #endif diff --git a/src/math/sizef.cpp b/src/math/sizef.cpp new file mode 100644 index 000000000..a78db8647 --- /dev/null +++ b/src/math/sizef.cpp @@ -0,0 +1,34 @@ +// SuperTux +// Copyright (C) 2009 Ingo Ruhnke +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#include "math/sizef.hpp" + +#include + +#include "math/size.hpp" + +Sizef::Sizef(const Size& rhs) : + width(static_cast(rhs.width)), + height(static_cast(rhs.height)) +{ +} + +std::ostream& operator<<(std::ostream& s, const Sizef& size) +{ + return s << "Size(" << size.width << ", " << size.height << ")"; +} + +/* EOF */ diff --git a/src/math/sizef.hpp b/src/math/sizef.hpp new file mode 100644 index 000000000..9339eeced --- /dev/null +++ b/src/math/sizef.hpp @@ -0,0 +1,121 @@ +// SuperTux +// Copyright (C) 2009 Ingo Ruhnke +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#ifndef HEADER_SUPERTUX_MATH_SIZEF_HPP +#define HEADER_SUPERTUX_MATH_SIZEF_HPP + +#include + +class Size; + +class Sizef +{ +public: + Sizef() : + width(0.0f), + height(0.0f) + {} + + Sizef(float width_, float height_) : + width(width_), + height(height_) + {} + + Sizef(const Sizef& rhs) : + width(rhs.width), + height(rhs.height) + {} + + Sizef(const Size& rhs); + + Sizef& operator*=(int factor) + { + width *= factor; + height *= factor; + return *this; + } + + Sizef& operator/=(int divisor) + { + width /= divisor; + height /= divisor; + return *this; + } + + Sizef& operator+=(const Sizef& rhs) + { + width += rhs.width; + height += rhs.height; + return *this; + } + + Sizef& operator-=(const Sizef& rhs) + { + width -= rhs.width; + height -= rhs.height; + return *this; + } + +public: + float width; + float height; +}; + +inline Sizef operator*(const Sizef& lhs, int factor) +{ + return Sizef(lhs.width * factor, + lhs.height * factor); +} + +inline Sizef operator*(int factor, const Sizef& rhs) +{ + return Sizef(rhs.width * factor, + rhs.height * factor); +} + +inline Sizef operator/(const Sizef& lhs, int divisor) +{ + return Sizef(lhs.width / divisor, + lhs.height / divisor); +} + +inline Sizef operator+(const Sizef& lhs, const Sizef& rhs) +{ + return Sizef(lhs.width + rhs.width, + lhs.height + rhs.height); +} + +inline Sizef operator-(const Sizef& lhs, const Sizef& rhs) +{ + return Sizef(lhs.width - rhs.width, + lhs.height - rhs.height); +} + +inline bool operator==(const Sizef& lhs, const Sizef& rhs) +{ + return (lhs.width == rhs.width) && (rhs.height == rhs.height); +} + +inline bool operator!=(const Sizef& lhs, const Sizef& rhs) +{ + return (lhs.width != rhs.width) || (lhs.height != rhs.height); +} + +std::ostream& operator<<(std::ostream& s, const Sizef& size); + +#endif + +/* EOF */ diff --git a/test/size_test.cpp b/test/size_test.cpp new file mode 100644 index 000000000..55896028a --- /dev/null +++ b/test/size_test.cpp @@ -0,0 +1,40 @@ +// SuperTux +// Copyright (C) 2009 Ingo Ruhnke +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#include + +#include "math/size.hpp" + +int main() +{ + Size size(800, 600); + + std::cout << size << std::endl; + std::cout << size * 2 << std::endl; + std::cout << 2 * size << std::endl; + std::cout << size / 2 << std::endl; + std::cout << size + size << std::endl; + size *= 2; + std::cout << size << std::endl; + size /= 2; + std::cout << size << std::endl; + size += size; + std::cout << size << std::endl; + + return 0; +} + +/* EOF */ diff --git a/test/sizef_test.cpp b/test/sizef_test.cpp new file mode 100644 index 000000000..1cfe54cf1 --- /dev/null +++ b/test/sizef_test.cpp @@ -0,0 +1,40 @@ +// SuperTux +// Copyright (C) 2009 Ingo Ruhnke +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#include + +#include "math/sizef.hpp" + +int main() +{ + Sizef size(800, 600); + + std::cout << size << std::endl; + std::cout << size * 2 << std::endl; + std::cout << 2 * size << std::endl; + std::cout << size / 2 << std::endl; + std::cout << size + size << std::endl; + size *= 2; + std::cout << size << std::endl; + size /= 2; + std::cout << size << std::endl; + size += size; + std::cout << size << std::endl; + + return 0; +} + +/* EOF */