From 857fe370dd2f03245205bf69491a4bf568e36177 Mon Sep 17 00:00:00 2001 From: Prune Juice Date: Wed, 2 Apr 2025 19:52:04 -0500 Subject: [PATCH] [WIP] Merge Rect and Rectf classes. --- src/badguy/crusher.cpp | 24 +-- src/badguy/root_sapling.cpp | 6 +- src/collision/collision.cpp | 2 +- src/collision/collision.hpp | 1 - src/collision/collision_object.hpp | 2 +- src/collision/collision_system.cpp | 16 +- src/collision/collision_system.hpp | 1 - src/control/mobile_controller.hpp | 3 +- src/editor/button_widget.hpp | 2 +- src/editor/object_icon.cpp | 4 +- src/editor/overlay_widget.hpp | 1 - src/editor/particle_editor.cpp | 6 +- src/editor/tilebox.hpp | 3 +- src/editor/toolbox_widget.hpp | 2 +- src/gui/dialog.hpp | 2 +- src/gui/item_horizontalmenu.hpp | 2 +- src/gui/menu_manager.hpp | 2 +- src/gui/menu_transition.hpp | 2 +- src/gui/notification.hpp | 2 +- src/interface/container.hpp | 2 +- src/interface/control_scrollbar.hpp | 2 +- src/interface/control_textbox.cpp | 2 +- src/math/aatriangle.hpp | 2 +- src/math/anchor_point.cpp | 2 +- src/math/anchor_point.hpp | 4 +- src/math/fwd.hpp | 18 +- src/math/rect.cpp | 38 ++-- src/math/rect.hpp | 295 +++++++++++++++++++-------- src/math/rectf.cpp | 47 ----- src/math/rectf.hpp | 193 ------------------ src/math/size.cpp | 17 +- src/math/size.hpp | 107 +++++----- src/math/sizef.cpp | 34 --- src/math/sizef.hpp | 136 ------------ src/math/vector.hpp | 13 +- src/object/path_walker.hpp | 2 +- src/object/tilemap.cpp | 33 ++- src/object/tilemap.hpp | 5 +- src/object/vertical_stripes.cpp | 2 +- src/object/vertical_stripes.hpp | 1 - src/sprite/sprite_data.cpp | 8 +- src/supertux/console.cpp | 2 +- src/supertux/controller_hud.hpp | 2 +- src/supertux/info_box_line.hpp | 1 - src/supertux/moving_object.hpp | 2 +- src/supertux/sector.hpp | 2 - src/supertux/tile.hpp | 2 +- src/supertux/tile_set_parser.cpp | 8 +- src/util/log.cpp | 2 +- src/video/bitmap_font.hpp | 2 +- src/video/canvas.cpp | 5 +- src/video/canvas.hpp | 2 +- src/video/compositor.cpp | 2 + src/video/compositor.hpp | 1 - src/video/drawing_context.hpp | 1 - src/video/drawing_request.hpp | 4 +- src/video/font.hpp | 2 +- src/video/gl/gl33core_context.cpp | 4 +- src/video/gl/gl_painter.cpp | 8 +- src/video/gl/gl_screen_renderer.cpp | 2 +- src/video/gl/gl_texture_renderer.hpp | 1 - src/video/gl/gl_video_system.hpp | 1 - src/video/null/null_renderer.hpp | 1 - src/video/painter.hpp | 1 - src/video/renderer.hpp | 1 - src/video/sdl/sdl_painter.cpp | 8 +- src/video/surface_batch.cpp | 2 +- src/video/surface_batch.hpp | 2 - src/video/texture_manager.cpp | 38 ++-- src/video/video_system.hpp | 1 - src/video/viewport.cpp | 24 ++- 71 files changed, 461 insertions(+), 719 deletions(-) delete mode 100644 src/math/rectf.cpp delete mode 100644 src/math/rectf.hpp delete mode 100644 src/math/sizef.cpp delete mode 100644 src/math/sizef.hpp diff --git a/src/badguy/crusher.cpp b/src/badguy/crusher.cpp index 23630c04b9e..6df2c3df24d 100644 --- a/src/badguy/crusher.cpp +++ b/src/badguy/crusher.cpp @@ -426,34 +426,34 @@ Crusher::spawn_roots(Direction direction) vertical = true; origin.x = m_col.m_bbox.get_middle().x - 16.f; origin.y = m_col.m_bbox.get_bottom(); - test_empty_offset = Rectf(Vector(4, -4), Size(16, 1)); - test_solid_offset_1 = Rectf(Vector(6, 8), Size(1, 1)); - test_solid_offset_2 = Rectf(Vector(16, 8), Size(1, 1)); + test_empty_offset = Rectf(Vector(4, -4), Sizef(16, 1)); + test_solid_offset_1 = Rectf(Vector(6, 8), Sizef(1, 1)); + test_solid_offset_2 = Rectf(Vector(16, 8), Sizef(1, 1)); break; case Direction::UP: vertical = true; origin.x = m_col.m_bbox.get_middle().x - 16.f; origin.y = m_col.m_bbox.get_top() - 6.f; - test_empty_offset = Rectf(Vector(4, 4), Size(16, 1)); - test_solid_offset_1 = Rectf(Vector(6, -8), Size(1, 1)); - test_solid_offset_2 = Rectf(Vector(16, -8), Size(1, 1)); + test_empty_offset = Rectf(Vector(4, 4), Sizef(16, 1)); + test_solid_offset_1 = Rectf(Vector(6, -8), Sizef(1, 1)); + test_solid_offset_2 = Rectf(Vector(16, -8), Sizef(1, 1)); break; case Direction::LEFT: origin.x = m_col.m_bbox.get_left() - 6.f; origin.y = m_col.m_bbox.get_middle().y - 16.f; - test_empty_offset = Rectf(Vector(8, 0), Size(1, 16)); - test_solid_offset_1 = Rectf(Vector(0, 4), Size(1, 1)); - test_solid_offset_2 = Rectf(Vector(0, 12), Size(1, 1)); + test_empty_offset = Rectf(Vector(8, 0), Sizef(1, 16)); + test_solid_offset_1 = Rectf(Vector(0, 4), Sizef(1, 1)); + test_solid_offset_2 = Rectf(Vector(0, 12), Sizef(1, 1)); break; case Direction::RIGHT: origin.x = m_col.m_bbox.get_right() + 12.f; origin.y = m_col.m_bbox.get_middle().y - 16.f; - test_empty_offset = Rectf(Vector(-16, 0), Size(1, 16)); - test_solid_offset_1 = Rectf(Vector(0, 4), Size(1, 1)); - test_solid_offset_2 = Rectf(Vector(0, 12), Size(1, 1)); + test_empty_offset = Rectf(Vector(-16, 0), Sizef(1, 16)); + test_solid_offset_1 = Rectf(Vector(0, 4), Sizef(1, 1)); + test_solid_offset_2 = Rectf(Vector(0, 12), Sizef(1, 1)); break; } diff --git a/src/badguy/root_sapling.cpp b/src/badguy/root_sapling.cpp index 3deeea88e9a..9e51b6b02fd 100644 --- a/src/badguy/root_sapling.cpp +++ b/src/badguy/root_sapling.cpp @@ -277,10 +277,8 @@ RootSapling::should_summon_root(const Rectf& bbox) // Test with all tiles in the root's hitbox const Rect test_tiles = solids->get_tiles_overlapping(bbox); - for (int x = test_tiles.left; x < test_tiles.right; ++x) - { - for (int y = test_tiles.top; y < test_tiles.bottom; ++y) - { + for (int x = test_tiles.get_left(); x < test_tiles.get_right(); ++x) { + for (int y = test_tiles.get_top(); y < test_tiles.get_bottom(); ++y) { const Tile& tile = solids->get_tile(x, y); if (!(tile.get_attributes() & Tile::SOLID)) diff --git a/src/collision/collision.cpp b/src/collision/collision.cpp index b4faaf3491e..befbf29a8f4 100644 --- a/src/collision/collision.cpp +++ b/src/collision/collision.cpp @@ -19,7 +19,7 @@ #include #include "math/aatriangle.hpp" -#include "math/rectf.hpp" +#include "math/rect.hpp" namespace collision { diff --git a/src/collision/collision.hpp b/src/collision/collision.hpp index 76b4ace4838..0f6add59327 100644 --- a/src/collision/collision.hpp +++ b/src/collision/collision.hpp @@ -23,7 +23,6 @@ #include "collision/collision_hit.hpp" #include "math/fwd.hpp" -class Rectf; class AATriangle; namespace collision { diff --git a/src/collision/collision_object.hpp b/src/collision/collision_object.hpp index eb81f0b913e..aac5671149e 100644 --- a/src/collision/collision_object.hpp +++ b/src/collision/collision_object.hpp @@ -24,7 +24,7 @@ #include "collision/collision_group.hpp" #include "collision/collision_hit.hpp" -#include "math/rectf.hpp" +#include "math/rect.hpp" class CollisionGroundMovementManager; class MovingObject; diff --git a/src/collision/collision_system.cpp b/src/collision/collision_system.cpp index 558c454d61c..1a0569b086b 100644 --- a/src/collision/collision_system.cpp +++ b/src/collision/collision_system.cpp @@ -237,10 +237,8 @@ CollisionSystem::collision_tilemap(collision::Constraints* constraints, bool hits_bottom = false; - for (int x = test_tiles.left; x < test_tiles.right; ++x) - { - for (int y = test_tiles.top; y < test_tiles.bottom; ++y) - { + for (int x = test_tiles.get_left(); x < test_tiles.get_right(); ++x) { + for (int y = test_tiles.get_top(); y < test_tiles.get_bottom(); ++y) { const Tile& tile = solids->get_tile(x, y); // Skip non-solid tiles. @@ -306,16 +304,16 @@ CollisionSystem::collision_tile_attributes(const Rectf& dest, const Vector& mov) // For ice (only), add a little fudge to recognize tiles Tux is standing on. const Rect test_tiles_ice = solids->get_tiles_overlapping(Rectf(x1, y1, x2, y2 + SHIFT_DELTA)); - for (int x = test_tiles.left; x < test_tiles.right; ++x) { + for (int x = test_tiles.get_left(); x < test_tiles.get_right(); ++x) { int y; - for (y = test_tiles.top; y < test_tiles.bottom; ++y) { + for (y = test_tiles.get_top(); y < test_tiles.get_bottom(); ++y) { const Tile& tile = solids->get_tile(x, y); if ( tile.is_collisionful( solids->get_tile_bbox(x, y), dest, mov) ) { result |= tile.get_attributes(); } } - for (; y < test_tiles_ice.bottom; ++y) { + for (; y < test_tiles_ice.get_bottom(); ++y) { const Tile& tile = solids->get_tile(x, y); if ( tile.is_collisionful( solids->get_tile_bbox(x, y), dest, mov) ) { result |= (tile.get_attributes() & Tile::ICE); @@ -682,8 +680,8 @@ CollisionSystem::is_free_of_tiles(const Rectf& rect, const bool ignoreUnisolid, // Test with all tiles in this rectangle. const Rect test_tiles = solids->get_tiles_overlapping(rect); - for (int x = test_tiles.left; x < test_tiles.right; ++x) { - for (int y = test_tiles.top; y < test_tiles.bottom; ++y) { + for (int x = test_tiles.get_left(); x < test_tiles.get_right(); ++x) { + for (int y = test_tiles.get_top(); y < test_tiles.get_bottom(); ++y) { const Tile& tile = solids->get_tile(x, y); if (!(tile.get_attributes() & tiletype)) diff --git a/src/collision/collision_system.hpp b/src/collision/collision_system.hpp index 23d9b84e549..94b68cd6c1b 100644 --- a/src/collision/collision_system.hpp +++ b/src/collision/collision_system.hpp @@ -30,7 +30,6 @@ class CollisionObject; class CollisionGroundMovementManager; class DrawingContext; -class Rectf; class Sector; class CollisionSystem final diff --git a/src/control/mobile_controller.hpp b/src/control/mobile_controller.hpp index 6f126cefbed..3e816b31531 100644 --- a/src/control/mobile_controller.hpp +++ b/src/control/mobile_controller.hpp @@ -22,8 +22,7 @@ #include "config.h" - -#include "math/rectf.hpp" +#include "math/rect.hpp" #include "math/vector.hpp" #include "video/surface_ptr.hpp" diff --git a/src/editor/button_widget.hpp b/src/editor/button_widget.hpp index 747d8cbbcb3..1fb96ccc251 100644 --- a/src/editor/button_widget.hpp +++ b/src/editor/button_widget.hpp @@ -21,7 +21,7 @@ #include -#include "math/rectf.hpp" +#include "math/rect.hpp" #include "sprite/sprite.hpp" #include "sprite/sprite_manager.hpp" diff --git a/src/editor/object_icon.cpp b/src/editor/object_icon.cpp index b4d993a1d65..d92a3bf940b 100644 --- a/src/editor/object_icon.cpp +++ b/src/editor/object_icon.cpp @@ -16,10 +16,10 @@ #include "editor/object_icon.hpp" -#include "math/rectf.hpp" +#include "math/rect.hpp" #include "util/reader_mapping.hpp" -#include "video/surface.hpp" #include "video/drawing_context.hpp" +#include "video/surface.hpp" ObjectIcon::ObjectIcon(const std::string& object_class, const std::string& icon) : m_object_class(object_class), diff --git a/src/editor/overlay_widget.hpp b/src/editor/overlay_widget.hpp index c5f5c57b3ca..394eaecdbf8 100644 --- a/src/editor/overlay_widget.hpp +++ b/src/editor/overlay_widget.hpp @@ -36,7 +36,6 @@ class GameObject; class MovingObject; class NodeMarker; class Path; -class Rectf; class Tip; /** A widget that is drawn on top of the current sector and handles diff --git a/src/editor/particle_editor.cpp b/src/editor/particle_editor.cpp index 44241539c4d..b1870be9f6e 100644 --- a/src/editor/particle_editor.cpp +++ b/src/editor/particle_editor.cpp @@ -662,8 +662,10 @@ ParticleEditor::draw(Compositor& compositor) if (m_in_texture_tab) { - context.color().draw_surface_scaled((m_particles->m_textures.begin() + m_texture_current)->texture, - Rect(75, 200, 275, 400), LAYER_GUI); + context.color() + .draw_surface_scaled((m_particles->m_textures.begin() + m_texture_current)->texture, + Rectf(75, 200, 275, 400), + LAYER_GUI); context.color().draw_text(Resources::control_font, std::to_string(m_texture_current + 1) + "/" + std::to_string(m_particles->m_textures.size()), diff --git a/src/editor/tilebox.hpp b/src/editor/tilebox.hpp index 3284605a72e..d7479107350 100644 --- a/src/editor/tilebox.hpp +++ b/src/editor/tilebox.hpp @@ -24,14 +24,13 @@ #include "editor/tip.hpp" #include "interface/control_scrollbar.hpp" -#include "math/rectf.hpp" +#include "math/rect.hpp" #include "math/vector.hpp" #include "supertux/tile_set.hpp" class Editor; class ObjectGroup; class ObjectInfo; -class Rectf; class TileSelection; /** diff --git a/src/editor/toolbox_widget.hpp b/src/editor/toolbox_widget.hpp index e763e5efd8c..5aa657af038 100644 --- a/src/editor/toolbox_widget.hpp +++ b/src/editor/toolbox_widget.hpp @@ -22,7 +22,7 @@ #include -#include "math/rectf.hpp" +#include "math/rect.hpp" #include "math/vector.hpp" #include "video/surface_ptr.hpp" diff --git a/src/gui/dialog.hpp b/src/gui/dialog.hpp index 7de6e5f3673..d09fc216eb2 100644 --- a/src/gui/dialog.hpp +++ b/src/gui/dialog.hpp @@ -23,7 +23,7 @@ #include #include "gui/menu_manager.hpp" -#include "math/sizef.hpp" +#include "math/size.hpp" #include "util/gettext.hpp" class Controller; diff --git a/src/gui/item_horizontalmenu.hpp b/src/gui/item_horizontalmenu.hpp index f94e9189ec6..d19eedac675 100644 --- a/src/gui/item_horizontalmenu.hpp +++ b/src/gui/item_horizontalmenu.hpp @@ -21,7 +21,7 @@ #include -#include "math/rectf.hpp" +#include "math/rect.hpp" #include "video/surface_ptr.hpp" class DrawingContext; diff --git a/src/gui/menu_manager.hpp b/src/gui/menu_manager.hpp index 91ea5c4a566..db2764243be 100644 --- a/src/gui/menu_manager.hpp +++ b/src/gui/menu_manager.hpp @@ -26,7 +26,7 @@ #include "control/controller.hpp" #include "gui/menu_action.hpp" #include "gui/menu_transition.hpp" -#include "math/rectf.hpp" +#include "math/rect.hpp" class Controller; class Dialog; diff --git a/src/gui/menu_transition.hpp b/src/gui/menu_transition.hpp index 8360a4a6460..d9797409ab4 100644 --- a/src/gui/menu_transition.hpp +++ b/src/gui/menu_transition.hpp @@ -18,7 +18,7 @@ #ifndef HEADER_SUPERTUX_GUI_MENU_TRANSITION_HPP #define HEADER_SUPERTUX_GUI_MENU_TRANSITION_HPP -#include "math/rectf.hpp" +#include "math/rect.hpp" class DrawingContext; diff --git a/src/gui/notification.hpp b/src/gui/notification.hpp index 086df411e25..b0fc618517f 100644 --- a/src/gui/notification.hpp +++ b/src/gui/notification.hpp @@ -22,7 +22,7 @@ #include #include "control/controller.hpp" -#include "math/sizef.hpp" +#include "math/size.hpp" #include "video/drawing_context.hpp" class Notification diff --git a/src/interface/container.hpp b/src/interface/container.hpp index eb6a92fc067..ffc8128b85c 100644 --- a/src/interface/container.hpp +++ b/src/interface/container.hpp @@ -20,7 +20,7 @@ #include #include "interface/control.hpp" -#include "math/rectf.hpp" +#include "math/rect.hpp" #include "video/drawing_context.hpp" /** Class that's designed to contain other controls */ diff --git a/src/interface/control_scrollbar.hpp b/src/interface/control_scrollbar.hpp index 79f5c523ad8..85de38448ee 100644 --- a/src/interface/control_scrollbar.hpp +++ b/src/interface/control_scrollbar.hpp @@ -20,7 +20,7 @@ #include "editor/widget.hpp" -#include "math/rectf.hpp" +#include "math/rect.hpp" #include "math/vector.hpp" /** A scrollbar, which manages a scroll progress variable. */ diff --git a/src/interface/control_textbox.cpp b/src/interface/control_textbox.cpp index 299469931c8..3ed2239f5af 100644 --- a/src/interface/control_textbox.cpp +++ b/src/interface/control_textbox.cpp @@ -21,7 +21,7 @@ #include #include "math/vector.hpp" -#include "math/rectf.hpp" +#include "math/rect.hpp" #include "supertux/resources.hpp" #include "util/log.hpp" #include "video/video_system.hpp" diff --git a/src/math/aatriangle.hpp b/src/math/aatriangle.hpp index 03fbfc13f03..ddfbed34a7f 100644 --- a/src/math/aatriangle.hpp +++ b/src/math/aatriangle.hpp @@ -17,7 +17,7 @@ #ifndef HEADER_SUPERTUX_MATH_AATRIANGLE_HPP #define HEADER_SUPERTUX_MATH_AATRIANGLE_HPP -#include "math/rectf.hpp" +#include "math/rect.hpp" /** * An axis-aligned triangle (ie. a triangle where 2 sides are parallel to the x- diff --git a/src/math/anchor_point.cpp b/src/math/anchor_point.cpp index 33ae7691a80..e7b6d50e6b9 100644 --- a/src/math/anchor_point.cpp +++ b/src/math/anchor_point.cpp @@ -21,7 +21,7 @@ #include #include -#include "math/rectf.hpp" +#include "math/rect.hpp" #include "util/log.hpp" #include "util/gettext.hpp" diff --git a/src/math/anchor_point.hpp b/src/math/anchor_point.hpp index 8709dfbda42..120bf6bd1ff 100644 --- a/src/math/anchor_point.hpp +++ b/src/math/anchor_point.hpp @@ -22,7 +22,9 @@ #include "math/vector.hpp" -class Rectf; +template +class Rect_t; +using Rectf = Rect_t; #ifdef DOXYGEN_SCRIPTING /** diff --git a/src/math/fwd.hpp b/src/math/fwd.hpp index c269b7a2685..d6d2540c4e7 100644 --- a/src/math/fwd.hpp +++ b/src/math/fwd.hpp @@ -19,7 +19,23 @@ #include -using Vector = glm::vec2; +template +using Vector_t = glm::vec<2, T, glm::qualifier::defaultp>; + +using Vector = Vector_t; +using Vectori = Vector_t; + +template +class Rect_t; + +using Rect = Rect_t; +using Rectf = Rect_t; + +template +class Size_t; + +using Size = Size_t; +using Sizef = Size_t; #endif diff --git a/src/math/rect.cpp b/src/math/rect.cpp index fe1995bd43b..d3f85784910 100644 --- a/src/math/rect.cpp +++ b/src/math/rect.cpp @@ -14,34 +14,34 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#include "math/rect.hpp" +#include "rect.hpp" -#include - -#include "math/rectf.hpp" - -Rect::Rect(const Rectf& other) : - left(static_cast(other.get_left())), - top(static_cast(other.get_top())), - right(static_cast(other.get_right())), - bottom(static_cast(other.get_bottom())) +template<> +Rect::SDL_Rect_Wrapper Rect::to_sdl() const { + SDL_Rect_Wrapper wrapper; + wrapper.rect_i = {m_pos.x, m_pos.y, m_size.width, m_size.height}; + return wrapper; } -Rectf -Rect::to_rectf() const +template<> +Rectf::SDL_Rect_Wrapper Rectf::to_sdl() const { - return { static_cast(left), static_cast(top), - static_cast(right), static_cast(bottom) }; + SDL_Rect_Wrapper wrapper; + wrapper.rect_f = {m_pos.x, m_pos.y, m_size.width, m_size.height}; + return wrapper; } std::ostream& operator<<(std::ostream& out, const Rect& rect) { - out << "Rect(" - << rect.left << ", " << rect.top << ", " - << rect.right << ", " << rect.bottom - << ")"; + out << "Rect(" << rect.get_left() << ", " << rect.get_top() << ", " << rect.get_right() << ", " + << rect.get_bottom() << ")"; return out; } -/* EOF */ +std::ostream& operator<<(std::ostream& out, const Rectf& rect) +{ + out << "Rectf(" << rect.get_left() << ", " << rect.get_top() << ", " << rect.get_right() << ", " + << rect.get_bottom() << ")"; + return out; +} diff --git a/src/math/rect.hpp b/src/math/rect.hpp index f73e0e86180..4014b03c019 100644 --- a/src/math/rect.hpp +++ b/src/math/rect.hpp @@ -14,148 +14,267 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#ifndef HEADER_SUPERTUX_MATH_RECT_HPP -#define HEADER_SUPERTUX_MATH_RECT_HPP - -#include +#ifndef RECT_HPP +#define RECT_HPP +#include #include +#include +#include #include -#include - -#include "math/size.hpp" +#include -class Rectf; +#include "anchor_point.hpp" +#include "fwd.hpp" +#include "size.hpp" +#include "vector.hpp" -class Rect final +template +class Rect_t final { public: - int left; - int top; - int right; - int bottom; + using type = T; + + union SDL_Rect_Wrapper { + SDL_Rect rect_i; + SDL_FRect rect_f; + + operator SDL_Rect() const { return rect_i; } + operator SDL_FRect() const { return rect_f; } + }; public: - static Rect from_center(int center_x, int center_y, int width, int height) + static Rect_t from_center(const Vector_t& center, const Size_t& size) { - return Rect(center_x - width / 2, - center_y - height / 2, - center_x + width / 2, - center_y + height / 2); + return Rect_t(center.x - size.width / T(2), + center.y - size.height / T(2), + center.x + size.width / T(2), + center.y + size.height / T(2)); + } + + static Rect_t from_center(T center_x, T center_y, T width, T height) + { + return Rect_t(center_x - width / T(2), + center_y - height / T(2), + center_x + width / T(2), + center_y + height / T(2)); } public: - Rect() : - left(0), - top(0), - right(0), - bottom(0) + Rect_t() + : m_pos(0, 0) + , m_size() {} - Rect(const Rect& rhs) = default; - Rect& operator=(const Rect& rhs) = default; + Rect_t(const Rect_t& rhs) = default; + Rect_t& operator=(const Rect_t& rhs) = default; + + Rect_t(const Vector_t& np1, const Vector_t& np2) + : m_pos(np1) + , m_size(np2.x - np1.x, np2.y - np1.y) + { + assert(m_size.width >= 0 && m_size.height >= 0); + } - Rect(int left_, int top_, int right_, int bottom_) : - left(left_), - top(top_), - right(right_), - bottom(bottom_) + template + Rect_t(const Rect_t& other) + : m_pos(U(other.get_left()), U(other.get_top())) + , m_size(U(other.get_width()), U(other.get_height())) {} - Rect(int left_, int top_, const Size& size) : - left(left_), - top(top_), - right(left_ + size.width), - bottom(top_ + size.height) + Rect_t(T left, T top, T right, T bottom) + : m_pos(left, top) + , m_size(right - left, bottom - top) + { + assert(m_size.width >= 0 && m_size.height >= 0); + } + + Rect_t(const SDL_FRect& rect) + : m_pos(rect.x, rect.y) + , m_size(rect.w, rect.h) {} - Rect(const SDL_Rect& rect) : - left(rect.x), - top(rect.y), - right(rect.x + rect.w), - bottom(rect.y + rect.h) + Rect_t(const SDL_Rect& rect) + : m_pos(rect.x, rect.y) + , m_size(rect.w, rect.h) {} - explicit Rect(const Rectf& other); + Rect_t(const Vector_t& p1, const Size_t& size) + : m_pos(p1) + , m_size(size) + { + assert(m_size.width >= 0 && m_size.height >= 0); + } + + Rect_t(T x, T y, const Size_t& size) + : m_pos(x, y) + , m_size(size) + { + assert(m_size.width >= 0 && m_size.height >= 0); + } + + bool operator==(const Rect_t& other) const + { + return (m_pos == other.m_pos && m_size == other.m_size); + } + + inline T& get_left() { return m_pos.x; } + inline T& get_top() { return m_pos.y; } + + inline T get_left() const { return m_pos.x; } + inline T get_right() const { return m_pos.x + m_size.width; } + inline T get_top() const { return m_pos.y; } + inline T get_bottom() const { return m_pos.y + m_size.height; } + + inline T& get_width() { return m_size.width; } + inline T& get_height() { return m_size.height; } - bool operator==(const Rect& other) const + inline T get_width() const { return m_size.width; } + inline T get_height() const { return m_size.height; } + + inline void set_left(T v) { - return (left == other.left && - top == other.top && - right == other.right && - bottom == other.bottom); + m_size.width -= v - m_pos.x; + m_pos.x = v; } + inline void set_right(T v) { m_size.width += v - get_right(); } - bool contains(int x, int y) const + inline void set_top(T v) { - return (left <= x && x < right && - top <= y && y < bottom); + m_size.height -= v - m_pos.y; + m_pos.y = v; } + inline void set_bottom(T v) { m_size.height += v - get_bottom(); } - bool contains(const Rect& other) const + Vector_t get_middle() const { - return (left <= other.left && other.right <= right && - top <= other.top && other.bottom <= bottom); + return Vector_t(m_pos.x + m_size.width / T(2), m_pos.y + m_size.height / T(2)); } - inline const int& get_left() const { return left; } - inline const int& get_right() const { return right; } - inline const int& get_top() const { return top; } - inline const int& get_bottom() const { return bottom; } + inline void set_pos(const Vector_t& v) { m_pos = v; } + + inline void set_width(T width) { m_size.width = width; } + inline void set_height(T height) { m_size.height = height; } + + inline void set_size(T width, T height) { m_size = Size_t(width, height); } + inline Size_t get_size() const { return m_size; } - int get_width() const { return right - left; } - inline int get_height() const { return bottom - top; } - inline Size get_size() const { return Size(right - left, bottom - top); } - inline int get_area() const { return get_width() * get_height(); } + inline T get_area() const { return get_width() * get_height(); } - bool empty() const + inline bool empty() const { return get_width() <= 0 || get_height() <= 0; } + inline bool valid() const { return get_left() <= get_right() && get_top() <= get_bottom(); } + + Rect_t normalized() const + { + return Rect_t(std::min(get_left(), get_right()), + std::min(get_top(), get_bottom()), + std::max(get_left(), get_right()), + std::max(get_top(), get_bottom())); + } + + inline void move(const Vector_t& v) { m_pos += v; } + inline Rect_t moved(const Vector_t& v) const { return Rect_t(m_pos + v, m_size); } + + inline bool contains(const Vector_t& v) const + { + return v.x >= m_pos.x && v.y >= m_pos.y && v.x < get_right() && v.y < get_bottom(); + } + + inline bool contains(const Rect_t& other) const + { + return (get_left() <= other.get_left() && other.get_right() <= get_right() + && get_top() <= other.get_top() && other.get_bottom() <= get_bottom()); + } + + bool overlaps(const Rect_t& other) const + { + if (get_right() < other.get_left() || get_left() > other.get_right()) + return false; + if (get_bottom() < other.get_top() || get_top() > other.get_bottom()) + return false; + + return true; + } + + T distance(const Vector_t& other, AnchorPoint ap = ANCHOR_MIDDLE) const { - return (get_width() <= 0 || - get_height() <= 0); + Vector_t v = get_anchor_pos(*this, ap); + return glm::distance(v, other); } - bool valid() const + T distance(const Rect_t& other, AnchorPoint ap = ANCHOR_MIDDLE) const { - return left <= right && top <= bottom; + Vector_t v1 = get_anchor_pos(*this, ap); + Vector_t v2 = get_anchor_pos(other, ap); + + return glm::distance(v1, v2); } - Rect normalized() const + Rect_t grown(T border) const + { + // If the size would be shrunk below 0, do not resize. + if (m_size.width + border * 2 < 0.f || m_size.height + border * 2 < 0.f) + return *this; + + return Rect_t(m_pos.x - border, m_pos.y - border, get_right() + border, get_bottom() + border); + } + + // leave these two public to save the headaches of set/get functions for such + // simple things (: + + inline const Vector_t& p1() const { return m_pos; } + inline Vector_t p2() const { - return Rect(std::min(left, right), - std::min(top, bottom), - std::max(left, right), - std::max(top, bottom)); + return Vector_t(m_pos.x + m_size.width, m_pos.y + m_size.height); } - Rect moved(int x, int y) const + void set_p1(const Vector_t& p) { - return Rect(left + x, - top + y, - right + x, - bottom + y); + m_size = Size_t(m_size.width + (m_pos.x - p.x), m_size.height + (m_pos.y - p.y)); + m_pos = p; } - Rect grown(int border) const + inline void set_p2(const Vector_t& p) { m_size = Size_t(p.x - m_pos.x, p.y - m_pos.y); } + + inline Rect to_rect() const { - return Rect(left - border, - top - border, - right + border, - bottom + border); + return {static_cast(m_pos.x), + static_cast(m_pos.y), + static_cast(get_right()), + static_cast(get_bottom())}; } - Rectf to_rectf() const; - SDL_Rect to_sdl() const + inline Rectf to_rectf() const { - return { left, top, get_width(), get_height() }; + return {static_cast(m_pos.x), + static_cast(m_pos.y), + static_cast(get_right()), + static_cast(get_bottom())}; } - bool operator<(const Rect& other) const { - return std::tie(left, top, right, bottom) < std::tie(other.left, other.top, other.right, other.bottom); + SDL_Rect_Wrapper to_sdl() const; + + bool operator<(const Rect_t& other) const + { + T rl = get_left(); + T rt = get_top(); + T rr = get_right(); + T rb = get_bottom(); + + T ol = other.get_left(); + T ot = other.get_top(); + T or_ = other.get_right(); + T ob = other.get_bottom(); + + return std::tie(rl, rt, rr, rb) < std::tie(ol, ot, or_, ob); } + +private: + Vector_t m_pos; + Size_t m_size; }; std::ostream& operator<<(std::ostream& out, const Rect& rect); +std::ostream& operator<<(std::ostream& out, const Rectf& rect); -#endif - -/* EOF */ +#endif // RECT_HPP diff --git a/src/math/rectf.cpp b/src/math/rectf.cpp deleted file mode 100644 index 2397c185653..00000000000 --- a/src/math/rectf.cpp +++ /dev/null @@ -1,47 +0,0 @@ -// SuperTux -// Copyright (C) 2016 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/rectf.hpp" - -#include - -#include "math/rect.hpp" - -Rectf::Rectf(const Rect& rect) : - m_p1(static_cast(rect.left), - static_cast(rect.top)), - m_size(static_cast(rect.get_width()), - static_cast(rect.get_height())) -{ -} - -Rect -Rectf::to_rect() const -{ - return { static_cast(m_p1.x), static_cast(m_p1.y), - static_cast(get_right()), static_cast(get_bottom()) }; -} - -std::ostream& operator<<(std::ostream& out, const Rectf& rect) -{ - out << "Rectf(" - << rect.get_left() << ", " << rect.get_top() << ", " - << rect.get_right() << ", " << rect.get_bottom() - << ")"; - return out; -} - -/* EOF */ diff --git a/src/math/rectf.hpp b/src/math/rectf.hpp deleted file mode 100644 index d543a042524..00000000000 --- a/src/math/rectf.hpp +++ /dev/null @@ -1,193 +0,0 @@ -// SuperTux -// Copyright (C) 2006 Matthias Braun -// -// 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_RECTF_HPP -#define HEADER_SUPERTUX_MATH_RECTF_HPP - -#include -#include - -#include - -#include "math/anchor_point.hpp" -#include "math/sizef.hpp" -#include "math/vector.hpp" - -class Rect; - -class Rectf final -{ -public: - static Rectf from_center(const Vector& center, const Sizef& size) - { - return Rectf(center.x - size.width / 2.0f, - center.y - size.height / 2.0f, - center.x + size.width / 2.0f, - center.y + size.height / 2.0f); - } - -public: - Rectf() : - m_p1(0.0f, 0.0f), - m_size() - { } - - Rectf(const Rectf& rhs) = default; - Rectf& operator=(const Rectf& rhs) = default; - - Rectf(const Vector& np1, const Vector& np2) : - m_p1(np1), m_size(np2.x - np1.x, np2.y - np1.y) - { - assert(m_size.width >= 0 && - m_size.height >= 0); - } - - Rectf(float x1, float y1, float x2, float y2) : - m_p1(x1, y1), m_size(x2 - x1, y2 - y1) - { - assert(m_size.width >= 0 && - m_size.height >= 0); - } - - Rectf(const Vector& p1, const Sizef& size) : - m_p1(p1), - m_size(size) - { - } - - Rectf(const SDL_FRect& rect) : - m_p1(rect.x, rect.y), m_size(rect.w, rect.h) - { - } - - Rectf(const Rect& rect); - - bool operator==(const Rectf& other) const - { - return (m_p1 == other.m_p1 && - m_size == other.m_size); - } - - inline float& get_left() { return m_p1.x; } - inline float& get_top() { return m_p1.y; } - - inline float get_left() const { return m_p1.x; } - inline float get_right() const { return m_p1.x + m_size.width; } - inline float get_top() const { return m_p1.y; } - inline float get_bottom() const { return m_p1.y + m_size.height; } - - inline float& get_width() { return m_size.width; } - inline float& get_height() { return m_size.height; } - - inline float get_width() const { return m_size.width; } - inline float get_height() const { return m_size.height; } - - inline void set_left(float v) { m_size.width -= v - m_p1.x; m_p1.x = v; } - inline void set_right(float v) { m_size.width += v - get_right(); } - - inline void set_top(float v) { m_size.height -= v - m_p1.y; m_p1.y = v; } - inline void set_bottom(float v) { m_size.height += v - get_bottom(); } - - Vector get_middle() const { return Vector(m_p1.x + m_size.width / 2.0f, - m_p1.y + m_size.height / 2.0f); } - - inline void set_pos(const Vector& v) { m_p1 = v; } - - inline void set_width(float width) { m_size.width = width; } - inline void set_height(float height) { m_size.height = height; } - inline void set_size(float width, float height) { m_size = Sizef(width, height); } - inline Sizef get_size() const { return m_size; } - - bool empty() const - { - return get_width() <= 0 || - get_height() <= 0; - } - - inline void move(const Vector& v) { m_p1 += v; } - Rectf moved(const Vector& v) const { return Rectf(m_p1 + v, m_size); } - - bool contains(const Vector& v) const { - return v.x >= m_p1.x && v.y >= m_p1.y && v.x < get_right() && v.y < get_bottom(); - } - - bool overlaps(const Rectf& other) const - { - if (get_right() < other.get_left() || get_left() > other.get_right()) - return false; - if (get_bottom() < other.get_top() || get_top() > other.get_bottom()) - return false; - - return true; - } - - float distance (const Vector& other, AnchorPoint ap = ANCHOR_MIDDLE) const - { - Vector v = get_anchor_pos (*this, ap); - return glm::distance(v, other); - } - - float distance (const Rectf& other, AnchorPoint ap = ANCHOR_MIDDLE) const - { - Vector v1 = get_anchor_pos(*this, ap); - Vector v2 = get_anchor_pos(other, ap); - - return glm::distance(v1, v2); - } - - Rectf grown(float border) const - { - // If the size would be shrunk below 0, do not resize. - if (m_size.width + border * 2 < 0.f || m_size.height + border * 2 < 0.f) - return *this; - - return Rectf(m_p1.x - border, m_p1.y - border, - get_right() + border, get_bottom() + border); - } - - // leave these two public to save the headaches of set/get functions for such - // simple things :) - - inline const Vector& p1() const { return m_p1; } - inline Vector p2() const { return Vector(m_p1.x + m_size.width, m_p1.y + m_size.height); } - - void set_p1(const Vector& p) { - m_size = Sizef(m_size.width + (m_p1.x - p.x), - m_size.height + (m_p1.y - p.y)); - m_p1 = p; - } - void set_p2(const Vector& p) { - m_size = Sizef(p.x - m_p1.x, - p.y - m_p1.y); - } - - Rect to_rect() const; - SDL_FRect to_sdl() const - { - return { m_p1.x, m_p1.y, m_size.width, m_size.height }; - } - -private: - /// upper left edge - Vector m_p1; - Sizef m_size; -}; - -std::ostream& operator<<(std::ostream& out, const Rectf& rect); - -#endif - -/* EOF */ diff --git a/src/math/size.cpp b/src/math/size.cpp index a8b46aa3f63..ec21319a501 100644 --- a/src/math/size.cpp +++ b/src/math/size.cpp @@ -14,21 +14,14 @@ // 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)) -{ -} +#include "size.hpp" std::ostream& operator<<(std::ostream& s, const Size& size) { return s << "Size(" << size.width << ", " << size.height << ")"; } -/* EOF */ +std::ostream& operator<<(std::ostream& s, const Sizef& size) +{ + return s << "Sizef(" << size.width << ", " << size.height << ")"; +} diff --git a/src/math/size.hpp b/src/math/size.hpp index 2a2225110dc..ac15abd750a 100644 --- a/src/math/size.hpp +++ b/src/math/size.hpp @@ -14,111 +14,122 @@ // 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 +#ifndef SIZE_HPP +#define SIZE_HPP -#include +#include "fwd.hpp" +#include "vector.hpp" -class Sizef; - -class Size final +template +class Size_t final { public: - Size() : - width(0), - height(0) + using type = T; + + Size_t() + : width(T(0)) + , height(T(0)) {} - Size(int width_, int height_) : - width(width_), - height(height_) + Size_t(T width_, T height_) + : width(width_) + , height(height_) {} - Size(const Size& rhs) = default; - Size& operator=(const Size& rhs) = default; + explicit Size_t(const Vector_t& v) + : width(v.x) + , height(v.y) + {} - explicit Size(const Sizef& rhs); + Size_t(const Size_t& rhs) = default; + Size_t& operator=(const Size_t& rhs) = default; - Size& operator*=(int factor) + Size_t& operator*=(T factor) { - width *= factor; + width *= factor; height *= factor; return *this; } - Size& operator/=(int divisor) + Size_t& operator/=(T divisor) { - width /= divisor; + width /= divisor; height /= divisor; return *this; } - Size& operator+=(const Size& rhs) + Size_t& operator+=(const Size_t& rhs) { - width += rhs.width; + width += rhs.width; height += rhs.height; return *this; } - Size& operator-=(const Size& rhs) + Size_t& operator-=(const Size_t& rhs) { - width -= rhs.width; + width -= rhs.width; height -= rhs.height; return *this; } - bool is_valid() const + inline Vector_t as_vector() const { return Vector_t(width, height); } + + inline bool is_valid() const { return width > 0 && height > 0; } + + template + operator Size_t() const { - return width > 0 && height > 0; + return Size_t(U(width), U(height)); } public: - int width; - int height; + T width; + T height; }; -inline Size operator*(const Size& lhs, int factor) +template +inline Size_t operator*(const Size_t& lhs, T factor) { - return Size(lhs.width * factor, - lhs.height * factor); + return Size_t(lhs.width * factor, lhs.height * factor); } -inline Size operator*(int factor, const Size& rhs) +template +inline Size_t operator*(T factor, const Size_t& rhs) { - return Size(rhs.width * factor, - rhs.height * factor); + return Size_t(rhs.width * factor, rhs.height * factor); } -inline Size operator/(const Size& lhs, int divisor) +template +inline Size_t operator/(const Size_t& lhs, T divisor) { - return Size(lhs.width / divisor, - lhs.height / divisor); + return Size_t(lhs.width / divisor, lhs.height / divisor); } -inline Size operator+(const Size& lhs, const Size& rhs) +template +inline Size_t operator+(const Size_t& lhs, const Size_t& rhs) { - return Size(lhs.width + rhs.width, - lhs.height + rhs.height); + return Size_t(lhs.width + rhs.width, lhs.height + rhs.height); } -inline Size operator-(const Size& lhs, const Size& rhs) +template +inline Size_t operator-(const Size_t& lhs, const Size_t& rhs) { - return Size(lhs.width - rhs.width, - lhs.height - rhs.height); + return Size_t(lhs.width - rhs.width, lhs.height - rhs.height); } -inline bool operator==(const Size& lhs, const Size& rhs) +template +inline bool operator==(const Size_t& lhs, const Size_t& rhs) { - return (lhs.width == rhs.width) && (lhs.height == rhs.height); + return (lhs.width == rhs.width) && (rhs.height == rhs.height); } -inline bool operator!=(const Size& lhs, const Size& rhs) +template +inline bool operator!=(const Size_t& lhs, const Size_t& rhs) { return (lhs.width != rhs.width) || (lhs.height != rhs.height); } std::ostream& operator<<(std::ostream& s, const Size& size); +std::ostream& operator<<(std::ostream& s, const Sizef& size); -#endif - -/* EOF */ +#endif // SIZE_HPP diff --git a/src/math/sizef.cpp b/src/math/sizef.cpp deleted file mode 100644 index fbe569f3c71..00000000000 --- a/src/math/sizef.cpp +++ /dev/null @@ -1,34 +0,0 @@ -// 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 << "Sizef(" << size.width << ", " << size.height << ")"; -} - -/* EOF */ diff --git a/src/math/sizef.hpp b/src/math/sizef.hpp deleted file mode 100644 index 362244c5583..00000000000 --- a/src/math/sizef.hpp +++ /dev/null @@ -1,136 +0,0 @@ -// 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 - -#include "math/vector.hpp" - -class Size; - -class Sizef final -{ -public: - Sizef() : - width(0.0f), - height(0.0f) - {} - - explicit Sizef(const Vector& v) : - width(v.x), - height(v.y) - {} - - Sizef(float width_, float height_) : - width(width_), - height(height_) - {} - - Sizef(const Sizef& rhs) = default; - Sizef& operator=(const Sizef& rhs) = default; - - Sizef(const Size& rhs); - - Sizef& operator*=(float factor) - { - width *= factor; - height *= factor; - return *this; - } - - Sizef& operator/=(float 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; - } - - Vector as_vector() const - { - return Vector(width, height); - } - - bool is_valid() const - { - return width > 0 && height > 0; - } - -public: - float width; - float height; -}; - -inline Sizef operator*(const Sizef& lhs, float factor) -{ - return Sizef(lhs.width * factor, - lhs.height * factor); -} - -inline Sizef operator*(float factor, const Sizef& rhs) -{ - return Sizef(rhs.width * factor, - rhs.height * factor); -} - -inline Sizef operator/(const Sizef& lhs, float 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/src/math/vector.hpp b/src/math/vector.hpp index e84c2f1a589..d3c3a705df9 100644 --- a/src/math/vector.hpp +++ b/src/math/vector.hpp @@ -23,21 +23,24 @@ #include #include -using Vector = glm::vec2; +#include "fwd.hpp" namespace math { -inline Vector vec2_from_polar(float length, float angle) +template +inline Vector_t vec2_from_polar(T length, T angle) { - return Vector(cosf(angle), sinf(angle)) * length; + return Vector_t(cosf(angle), sinf(angle)) * length; } -inline float angle(Vector const& v) +template +inline T angle(Vector_t const& v) { return (v.x == 0 && v.y == 0) ? 0 : atan2f(v.y, v.x); } -inline Vector at_angle(Vector const& v, float angle) +template +inline Vector_t at_angle(Vector_t const& v, T angle) { return vec2_from_polar(glm::length(v), angle); } diff --git a/src/object/path_walker.hpp b/src/object/path_walker.hpp index 00a4f748811..ff1e3e66330 100644 --- a/src/object/path_walker.hpp +++ b/src/object/path_walker.hpp @@ -20,7 +20,7 @@ #include #include -#include "math/sizef.hpp" +#include "math/size.hpp" #include "object/path.hpp" #include "util/uid.hpp" diff --git a/src/object/tilemap.cpp b/src/object/tilemap.cpp index 5e113957749..ce29ab98850 100644 --- a/src/object/tilemap.cpp +++ b/src/object/tilemap.cpp @@ -16,6 +16,7 @@ #include "object/tilemap.hpp" +#include #include #include @@ -491,7 +492,7 @@ TileMap::draw(DrawingContext& context) Rectf draw_rect = context.get_cliprect(); Rect t_draw_rect = get_tiles_overlapping(draw_rect); - Vector start = get_tile_position(t_draw_rect.left, t_draw_rect.top); + Vector start = get_tile_position(t_draw_rect.get_left(), t_draw_rect.get_top()); Vector pos(0.0f, 0.0f); int tx, ty; @@ -500,8 +501,10 @@ TileMap::draw(DrawingContext& context) std::tuple, std::vector>> batches; - for (pos.x = start.x, tx = t_draw_rect.left; tx < t_draw_rect.right; pos.x += 32, ++tx) { - for (pos.y = start.y, ty = t_draw_rect.top; ty < t_draw_rect.bottom; pos.y += 32, ++ty) { + for (pos.x = start.x, tx = t_draw_rect.get_left(); tx < t_draw_rect.get_right(); + pos.x += 32, ++tx) { + for (pos.y = start.y, ty = t_draw_rect.get_top(); ty < t_draw_rect.get_bottom(); + pos.y += 32, ++ty) { int index = ty*m_width + tx; assert (index >= 0); assert (index < (m_width * m_height)); @@ -624,16 +627,32 @@ void TileMap::resize(const Size& newsize, const Size& resize_offset) { resize(newsize.width, newsize.height, 0, resize_offset.width, resize_offset.height); } -Rect -TileMap::get_tiles_overlapping(const Rectf &rect) const +//#include // temp + +Rect TileMap::get_tiles_overlapping(const Rectf& rect) const { Rectf rect2 = rect; rect2.move(-m_offset); - int t_left = std::max(0 , int(floorf(rect2.get_left () / 32))); + int t_left = std::max(0 , int(floorf(rect2.get_left () / 32))); int t_right = std::min(m_width , int(ceilf (rect2.get_right () / 32))); - int t_top = std::max(0 , int(floorf(rect2.get_top () / 32))); + int t_top = std::max(0 , int(floorf(rect2.get_top () / 32))); int t_bottom = std::min(m_height, int(ceilf (rect2.get_bottom() / 32))); + + /* + int t_left = floorf(rect2.get_left () / 32); + int t_right = ceilf (rect2.get_right () / 32); + int t_top = floorf(rect2.get_top () / 32); + int t_bottom = ceilf (rect2.get_bottom() / 32); + + std::clamp(t_left , 0 , int(rect2.get_right()) ); + std::clamp(t_right , t_left, m_width ); + std::clamp(t_top , 0 , int(rect2.get_bottom())); + std::clamp(t_bottom, t_top , m_height ); + + std::cout << t_right - t_left << std::endl; + */ + return Rect(t_left, t_top, t_right, t_bottom); } diff --git a/src/object/tilemap.hpp b/src/object/tilemap.hpp index 198088a7820..01a0ea925f6 100644 --- a/src/object/tilemap.hpp +++ b/src/object/tilemap.hpp @@ -23,14 +23,13 @@ #include #include "math/rect.hpp" -#include "math/rectf.hpp" #include "math/size.hpp" #include "object/path_object.hpp" #include "object/path_walker.hpp" #include "supertux/autotile.hpp" #include "video/color.hpp" -#include "video/flip.hpp" #include "video/drawing_target.hpp" +#include "video/flip.hpp" class AutotileSet; class CollisionObject; @@ -138,7 +137,7 @@ class TileMap final : public LayerObject, /** Returns the half-open rectangle of (x, y) tile indices that overlap the given rectangle in the sector. */ - Rect get_tiles_overlapping(const Rectf &rect) const; + Rect get_tiles_overlapping(const Rectf& rect) const; /** Called by the collision mechanism to indicate that this tilemap has been hit on the top, i.e. has hit a moving object on the bottom of its collision rectangle. */ diff --git a/src/object/vertical_stripes.cpp b/src/object/vertical_stripes.cpp index aefd03fa614..3aeb712825e 100644 --- a/src/object/vertical_stripes.cpp +++ b/src/object/vertical_stripes.cpp @@ -18,7 +18,7 @@ #include "editor/editor.hpp" #include "math/rect.hpp" -#include "math/rectf.hpp" +#include "math/rect.hpp" #include "supertux/sector.hpp" #include "video/drawing_context.hpp" #include "video/surface.hpp" diff --git a/src/object/vertical_stripes.hpp b/src/object/vertical_stripes.hpp index b789d754af3..4d48764dbf8 100644 --- a/src/object/vertical_stripes.hpp +++ b/src/object/vertical_stripes.hpp @@ -18,7 +18,6 @@ #define HEADER_SUPERTUX_OBJECT_VERTICAL_STRIPES_HPP #include "math/rect.hpp" -#include "math/rectf.hpp" #include "supertux/game_object.hpp" diff --git a/src/sprite/sprite_data.cpp b/src/sprite/sprite_data.cpp index 3a70d16130d..83eb1fa5d7f 100644 --- a/src/sprite/sprite_data.cpp +++ b/src/sprite/sprite_data.cpp @@ -372,12 +372,12 @@ SpriteData::parse_action(const ReaderMapping& mapping) } Rect region; - region.left = arr[2].as_int(); - region.top = arr[3].as_int(); + region.get_left() = arr[2].as_int(); + region.get_top() = arr[3].as_int(); const int w = arr[4].as_int(); const int h = arr[5].as_int(); - region.right = region.left + w; - region.bottom = region.top + h; + region.set_right(region.get_left() + w); + region.set_bottom(region.get_top() + h); max_w = std::max(max_w, static_cast(w)); max_h = std::max(max_w, static_cast(h)); diff --git a/src/supertux/console.cpp b/src/supertux/console.cpp index 7b0b71bc788..973aa0687d4 100644 --- a/src/supertux/console.cpp +++ b/src/supertux/console.cpp @@ -16,7 +16,7 @@ #include "supertux/console.hpp" -#include "math/sizef.hpp" +#include "math/size.hpp" #include "physfs/ifile_stream.hpp" #include "squirrel/squirrel_util.hpp" #include "squirrel/squirrel_virtual_machine.hpp" diff --git a/src/supertux/controller_hud.hpp b/src/supertux/controller_hud.hpp index 5dd76ef409e..6ed43627fbf 100644 --- a/src/supertux/controller_hud.hpp +++ b/src/supertux/controller_hud.hpp @@ -20,7 +20,7 @@ #include #include "control/controller.hpp" -#include "math/rectf.hpp" +#include "math/rect.hpp" class DrawingContext; diff --git a/src/supertux/info_box_line.hpp b/src/supertux/info_box_line.hpp index 6e702dc7381..17c6b5b7940 100644 --- a/src/supertux/info_box_line.hpp +++ b/src/supertux/info_box_line.hpp @@ -27,7 +27,6 @@ #include "video/surface_ptr.hpp" class DrawingContext; -class Rectf; /** * Helper class for InfoBox: Represents a line of text diff --git a/src/supertux/moving_object.hpp b/src/supertux/moving_object.hpp index 028742d1031..0fb506b8214 100644 --- a/src/supertux/moving_object.hpp +++ b/src/supertux/moving_object.hpp @@ -21,7 +21,7 @@ #include "collision/collision_hit.hpp" #include "collision/collision_object.hpp" -#include "math/rectf.hpp" +#include "math/rect.hpp" class Dispenser; class Sector; diff --git a/src/supertux/sector.hpp b/src/supertux/sector.hpp index 07606089a6a..259f6b92d5e 100644 --- a/src/supertux/sector.hpp +++ b/src/supertux/sector.hpp @@ -42,8 +42,6 @@ class Level; class MovingObject; class Player; class ReaderMapping; -class Rectf; -class Size; class SpawnPointMarker; class TextObject; class TileMap; diff --git a/src/supertux/tile.hpp b/src/supertux/tile.hpp index 4c859ed4bb2..f42ad0c1da8 100644 --- a/src/supertux/tile.hpp +++ b/src/supertux/tile.hpp @@ -22,7 +22,7 @@ #include #include -#include "math/rectf.hpp" +#include "math/rect.hpp" #include "video/color.hpp" #include "video/surface_ptr.hpp" diff --git a/src/supertux/tile_set_parser.cpp b/src/supertux/tile_set_parser.cpp index 2272dc39753..3a7e92f44b1 100644 --- a/src/supertux/tile_set_parser.cpp +++ b/src/supertux/tile_set_parser.cpp @@ -507,11 +507,11 @@ std::vector if (surface_region) { - rect.left += surface_region->left; - rect.top += surface_region->top; + rect.set_left(rect.get_left() + surface_region->get_left()); + rect.set_top(rect.get_top() + surface_region->get_top()); - rect.right = rect.left + surface_region->get_width(); - rect.bottom = rect.top + surface_region->get_height(); + rect.set_right(rect.get_right() + rect.get_left() + surface_region->get_width()); + rect.set_bottom(rect.get_bottom() + rect.get_top() + surface_region->get_height()); } surfaces.push_back(Surface::from_file(FileSystem::join(m_tiles_path, file), diff --git a/src/util/log.cpp b/src/util/log.cpp index 2013c6ed651..e0989977588 100644 --- a/src/util/log.cpp +++ b/src/util/log.cpp @@ -22,7 +22,7 @@ #include #endif -#include "math/rectf.hpp" +#include "math/rect.hpp" #include "supertux/console.hpp" #include "supertux/gameconfig.hpp" #include "supertux/globals.hpp" diff --git a/src/video/bitmap_font.hpp b/src/video/bitmap_font.hpp index 9a8832b5500..bf1b519c69d 100644 --- a/src/video/bitmap_font.hpp +++ b/src/video/bitmap_font.hpp @@ -20,7 +20,7 @@ #include -#include "math/rectf.hpp" +#include "math/rect.hpp" #include "math/vector.hpp" #include "video/color.hpp" #include "video/font.hpp" diff --git a/src/video/canvas.cpp b/src/video/canvas.cpp index e627a6ed886..dc1d34a3352 100644 --- a/src/video/canvas.cpp +++ b/src/video/canvas.cpp @@ -382,8 +382,9 @@ Vector Canvas::apply_translate(const Vector& pos) const { const Vector& translation = m_context.transform().translation; - return (pos - translation) + Vector(static_cast(m_context.get_viewport().left), - static_cast(m_context.get_viewport().top)); + return (pos - translation) + + Vector(static_cast(m_context.get_viewport().get_left()), + static_cast(m_context.get_viewport().get_top())); } float diff --git a/src/video/canvas.hpp b/src/video/canvas.hpp index 8ed28d0c2da..90dc6bf8d38 100644 --- a/src/video/canvas.hpp +++ b/src/video/canvas.hpp @@ -23,7 +23,7 @@ #include #include -#include "math/rectf.hpp" +#include "math/rect.hpp" #include "math/vector.hpp" #include "video/blend.hpp" #include "video/color.hpp" diff --git a/src/video/compositor.cpp b/src/video/compositor.cpp index c6c07f83db6..8a81f64d508 100644 --- a/src/video/compositor.cpp +++ b/src/video/compositor.cpp @@ -17,6 +17,8 @@ #include "video/compositor.hpp" +#include + #include "math/rect.hpp" #include "video/drawing_context.hpp" #include "video/drawing_request.hpp" diff --git a/src/video/compositor.hpp b/src/video/compositor.hpp index 5d2f1c9b006..ebdce9c8f41 100644 --- a/src/video/compositor.hpp +++ b/src/video/compositor.hpp @@ -23,7 +23,6 @@ #include "util/obstackpp.hpp" class DrawingContext; -class Rect; class VideoSystem; class Compositor final diff --git a/src/video/drawing_context.hpp b/src/video/drawing_context.hpp index e21b746b2d9..a814b230b07 100644 --- a/src/video/drawing_context.hpp +++ b/src/video/drawing_context.hpp @@ -23,7 +23,6 @@ #include #include "math/rect.hpp" -#include "math/rectf.hpp" #include "math/vector.hpp" #include "video/canvas.hpp" #include "video/color.hpp" diff --git a/src/video/drawing_request.hpp b/src/video/drawing_request.hpp index b0227d2571d..38042aa8cff 100644 --- a/src/video/drawing_request.hpp +++ b/src/video/drawing_request.hpp @@ -20,8 +20,8 @@ #include #include -#include "math/rectf.hpp" -#include "math/sizef.hpp" +#include "math/rect.hpp" +#include "math/size.hpp" #include "math/vector.hpp" #include "video/blend.hpp" #include "video/color.hpp" diff --git a/src/video/font.hpp b/src/video/font.hpp index 6ebd1348ec8..ee4b52dcecb 100644 --- a/src/video/font.hpp +++ b/src/video/font.hpp @@ -20,7 +20,7 @@ #include -#include "math/rectf.hpp" +#include "math/rect.hpp" #include "math/vector.hpp" #include "video/color.hpp" #include "video/surface_ptr.hpp" diff --git a/src/video/gl/gl33core_context.cpp b/src/video/gl/gl33core_context.cpp index a65dfaa0f86..2d1973ae959 100644 --- a/src/video/gl/gl33core_context.cpp +++ b/src/video/gl/gl33core_context.cpp @@ -91,8 +91,8 @@ GL33CoreContext::bind() const float sx = tsx / static_cast(rect.get_width()); const float sy = tsy / static_cast(rect.get_height()); - const float tx = -static_cast(rect.left) / static_cast(rect.get_width()); - const float ty = -static_cast(rect.top) / static_cast(rect.get_height()); + const float tx = -static_cast(rect.get_left()) / static_cast(rect.get_width()); + const float ty = -static_cast(rect.get_top()) / static_cast(rect.get_height()); const float matrix[3*3] = { sx, 0.0, 0, diff --git a/src/video/gl/gl_painter.cpp b/src/video/gl/gl_painter.cpp index 38508b74af7..c02c7374594 100644 --- a/src/video/gl/gl_painter.cpp +++ b/src/video/gl/gl_painter.cpp @@ -482,8 +482,8 @@ GLPainter::get_pixel(const GetPixelRequest& request) const float x = request.pos.x * static_cast(rect.get_width()) / static_cast(logical_size.width); float y = request.pos.y * static_cast(rect.get_height()) / static_cast(logical_size.height); - x += static_cast(rect.left); - y += static_cast(rect.top); + x += static_cast(rect.get_left()); + y += static_cast(rect.get_top()); #if 0 // #ifndef USE_OPENGLES2 @@ -516,8 +516,8 @@ GLPainter::set_clip_rect(const Rect& clip_rect) const Rect& rect = m_renderer.get_rect(); const Size& logical_size = m_renderer.get_logical_size(); - glScissor(rect.left + rect.get_width() * clip_rect.left / logical_size.width, - rect.bottom - rect.get_height() * clip_rect.bottom / logical_size.height, + glScissor(rect.get_left() + rect.get_width() * clip_rect.get_left() / logical_size.width, + rect.get_bottom() - rect.get_height() * clip_rect.get_bottom() / logical_size.height, rect.get_width() * clip_rect.get_width() / logical_size.width, rect.get_height() * clip_rect.get_height() / logical_size.height); glEnable(GL_SCISSOR_TEST); diff --git a/src/video/gl/gl_screen_renderer.cpp b/src/video/gl/gl_screen_renderer.cpp index f302b250837..6c214cb8a03 100644 --- a/src/video/gl/gl_screen_renderer.cpp +++ b/src/video/gl/gl_screen_renderer.cpp @@ -49,7 +49,7 @@ GLScreenRenderer::start_draw() const Viewport& viewport = m_video_system.get_viewport(); const Rect& rect = viewport.get_rect(); - glViewport(rect.left, rect.top, rect.get_width(), rect.get_height()); + glViewport(rect.get_left(), rect.get_top(), rect.get_width(), rect.get_height()); context.ortho(static_cast(viewport.get_screen_width()), static_cast(viewport.get_screen_height()), diff --git a/src/video/gl/gl_texture_renderer.hpp b/src/video/gl/gl_texture_renderer.hpp index aa11ba60721..97ed491bc1c 100644 --- a/src/video/gl/gl_texture_renderer.hpp +++ b/src/video/gl/gl_texture_renderer.hpp @@ -27,7 +27,6 @@ class GLFramebuffer; class GLTexture; class GLVideoSystem; -class Rect; class Texture; struct DrawingRequest; diff --git a/src/video/gl/gl_video_system.hpp b/src/video/gl/gl_video_system.hpp index 31629249e5b..f5ac2f23234 100644 --- a/src/video/gl/gl_video_system.hpp +++ b/src/video/gl/gl_video_system.hpp @@ -31,7 +31,6 @@ class GLScreenRenderer; class GLTexture; class GLTextureRenderer; class GLVertexArrays; -class Rect; class TextureManager; struct SDL_Surface; diff --git a/src/video/null/null_renderer.hpp b/src/video/null/null_renderer.hpp index 9c46d839d2b..742406c46be 100644 --- a/src/video/null/null_renderer.hpp +++ b/src/video/null/null_renderer.hpp @@ -20,7 +20,6 @@ #include "video/renderer.hpp" class NullPainter; -class Rect; struct DrawingRequest; struct SDL_Window; diff --git a/src/video/painter.hpp b/src/video/painter.hpp index 244c4b53597..17b519dbf15 100644 --- a/src/video/painter.hpp +++ b/src/video/painter.hpp @@ -22,7 +22,6 @@ #include "math/vector.hpp" #include "video/color.hpp" -class Rect; struct DrawingRequest; struct FillRectRequest; struct GetPixelRequest; diff --git a/src/video/renderer.hpp b/src/video/renderer.hpp index 48f71f0331f..cb03c7faf35 100644 --- a/src/video/renderer.hpp +++ b/src/video/renderer.hpp @@ -23,7 +23,6 @@ #include "video/texture_ptr.hpp" class Painter; -class Rect; struct DrawingRequest; struct SDL_Window; diff --git a/src/video/sdl/sdl_painter.cpp b/src/video/sdl/sdl_painter.cpp index 3797f3137af..1d8f7f971bc 100644 --- a/src/video/sdl/sdl_painter.cpp +++ b/src/video/sdl/sdl_painter.cpp @@ -620,8 +620,12 @@ SDLPainter::get_pixel(const GetPixelRequest& request) const const Size& logical_size = m_renderer.get_logical_size(); SDL_Rect srcrect; - srcrect.x = rect.left + static_cast(request.pos.x * static_cast(rect.get_width()) / static_cast(logical_size.width)); - srcrect.y = rect.top + static_cast(request.pos.y * static_cast(rect.get_height()) / static_cast(logical_size.height)); + srcrect.x = rect.get_left() + + static_cast(request.pos.x * static_cast(rect.get_width()) + / static_cast(logical_size.width)); + srcrect.y = rect.get_top() + + static_cast(request.pos.y * static_cast(rect.get_height()) + / static_cast(logical_size.height)); srcrect.w = 1; srcrect.h = 1; diff --git a/src/video/surface_batch.cpp b/src/video/surface_batch.cpp index 9fe6c4c6711..6dbea8301af 100644 --- a/src/video/surface_batch.cpp +++ b/src/video/surface_batch.cpp @@ -16,7 +16,7 @@ #include "video/surface_batch.hpp" -#include "math/rectf.hpp" +#include "math/rect.hpp" #include "video/surface.hpp" SurfaceBatch::SurfaceBatch(const SurfacePtr& surface, const Color& color) : diff --git a/src/video/surface_batch.hpp b/src/video/surface_batch.hpp index dcca8c702bd..a4868a10a9a 100644 --- a/src/video/surface_batch.hpp +++ b/src/video/surface_batch.hpp @@ -23,8 +23,6 @@ #include "video/paint_style.hpp" #include "video/surface_ptr.hpp" -class Rectf; - class SurfaceBatch { public: diff --git a/src/video/texture_manager.cpp b/src/video/texture_manager.cpp index 0e1d7988526..c44d6e99c9b 100644 --- a/src/video/texture_manager.cpp +++ b/src/video/texture_manager.cpp @@ -185,11 +185,11 @@ TextureManager::get(const ReaderMapping& mapping, const std::optional& reg } else { - rect->left += region->left; - rect->top += region->top; + rect->set_left(rect->get_left() + region->get_left()); + rect->set_top(rect->get_top() += region->get_top()); - rect->right = rect->left + region->get_width(); - rect->bottom = rect->top + region->get_height(); + rect->set_right(rect->get_left() + region->get_width()); + rect->set_bottom(rect->get_top() + region->get_height()); } } @@ -333,26 +333,28 @@ TextureManager::create_image_surface_raw(const std::string& filename, const Rect surface.format->Bmask, surface.format->Amask)); - Rect clipped_rect(std::max(0, rect.left), - std::max(0, rect.top), - std::min(subimage->w, rect.right), - std::min(subimage->w, rect.bottom)); + Rect clipped_rect(std::max(0, rect.get_left()), + std::max(0, rect.get_top()), + std::min(subimage->w, rect.get_right()), + std::min(subimage->h, rect.get_bottom())); SDL_Rect srcrect = clipped_rect.to_sdl(); SDL_BlitSurface(const_cast(&surface), &srcrect, subimage.get(), nullptr); } else { - subimage = SDLSurfacePtr(SDL_CreateRGBSurfaceFrom(static_cast(surface.pixels) + - rect.top * surface.pitch + - rect.left * surface.format->BytesPerPixel, - rect.get_width(), rect.get_height(), - surface.format->BitsPerPixel, - surface.pitch, - surface.format->Rmask, - surface.format->Gmask, - surface.format->Bmask, - surface.format->Amask)); + subimage = SDLSurfacePtr( + SDL_CreateRGBSurfaceFrom(static_cast(surface.pixels) + + rect.get_top() * surface.pitch + + rect.get_left() * surface.format->BytesPerPixel, + rect.get_width(), + rect.get_height(), + surface.format->BitsPerPixel, + surface.pitch, + surface.format->Rmask, + surface.format->Gmask, + surface.format->Bmask, + surface.format->Amask)); if (!subimage) { throw std::runtime_error("SDL_CreateRGBSurfaceFrom() call failed"); diff --git a/src/video/video_system.hpp b/src/video/video_system.hpp index 50e40f31958..143e5362d49 100644 --- a/src/video/video_system.hpp +++ b/src/video/video_system.hpp @@ -26,7 +26,6 @@ #include "video/sampler.hpp" #include "video/texture_ptr.hpp" -class Rect; class Renderer; class SDLSurfacePtr; class Sampler; diff --git a/src/video/viewport.cpp b/src/video/viewport.cpp index 6a62a47f505..43619676337 100644 --- a/src/video/viewport.cpp +++ b/src/video/viewport.cpp @@ -48,10 +48,12 @@ apply_pixel_aspect_ratio_post(const Size& real_window_size, const Size& window_s Vector transform(static_cast(real_window_size.width) / static_cast(window_size.width), static_cast(real_window_size.height) / static_cast(window_size.height)); - out_viewport.left = static_cast(static_cast(out_viewport.left) * transform.x); - out_viewport.top = static_cast(static_cast(out_viewport.top) * transform.y); - out_viewport.right = static_cast(static_cast(out_viewport.right) * transform.x); - out_viewport.bottom = static_cast(static_cast(out_viewport.bottom) * transform.y); + out_viewport.set_left(static_cast(static_cast(out_viewport.get_left()) * transform.x)); + out_viewport.set_top(static_cast(static_cast(out_viewport.get_top()) * transform.y)); + out_viewport.set_right( + static_cast(static_cast(out_viewport.get_right()) * transform.x)); + out_viewport.set_bottom( + static_cast(static_cast(out_viewport.get_bottom()) * transform.y)); out_scale.x = scale * transform.x; out_scale.y = scale * transform.y; @@ -101,11 +103,11 @@ calculate_viewport(const Size& max_size, const Size& window_size, float scale) // Center the viewport in the window Rect viewport; - viewport.left = std::max(0, (window_size.width - viewport_width) / 2); - viewport.top = std::max(0, (window_size.height - viewport_height) / 2); + viewport.set_left(std::max(0, (window_size.width - viewport_width) / 2)); + viewport.set_top(std::max(0, (window_size.height - viewport_height) / 2)); - viewport.right = viewport.left + viewport_width; - viewport.bottom = viewport.top + viewport_height; + viewport.set_right(viewport.get_left() + viewport_width); + viewport.set_bottom(viewport.get_top() + viewport_height); return viewport; } @@ -209,14 +211,14 @@ Viewport::get_screen_size() const Vector Viewport::to_logical(int physical_x, int physical_y) const { - return Vector(static_cast(physical_x - m_rect.left) / m_scale.x, - static_cast(physical_y - m_rect.top) / m_scale.y); + return Vector(static_cast(physical_x - m_rect.get_left()) / m_scale.x, + static_cast(physical_y - m_rect.get_top()) / m_scale.y); } bool Viewport::needs_clear_screen() const { - return (m_rect.left != 0 || m_rect.top != 0); + return (m_rect.get_left() != 0 || m_rect.get_top() != 0); } /* EOF */