Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
3  
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. –  Justin Skiles Oct 22 '12 at 11:37
1  
Will keep that in mind! –  Max Oct 22 '12 at 12:02
    
@JustinSkiles I just edited my question, its now more specific.. can you have a look a it please? –  Max Oct 22 '12 at 20:14
    
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 –  Jimmy Oct 22 '12 at 22:55
    
@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 :) –  Max Oct 23 '12 at 6:12
add comment

1 Answer 1

up vote 7 down vote accepted

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) );
share|improve this answer
    
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? –  Max Oct 22 '12 at 12:02
1  
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. –  Asakeron Oct 22 '12 at 12:10
    
Solved it by rendering a frame, then Sleep(1ms) for the rest of the time, until the total time is 1000/FPS milliseconds.. thank you! –  Max Oct 23 '12 at 6:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.