Wednesday, April 7, 2010

Using short value in vertex array instead of float

I've been trying to squeeze more FPS from the rendering part of Smart Brick. I came across an article which suggests using GL_SHORT instead of GL_FLOAT in vertex array to boost performance:

http://stackoverflow.com/questions/1762866/using-gl-short-instead-of-gl-float-in-an-opengl-es-vertex-array

The solution quite makes sense. Basically you use short value to replace float values; to compensate the precision loss, a pair of additional glScalef and glTranslatef is required per-model.

A reply attached at the end of the post suggests that on iPhone it would be 30% faster. However, this is not the case for Smart Brick; not because the article is wrong, but I think the performance increment depends on actual scenarios. Here is my guess:

If your model contains large amounts of vertices the solution may help a lot. You get big computational savings for using short instead of float, with the price of only one additional glScale and glTranslate calls respectively.

However, it does not help Smart Brick, because Smart Brick renders large amounts of models, each of them contains only few vertices. This way, the savings on float vertices are probably offset by glScale and glTranslate calls, because they are called once per-model, and they are called a lot when there are a lot of models.

Monday, April 5, 2010

Smark Brick Game Play

This is a demo showing 2D mode in Smart Brick. Well, 2D is a special case of 3D after all.


The true 3D mode in Smart Brick.

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.

Sunday, November 29, 2009

Mix C++ code with Objective C for iPhone

Objective C is not the only language you can use to program for iPhone; you can use C/C++ and mix the C/C++ code with your Objective C code. And I recommend to do so:

Reason 1: for cross-platform purposes. If you use only Objective C then you are locked on the Apple platform. Don't give up the chance of porting your program to other devices such as Zune HD.

Reason 2: for the better C++ standard STL library. IMHO the STL library is much better than Objective C Cocoa libraries; it is portable and easier to use. Especially the container part.

To those who are worried, iphone run-time have full support for C++, including virtual function, exception handling and RTTI. And Xcode does support template.

There is no explicit boundary between C++ and Objective C when you code for iphone. You can write Objective C code in C++ classes/functions, and vice versa. But notice that the source code file other than headers should be named as ".mm".

Althrough you can mix C++ and Objective C, a good idea is to separate them for better portability and maintainability. When comes to a problem that both C++ and Objective C have solutions, I prefer to write the C++ code when possible and only write Objective C code where Cocoa library provides an easier solution, or when only it has the solution. For example, the Cocoa container classes suck but they do provide very easy to use persistance functions; so I use STL container and only translate the content to the Cocoa counter part when I need to save the content to a file.

A trick is that, you can provide Objective C implementation for C++ classes. I would use multiple implementation files for a C++ class interface: keep C++ only impl in one and Objective C impl in another.