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

Author Topic: strange freeze  (Read 1704 times)

0 Members and 1 Guest are viewing this topic.

roccio

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
strange freeze
« on: March 05, 2014, 02:58:29 pm »
Hello, I have a strange problem with a simple test program. I try to display a number of rectangles, but I noticed that a number less than 279 works well, 279 or more freeze.

#include <SFML/Graphics.hpp>

void drawrect(int x, int y, int size, sf::RenderTarget& target)
{
    sf::RectangleShape rectangle;
    rectangle.setSize(sf::Vector2f(size, size));
    rectangle.setOutlineColor(sf::Color::White);
    rectangle.setOutlineThickness(1);
    rectangle.setFillColor(sf::Color(0,0,0,0));
    rectangle.setPosition(x, y);
    target.draw(rectangle);
}
int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "TEST");
    window.setMouseCursorVisible(true);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        // draw
        window.clear();

        for (int i=0;i<279;i++)
            drawrect(i, i, 1, window);

        window.display();
    }

    return 0;
}

 

What's the problem here?

Thanks

smguyk

  • Jr. Member
  • **
  • Posts: 79
    • View Profile
Re: strange freeze
« Reply #1 on: March 05, 2014, 03:40:30 pm »
I'm no expert, but wouldn't it be better to create the rectangles once and just draw them instead of creating them every tick?

#include <SFML\Graphics.hpp>

sf::RectangleShape createRect(float x, float y, float size) {
  sf::RectangleShape rectangle;
  rectangle.setSize(sf::Vector2f(size, size));
  rectangle.setOutlineColor(sf::Color::White);
  rectangle.setOutlineThickness(1);
  rectangle.setFillColor(sf::Color(0,0,0,0));
  rectangle.setPosition(x, y);
  return rectangle;
}

int main() {
  sf::RenderWindow window(sf::VideoMode(800, 600), "TEST");
  window.setMouseCursorVisible(true);

  std::vector<sf::RectangleShape> rects;
  for (std::size_t i = 0; i < 500; i++) {
    rects.push_back(createRect((float)i, (float)i, 1));
  }

  while (window.isOpen()) {
    sf::Event event;
    while (window.pollEvent(event)) {
      if (event.type == sf::Event::Closed) {
        window.close();
      }
    }

    window.clear();

    for (std::size_t i = 0; i < rects.size(); i++) {
      window.draw(rects.at(i));
    }

    window.display();
  }

  return 0;
}

I use 500 here and it works fine for me
« Last Edit: March 05, 2014, 04:33:32 pm by smguyk »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: strange freeze
« Reply #2 on: March 05, 2014, 04:17:50 pm »
Roccio, I think it might be something at your end. I ran your code as-is and ramped the for loop up to 5000 and it's still perfectly responsive. However, I increased it to 50k and it starts to take its toll. 500k and it crashes. (just for me)

Creating and displaying so many rectangles is technically blocking your window from processing events for a long time so the OS will think it's crashed.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything