2

I guess I have some explaining to do:

  • I'm fairly new to game programming, so please don't get mad if I don't understand a concept immediately
  • The game makes use of DirectX 10 and is written in C++
  • It's very simple 2D game

The situation: Despite of being very simple in both game logic and graphics, it still takes my CPU and GPU load to 100%. Even the menu is displayed with more than 2000 frames per second.

My problem is not that the game runs too fast. I already timed sprite animations and game logic using the QueryPerformanceCounter function.

The actual problem is that the game calculates the same code numerous times without anything happening on the screen, therefore putting a massive load on my hardware.

In what ways can I decrease the hardware load of my game? I feel like using Sleep is "cheating".

  • What is the problem with your hardware running at full steam if it actually has that much processing to do? You could cap your framerate, as you suggest. You could simply turn on vsync... – Bart Jun 05 '12 at 10:20
  • I don't know how to do that that. The main problem is that it runs through the whole game loop more than 2000 times a second, while only 60 times would be sufficient. – user1437050 Jun 05 '12 at 10:23
  • 2
    See [here](http://msdn.microsoft.com/en-us/library/windows/desktop/bb172585%28v=vs.85%29.aspx) and [here](http://msdn.microsoft.com/en-us/library/windows/desktop/bb172588%28v=vs.85%29.aspx). `D3DPRESENT_INTERVAL_ONE` is what you want. Unless your monitor has a refresh rate of 2000 Hz (rather unlikely) this will cut down your framerate to something sensible. Except, if you have set "force vsync off" in your driver settings (then the application is without luck). – Damon Jun 05 '12 at 10:24
  • 1
    As for using `Sleep`, it isn't "cheating", but it's unwise. `Sleep` is unreliable and its default resolution is way too coarse for any application that wants to display something at interactive rates. – Damon Jun 05 '12 at 10:31
  • You pointed me in the right direction Damon! =) I hope there's some way to upvote here. I'll post the solution further down. – user1437050 Jun 05 '12 at 10:49

2 Answers2

2

Thank to Damon for pointing me in the right direction, I looked into the present function. (http://msdn.microsoft.com/en-us/library/windows/desktop/bb174576(v=vs.85).aspx)

All it took to solve both CPU and GPU load problems was changing

swapChain->Present(0, 0);

to

swapChain->Present(1, 0);
0

Just a quick suggestion:

Everytime you enter the game loop calculate the time passed since the last time you entered. If this time is below a given threshold just return without processing anything.

tobsen
  • 5,328
  • 3
  • 34
  • 51
  • Thank to Damon for pointing me in the right direction, I looked into the present function. () All it took to solve both CPU and GPU load problems was changing swapChain->Present(0, 0); to swapChain->Present(1, 0); – user1437050 Jun 05 '12 at 10:52
  • Maybe you should add an answer yourself (some sample code might be nice) and accept your own answer to indicate that your question has been answered. – tobsen Jun 06 '12 at 08:20