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

Author Topic: Major lag when a C++ statement is removed  (Read 3023 times)

0 Members and 1 Guest are viewing this topic.

thatoneguy

  • Newbie
  • *
  • Posts: 25
    • View Profile
Major lag when a C++ statement is removed
« on: March 04, 2014, 10:42:40 am »
I have the style of the window set to none, and this snippet allow the user to drag the window when the mouse is pressed.

void TitleBar::HandleDrag(sf::RenderWindow *renderWindow) {
    dragging = true;
    while (dragging) {
        lastMousePosition = sf::Vector2i(sf::Mouse::getPosition(*renderWindow).x, sf::Mouse::getPosition(*renderWindow).y);
        if (Utilities::AABBCollision(float(lastMousePosition.x), float(lastMousePosition.y), float(lastMousePosition.x), float(lastMousePosition.y),
                                     float(area.left), float(area.top), float(area.left + area.width), float(area.top + area.height)) && draggable) {
            //std::cout << "Within area" << std::endl;
            int offsetX = lastMousePosition.x - sf::Mouse::getPosition(*renderWindow).x;
            int offsetY = lastMousePosition.y - sf::Mouse::getPosition(*renderWindow).y;
            renderWindow->setPosition(sf::Vector2i(renderWindow->getPosition().x - offsetX, renderWindow->getPosition().y - offsetY));
            sf::Mouse::setPosition(lastMousePosition, *renderWindow);
            if (!sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) {
                dragging = false;
            }
        }
    }
}

The code has CPU usage for the process spike up to the high 20's and the window stutters when dragging, but when I uncomment the console output statement the CPU usage for the process is about 6% and dragging works as intended. The length of the string that the console outputs also seems to impact performance. The shorter the length of the string the more lag there is.

I'll work on a minimal example tomorrow morning, but for now I hope someone can find a reason why.

MadMartin

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Major lag when a C++ statement is removed
« Reply #1 on: March 04, 2014, 10:50:47 am »
Console output is always slow and laggy, compared to the rest  ;)
Why do you need it?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Major lag when a C++ statement is removed
« Reply #2 on: March 04, 2014, 10:52:34 am »
You're effectively creating an infinite loop, which by itself could/should already eat up a full CPU core. But it doesn't seem to happen on your end, unless you add the std::cout, which takes up a bit more to process, thus increasing the CPU usage.

Your while loop is really not a good idea. You should halt your whole application that way, instead everything should run as usual or maybe pause it, while you drag the window. Thus you'll need to write a function which does the same thing, but that will get called in the update process of your main loop.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

thatoneguy

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Major lag when a C++ statement is removed
« Reply #3 on: March 04, 2014, 10:56:04 am »
Console output is always slow and laggy, compared to the rest  ;)
Why do you need it?

The console output was there for debugging purposes, but the weird thing is that it lags without the console output! I can't figure out why that is.

According to a performance analysis. Calling getPosition() for the window and mouse take up most of the process when the console output is commented out.

The Terminator

  • Full Member
  • ***
  • Posts: 224
  • Windows and Mac C++ Developer
    • View Profile
Major lag when a C++ statement is removed
« Reply #4 on: March 04, 2014, 01:29:09 pm »
eXpl0it3r just pointed out your issue. Your while loop is blocking anything thing else from occurring in your program. The while loop also has a std::cout statement, which is notoriously slow.
Current Projects:
Technoport

thatoneguy

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Major lag when a C++ statement is removed
« Reply #5 on: March 05, 2014, 12:10:51 am »
You're effectively creating an infinite loop, which by itself could/should already eat up a full CPU core. But it doesn't seem to happen on your end, unless you add the std::cout, which takes up a bit more to process, thus increasing the CPU usage.

Your while loop is really not a good idea. You should halt your whole application that way, instead everything should run as usual or maybe pause it, while you drag the window. Thus you'll need to write a function which does the same thing, but that will get called in the update process of your main loop.

This function is called in the main loop when I poll an event and the mouse is being pressed. I'm still trying to wrap my head around this. I understand that the infinite loop will take up a lot of CPU usuage and run as fast as it can, but the std::cout is actually slowing down that infinite loop?

Tuffywub

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: Major lag when a C++ statement is removed
« Reply #6 on: March 10, 2014, 12:16:49 am »
When you call std::cout, the program will have to wait to write the output to the console. This is often a very slow process, so it will take longer for the program to complete every loop cycle. This is why it is a lot slower.

As far as the getPosition() functions taking a long time, I would only call each of them once, and then store the result. This will mean less calls to get the position of the mouse and window, which I expect are somewhat expensive if called very frequently.

Overall though, I think that this dragging process should be handled within the main game loop using events. Currently, this function will block until the mouse has been finished dragging, meaning that you cannot redraw the window while its being dragged. Implementing this within the loop will be much simpler with the use of events too.