# Thread: Handling logic and draw cycles

1. ## Handling logic and draw cycles

So a friend of mine and I are working on a game project (and have been for a few months now) based on the little army men figurines that many of us have probably played with before as kids (or as adults!).

If you don't know what I am talking about, check out this image I found doing a quick search on Google Images:

http://ecx.images-amazon.com/images/...500_AA280_.jpg

Anyways...

We have got quite a lot of the back end done, but we were not happy with the performance we were getting. Of course...we initially hadn't coded our back end for performance, but for functionality. Now, having gotten an acceptable amount of functionality in, I went back and revamped a lot of things to increase performance. The most notable - and obvious - change was to simply use display lists for rendering our models on the screen every frame. Initially we weren't using them. After the implementation of display lists, our frame rate went from roughly 45 fps up to 150 fps.

Now, after I implemented that, I wanted to revamp how we were handling the actual drawing and logic in the game. Up until this point, we just had a simple loop that spun around and tried to do logic and drawing every single frame as fast as possible, as I am sure you have all seen and done before, like this:

Code:
```while ( MessageLoop ( event ) )
{
TickClock ( );
RunLogic();
DrawObjects();
}```
I wanted to make it so that everything gets drawn a fixed number of times per second, and so that the logic happens a fixed number of times per second, ...since that is the proper way to do things anyways..., and it makes the game less susceptible to differences in performance of two different computers.

So I edited the TickClock() function to let me know at certain intervals when I should do the draw phase and when I should do the logic phase. I interlace the two, and never do them at the same time. Now it looks like this:

Code:
```while ( MessageLoop ( event ) )
{
TickClock ( );
if ( logicFrame ) RunLogic();
if ( drawFrame ) DrawObjects();
}```
logicFrame and drawFrame will each be true every 8/1000 of a second, but they switch off, so each one runs roughly 65 times a second right now.

Anyways, so here are my stats:

Before doing anything (including display lists): everything at about 45 fps

After display lists: everything at 150 fps

The main loops runs between 10000 and 20000 times per second.
Logic and graphics each run very consistently 65 times per second.

I was wondering if you have any suggestions on my implementation of things, particularly how I have done my timer. Here is the code for the actual TickClock() function:

Code:
```void SIMULATION::TickClock ( void )
{
static int waitTime = 8;
static int prevSecond = clock();
static int prevDrawFrame = clock();
static int prevLogicFrame = prevDrawFrame + (waitTime / 2);
int newTime = clock();

loopFrameCount++;
drawFrame = false;
logicFrame = false;

if ( newTime > (prevDrawFrame + waitTime) )
{
drawFrame = true;
prevDrawFrame = newTime;
}

if ( newTime > (prevLogicFrame + waitTime) )
{
logicFrame = true;
prevLogicFrame = newTime;
}

if ( newTime > (prevSecond + CLOCKS_PER_SEC) )
{
prevSecond = newTime;

char loopFrameCstr[10];
char graphicsFrameCstr[10];
char logicFrameCstr[10];

sprintf_s(loopFrameCstr, "%d", loopFrameCount);
loopFrameString = "Clock Frames: " + string(loopFrameCstr);

sprintf_s(graphicsFrameCstr, "%d", graphicsFrameCount);
graphicsFrameString = "Graphics Frames: " + string(graphicsFrameCstr);

sprintf_s(logicFrameCstr, "%d", logicFrameCount);
logicFrameString = "Logic Frames: " + string(logicFrameCstr);

loopFrameCount = 0;
graphicsFrameCount = 0;
logicFrameCount = 0;
}
}```
Note that graphicsFrameCount and logicFrameCount are incremented in their respective functions: RunLogic() and DrawObjects().

Does this implementation seem solid to you guys or would you have any suggestions on things to fix or improve?

2. I wanted to make it so that everything gets drawn a fixed number of times per second, and so that the logic happens a fixed number of times per second, ...since that is the proper way to do things anyways..., and it makes the game less susceptible to differences in performance of two different computers.
It is? The only time you would need to cap the framerate manually is if you had a physics system. Other than that vsync will cap it for you quite nicely. If you do have a physics system you must keep consistent deltas or your equations will get odd results.

Just compute the frame delta and pass that into your update function to update your game world. Then draw.

Code:
```...
QueryPerformanceCounter(&m_nowTime);