3
\$\begingroup\$

I just made my first game, a clone of Pong, using OpenGL and C++. But its using ~50% of the CPU, which I guess is very high for a game like this. How can I improve that? Can you please look up my code and tell me what all things I am doing wrong? Any feedback is welcome.

http://pastebin.com/L5zE3axh

http://pastebin.com/rMJ6PG9k

Also it would be extremely helpful if you give some general points on how to develop games in OpenGL efficiently..

Thanks in advance!

EDIT 1:

I forced VSync on from CCC, and now the game just shows ~5% CPU usage, except when my own wait() method is called (my fault). I can solve that...but now another problem has come, the game feels very choppy and its not smooth after forcing on VSync. Maybe tearing because of single buffering?

Any ideas?

EDIT 2:

Was calling the display callback twice from my idle function, using display() and glutPostRedisplay()! Also, changed the mode to GLUT_DOUBLE, now Fraps works on it..

One more thing! This game is working fine with VSync on (or forced) at 60fps.. but when I turn off VSync, it goes too fast. Do I have an option to turn on VSync? Or is any any other way?

\$\endgroup\$
5
  • 3
    \$\begingroup\$ Welcome to GDSE. Questions that require people to look at large pastes of code and essentially find problems for you is not a good fit for this site. When you identify the problem, you can ask the specific question and we can assist you. \$\endgroup\$ Commented Oct 22, 2012 at 11:37
  • 1
    \$\begingroup\$ Will keep that in mind! \$\endgroup\$
    – max
    Commented Oct 22, 2012 at 12:02
  • \$\begingroup\$ @JustinSkiles I just edited my question, its now more specific.. can you have a look a it please? \$\endgroup\$
    – max
    Commented Oct 22, 2012 at 20:14
  • \$\begingroup\$ hi max, your Edit2 "it goes too fast when turn off VSync" is because your gameplay depends on how fast your framerate is. see this question and in particular the Fix Your Timestep article \$\endgroup\$
    – Jimmy
    Commented Oct 22, 2012 at 22:55
  • \$\begingroup\$ @Jimmy Thank you! I solved it by rendering a frame, then Sleep(1ms) for the rest of the time, until the total time is 1000/FPS milliseconds :) \$\endgroup\$
    – max
    Commented Oct 23, 2012 at 6:12

1 Answer 1

8
\$\begingroup\$

You've implemented your own wait function that keeps the CPU running a small loop instead of letting it process other threads.

Call a sleep() method instead. It comes in various styles and flavors (depending on platform) or, if you want to keep your code portable, you could use Boost Thread e.g

 boost::this_thread::sleep( boost::posix_time::seconds(1) );
\$\endgroup\$
2
  • \$\begingroup\$ Hi, you were right. But, earlier the game was using ~50% of the CPU even on the main menu. What I did then is forced VSync on from CCC. It now gives me only ~5% of CPU usage, and goes to 50% only when my wait() is called. And a new problem has arisen, the game feels very choppy, and the flow is not smooth. What could be the reason for this? \$\endgroup\$
    – max
    Commented Oct 22, 2012 at 12:02
  • 1
    \$\begingroup\$ You can replace your wait function with the code that lithander suggested. Also, take a look at glutTimerFunc, it might solve your problem without adding another library such as boost. \$\endgroup\$
    – Asakeron
    Commented Oct 22, 2012 at 12:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .