richelbilderbeek / djog_unos_2018

Project by the Uno's at DJOG 2018-2019: Nature Zen
GNU General Public License v3.0
6 stars 2 forks source link

Split up sfml_resources::get_tile_sprite #538

Closed richelbilderbeek closed 5 years ago

richelbilderbeek commented 5 years ago

Is your feature request related to a problem? Please describe.

There is a clear pattern in sfml_resources::get_tile_sprite:

sf::Texture &sfml_resources::get_tile_sprite(const tile &t) noexcept //!OCLINT too long, needs to be fixed
{
  switch (t.get_type()) //!OCLINT too few branches for now
  {
    case tile_type::tundra:
      if (t.get_width() > 100) {
        assert(t.get_height() == 100.0);
        return m_tundra_laying;
      }
      assert(t.get_width() == 100.0);
      return m_tundra_standing;
    case tile_type::beach:
      if (t.get_width() > 100) {
        return m_beach_laying;
      }
      return m_beach_standing;
    case tile_type::water:
      if (t.get_width() > 100) {
        return m_water_laying;
      }
      return m_water_standing;
    case tile_type::dunes:
      if (t.get_width() > 100) {
        return m_dunes_laying;
      }
      return m_dunes_standing;
    case tile_type::hills:
      if (t.get_width() > 100) {
        return m_hills_laying;
      }
      return m_hills_standing;
    default:
      return m_empty_tile;
  }
}

For each tile, first the tile_type is checked, then its orientation. Reversing this simplifies the code and makes it more readable.

Describe the solution you'd like

(note: I think 'portrait' is the more commonly used name for 'standing', 'landscape' for 'lying')

sf::Texture &sfml_resources::get_tile_sprite_portrait(const tile_type t) noexcept //!OCLINT cannot be simpler

and

sf::Texture &sfml_resources::get_tile_sprite_landscape(const tile_type t) noexcept //!OCLINT cannot be simpler
sf::Texture &sfml_resources::get_tile_sprite(const tile &t) noexcept
{
  if (t.get_width() > t.get_height() 
  {
    return get_tile_sprite_landscape(t.get_type());
  }
  else
  {
    //There are no square tiles (yet?)
    assert(t.get_width() < t.get_height() 
    return get_tile_sprite_portrait(t.get_type());
  }
}
  //#define FIX_ISSUE_538
  #ifdef FIX_ISSUE_538
  // Can get the sprite of a tile type
  {
    assert(resources.get_tile_sprite_portrait(tile_type::grassland).getSize().x > 0);
    assert(resources.get_tile_sprite_landscape(tile_type::grassland).getSize().x > 0);
  }
  #endif // FIX_ISSUE_538

Remove the preprocessor directives. If it compiles, you are done :+1:

Describe alternatives you've considered

None.

Additional context

None.