Wednesday, August 30, 2006

Faster Build Times - Reducing Header Dependencies


Long compile times are a pain in the neck. They waste time, cause frustration, interrupt your ‘flow’ etc. They do cause a lot of programmers to learn how to juggle, which is a slight positive, but lets proceed with the assumption that long compile times are bad.

As I’ve already mentioned in the past, a lot of compilation time goes into parsing headers. Using pre-compiled headers is a good way to reduce them, but there are a couple of other ways.

You can wrap your header with the following kind of pre-processor directives:

#ifndef _MYFILENAME_H_

#define _MYFILENAME_H_

… normal header contents go here …

#endif // _MYFILENAME_H_

So what does this do? Well the defined value “_MYFILENAME_H_” is arbitrary, it’s just some unique value, the filename with some underscores is commonly used. The first time this is encountered, the parser will process the contents of the header, but also define the property. If the header gets included again (for the current cpp file it’s compiling) the value will already be defined and the contents of the header will be skipped. This is a time saving, and also avoids cyclic loops of headers.

But wait! For that trick to work, the parser still has to read the header to get to the point where the #endif is, so it still has to read the whole file.

Microsoft have an answer for this. You can put:

#pragma once

at the top of your file, and the parser will only include the file once. The parser knows that if it encounters this pragma for a second time for one cpp file, it can just stop there. This is good, but it only works on the Microsoft compiler, and so might not meet your needs. Also even though we’re preventing repeated parsing of the same file, we could still be including a lot of files.

The best thing is for us to reduce the amount of headers getting included. Most people know this, but it’s still a very common problem. A header can need to include another header for several reasons, including:

  • So a class/struct from another file can be embedded in a class.
  • So that functions defined in the header can ‘use’ another data types' functions and data.
  • So pointers to other classes/structs can be used in structures and function prototypes.

The last bullet point here is interesting. The header that’s doing the including is not needing to know the size of the other structure, or call functions related to it. In this case, we can just make a forward declaration instead of including the other header. So to embed a pointer to the class 'Foo', instead of:

#include “foo.h”

we can just say:

class Foo;

This tells the compiler “there’s a class called Foo, and all you need to know right now is that it exists and I’m going to be dealing with pointers to it”. You’ll then likely need to put the #include into one or more cpp files where the pointer gets used. This can seem like a pointless exercise at first, but it reduces the overall amount of headers which need including, by breaking the chains of headers which rely on other headers.

You can also think about the other cases – do I need to implement this function in the header, or can it be moved to the cpp, allowing me to remove the include.

If you want faster build times, turn on build timing so you can see how long your compiles take. In .NET, go to “Tools->Options->Projects and Solutions->VC++ Project Settings->Build Timing” and turn it on. Then turn on “Project->Properties->C/C++->Advanced->Show Includes” to show a list of all the included files when you compile. Then look at the build output and find header include chains you think you can break up.

It can be tricky to reduce header dependencies, it requires many large rebuilds of your project. But assuming you can find some optimisations, you get paid back in shorter compile times and the benefits they bring.

Friday, August 25, 2006

Getting a games programming job - Tip 7 - Practice Answering Interview Questions

During a phone interview, programming test, and interview, the chances are that you’re going to get asked a load of questions. Many of the questions you’ll be asked are general programming questions, perhaps dressed up with a game related theme. Doing a quick search for “programming interview questions yields a bunch sites which round up the types of questions you get asked.

Reading and answering a few questions probably isn’t going to suddenly make the interview process a cakewalk, but it can show you some areas to practice and can give you an idea of the type of questions you’re going to be asked. Beyond standard interview questions, in a games interview you’ll likely also get asked questions about 3D math, and lower level operations like memory allocation, pointers, bitwise operations, and hardware and operating system concepts.

One more thing. It's easy to read a question, read the answer, and fool yourself into thinking you'd be great at answering. Put a bit more effort into it, write your answer out, talk out loud as if you were talking to an interviewer. You might find that the answer is harder to write down than you think, or that you've forgotten your impressive vocabulary of cromulent words. Another good idea is to code it up and make it work - if you've not written a linked list class in a while, an interview is a pretty rough place to re-learn it. This is why I recommend working on demos, and keeping on coding while you go through the interview process.

Thursday, August 17, 2006

Getting a games programming job - Tip 6 - Submitting Your Resume

The first stage in your application process is sending in your resume and cover letter. Here are a few of my tips, mainly aimed at people new to the industry:

  • It doesn’t hurt to personalize your application by mentioning the name of the company, and saying why you’d like to work with them in particular. If I read ‘I liked playing ‘The Incredibles’ and would like to work on similar titles’ then that makes the person stand out. It shows they’ve researched the company, and are particularly interested in it. It’s the difference between getting junk mail that says Dear Sir, and Dear .
  • If you’ve been doing little demo projects like I suggest, mention it, and give a link to your web page. Being able to quickly show someone what you can do can make a massive difference.
  • I like it when the application includes a cover letter, and perhaps a note about hobbies/interests. It gives a chance for me to see the applicant as a person, and not just a completed University course. Expressing why you want to work in games, and the fact that you’re crazy about playing the Sax lets your personality show, and makes you memorable. You become ‘that Sax guy’. It also shows your communication skills. The ability to communicate well is important in almost any job.
  • Be honest about your abilities. If you get an interview, and people find that you’ve been exaggerating in some area, then it casts doubt on everything you’ve put. Don’t undersell yourself either, as you might not get called.
  • If you’re proud of your GPA, don’t forget to put it.
  • Format what you’re sending nicely. Check the spelling and grammar.
  • If you’re coming from an educational establishment, then they likely have a group who will check your resume for you. Make sure you use this. If you don’t have this kind of service available, then find one or two people who can proof read it for you.
  • It’s best to apply directly to the company instead of using a recruiter. Using a recruiter would be free to you, but would usually cost the company several thousand dollars to hire you. You’re saving the company money by applying directly. Another thing is that sometimes your resume gets mangled down to a badly formatted txt file, which can give a bad impression.
  • At some companies, the online job application database, or centralized corporate HR department can be a bit of a quagmire that applications get stuck in. Whenever possible I try and avoid this route by contacting the studio directly, or finding a contact there through a friend of a friend.
  • While you’re going through the interview process, keep on coding for fun. It’ll keep your skills nice and sharp, and you also wont have much time for your own coding once you get a job.
Hopefully, with your resume in good order, you'll soon be contacted and then move onto the next stage. As it's videogames, you have to complete three stages and then you face a big angry boss.

Oh come on, that was a good one...

Monday, August 14, 2006

Homebrew Xbox 360

In the news today.

"Microsoft Corp. on Monday will announce the availability of software tools aimed at encouraging independent and hobbyist video game makers to create titles for its new Xbox 360 console."

"A test version of the tool kit will be out by Aug. 30. The final product will be available this holiday season for an annual subscription of $99 per year for Xbox 360 game development. The software will be free to people making games to run on Windows."

I think this is great news. It really opens the door to people wanting to get into the industry, to creative new game ideas, indie developers, all sorts...