January 2010

Warning! Some information on this page is older than 5 years now. I keep it for reference, but it probably doesn't reflect my current knowledge and beliefs.

# Doing Something Every X Time

Jan 2010

Games are one of these applications where we have a loop spinning infinitely as fast as it can (or at least 30 times per second) and calling some DoFrame function that make all the computations and rendering of a game frame. Dealing with time is an important task in game programming. One of the things game frameworks usually do are retrieving precise current time from the system and measuring FPS. Xion wrote an interesting article about all this stuff some time ago, entitled Pętla czasu rzeczywistego [pl].

By the way, a fact not so well known by beginners is that FPS (frames per second) is not good measurement of a game performance, because it is expressed in Hz which is reciprocal of average frame time and thus it is nonlinear. The real metric for how much more optimization do you have to put into your game should be average frame time in milliseconds. But that's another topic. I've blogged about this some time ago here [pl]. Others also did this, like Humus or Real-Time Rendering Blog.

Subject I want to write about today is inspired by recent question from forum.gamedev.pl - Koniec z timerami [pl]. It's about implementing timers in games. The question is how to execute some code - let's say to call DoSomething() function - every EventTime time, e.g. every 200 ms. Two solutions have been proposed there by Kos and cybek. The difference between them is suble but important.

Let's assume we have have access to variables:

Time may be expressed in seconds, milliseconds or any units - it doesn't matter here. First solutions looks like this:

// Defining helper variable
float EventCounter;
// Initialization
EventCounter = 0.f;

// Inside DoFrame, called every frame
EventCounter += DeltaTime;
while (EventCounter >= EventTime)
  EventCounter -= EventTime; // !

And here is the second example:

// Inside DoFrame, called every frame
EventCounter += DeltaTime;
if (EventCounter >= EventTime)
  EventCounter = 0.f; // !

So what's the difference? Method number 1 is precise. DoSometing will be always called every EventTime, plus/minus one frame and exactly every EventTime on average. On the other hand, method 2 calls DoSomething() not more often than every EventTime, but usually more rarely. I suppose it's EventTime + average_frame_time/2 on average. I hope you can see from this code why is that.

Why does it make a difference? First case is when you have a stall for, let's say, 500 ms between frames because you did some very expensive computations like failed A* pathfinding or some background process like antivirus slowed down the system. In the next executed frame, the first code above will "catch up" executing DoSomething two or three times, but the second code will just execute DoSomething() once and continue as nothing happened. Second difference is when you measure how many times was DoSomething() executed during some longer period. First code will probably do it exactly period/EventTime +/- 1 and the second code will result in no more, but probably less number of calls.

There is another question: How to express EventCounter variable? This problem is orthogonal to the one described above and make no difference to the code logic, so it's just your choice. Here are the options: As time passes since the last DoSomething call...

As a side note, you don't have to tell me that float is not a good type for expressing absolute and precise time inside games. I know that and I will write more about it another time. The code above was just and example.

Comments | #algorithms Share

# Overtime Concern in Game Industry

Jan 2010

For those who are not up-to-date with news from the industry: There is lots of stuff posted and commented recently about the the concern of overtime work at Rockstar San Diego. I won't comment it as I don't like controversies, I'll just show interesting links:

Comments | #game industry Share

# Processing File Paths in C++

Jan 2010

Paths to files and directories are special kind of strings. Sometimes they have to be processed, e.g. merged or splitted into some parts like drive letter, directory, file name and extension. Standard C library provides two function for this: makepath and splitpath. They use char* strings and operate always on all possible parts (drive, dir, fname, ext). In my opinion they are not very convenient. On the other hand, modern programming languages have more functionality in this field. For example, .NET has System.IO.Path static class with methods like GetExtension, GetFileName, GetDirectoryName.

To make operations on file paths convenient in C++, I've developed some time ago my own functions for that which operate on std::string strings. I've included them in my CommonLib library. They are similar a bit to these from Delphi :) Here is how I approach this topic: First, a common task is to extract part of a path (e.g. "C:\Windows\notepad.exe"). My functions for this are:

I also have some functions for processing paths:

Path can be absolute or relative. In Windows, absolute paths start with drive letter like "C:\" (which we can recognized by looking for colon) or network share (paths that begin with "\\"). Relative paths (where most basic example is a single file name) make sense only in some context. When we use relative paths to open real files, we address them relative to the "Current Directory" (which is a separate topic, I will write about it another time). To deal with absolute and relative paths, I've coded functions such as:

