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

Author Topic: Very High CPU Usage.  (Read 6728 times)

0 Members and 1 Guest are viewing this topic.

MW2TopTenWORLD

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Very High CPU Usage.
« on: August 19, 2014, 12:05:23 am »
Hello, I am currently working on a game with SFML (2.1), I am using Visual C++ 2010.
Just to start my computer is not the best, maybe not even close :P however this is way too exagerated as all I have done so far is a little bit of the main menu and it uses 56% of the CPU... And However I can play Call of Duty : Modern Warfare 2 (which I Suppose it's heavier then a little bit of the main menu of my game...) and it only uses about 36%...

So yeah, I Do not know where the problem is... I have tried many things, I have tried not loading an intro video (I Am Using SfTheora 1.4 to do so) by removing all of the SfTheora calls and everything associated to it, I had one multi-thread call which was just scrolling text around the screen , I tried removing it as well, I tried removing a function (void) which was cycling the colors of a text, I tried commenting out the WHOLE events  code block ... I tried removing vSync, I tried using framerate limit, I tried who knows what! I went from every part of my code, changing things... NOTHING, The CPU just keeps at that level pretty much :( With a thing THIS small (so far) I guess it shouldn't even be on 1% CPU... MAX I would say like 4%... So... Why does it stay like that?

Please Make Any Suggestions and tell me if you need anything to help me out :(



Thanks :)

PsichiX

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Very High CPU Usage.
« Reply #1 on: August 19, 2014, 12:11:59 am »
and you have implemented fixed step or fps limit? because if not, main loop will draw every frame so it will consume whole available processing time.

MW2TopTenWORLD

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Re: Very High CPU Usage.
« Reply #2 on: August 19, 2014, 12:13:32 am »
I think so? I used window.setFramerateLimit(60); (I also tried other values)

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Very High CPU Usage.
« Reply #3 on: August 19, 2014, 12:13:39 am »
Are you testing with a release build (optimization enabled)? If not then do that ;)
A debug build will usually have all optimizations disabled, to make debugging easier, which means it will perform poorly. It will usually also have extra assertions and other sanity checks enabled which makes it slower still.
Also, if you are running the program via a debugger it can be a tiny bit slower still.

MW2TopTenWORLD

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Re: Very High CPU Usage.
« Reply #4 on: August 19, 2014, 12:15:44 am »
Nope, I have the build set as Release and I open it with "Debug->Build Solution (F7)" I wait untill it says "Build Completed" down there, then I go to Visual Studio 2010/Projects/Project Name/Release/.exe here <- and I open it

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Very High CPU Usage.
« Reply #5 on: August 19, 2014, 12:28:13 am »
Could you provide a complete and minimal example that reproduces the problem, so that we can test it?

Is your SFML pre-built or custom compiled?
Have you tried the latest master branch from GitHub?
« Last Edit: August 19, 2014, 12:30:23 am by Jesper Juhl »

MW2TopTenWORLD

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Re: Very High CPU Usage.
« Reply #6 on: August 19, 2014, 12:35:16 am »
Alright I'll put a minimal example here...

I am using SFML 2.1 and I got it from SFML site -> Download -> 2.1 -> Windows -> Visual Studio 2010 10 (I didn't recompiled with CMAKE or whatever, just straight from the site...)

So I guess this example is ok? :

#include <sfTheora.h>
#include <SFML/Audio.hpp>
#include <SFML/Config.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Network.hpp>
#include <SFML/OpenGL.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include "textCreator.h"
#include "mainScreen.h"
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <string>
#include "variables.h"
#include "Funcs.h"

using namespace std;

int main()
{
        sf::ContextSettings settings;
        settings.antialiasingLevel = 8;
        sf::RenderWindow window(sf::VideoMode(fullscreen.width, fullscreen.height), "SFML Project", sf::Style::Fullscreen, settings);
        sf::Clock clock;

//      window.setVerticalSyncEnabled(true); <- I removed it because it wasnt doing anything and yes I tried //frame rate limit as well!
       
        mainFont.loadFromFile("Fonts\\sansation.ttf"); //mainFont is located on file variables.h

        sf::Text pressENT;
        pressENT.setColor(sf::Color::White);
        pressENT.setFont(mainFont);
        pressENT.setString("Press [ENTER] to Skip this Video");
        pressENT.setPosition(fullscreen.width-(fullscreen.width*0.65), fullscreen.height-(fullscreen.height*0.10));

        sftheora::Video introVideo("intro.ogg");

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
          //... normal event codes here + a few ones like mouse clicks, F12 etc that I inserted
                               
        }

                introVideo.update(clock.restart());

        window.clear();
                if(introVideo.isDone() == false)
                {
                        window.draw(introVideo);
                        window.draw(pressENT);
                }
                if(introVideo.isDone() == true)
                {
                        if(isMainScreenOn==true)
                        {
                                mainScreen::drawMainScreen(&window); //mainScreen.h / mainScreen.cpp... it's basically //a collection of items that are drawn if you want I can post that as well?
                        }
                }
        window.display();
    }
    return EXIT_SUCCESS;
}

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: Very High CPU Usage.
« Reply #7 on: August 19, 2014, 03:16:39 am »
you should try implementing a fixed timstep, link here for you: http://gafferongames.com/game-physics/fix-your-timestep/

