posted by Sylpher @ 1:38pm, Saturday 2 August 2008.

I've been working my way back into C/C++ after a long trek through the wilds of Microsoft's languages. After coming to understand why Visual Basic 6 is so hated, and settling in the .NET Framework I find working in C# is quite nice. When I get a hankering to do some game code, though, I find myself ultimately coming back, like an abused puppy, to my master. The amount of documentation, libraries, engines, and examples built in C/C++ make it difficult to move past. Once you get the core ideas out of the way you can work in any language, but I figure I might as well learn them at the source and branch out from there.

I built a complete, yet simple, game loop using conio and some of the Window's libraries. Dwarf Fortress it is not, but I finally understand how to limit the game loop to run at a certain FPS. Most books and tutorials are quick to teach you how to do a loop, it is a simple but extremely important concept. I haven't seen many that follow up with letting you know that looping without a fixed amount of time or max iteration is a good way to devour the processor. This might be due to needing to use some very slightly advanced features at that point in the learning process, and a section like that is better suited later in the book, but throw a guy a warning why don't you?

Understand I am by no means an expert on the subject after building a couple examples, but the basic idea is as follows...

while (true)
        // Loop Forever

Hopefully, even if you are not familiar with programming this will make some since. While the condition in the parenthesis is true any code inside { } will execute over and over and over. In this case we have the condition to be setup as absolute true so it will never stop looping unless something special happens. We are not too concerned about the looping forever thing, what we are concerned about is that if we were to run the above example and take a look at our processor we would see something like this:

Windows Task Manager with the CPU Usage running at 57%

With a standard processor we would likely see the CPU Usage around 99-100%. Since I am running a dual core it is better suited to handle a situation like this, but 57% is still quite high for a single application to take with such a small process. The solution is to slow the loop down so it is only running at whatever interval you need it to. I will give a high level example, if you would like to try out some actual code you will need to reference the compiler and/or library you are using for how it handles time and processes.

EndTime = CurrentTime + 100;      // The current time plus 100 milliseconds
        if (CurrentTime > EndTime) EndTime = CurrentTime + 100;
        else Sleep(1);

What we are doing is checking to see if 100 milliseconds have passed. If, for example, it has only been 37ms then we will tell the application to sleep for 1ms and then check again. At this rate the loop will run around 1000 times a second, as opposed to the 100,000+ a second it was doing before. This saves a ton of load on the processor that has no reason being there in the first place. The other trick about this is that any code inside the if statement will only be processed 10 times a second. We have the timer setup at an interval of 100ms, so 1000 / 100 = 10 so any code we want to only run 10 times a second we can place inside the if statement.

This particular example is not exactly how I would structure this, but it gives a simple example on how to control these things that may seem more tricky than they really are.

Hopefully things didn't get too convoluted towards the end, and sorry to the non-programmers out there. I promise this won't be all technical rambling. I'll make sure to put plenty of regular rambling in here too!

Peace out, yo!

  • Del.icio.us
  • Digg
  • Technorati
  • Blinklist
  • Furl
  • Reddit
  • Facebook


Submit Your Comment

You are not logged in.


Flowing Malaya Skirtini -...
<strong><a href="http://www.bikinis-...
howelldickson @ 10:40am, 4 May 2015

New Arrival Toms Geometri...
<strong><a href="http://www.tomsshoe...
howelldickson @ 3:34pm, 3 May 2015

New Balance Outlet Store,...
<strong><a href="http://www.nbalance...
howelldickson @ 1:04pm, 3 May 2015

Timberland Mens 6 Inch Pr...
<strong><a href="http://www.saletimb...
howelldickson @ 2:56am, 3 May 2015

Replica Rolex Datejust II...
<strong><a href="http://www.highrepl...
howelldickson @ 2:19pm, 2 May 2015