From 13f8d125c943ea64d7cc318e781884459480f7af Mon Sep 17 00:00:00 2001 From: "Jesse A. Tov" Date: Thu, 21 May 2020 18:47:06 -0500 Subject: [PATCH] Added explicit casts between geometry types. This is probably preferable to into(). --- example/fireworks.cxx | 13 +++++++------ include/ge211_geometry.hxx | 27 +++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/example/fireworks.cxx b/example/fireworks.cxx index 2f78f28..f8f9d64 100644 --- a/example/fireworks.cxx +++ b/example/fireworks.cxx @@ -248,13 +248,13 @@ void Fireworks::draw_fireworks(Sprite_set& sprites) const switch (firework.stage) { case Firework::Stage::mortar: sprites.add_sprite(view.mortar, - firework.mortar.position.into()); + Position(firework.mortar.position)); break; case Firework::Stage::stars: for (Projectile const& star : firework.stars) { sprites.add_sprite(view.stars[firework.star_color], - star.position.into()); + Position(star.position)); } break; @@ -303,8 +303,8 @@ void Fireworks::on_key(Key key) is_paused = !is_paused; } else if (key == Key::code(' ') && !is_paused) { auto dims = get_window().get_dimensions(); - auto initial_position = Position{dims.width / 2, dims.height}; - model.add_random(get_random(), initial_position.into()); + auto pos0 = Projectile::Position(dims.width / 2., dims.height); + model.add_random(get_random(), pos0); } } @@ -321,6 +321,7 @@ void Fireworks::on_frame(double dt) void Fireworks::on_mouse_up(Mouse_button, Position position) { - if (!is_paused) - model.add_random(get_random(), position.into()); + if (is_paused) return; + + model.add_random(get_random(), Projectile::Position(position)); } diff --git a/include/ge211_geometry.hxx b/include/ge211_geometry.hxx index 0197e3b..3490352 100644 --- a/include/ge211_geometry.hxx +++ b/include/ge211_geometry.hxx @@ -41,6 +41,15 @@ struct Basic_dimensions { return {U(width), U(height)}; } + + /// Alias for `into()` to support casting between coordinate + /// types. + template + explicit operator Basic_dimensions() const + NOEXCEPT_(noexcept(into())) + { + return into(); + } }; /// Type alias for the most common use of Basic_dimensions, which is with @@ -254,6 +263,15 @@ struct Basic_position return {U(x), U(y)}; } + /// Alias for `into()` to support casting between coordinate + /// types. + template + explicit operator Basic_position() const + NOEXCEPT_(noexcept(into())) + { + return into(); + } + /// @} /// \name Shifting member functions @@ -423,6 +441,15 @@ struct Basic_rectangle return {U(x), U(y), U(width), U(height)}; } + /// Alias for `into()` to support casting between coordinate + /// types. + template + explicit operator Basic_rectangle() const + NOEXCEPT_(noexcept(into())) + { + return into(); + } + /// Creates a Basic_rectangle given the position of its top left vertex /// and its dimensions. static Basic_rectangle from_top_left(Position tl, Dimensions dims)