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

Author Topic: [FIXED] CPU over 30%  (Read 5215 times)

0 Members and 1 Guest are viewing this topic.

vEjEsE

  • Newbie
  • *
  • Posts: 28
    • View Profile
[FIXED] CPU over 30%
« on: July 27, 2010, 04:59:46 pm »
I have a basic SFML program running on my computer and it takes about 30% of CPU even when I don't input anything in the program.
Here's the code:
Code: [Select]

#include <SFML\Graphics.hpp>

void MoveView(const sf::Input *Input, sf::View &View);
void MoveShape(const sf::Input *Input, sf::Shape &Box);

int main() {

sf::RenderWindow cWin(sf::VideoMode(800, 600), "Camera test");
cWin.SetFramerateLimit(30);
sf::Event Event;

sf::Shape Box = sf::Shape::Rectangle(0.0f, 0.0f, 150.0f, 150.0f, sf::Color::Red);
Box.SetCenter(75.0f, 75.0f);

sf::View View(sf::Vector2f(400, 300), sf::Vector2f(400, 300));

while ( cWin.IsOpened() ) {
if ( cWin.GetEvent(Event) ) {
if ( Event.Type == sf::Event::Closed )
cWin.Close();
if ( cWin.GetInput().IsKeyDown(sf::Key::Escape) )
cWin.Close();
}

MoveShape(&cWin.GetInput(), Box);

MoveView(&cWin.GetInput(), View);
cWin.SetView(View);


cWin.Clear(sf::Color::White);

cWin.Draw(Box);

cWin.Display();
}

return 0;
}


void MoveView(const sf::Input *Input, sf::View &View) {

if ( Input->IsKeyDown(sf::Key::Left) )
View.Move(-20.0f, 0.0f);
else if ( Input->IsKeyDown(sf::Key::Right) )
View.Move(20.0f, 0.0f);

if ( Input->IsKeyDown(sf::Key::Up) )
View.Move(0.0f, -10.0f);
else if ( Input->IsKeyDown(sf::Key::Down) )
View.Move(0.0f, 10.0f);
}

void MoveShape(const sf::Input *Input, sf::Shape &Box) {

if ( Input->IsKeyDown(sf::Key::A) )
Box.Move(-10.0f, 0.0f);
else if ( Input->IsKeyDown(sf::Key::D) )
Box.Move(10.0f, 0.0f);

if ( Input->IsKeyDown(sf::Key::W) )
Box.Move(0.0f, -10.0f);
else if ( Input->IsKeyDown(sf::Key::S) )
Box.Move(0.0f, 10.0f);
}

Using SFML 1.6 build for VS2010. But it's the same with SFML 1.6 with VS2008 and C::B. Realese or debug, same thing.

Can anyone tell me why does this little program use so much CPU? I have a larger project, if I run that it never goes lower then 50%.
Even a basic program like displaying the window without rendering anything does take more then it should.
If I try to display more then 100x100 sf::Shape-s it takes about 20 seconds to update the frame.
Should it work that slow? :\

I tested it on other computers too and the result is the same.
Thanks in advance.

vEjEsE

  • Newbie
  • *
  • Posts: 28
    • View Profile
[FIXED] CPU over 30%
« Reply #1 on: July 29, 2010, 12:20:46 am »
So, no answer for this one?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
[FIXED] CPU over 30%
« Reply #2 on: July 29, 2010, 01:20:42 am »
That is strange, I can't reconstruct your behaviour on my computer. It works with almost 0% CPU workload.

Even though I don't think it's related to your problem, you should write
Code: [Select]
while ( cWin.GetEvent(Event) )instead of
Code: [Select]
if ( cWin.GetEvent(Event) )to catch all events (because there may be more than one event each frame).

Are you linking everything correctly, and did you recompile the SFML project solution? You could also try a newer SFML version (for example SFML 2 on SVN)...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

vEjEsE

  • Newbie
  • *
  • Posts: 28
    • View Profile
[FIXED] CPU over 30%
« Reply #3 on: July 29, 2010, 02:03:31 am »
I had it
Code: [Select]

while ( cWin.GetEvent(Event) )

But change it to if, I thought it might be some wierd events incoming and maybe thats while it was taking that much CPU, but it doesn't and I forgot to change it back.

