Help with lag

I'm using OpenGL to create a snake game program but, for some reason, the program is extremely laggy. I didn't notice this at first but it came apparent as I tried to add difficulties to the game. I couldn't think of any way to have to program function to same and use less processing power, so I decided to ask for help?
The code is at
Last edited on
There is a doubly nested loop that runs 209000 times in the middle of the rendering loop that looks suspect. Other than that, the use of immediate mode doesn't help, but judging by the little amount of things to be drawn it wouldn't be that big of a bottleneck.

Another culprit is the way you're setting up your "Check" function:

glutTimerFunc(0, Check, 0);

This effectively says to glut: "Call Check as often as possible". You set up the timer call back from within "Check" itself:

glutTimerFunc(Difficulty, Check, 0);

So now you have glut calling "Check" as often as possible as well as every "Difficulty" milliseconds at the least. This is a recipe for disaster.

I would suggest not hooking up "Check" to a timer but rather only call Check when the snake moves. I would also find a way to reduce that doubly-nested loop to a reasonable size.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.