NormalizePath function preprocessess all the . and .. drectories in the path. A single dot used as directory name means "my own directory" (just does nothing) and two dots mean going up one level to the parent directory. So the path we consider here is logically equal to "C:\Windows\.\system32\..\notepad.exe". Paths like this still work, but they can look a bit unclear, so you can use NormalizePath function to clean them.

Paths in Linux are quite different. My functions also support them, although I didn't test this case in real situations yet. First of all, there are no drive letters in Linux, so absolute paths just start with '/', like "/etc/passwd". Second, a slash sign is used as a separator instead of backslash (it's interesting that slash also works in Windows). It's also more common in Linux than in Windows to meet files that don't have extension or have double extension (like "MyFile.tar.gz"). A question arises here whether what we call "extension" starts from the first or from the second dot :)

Comments | #algorithms #c++ #commonlib Share

# A Reminder About My Links

Jan 2010

For those who don't know yet, on my website there is more than my blog, photos and screenshots gallery and portfolio. I also maintain quite long list of links to other people's blogs, especially programmers dealing with game programming and graphics programming. So if you are interested in this subject, I encourage you to take a look. I try to keep it up to date by removing not working or inactive sites while adding new ones I find interesting. I also have my parsonal links collection called Links - gamedev.txt which I update sometiemes by addign URL-s to interesting articles and websites related to gamedev.

It may sound funny by I currently find blogs and Twitter as my most valuable source of interesting news and links. With careful choice of places you visit and people you follow it has much bigger "Signal/Noise Ratio" than any Internet forum :)

Comments | #web #homepage Share

# RegScript - Bidirectional Type Inference

Jan 2010

Coding my RegScript programming language is no longer easy as code grows bigger, but it's still much fun. In the last days I've added support for numeric types of different size. Here is the full list: float, double, uint8, uint16, uint32 (uint), uint64, int8, int16, int32 (int), int64.

I try to keep the syntax as close to C/C++ as possible, but at the same time I introduce some interesting details like:

I've also implemented function overloading and many compiler errors and warnings similar to these from C++ compiler. But most interesting feature so far is what I call "Bidirectional Type Inference" :) I first introduced auto keyword to allow skipping type name and next I've made literal constants like 123 typeless so their type is deduced from the context (because I hate typing this f, u or ll postfixes everywhere in C++ code). For example:

// Left to right - these numbers are int16
int16 myShort = -32000 + 10;
// Right to left - newVar is int16
auto newVar = myShort;

Comments | #c++ #compilers #regscript Share

# WTS Keyboard

Jan 2010

It definitely wasn't a good buy. I was dreaming about new keyboard with multimedia keys to control mp3 player instead of binding strange global hotkeys like Ctrl+Alt+Home, so I was more than happy when I chose nice looking A4-Tech X-SLIM KL-7MU model with USB, headphones and microphone extension cable. It was part of my hardware upgrade together with new processor, motherboard, monitor and more. I've also installed Windows 7 then.

It took place 1.5 months ago and I've experienced many things since then. Most things work fine but I couldn't get rid of strange feeling that coding on my new computer is less comfortable than before. I blamed it on Windows 7 and I was partially right as there is a scandalous flaw in this new Microsoft product - GUI is no longer hardware accelerated! So it's no suprise that I can often see windows and controls being drawn as I move them, scroll them or type my code, even after turning off all the animations, transparencies and even all this beautiful Aero. As a proof, look at this YouTube video: Windows 7 GUI slowness. I just feel like working under Linux before installing graphics drivers. What a shame, a big step backward...

But after comparing my experiences from work with these from home I've noticed that it has to do something with the keyboard. I've always been opposing to "flat", laptop-style keyboards and my new A4tech one is just like this. So I returned to my old-school, white keyboard. Now the GUI of Windows 7 still annoys me a bit, but coding is much better. You may tell it's the matter of taste but as for me, I'm sure now that I prefer to code on a keyboard with high "step", where I can clearly see, hear and feel whether I've pressed particular key or not. I just type faster and make less mistakes on such keyboard. I'm also sure it's not the matter of getting used to it as I tried to use laptop-style keyboard now for more than a month as well as for three months when I've been in USA and in both cases I couldn't start to like it. So now I want to sell my new great keyboard (see the auction). Anyone interested? :)

Comments | #hardware #shopping Share

[Download] [Dropbox] [pub] [Mirror] [Privacy policy]
Copyright © 2004-2020