title: Motion field display on tile map SFML
body: <p>I've tried to write a code that will draw a motion field by drawing green rectangles on a tile map everywhere where hero can go (hero can't move diagonally). It's very hard to me I have read: <a href="https://en.wikipedia.org/wiki/Taxicab_geometry" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Taxicab_geometry</a> but I still have problems.</p>
  <p>This is a class:</p>
  <pre><code>class Range : public sf::Drawable, public sf::Transformable
      {
      public:
          sf::Texture tileset;
          void load(sf::Vector2u tileSize, int* floorC, int hero, unsigned int width, unsigned int height, int speed);
      
      private:
      
          virtual void draw(sf::RenderTarget&amp; target, sf::RenderStates states) const
          {
              states.transform *= getTransform();
      
              states.texture = &amp;tileset;
      
              target.draw(m_vertices, states);
          }
      
          sf::VertexArray m_vertices;
      };
  </code></pre>
  <p>This a function:</p>
  <pre><code>void Range::load(sf::Vector2u tileSize, int* floorC, int hero, unsigned int width, unsigned int height, int speed)
      {
      int wid, hei;
      std::vector &lt;int&gt; tab;
      for (unsigned int i = 0; i &lt; width; i++)
      {
          for (unsigned int j = 0; j &lt; height; j++)//This means that the cell on left is i-1, on right i+1, up j-1, down j+1
          {
              tab.push_back(floorC[i + j * width] + 1);//every number will be higher because 0 means clear space and the class will create a tile map in clear space
              if (floorC[i + j * width] == hero)
              {
                  floorC[i + j * width] += 30;
                  wid = i;//I'm copying floorC which contains information about where are walls
                  hei = j;//I'm saving the numbers of a cell which contains a hero position
              }
          }
      }
  
  
      for (unsigned int m = 0; m &lt; speed; m++)//This loop should create left straight line
      {
          if (m == 0)//If m = 0 then 1 * k = 0 too. 
          {
              if (tab[(wid - 1) + hei * width] == 1 &amp;&amp; wid - 1 &gt; 0)
              {
                  tab[(wid - 1) + hei * width] -= 1;//Now it lower a number create clear space if number in a cell is 1
              }
              else
              {
                  m = speed;
              }
  
              for (unsigned int k = 0; k &lt; speed - m; k++)//this should create a line up from current point on the left line
              {
                  if (k == 0)
                  {
                      if (tab[(wid - 1) + (hei - 1) * width] == 1 &amp;&amp; hei - 1 &gt; 0)
                      {
                          tab[(wid - 1) + (hei - 1) * width] -= 1;
                      }
                      else
                      {
                          k = speed;//it prevent getting a negative cell number
                      }
                  }
                  else
                  {
                      if (tab[(wid - 1) + (hei - (1 * k)) * width] == 1 &amp;&amp; hei - (1 * k) &gt; 0)
                      {
                          tab[(wid - 1) + (hei - (1 * k)) * width] -= 1;
                      }
                      else
                      {
                          k = speed;
                      }
                  }
              }
              for (unsigned int l = 0; l &lt; speed - m; l++)//This loop creates a line down
              {
                  if (l == 0)
                  {
                      if (tab[(wid - 1) + (hei + 1) * width] == 1 &amp;&amp; hei + 1 &lt; height)
                      {
                          tab[(wid - 1) + (hei + 1) * width] -= 1;
                      }
                      else
                      {
                          l = speed;
                      }
                  }
                  else
                  {
                      if (tab[(wid - 1) + (hei + (1 * l)) * width] == 1 &amp;&amp; hei + (1 * l) &lt; height)
                      {
                          tab[(wid - 1) + (hei + (1 * l)) * width] -= 1;
                      }
                      else
                      {
                          l = speed;
                      }
                  }
              }
          }
          else//if m&gt;0 everything is ok
          {
              if (tab[(wid - (1 * m)) + hei * width] == 1 &amp;&amp; wid - (1 * m) &gt; 0)
              {
                  tab[(wid - (1 * m)) + hei * width] -= 1;
              }
              else
              {
                  m = speed;
              }
              //These loops are the same:
              for (unsigned int k = 0; k &lt; speed - m; k++)
              {
                  if (k == 0)
                  {
                      if (tab[(wid - (1 * m)) + (hei - 1) * width] == 1 &amp;&amp; hei - 1 &gt; 0)
                      {
                          tab[(wid - (1 * m)) + (hei - 1) * width] -= 1;
                      }
                      else
                      {
                          k = speed;
                      }
                  }
                  else
                  {
                      if (tab[(wid - (1 * m)) + (hei - (1 * k)) * width] == 1 &amp;&amp; hei - (1 * k) &gt; 0)
                      {
                          tab[(wid - (1 * m)) + (hei - (1 * k)) * width] -= 1;
                      }
                      else
                      {
                          k = speed;
                      }
                  }
              }
              for (unsigned int l = 0; l &lt; speed - m; l++)
              {
                  if (l == 0)
                  {
                      if (tab[(wid - (1 * m)) + (hei + 1) * width] == 1 &amp;&amp; hei + 1 &lt; height)
                      {
                          tab[(wid - (1 * m)) + (hei + 1) * width] -= 1;
                      }
                      else
                      {
                          l = speed;
                      }
                  }
                  else
                  {
                      if (tab[(wid - (1 * m)) + (hei + (1 * l)) * width] == 1 &amp;&amp; hei + (1 * l) &lt; height)
                      {
                          tab[(wid - (1 * m)) + (hei + (1 * l)) * width] -= 1;
                      }
                      else
                      {
                          l = speed;
                      }
                  }
              }
          }
      }
      //second loop does the same thing but on right and that two loops at the end creates lines up and down
      //sorry if my English wasn't perfect it's a foreign language for me
  
      
      
      
          for (unsigned int t = 0; t &lt; speed; t++)
          {
              if (t == 0)
              {
                  if (tab[(wid + 1) + hei * width] == 1 &amp;&amp; wid + 1 &lt; width)
                  {
                      tab[(wid + 1) + hei * width] -= 1;
                  }
                  else
                  {
                      t = speed;
                  }
      
                  for (unsigned int o = 0; o &lt; speed - t; o++)
                  {
                      if (o == 0)
                      {
                          if (tab[(wid + 1) + (hei - 1) * width] == 1 &amp;&amp; hei - 1 &gt; 0)
                          {
                              tab[(wid + 1) + (hei - 1) * width] -= 1;
                          }
                          else
                          {
                              o = speed;
                          }
                      }
                      else
                      {
                          if (tab[(wid + 1) + (hei - (1 * o)) * width] == 1 &amp;&amp; hei - (1 * o) &gt; 0)
                          {
                              tab[(wid + 1) + (hei - (1 * o)) * width] -= 1;
                          }
                          else
                          {
                              o = speed;
                          }
                      }
                  }
                  for (unsigned int p = 0; p &lt; speed - t; p++)
                  {
                      if (p == 0)
                      {
                          if (tab[(wid + 1) + (hei + 1) * width] == 1 &amp;&amp; hei + 1 &lt; height)
                          {
                              tab[(wid + 1) + (hei + 1) * width] -= 1;
                          }
                          else
                          {
                              p = speed;
                          }
                      }
                      else
                      {
                          if (tab[(wid + 1) + (hei + (1 * p)) * width] == 1 &amp;&amp; hei + (1 * p) &lt; height)
                          {
                              tab[(wid + 1) + (hei + (1 * p)) * width] -= 1;
                          }
                          else
                          {
                              p = speed;
                          }
                      }
                  }
              }
              else
              {
                  if (tab[(wid + (1 * t)) + hei * width] == 1 &amp;&amp; wid + (1 * t) &lt; width)
                  {
                      tab[(wid + (1 * t)) + hei * width] -= 1;
                  }
                  else
                  {
                      t = speed;
                  }
      
                  for (unsigned int o = 0; o &lt; speed - t; o++)
                  {
                      if (o == 0)
                      {
                          if (tab[(wid + (1 * t)) + (hei - 1) * width] == 1 &amp;&amp; hei - 1 &gt; 0)
                          {
                              tab[(wid + (1 * t)) + (hei - 1) * width] -= 1;
                          }
                          else
                          {
                              o = speed;
                          }
                      }
                      else
                      {
                          if (tab[(wid + (1 * t)) + (hei - (1 * o)) * width] == 1 &amp;&amp; hei - (1 * o) &gt; 0)
                          {
                              tab[(wid + (1 * t)) + (hei - (1 * o)) * width] -= 1;
                          }
                          else
                          {
                              o = speed;
                          }
                      }
                  }
                  for (unsigned int p = 0; p &lt; speed - t; p++)
                  {
                      if (p == 0)
                      {
                          if (tab[(wid + (1 * t)) + (hei + 1) * width] == 1 &amp;&amp; hei + 1 &lt; height)
                          {
                              tab[(wid + (1 * t)) + (hei + 1) * width] -= 1;
                          }
                          else
                          {
                              p = speed;
                          }
                      }
                      else
                      {
                          if (tab[(wid + (1 * t)) + (hei + (1 * p)) * width] == 1 &amp;&amp; hei + (1 * p) &lt; height)
                          {
                              tab[(wid + (1 * t)) + (hei + (1 * p)) * width] -= 1;
                          }
                          else
                          {
                              p = speed;
                          }
                      }
                  }
              }
          }
      
      
          for (unsigned int r = 0; r &lt; speed; r++)
          {
              if (r == 0)
              {
                  if (tab[wid + (hei - 1) * width] == 1 &amp;&amp; hei - 1 &gt; 0)
                  {
                      tab[wid + (hei - 1) * width] -= 1;
                  }
                  else
                  {
                      r = speed;
                  }
              }
              else
              {
                  if (tab[wid + (hei - (1 * r)) * width] == 1 &amp;&amp; hei - (1 * r) &gt; 0)
                  {
                      tab[wid + (hei - (1 * r)) * width] -= 1;
                  }
                  else
                  {
                      r = speed;
                  }
              }
          }
          for (unsigned int s = 0; s &lt; speed; s++)
          {
              if (s == 0)
              {
                  if (tab[wid + (hei + 1) * width] == 1 &amp;&amp; hei + 1 &lt; height)
                  {
                      tab[wid + (hei + 1) * width] -= 1;
                  }
                  else
                  {
                      s = speed;
                  }
              }
              else
              {
                  if (tab[wid + (hei + (1 * s)) * width] == 1 &amp;&amp; hei + (1 * s) &lt; height)
                  {
                      tab[wid + (hei + (1 * s)) * width] -= 1;
                  }
                  else
                  {
                      s = speed;
                  }
              }
          }
      
      
      
      
          m_vertices.setPrimitiveType(sf::Quads);
          m_vertices.resize(width * height * 4);
      
          int counter = 0;
      
          for (unsigned int g = 0; g &lt; width; g++)
          {
              for (unsigned int h = 0; h &lt; height; h++)
              {
                  int tileNumber = tab[g + h * width];
      
                  int tu = tileNumber % (tileset.getSize().x / tileSize.x);
                  int tv = tileNumber / (tileset.getSize().x / tileSize.x);
      
                  sf::Vertex* quad = &amp;m_vertices[counter * 4];
      
                  quad[0].position = sf::Vector2f((g * tileSize.x), (h * tileSize.y));
                  quad[1].position = sf::Vector2f(((g + 1) * tileSize.x), (h * tileSize.y));
                  quad[2].position = sf::Vector2f(((g + 1) * tileSize.x), ((h + 1) * tileSize.y));
                  quad[3].position = sf::Vector2f((g * tileSize.x), ((h + 1) * tileSize.y));
      
                  if (tileNumber == 0)
                  {
                      counter++;
                      quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
                      quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
                      quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
                      quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
                  }
              }
          }
          m_vertices.resize(counter * 4);
      }
  </code></pre>
  <p>And this is a code in in main:</p>
  <pre><code>    int floorCreatures[] =
  {
      2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
      2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
      2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
      2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
      2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
      2, 2, 0, 0, 0, 5, 0, 2, 2, 2,
      2, 2, 0, 0, 0, 0, 0, 0, 0, 2,
      2, 2, 0, 0, 0, 0, 0, 0, 0, 2,
  };
  
  Range move;
  move.tileset.loadFromFile(&quot;moveTile.png&quot;);
  move.load(sf::Vector2u(64, 57), floorCreatures, 5, 10, 8, 2);
  </code></pre>
  <p>If I run the code only one rectangle will appear. What I did wrong?</p>