Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Strange behaviour when drawing convex shape figure in SFML  (Read 2111 times)

0 Members and 1 Guest are viewing this topic.

normaldeveloper

  • Newbie
  • *
  • Posts: 3
    • View Profile
Strange behaviour when drawing convex shape figure in SFML
« on: September 14, 2016, 11:06:34 am »
I'm drawing a convex shape figure using SFML library:

#include <SFML/Graphics.hpp>

using namespace sf;

int main()
{
  RenderWindow window(VideoMode(400, 400), "SFML window");

  ConvexShape convex;

  convex.setPointCount(8);
  convex.setPoint(0, sf::Vector2f(0, 0));
  convex.setPoint(1, sf::Vector2f(180, 0));
  convex.setPoint(2, sf::Vector2f(180, 90));
  convex.setPoint(3, sf::Vector2f(100, 90));
  convex.setPoint(4, sf::Vector2f(100, 180));
  convex.setPoint(5, sf::Vector2f(30, 180));
  convex.setPoint(6, sf::Vector2f(30, 90));
  convex.setPoint(7, sf::Vector2f(0, 90));
  convex.setPosition(100, 100);

  while (window.isOpen())
  {
    sf::Event event;
    while (window.pollEvent(event))
    {
      if (event.type == sf::Event::Closed)
        window.close();
    }
    window.clear();
    window.draw(convex);
    window.display();
  }
  return EXIT_SUCCESS;
}

The points defined in clockwise order and everything is ok!


But when I change two coordinates a little bit I'm getting something that I really don't expect:

#include <SFML/Graphics.hpp>

using namespace sf;

int main()
{
  RenderWindow window(VideoMode(400, 400), "SFML window");

  ConvexShape convex;

  convex.setPointCount(8);
  convex.setPoint(0, sf::Vector2f(0, 0));
  convex.setPoint(1, sf::Vector2f(180, 0));
  convex.setPoint(2, sf::Vector2f(180, 90));
  convex.setPoint(3, sf::Vector2f(100, 90));
  convex.setPoint(4, sf::Vector2f(100, 200));   // CHANGED 180 to 200
  convex.setPoint(5, sf::Vector2f(30, 200));    // CHANGED 180 to 200
  convex.setPoint(6, sf::Vector2f(30, 90));
  convex.setPoint(7, sf::Vector2f(0, 90));
  convex.setPosition(50, 50);

  while (window.isOpen())
  {
    sf::Event event;
    while (window.pollEvent(event))
    {
      if (event.type == sf::Event::Closed)
        window.close();
    }
    window.clear();
    window.draw(convex);
    window.display();
  }
  return EXIT_SUCCESS;
}

I'm getting the following window:


Why this situation take place?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Strange behaviour when drawing convex shape figure in SFML
« Reply #1 on: September 14, 2016, 11:22:24 am »
Your shape is not convex, thus you can't expect sf::ConvexShape to render it correctly.
Laurent Gomila - SFML developer

normaldeveloper

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Strange behaviour when drawing convex shape figure in SFML
« Reply #2 on: September 14, 2016, 11:32:39 am »
Your shape is not convex, thus you can't expect sf::ConvexShape to render it correctly.

I see. Thank you.