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

Author Topic: [Solved] SFML builds are eating up way too much CPU  (Read 943 times)

0 Members and 1 Guest are viewing this topic.

CasualCollision

  • Newbie
  • *
  • Posts: 2
    • View Profile
[Solved] SFML builds are eating up way too much CPU
« on: April 14, 2018, 04:03:04 am »
So for whatever reason, any program I write, no matter how simple or complex, is a complete CPU hog. It consistently uses up 50% of my CPU or more. I've googled this problem, and it seems to be a common one, but none of the solutions suggested in those threads worked.

To recap, I'm using Visual Studio 2013 (I tried to configure it to work with Code::Blocks, but I couldn't for the life of me get it to work, so I opted for VS instead). I'm using a dynamic linker rather than static, simply because that's the one that allows me to build the project. I have tried both setFramerateLimit and setVerticalSyncEnabled, with it having no effect on performance. I thought that perhaps it was my graphics driver, so I tried updating it. Granted, my laptop is quite old, and my drivers haven't been officially supported for a few years, but I tried nonetheless. Alas, that had no effect either. Lastly, I tried building SFML from the source code rather than using the VS2013 package. Not even that seemed to work.

I run Windows 7 Ultimate, Service Pack 1
My processor is an AMD A4-3320M APU with Radeon HD Graphics 2.00 GHz (It sucks I know, but it should be able to handle something like this regardless)
8GB of RAM
64-bit Operating system

All of my other drivers are up to date, to my knowledge.

Finally, just for safe measure, here's my code:
#include <SFML/Window.hpp>
#include <iostream>
using namespace sf;

int main()
{

    //Window 1
    Window window1(VideoMode(800, 600), "Hello, World!");
    window1.setFramerateLimit(30); // call it once, after creating the window
    window1.setKeyRepeatEnabled(false); // makes it so that holding down a key doesn't repeat the action rapidly

    //Main loop
    while (window1.isOpen())
    {
        //Check all of the window's events that were triggered since the last iteration of the loop
        Event event1;

        while (window1.pollEvent(event1))
        {
            switch (event1.type)
            {
                case Event::Closed:
                {
                    window1.close();
                    break;
                }

                case Event::KeyPressed:
                {

                    std::cout << "Formless, ";

                    switch (event1.key.code)
                    {
                        case Keyboard::Up:
                        {
                            std::cout << "you are moving up ";
                            break;
                        }

                        case Keyboard::Left:
                        {
                            std::cout << "you are moving left ";
                            break;
                        }

                        case Keyboard::Down:
                        {
                            std::cout << "you are moving down ";
                            break;
                        }

                        case Keyboard::Right:
                        {
                            std::cout << "you are moving right ";
                            break;
                        }

                    }

                    std::cout << "in an endless void!\n";
                    break;

                }

                case Event::KeyReleased:
                {
                    switch (event1.key.code)
                    {
                        case Keyboard::Up:
                        {
                            std::cout << "You have stopped moving up.\n";
                            break;
                        }

                        case Keyboard::Left:
                        {
                            std::cout << "You have stopped moving left.\n";
                            break;
                        }

                        case Keyboard::Down:
                        {
                            std::cout << "You have stopped moving down.\n";
                            break;
                        }

                        case Keyboard::Right:
                        {
                            std::cout << "You have stopped moving right.\n";
                            break;
                        }
                    }
                    break;
                }

                default:
                {
                break;
                }
            }
        }
    }

    return 0;
}
 

At first I thought it might have been simply the nature of SFML, since the example programs provided in the package seems to give me the same result. However, when I tried a simple game I downloaded that was written in SFML, any CPU-hogging was absent, so I honestly don't know what gives.

EDIT: Managed to get it working with Code::Blocks. Didn't make any difference whatsoever.
« Last Edit: April 15, 2018, 03:54:39 am by CasualCollision »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10848
    • View Profile
    • development blog
    • Email
Re: SFML builds are eating up way too much CPU
« Reply #1 on: April 14, 2018, 08:47:16 am »
You're not calling clear and display, as such your event loop runs as fast as possible and thus consums one whole core.

window.display() is the function that will call sf::sleep if you set a frame rate limit and it's also there where the application will wait if you enabled VSync.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

CasualCollision

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: SFML builds are eating up way too much CPU
« Reply #2 on: April 15, 2018, 03:54:23 am »
Ah, I somehow missed that when walking myself through the documentation. Thanks for clearing that up for me. The program seems to be working fine now. Thanks!  :)