Tuesday, March 23, 2010

I love POC

POC stands for Plain Old C structure. A POC contains no references nor pointers, but only primitive value types or other POCs. No C++ elements (no user defined constructor, operator, etc)

The good of POC is that it is guaranteed to be allocated within a continuous memory space, so classic memory functions such as memcpy, memset works nicely on POC or array of POCs. Additionally, you get a free assignment operator, which is quite important for data types.

POC works very good with NSData for local file storage. Just copy the memory content of a POC into NSData and call writeToFile, saving the whole byte string to the file, easily done. If it's not a POC... well good luck manual-serializing fields of an instance while saving and de-serializing them while loading, don't get the order wrong! Of course, using POC you probably need to mind the debug/release settings, for the compiler may pad within POC, causing a different size between debug and release mode. Files written by application compiled under different modes may not compatible.

Tuesday, March 16, 2010

Tips to Use Interface Builder As Much As Possible

The Interface Builder of the XCode Suite is particularly powerful. It is very handy when developing for iPhone/iPod touch. However, it comes with some difficulty when you actually want to generate and place your widget programmingly. For example: you have a scroll view with several buttons inside that align vertically, however the number of buttons and the texts on them are decided at run-time so you can't simply drag some in using Interface Builder. But neither do you want to go after all these tedious API calls to create buttons then configure formats manually either; what then?

You can actually design a "sample button" using Interface Builder, have all the formats done (which is nice and easy using IB), then put it at the root of the widget hierarchy. Since it is not a sub-view of any other widgets, it is not shown at run-time. Then you can use the following code to copy this button as you want, place it anywhere you want, without worrying the format:

NSData *archived = [NSKeyedArchiver archivedDataWithRootObject: button_sample];
//Initiate buttons based on a sample
UIButton* b = [NSKeyedUnarchiver unarchiveObjectWithData: archived];
//set text, change position...

Thursday, March 11, 2010

The Upcoming Game for iPhone

Well... bad news first. The Matrix Phone are not going to be on App Store any sooner. Apparently it is too close to the movie The Matrix thus there is a copyright issue (although I have avoided reference to The Matrix in the application description) . Maybe I would negotiate with Walt Brothers about authorization; but that would be after my next game publishes.

Ok, the next game: Smart Brick. You can think it as a 3D version of Tetris. Instead of filling an 2D plane as in Tetris, the user should fill a 3D volume using bricks that stretches on all three dimensions. It is not a fresh idea actually, there are several versions of 3D Tetris over the web; however, the key is not about the idea, it's about the control. Manipulating bricks in a 3D space, like flipping and movement in three dimensions is far more complicated than in 2D. It will leads to frustration and fun-less-ness. However, thanks to the multi-touch technology of iPhone, "3D Tetris" is made possible. Screenshots and demos will go online soon. Stick closely.

It is also planned to port Smart Brick on iPad - natively, not "stretched" up. But not until I get the product. I need to wait longer because I actually want the 3G version.

For the end of the post, a little tip for the mix C++/Objective C development:

You can have C++ classes as parameters of Objective C functions. However, Objective C does not allow namespace syntax, the "::", in the function declaration. So you need to put a namespace using at the top of your Objective C header. It's kind of nasty, and I still prefer plain C struct as means of communication between C++ and Objective C.