Skip to content

Commit

Permalink
Added explicit casts between geometry types.
Browse files Browse the repository at this point in the history
This is probably preferable to into<C>().
  • Loading branch information
tov committed May 21, 2020
1 parent c280ddd commit 13f8d12
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
13 changes: 7 additions & 6 deletions example/fireworks.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>());
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<int>());
Position(star.position));
}
break;

Expand Down Expand Up @@ -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<double>());
auto pos0 = Projectile::Position(dims.width / 2., dims.height);
model.add_random(get_random(), pos0);
}
}

Expand All @@ -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<double>());
if (is_paused) return;

model.add_random(get_random(), Projectile::Position(position));
}
27 changes: 27 additions & 0 deletions include/ge211_geometry.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ struct Basic_dimensions
{
return {U(width), U(height)};
}

/// Alias for `into<U>()` to support casting between coordinate
/// types.
template <class U>
explicit operator Basic_dimensions<U>() const
NOEXCEPT_(noexcept(into<U>()))
{
return into<U>();
}
};

/// Type alias for the most common use of Basic_dimensions, which is with
Expand Down Expand Up @@ -254,6 +263,15 @@ struct Basic_position
return {U(x), U(y)};
}

/// Alias for `into<U>()` to support casting between coordinate
/// types.
template <class U>
explicit operator Basic_position<U>() const
NOEXCEPT_(noexcept(into<U>()))
{
return into<U>();
}

/// @}

/// \name Shifting member functions
Expand Down Expand Up @@ -423,6 +441,15 @@ struct Basic_rectangle
return {U(x), U(y), U(width), U(height)};
}

/// Alias for `into<U>()` to support casting between coordinate
/// types.
template <class U>
explicit operator Basic_rectangle<U>() const
NOEXCEPT_(noexcept(into<U>()))
{
return into<U>();
}

/// 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)
Expand Down

0 comments on commit 13f8d12

Please sign in to comment.