Friday, July 31, 2009

Always initialize your variables... never assume default value

I had a problem which was going to drive me insane. The app on the simulator worked fine, but not on the device. I introduced depth in the prototype I'm working on, where buttons need to rotate and look 3d-ish. So when I added the z value, initially this was going to be zero. So I assumed that when I'm creating a variable it was going to be zero by default. I was wrong. It is only zero by default on the simulator. On the device it won't be, at least for floats. The result was a psychedelic trippy 3d-effect, which were ugly to say the least.

Now I know why Java pesters the programmer to not allow him to use variables which have not been initialized.

Thursday, July 30, 2009

Copying files to boot camp windows drive

I discovered that by default I can only view the files availabe on the Windows boot camp drive when I'm in Mac OS X. I needed to put some files on my Windows drive before booting Windows 7 for a while. After a quick google I discovered an article which mentions NTFS-3g that will allow you to write to any NTFS drive. Apparently it uses MacFUSE underneath.

Tuesday, July 28, 2009

LLVM/CLang static analyzer minor problem with iPhone SDK3

The solution is to just tell scan-build to use gcc 4.2.
The command would then be something like this
scan-build --use-cc=/usr/bin/gcc-4.2 -k -V xcodebuild

Audacity audio caf file problem with iPhone SDK

Today I came across a problem which wasted me an hour of my life! I wanted to add new sound files to my prototype. And I downloaded Audacity to convert some sound files. I exported them as uncompressed CAF files. I had read somewhere that the iphone supports some specific types of encoding, two of which are the U-law and the A-law. I tried both from within Audacity but they didn't won't to play on the simulator/device. But all was good when I saved them as Wav files. Not sure why the CAF files didn't work. Someday I have to dig this problem a bit deeper. For now, I'm happy with the placeholder sounds :)

Bought SVN service subscription

I required another repository as I'm doing a quick prototype of a very simple game I had done once with a friend of mine. I have decided to buy a subscription for, the online SVN service. It has worked pretty well so far and when you upgrade they provide a backup service as well, besides no limitations (diskspace increased to 2g), and multiple repositories. 40$ is a good price for hassle-free SVN service which works without any problems in XCode.

Monday, July 27, 2009

gluLookAt for iPhone

On the iPhone SDK we don't have the glu utility library, so there is no glu functions. The gluLookAt is a very helpful function for conceptualizing a camera. I needed the same behavior since I needed to add some depth. So no more glOrthof, but glFrustumf did not cut it.

After googling a bit I found some gluLookAt implementation which is working wonders.

Saturday, July 25, 2009

No problems with Boot Camp and Windows 7

The hard disk partitioning and installation of Windows 7 through Boot Camp was smooth as silk. No problems whatsoever. Having said that, my main OS of choice will be Mac OS X. Windows 7 will just be there mainly for some unforeseen peculiarities with some windows file, and minor gaming. Minor because I only got the intel GMA 950 on my macbook, so I'm lucky if I can play Quake3 and Halo.

Friday, July 24, 2009

Preparing to install Windows 7 RC on macbook

Before I try installing Windows 7, I decided to make a backup of my macbook using Time Macine. I tried using iTimeMachine to backup over the network. This small app will simply enable TM to be able to use some network shared folder on my Windows macine. After using this small utility I found out that you could easily do a command in the Terminal as will be shown in the link below.

I came across 2 problems when trying to use my Windows machine as a sort of Time Capsule. The first was that I needed to create a sparce bundle myself using Disk Utility and copying it over to the remote folder. That wasn't difficult to do as such as higlighted in the above link

However the second problem wasted me a lot of time. When TM was starting to do the intial backup it would stop complaining about a problem with the network username or password. This didn't make sense at all as I could browse the folder with Finder using the same exact password. Apparently the solution was to reboot my mac and remove all keys to my windows machine (not just the Time Machine System key). Not sure about the reboot whether that helped or not. Right now I'm waiting for TM to finish so I can start installing Windows 7 RC.

Keyframe animation