with just a simple timestep implemented, I'm dropped to ~13% cpu in my own game (down from about 98%)

then I added window.setFramerateLimit(60); which dropped me to below 1% cpu.

MW2TopTenWORLD

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Re: Very High CPU Usage.
« Reply #8 on: August 19, 2014, 11:57:10 pm »
Please someone post more ideas ;(

MadMartin

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Very High CPU Usage.
« Reply #9 on: August 20, 2014, 11:19:08 am »
Why don't you just try the method Gobbles suggested instead of asking for more?

Asides, the code you showed will exploit every single bit of CPU time it gets. Why is that a problem for you? Seems to look like premature optimization...

MW2TopTenWORLD

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Re: Very High CPU Usage.
« Reply #10 on: August 20, 2014, 07:58:46 pm »
I am going to try what Gobbles suggested, just want more suggestions in case his dont work... And I don't think getting half full CPU is good? with such a little thing so far.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Very High CPU Usage.
« Reply #11 on: August 20, 2014, 08:04:03 pm »
If you don't do anything to limit CPU use (like setFramerateLimit/vsync) and your game loop just renders frames as fast as it can, then you'll eat up 100% CPU. Why would you expect anything else?
(The reason it is 50% in your case, I guess, is due to you having a dual-core CPU or a single core with hyperthreading)
« Last Edit: August 20, 2014, 08:12:29 pm by Jesper Juhl »

MW2TopTenWORLD

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Re: Very High CPU Usage.
« Reply #12 on: August 20, 2014, 08:24:11 pm »
Ok I Figured it out what is going on now can someone help me figure it out???
Well I downloaded Fraps to benchmark my game and either with or without window.SetFramerateLimit(60) / window.SetVirtualSync(enabled) <- something close  to that , dont remember the syntax atm... fraps only counts about 4... 5 fps!!!!!! so he is not maxing out the fps, also btw Fraps is not recording the game???? Just a black screen and my mouse moving around. Please Help :)

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Very High CPU Usage.
« Reply #13 on: August 20, 2014, 08:29:34 pm »
Don't use an external program like fraps to measure fps. It influences your program by capturing frames and other stuff.
Just calculate fps yourself in your app - it's trivial; fps is "frames per seconds" which is just the number of display() calls you make each second. So just keep track of the number of display() calls you make and the time that has passed and then print out (every 2-5 seconds or so) the frames rendered pr second to std::out or std::cerr.

MW2TopTenWORLD

  • Newbie
  • *
  • Posts: 23
    • View Profile
    • Email
Re: Very High CPU Usage.
« Reply #14 on: August 26, 2014, 08:07:50 pm »
SOLVED
SOLVED
SOLVED!

All I had to do was update my video driver!
I Don't know why but it was "fucking" with SFML :P... And I actually found out that the game is so fast that my scrolling message just flies through the screen hehe... gotta fix that.

Well this was my problem... graphics card driver.
Hopefully its the same for others (if they have this problem)

 

anything