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

Simplify get_agent_sprite #537

Closed richelbilderbeek closed 5 years ago

richelbilderbeek commented 5 years ago

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

Currently, sfml_resources::get_agent_sprite needs an agent as a function argument to return a texture:

sf::Texture &sfml_resources::get_agent_sprite(const agent &a) noexcept

Taking a look into the function, only the agent_type is used:

sf::Texture &sfml_resources::get_agent_sprite(const agent &a) noexcept
{
  switch (a.get_type()) {
    // ...
  }
}

If the function only needs the agent_type of an agent, then that should be supported.

Describe the solution you'd like

sf::Texture &sfml_resources::get_agent_sprite(const agent_type t) noexcept
{
  // Most of the code here
}

Yes, functions can have identical names, as long as the compiler can figure out which one is which.

sf::Texture &sfml_resources::get_agent_sprite(const agent &a) noexcept
{
  return get_agent_sprite(a.get_type());
}

This ensures nothing will break.

There is a test written:

  //#define FIX_ISSUE_537
  #ifdef FIX_ISSUE_537
  // Can get the sprite of an agent_type
  {
    assert(resources.get_agent_sprite(agent_type::cow).getSize().x > 0);
    assert(resources.get_agent_sprite(agent_type::crocodile).getSize().x > 0);
    assert(resources.get_agent_sprite(agent_type::fish).getSize().x > 0);
    assert(resources.get_agent_sprite(agent_type::goat).getSize().x > 0);
    assert(resources.get_agent_sprite(agent_type::grass).getSize().x > 0);
    assert(resources.get_agent_sprite(agent_type::plankton).getSize().x > 0);
    assert(resources.get_agent_sprite(agent_type::tree).getSize().x > 0);
  }
  #endif // FIX_ISSUE_537

Remove the lines with preprocessor directives (#define, #ifdef and #endif).

If these compile, you did it :+1:

Describe alternatives you've considered

None.

Additional context

You should only work within sfml_resources.cpp and sfml_resources.h: this is an easy Issue :+1:

richelbilderbeek commented 5 years ago

Well done!