I compiled the libs for VS2010, that's what I'm currently using. Tried SFML2, same thing. And although I tried everything, I'm going to try SFML 1.6 on VS2008, maybe there is something wrong with the libs for VS2010.

And on a related note:
Would a grid of 100x100 sf::Shape-s of 20x20 pixels take long to render to the screen? I mean would the program lag or the FPS drop? Example
Code: [Select]

std::vector< std::vector<sf::Shape> > Grid; // 2D array, 100x100
for ( unsigned short i = 0; i < 100; ++i ) {
    for ( unsigned short j = 0; j < 100; ++j ) {
        cWin.Draw(Grid[i][j]);
    }
}

vEjEsE

  • Newbie
  • *
  • Posts: 28
    • View Profile
[FIXED] CPU over 30%
« Reply #4 on: July 29, 2010, 02:39:58 am »
I only get linker warnings
Code: [Select]

LINK : C:\Users\VjS\Documents\Visual Studio 2008\Projects\SFML\SFML test\Debug\SFML test.exe not found or not built by the last incremental link; performing full link
sfml-system-s-d.lib(Platform.obj) : warning LNK4099: PDB 'vc90.pdb' was not found with 'C:\Program Files\Microsoft Visual Studio 10.0\SFML-1.6\lib\sfml-system-s-d.lib' or at 'c:\Users\VjS\Documents\Visual Studio 2008\Projects\SFML\SFML test\Debug\vc90.pdb'; linking object as if no debug info
...
// And it goes on for every .obj

Has this anything to do with CPU usage? I think not...

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
[FIXED] CPU over 30%
« Reply #5 on: July 29, 2010, 11:00:51 am »
As no one else is having this problem, I think it's rather a driver issue (your code is also taking about 1% CPU for me).
Want to play movies in your SFML application? Check out sfeMovie!

vEjEsE

  • Newbie
  • *
  • Posts: 28
    • View Profile
[FIXED] CPU over 30%
« Reply #6 on: July 29, 2010, 02:56:27 pm »
Quote from: "Ceylo"
As no one else is having this problem, I think it's rather a driver issue (your code is also taking about 1% CPU for me).

It actually is a driver issue!
I could not remember if I installed the graphics driver after I installed windows 7 since it works without it too. But I downloaded the driver and installed, and now it takes < 1% CPU.

Thanks for the replay xD
[FIXED]

vEjEsE

  • Newbie
  • *
  • Posts: 28
    • View Profile
[FIXED] CPU over 30%
« Reply #7 on: July 29, 2010, 03:17:34 pm »
And on a related note:
Would a grid of 100x100 sf::Shape-s of 20x20 pixels take long to render to the screen? I mean would the program lag or the FPS drop? Example
Code: [Select]

std::vector< std::vector<sf::Shape> > Grid; // 2D array, 100x100
for ( unsigned short i = 0; i < 100; ++i ) {
    for ( unsigned short j = 0; j < 100; ++j ) {
        cWin.Draw(Grid[i][j]);
    }
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[FIXED] CPU over 30%
« Reply #8 on: July 29, 2010, 03:19:57 pm »
It totally depends on your hardware.
Laurent Gomila - SFML developer

vEjEsE

  • Newbie
  • *
  • Posts: 28
    • View Profile
[FIXED] CPU over 30%
« Reply #9 on: July 29, 2010, 04:20:54 pm »
Athlon LE-1600 2.20 GHz
nVidia GeForce 7300 GT
1 GB RAM

That isn't enough? :-s

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
[FIXED] CPU over 30%
« Reply #10 on: July 29, 2010, 04:32:37 pm »
And what framerate do you get? If you haven't tested yet, that's the first thing you should do ;)
Laurent Gomila - SFML developer

vEjEsE

  • Newbie
  • *
  • Posts: 28
    • View Profile
[FIXED] CPU over 30%
« Reply #11 on: July 29, 2010, 05:00:10 pm »
I tested, it takes about 20 seconds to update the display and 100% CPU.

Is that how it should be or I'm still having a problem although I installed the driver and it seems that smaller projects work fine? :\

strongdrink

  • Newbie
  • *
  • Posts: 43
    • View Profile
    • Email
[FIXED] CPU over 30%
« Reply #12 on: March 24, 2011, 03:35:24 pm »
I think this is just an issue with sfml 1.6... on my box it says its using 100% as well