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

Author Topic: Window only refreshes with mouse movement  (Read 2368 times)

0 Members and 1 Guest are viewing this topic.

My Name is Chef

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Window only refreshes with mouse movement
« on: February 07, 2016, 10:58:58 pm »
Oi oi oi,

I've been working on a sorting program, that takes the users input and then displays the sorting process in real time. The plan is, to display every number as a bar in a bar graph and update the graph after every single sorting step. The code for the sfml-graphics window looks like this:

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

                        window.clear();

                        // Draw X- and Y-Axis
                        sf::RectangleShape xAxis(sf::Vector2f(xAxisLength, 3));
                        xAxis.setFillColor(sf::Color::White);
                        xAxis.setPosition(x0xAxis, y0xAxis);
                        sf::RectangleShape yAxis(sf::Vector2f(3, yAxisLength));
                        yAxis.setFillColor(sf::Color::White);
                        yAxis.setPosition(x0yAxis, y0yAxis);
                        window.draw(xAxis);
                        window.draw(yAxis);

                        // Draw the bars
                        int i;
                        for (i = 0; i < entries; i++)
                        {
                                sf::RectangleShape bar(sf::Vector2f(barWidth, data[i] * factor));
                                bar.setFillColor(sf::Color::Blue);
                                bar.setPosition(3 + x0yAxis + i * barWidth, y0xAxis - data[i] * factor);
                                window.draw(bar);
                        }
                        window.display();

                        // Perform one step of the sorting process on the array "data"
                        startSort(method, data, entries);

                        Sleep(500);
                }
        }

Where "data" is an array full of the users data, "entries" is the number of entries, and "method" is an identifier, that defines which sorting method is used.

The program works with one little problem: The window is only updated every 500 milliseconds while i constantly move my cursor across the graphics window. If I don't move my cursor, nothing happens at all. So not only the graphics, but the entire program appears to be frozen until I move my mouse again.

Any suggestions on how to refresh the window automatically?

Thanks in advance,

HABEYOUSEENCHEFPLS?

freedom420

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Window only refreshes with mouse movement
« Reply #1 on: February 07, 2016, 11:41:19 pm »
Check and fix your braces and it will work.
Hint: You are only rendering while you have events to process such as mouse movement.

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Window only refreshes with mouse movement
« Reply #2 on: February 08, 2016, 06:29:42 am »
    while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                        {
                                window.close();
                        }

         } // This brace must be here!!!

                        window.clear();

                        // Draw X- and Y-Axis
                        sf::RectangleShape xAxis(sf::Vector2f(xAxisLength, 3));
                        xAxis.setFillColor(sf::Color::White);
                        xAxis.setPosition(x0xAxis, y0xAxis);
                        sf::RectangleShape yAxis(sf::Vector2f(3, yAxisLength));
                        yAxis.setFillColor(sf::Color::White);
                        yAxis.setPosition(x0yAxis, y0yAxis);
                        window.draw(xAxis);
                        window.draw(yAxis);

                        // Draw the bars
                        int i;
                        for (i = 0; i < entries; i++)
                        {
                                sf::RectangleShape bar(sf::Vector2f(barWidth, data[i] * factor));
                                bar.setFillColor(sf::Color::Blue);
                                bar.setPosition(3 + x0yAxis + i * barWidth, y0xAxis - data[i] * factor);
                                window.draw(bar);
                        }
                        window.display();

                        // Perform one step of the sorting process on the array "data"
                        startSort(method, data, entries);

                        Sleep(500);
                //}This don't
        }

I would like a spanish/latin community...
Problems building for Android? Look here

My Name is Chef

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Window only refreshes with mouse movement
« Reply #3 on: February 08, 2016, 08:25:35 pm »
Ah, I see, thank you very much.

 

anything