Building a Game Engine with Cocoa, Part 2
Date:   2007-01-10 05:17:24
From:   ptwobrussell

Great question, thanks for asking. One other person e-mailed me about this wondering the same thing.

The reason I chose to go with a macro is because in the next piece, we're going to refactor from using NSPoints for identifying spots on the board to using a custom struct that holds two integer values. I went ahead and introduced the macros to facilitate this process a bit.

Why do this at all? Because we'll be inspecting the contents of the board[][] array millions of times during our game tree search, and the cost of casting the floats contained in NSPoints to int values that can index the array is expensive. The cost of casting integer values to floats whenever we do drawing, however, is not so significant since there's going to be a very little bit of drawing going on compared the amount of searching and manipulating board[][] (not to mention that the code reads cleaner when you don't have a bunch of casts cluttering things up.) Even now, there's very little drawing going on, and it's certainly not a performance factor.

Your follow on question might be why I didn't just do this in the first place. No great answer here aside from the fact that this enhancement didn't seem obvious to me until after the first piece in this series had been published and I got more focused on thinking about the search that's coming up.