I had already done some keyframe animation code in java for Swing 'n Strike. I decided to migrate some of the code. The keyframe animation classes consisted mainly of two classes, a Keyframe class and a KeframesCollection class. Each instance of a Keyframe contains information about the position, transparency, frame number/time of the keyframe, what kind of interpolation to use with the next keyframe (linear,easing in/out, etc). The interpolation method I had in java, used to give back an instance of a Keyframe containing the interpolated values. But with Objective-C (actually, because of C), now I could keep a reference to a primitive value, e.g. any int, any unsigned char, any float, or any point... and the interpolate method would directly manipulate that reference. Awesome!

It's quite powerful and easy to use. Just create a keyframes collection. Give an address to what it will be modifying, give it some keyframes, and in the update method, we play the keyframe animation.

Wednesday, July 22, 2009

Faking transparency without alpha channel

I brushed again the gl Blending Functions (glBlendFunc) and I discovered you can fake transparent textures without having an alpha channel. You can have a simple RGB texture with a black background (like a lens flare for example) and then set the blending function to GL_ONE, GL_ONE (Some also make the source factor GL_SRC_ALPHA but since there is no alpha, might as well do it GL_ONE). So the source (pixel which is going to be output) factor and destination (pixel in the existing color buffer) factor are both 1. So if the source (texture) pixel is black (0, 0, 0), and the destination (frame buffer) pixel is red(1, 0, 0), the result would be red(1, 0, 0), i.e. nothing will change in the destination (frame buffer)... more formally the result is Sf * Sp + Df * Dp, where:
S = source
D = destination
p = pixel tuple
f = factor

Using our example, Sf and Df are 1 and we have black (0, 0, 0) and red (1, 0, 0)
1 * (0, 0, 0) + 1 * (1, 0, 0) = (0, 0, 0) + (1, 0, 0) = (1, 0, 0)
Using another example
1 * (0.2, 0, 0) + 1 * (0.4, 0, 0) = (0.6, 0, 0)

So it's just adding them together. If it's completely black, it's as if it is invisible.

Of course for accurate transparencies we need the alpha channel and use GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA so that they are blended together properly. If the alpha of the texture pixel is 0.2f then it would be
0.2 * (0.2, 0, 0) + (1-0.2) * (0.4, 0, 0) = (0.04, 0, 0) + ( 0.32, 0, 0) = (0.36, 0, 0)

glTexEnvi and GL_REPLACE

So I wanted something really simple. A textured object (with alpha channel) and I wanted it to fade it out, and perhaps color it at run time (instead of creating different colored textures).

I knew that I should just give the object texture coordinates, and color information (besides vertex positions). Simple but it wasn't working. It was like it was not taking into consideration the color info at all. So I played around with a demo which I use as my playground and the same code worked. I was going nuts and it was obvious I was missing something. I was looking at the gl commands in the demo, and I had done all of those - enabling blending, enabling textureing, enabling vertex/texture/color arrays, blending function etc. Still nothing. It then clicked that by default it must be working automagically in the demo, and I must have some line of code which is changing the default behaviour and I spotted that line of coding which was setting the texture mode in glTexEnvi to GL_REPLACE which basically will discard the color information. I had completely forgotten about what that line was doing. Ah well...

Sunday, July 12, 2009

Objective-C introduction

I have also found this quick tips for Objective-C at Cocoa Dev Central. Wish I had found this earlier :)

Selection List in UIKit

I also dared using a selection list using the UITableViewController class on top of openGL view. I used a modified version of the generic class by Jeff Lamarche. This video tutorial helped as well to get an idea of how the code is organised for UITableViews.

So now I got a pretty decent on-the-fly level editor for the game. I can also select powerups for enemy groups and draw the enemies paths, etc. Still needs polishing, but it's coming together. This week I will try do a decent level, but for now with just one enemy type, and no boss.

Friday, July 03, 2009

Texture atlas with Photoshop slicing

I needed to create a texture atlas and I wanted to use a tool to read off the coordinates and manage it easily. I tried looking at MapWin but it looked too complicated. So I opted for using Photoshop and just slice the images. But not to create separate images. I just use slicing for easily getting the coordinates and dimensions of the slice. If you double click on a slice with edit slice tool, you will get that information in the dialog box that comes up. That's all I needed. It would be a different story if I had hundreds of images but for now it's manageable. Maybe because it is still a prototype :)