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

Author Topic: Letting the user switch between windowed and fullscreen spikes CPU.  (Read 2866 times)

0 Members and 1 Guest are viewing this topic.

mercender

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Hi,
I'm new to SFML and a beginner with C++. I have the below code that I am interested about, because I'm really into making sure applications run as efficiently as possible.

       
while (window.isOpen())
        {
                sf::Event event;

                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        //case sf::Event::KeyReleased:
                        //      switch (event.key.code)
                        //      {
                        //      case sf::Keyboard::Return:
                        //              if (isFullscreen == true)
                        //              {
                        //              window.create(sf::VideoMode(1366, 768), "Smaller Window", sf::Style::Default);
                        //              isFullscreen = false;
                        //              }
                        //              else
                        //              {
                        //              window.create(sf::VideoMode(1920, 1080), "Bigger Window",sf::Style::Fullscreen);
                        //              isFullscreen = true;
                        //              }
                        //      }
                        //      break;
                        case sf::Event::Closed:
                                window.close();
                                break;
                        case sf::Event::MouseButtonPressed:
                                cout << "Mouse Pressed" << endl;
                                break;
                        }
                }

        }

important:


without the code that is commented above, my application runs with max cpu usage of 2% and will go to 0% at idle.

with the code that is commented above, my application runs with a sporadic cpu usage between 4 and 8%. It will not go to 0% at idle.

This is very strange to me as a beginner. I feel like it should have virtually no affect on cpu usage when the user is not changing the window resolution.

Thoughts?
« Last Edit: January 18, 2017, 04:58:25 am by mercender »

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: Letting the user switch between windowed and fullscreen spikes CPU.
« Reply #1 on: January 18, 2017, 07:44:09 am »
Well, window.create executes lots of commands, in short: window.create() is pretty expensive operation. Just looks at SFML/ source and you'll understand what I am taking about.  ;)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10818
    • View Profile
    • development blog
    • Email
Letting the user switch between windowed and fullscreen spikes CPU.
« Reply #2 on: January 18, 2017, 09:14:52 am »
The "test" doesn't matter as long as the code doesn't represent an actual application. You ndver call clear abd display. Plus you're most likely running this in debug mode, which again invalidates the result.

It's actually interesting that you get so little CPU usage, since you never call display the loop will run as fast as the CPU allows it, which should usually end up with one CPU core being utilized to the max.

Finally if you are ever worried about performance, which btw. should not be your highest priority, then use a profiler to find the bottlenecks.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

mercender

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Letting the user switch between windowed and fullscreen spikes CPU.
« Reply #3 on: January 18, 2017, 06:11:28 pm »
Well, window.create executes lots of commands, in short: window.create() is pretty expensive operation. Just looks at SFML/ source and you'll understand what I am taking about.  ;)

That doesn't make sense, the window.create() statement is not being run everytime the application is looped, not even close. Is there something I am missing?


mercender

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Letting the user switch between windowed and fullscreen spikes CPU.
« Reply #4 on: January 18, 2017, 06:16:10 pm »
The "test" doesn't matter as long as the code doesn't represent an actual application. You ndver call clear abd display. Plus you're most likely running this in debug mode, which again invalidates the result.

It's actually interesting that you get so little CPU usage, since you never call display the loop will run as fast as the CPU allows it, which should usually end up with one CPU core being utilized to the max.

Finally if you are ever worried about performance, which btw. should not be your highest priority, then use a profiler to find the bottlenecks.

Yes this is being run in debug mode, why would this code affect the cpu in debug mode? The code I showed doesn't include everything, I do have the window.clear/draw/display in the actual source code. And I have the framerate limited to 60, sorry I did not show everything, just for simplicity' sake. Hmm, I will look into the profiler. Thanks!

JayhawkZombie

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: Letting the user switch between windowed and fullscreen spikes CPU.
« Reply #5 on: January 20, 2017, 08:33:50 pm »
Yes this is being run in debug mode, why would this code affect the cpu in debug mode?
In debug mode, there is more work done underneath yours.  Generally, compilers won't try to optimize as heavily in debug mode and there are generally many more runtime checks.  You can introduce the compiler optimizations, like I'm doing in my project, but you lose access to debugging features, like pausing the program and analyzing variable values and being able to change their values at runtime - optimizations can ruin your ability to do that.

Creating the window looks simple to us in main, but underneath it's a much more hefty operation.  SFML has to communicate with the OS and initialize everything, among other operations (see - assuming you're on Windows - https://github.com/SFML/SFML/blob/master/src/SFML/Window/Win32/WindowImplWin32.cpp and https://github.com/SFML/SFML/blob/master/src/SFML/Window/Window.cpp).  You should definitely expect to see your CPU usage spike when you recreate the window.

You can enable the optimizations if you really want to squeeze out some more performance, but using 4-8% of the CPU isn't bad at all.  My engine, even when framerate limiting, sucks up like 12-15%. If I don't framerate limit, it can spike much higher, especially since it's multithreaded.

In any case, your slowdown is coming from the heft associated with recreating the window.  I'll second what eXpl0it3r said in that you shouldn't need to worry yourself about optimizations right now.

 

anything