Category Archives: Technology

Game Optimization Adventures, part 1

Starting on Wednesday (today is Friday) I began the process of optimizing the iPhone game app.

Using Instruments, I could see a total Live Bytes value of around 40 MB for an average while the game was running.  Now, I’ve done a couple tasks and also introduced a PVR texture (not got it working yet).  Suddenly, the live bytes (after everything has loaded) has been reduced not to 20 or 30, but 4.  When the app is started, it loads up about 15-20 MB of memory, and then dumps it all except 4.  I’ll mention more about this later on.

Now, that’s a colossal drop in memory use.  From peaking at 40 to down to 15-20, and then now a mysterious (but very happy) drop after loading to around 4.

In the last 4 hours (and some hours yesterday) I’ve been making pages of notes on values and changes from different configurations of the app.  For the configurations, one thing I’ve done is I have built 4 separate Instruments save files using the Xcode Run>Run with Performance Tool>Object Allocations automated setup.  The four setups use these configurations: using JPG texture and stop recording when at peak live bytes, using JPG texture and stop recording after drop to 4 MB, using PVR texture and stop recording when at peak live bytes, using PVR and stop recording after drop to 4 MB.

These files have let me discover the strange, inexplicable fact of the moment (and which I have not built any conclusion about):  the memory being allocated internally by glTexImage2D for my JPG textures is being released.

One reason I dropped initially from 40 MB to a moderately lower amount is I removed an unused texture that was hogging 12 MB by itself, and wasn’t even being used.  My bad.  Actually it wasn’t my fault. haha- blame someone else.  It was from the 3rd texture channel in blender, and the game engine I use claims to only use two texture channels from Blender, because OpenGL ES only supports two texture channels.  Fine.  So that’s why the documentation for the game engine recommends using texture channels outside the first two.  That I did.  But apparently it exports the rest anyway despite them being beyond the first and second channels.  So I removed that texture (2048×2048 pixels x 3 bytes/pixel = 12 MB).  Also I have removed a handful of other textures that I used before and now I have reduced the peak load size from 40 to 15-20.  The 5 MB difference there is the size of the JPG vs the PVR.

So assuming I’m still using a JPG texture

The peak live bytes is 20.30 MB, and the drop-live bytes is 4.67 MB.  Watching the ObjectAlloc graph in the upper pane of Instruments, it will be at the peak for a second, and then it suddenly nose-dives to the low drop.

Now to examine the useJPG-peakLiveBytes instruments file: sort the Live Bytes column in Summary View from high-to-low, and a person can see what Malloc categories are taking up the most space.  And in my case, the biggest ones  are  Malloc 4.00 MB ( # Living = 2 ), Malloc 1.00 MB (# Living = 5 ).   Beyond that, there is Malloc 2.66 MB (this is not a texture, it is part of the Bullet physics engine so I ignore it), and Malloc 256.00 KB ( # living = 4 ) Add up only the 2×4 + 5×1 + 4×0.25 and there’s 14 MB out of 20 MB right there.  There are dozens or hundreds more memory allocations but they become very small and they are not the significance that these large image textures are.

Examining the useJPG-dropLiveBytes Instruments file in the same manner, the biggest live bytes categories are Malloc 2.66 MB (the previously-mentioned physics stuff) … and nothing of the Malloc 4.00 MB and Malloc 1.00 MB.  So I clicked the Category column header to sort by category, and scrolled down to Malloc 4.00 MB and see the # living = 0, and Malloc 1.00 MB # living = 0, and Malloc 256.00 KB # living = 0.

That was predictable.

But the chart still shows a peak and a nose-dive, and so I asked myself “why does this build up and then nose-dive, and when I started this on Wednesday, it was building up to 40 MB and just staying there?”

Now I know why the peak isn’t so high, but why does it fall?  Instruments to the rescue!

Using the dropLiveBytes file, still sorted by Category, I click the right-pointing arrow-in-circle beside Malloc 4.00 MB.  This shows two rows, and both the rows have a blank cell under the Live column.  If looking at this same Malloc 4.00 category selection in the peakLiveBytes file, the cells would have a little black dot.

Then I clicked on the Extended Detail View button and looked at each of the two rows… in each case the 4 MB were being realloc’ed within glTexImage2D.

Now this is where random clicking and exploring Instruments really helped me.  Clicking the right-pointing arrow-in-circle beside the Object Address values in a row will show the history for that Object Address.  The columns of interest in my case are Event Type, Timestamp, Size, Responsible Caller.  I sort the list of two rows by Timestamp, ascending.

The order of Event type, shows Malloc, and Free. Okay! So what else is in the second row along with Free?  well, obviously the same Address, that’s not important. the Size, shows – 4194304 (which is 4x1024x1024.. = 4.00 MB) and Responsible caller shows glDrawElements.

So I examined the other 4MB and the 1 MB alloc objects in the same manner as above, and discover that they were all being Free’ed in glDrawElements.

So a quick jump over to khronos.org’s glDrawElements page and search the page for “free” came up with no successful information.

And now I’m puzzled… why are these objects being realloc’ed in glTexImage2D, and then freed, but they weren’t being freed before?

I have no idea.  But I know I’ve got to get the PVR texture working, and it’s nice to know that the game should not get dumped by the iPhone OS anymore ( I hope ).

More adventures will follow.  Take care.

Posted in Activities & Adventures, Technology | Leave a comment

Bezier timing

I have just finished a bezier timing algorithm, and I’ll give a small description here.

I will be using the bezier values a linear stepping value in a loop… layman’s terms: a counter that counts up evenly, 0,1,2,3 or 0,2,4,6, etc.

There are methods to do this in the iPhone SDK but I already wrote half of the code, and it wasn’t much effort to complete.  Plus, I looked up the CAMediaTimingFunction class, and it seemed too complicated just for what I wanted (just an in-and-out structure).

So all I did is this: create a 100-element array.

.. and it gets a bit complicated, so here’s the code to build the array:

float t;
int i;
CGPoint bpoints[1000];
CGPoint bypoints[100];
for (i = 0; i < 1000; i++) {
 t = i / 1000.0f;
 bpoints[i].x = (1-t)*(1-t)*(1-t)* 0  + 3*(1-t)*(1-t)*t*0.5 + 3*(1-t)*t*t* 0.5 + t*t*t*1.0;
 bpoints[i].y = (1-t)*(1-t)*(1-t)* 0  + 3*(1-t)*(1-t)*t*0.05 + 3*(1-t)*t*t* 0.95 + t*t*t*1.0;
}
for ( transitionTimeStep =0; transitionTimeStep < 100; transitionTimeStep++) {
 float closest = 1.1;
 int closestI = -1;
 // Find the index in bpoints where .x is the closest to the transitionTimestep percent
 for (int x = 0; x < 1000; x++) {
  if ( fabs( bpoints[x].x - ( transitionTimeStep /100.0f)) < closest) {
   closest = fabs( bpoints[x].x - ( transitionTimeStep /100.0f));
   closestI = x;
  }
 }
 bypoints[transitionTimeStep] = bpoints[closestI].y;
}

The first loop creates a high-resolution set of points on a quadratic bezier curve (see Wikipedia’s page for Bezier curves) where the 4 points are between 0.0 and 1.0.  The points (0,1,2,3) have the following x,y coordinates:

  • 0,0
  • 0.5, 0.05
  • 0.5, 0.95
  • 1.0, 1.0

If a person even multiplied these values by 100 and then used a program like Adobe Illustrator, the curve would look something vaguely like a capitol letter S.  Rather, it’s actually more like a forward-slash /  and little curves at the top and bottom .

So I collect the high-resolution points of the line, and then loop through my time-step array.  For every element in the time-step array, I examine every x-coordinate value of the bpoints array.  If the value is closest to the current time-step index value, then I save the bpoints array y-coordinate value.

Yes, this seems very complicated now, but when I was doing it, it seemed very easy.  I guess sometimes I just get on a roll and create complicated stuff.

Also, I know there are better ways to do this, but I only want a look-up table and when I see it in live operation before typing this post, it works beautifully.

An important note!  The bpoint array is VERY important.  It is not good to simply create an exact-resolution array and fill it with values.  I did that at first, and found that there were too many indexes being dropped, that when the timing animation was run, there was a lot of choppy movement, like if the smooth slope of the curve was suddenly turned into a staircase of jagged edges.

So, creating the higher-resolution array first and then scanning it for the closest x value was the logical solution.  Perhaps I could have done it with a 200 element array, but I didn’t try it.

The process only takes a split-second, and it’s only done once at the beginning of the program.

That’s all, take care everyone.

Posted in Technology | Tagged , , , , , , | Leave a comment

Late night quick post

Tonight I have finished hooking up and testing another major component of my project: cannons and cannonballs.
And in-game menu screens.

But the most important thing to mention is the bugs that still appear in the game app. Some are partly deliberate, those that allow me to break up the system when I need to see things but also bugs from stupid mistakes like memory leaks and such.
These are all good to talk about if I am going to build a conclusion but tonight I think a quick mention is all that can be mustered.
Tomorrow I will try implementing a health system. Seems the obvious next step after cannonballs have been added.

Posted in Technology | Tagged , | Leave a comment

iPhone accelerometer orientation

After searching for information about the iPhone accelerometer in search engines and the apple documentation, I did not find anything simple as what I am going to post here.

This is a bit of a tutorial, or just an explanation, how to determine the iPhone and iPod Touch (possibly the iPad) and its orientation relative to the flat ground at our feet.

The orientation uses 3-dimensional coordinate concepts, so a person has to be familiar with x, y, z and rotation concepts.  To get the information, you should be developing a native app (I don’t know if this works in Mobile Safari for web apps yet).  The UIAccelerometer class has a class method to provide you the shared accelerometer. The way to do this follows:

[[UIAccelerometer sharedAccelerometer] setDelegate: myObject ];

Additionally, setting the time interval (measured in seconds) to receive updates is a good task:

[[UIAccelerometer sharedAccelerometer] setUpdateInterval:( 1.0f / 30.0f )];

Now your class that myObject is instantiated from must implement the following method (the actual variable names can be customized to whatever you want):

– (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration

The UIAcceleration object called “acceleration” contains the important 3-dimensional values.

* * *

The iPhone cannot determine how far off the ground it is, but it can tell which side/edge/face of itself is closest to the ground.  That’s the point I am going to work with. Pretend the iphone is a 6-sided box, even though the 4 side edges are very slim, they are still sides of the box.

The device has 6 orientations, when one of the sides is directly facing the ground:

  1. Portrait
  2. Portrait upside-down
  3. Landscape left (rotate the device counter-clockwise 90 degrees from portrait)
  4. Landscape right (rotate the deviceclockwise 90 degrees from portrait)
  5. Face up
  6. Face down

Each time the accelerometer:didAccelerate method responds, you can get information about its orientation from the UIAcceleration object (using the above name) as follows:

float x = acceleration.x;
float y = acceleration.y;
float z = acceleration.z;

And now, comes the final bit of useful information, the pictures.

Basically, the pictures show the values of x, y, z,  for the 6 different orientations.

The way I personally interpret this is by way of the relative positions of one side to its opposite side.  The screen and the back are Z sides, the top with the lock button and bottom with the dock connection are the Y sides, and the two left-right edges are the X sides.

iPhone face-down

iPhone face-down, z approaches 1

iPhone face-up

iPhone face-up, z approaches -1

iPhone portrait

iPhone portrait, y approaches -1

iPhone portrait upside-down

iPhone portrait upside-down, y approaches 1

iPhone landscape right

iPhone landscape right, x approaches 1

iPhone landscape left

iPhone landscape left, x approaches -1

Until I wrote all this info down on paper, exactly as it’s described here, I couldn’t figure out how to interpret weird angles.

I had to do this research because the pre-packaged method for detecting orientation-changes was causing my app to jump back and forth.  Probably has to do with the update frequency too.  But this way now, I can build my own custom orientation code.

Take care everyone.

Posted in Technology | Tagged , , , , , , , , , | 2 Comments

The “freemium” business model.

From Wikipedia’s Freemium page:

Freemium is a business model that works by offering basic Web services, or a basic downloadable digital product, for free, while charging a premium for advanced or special features. The word “freemium” is a portmanteau created by combining the two aspects of the business model: “free” and “premium”.

There are hundreds or thousands of games on the Apple AppStore.  The popularity of it is rising according to numerous reports, and probably it is a very profitable model.  I just heard of the term and so looked it up.  It seemed fairly obvious given the context: a review written for an iPhone game, talking about the free nature and how the extra features are paid (something this particular reviewer did not like).

So, now the question of the moment: should this app I am developing follow the freemium model?  I don’t think I can answer that during the time of this blog post writing, but I will give it some writable thought.

A freemium app gives the public something to try-before-you-buy, but the Apple acceptance for new apps to the AppStore requires all apps to provide a “complete” product, even if it is minimal.

Pros:

  • You can get more initial market penetration
  • Possibly take advantage of alternate marketing techniques.
  • “Free-buyers” may be able to customize their experience of the product in the extensions
  • Coming from the previous point, the buyers may find their experience enhanced by a product that can interpret specific combinations of extensions, for example: a car-building game, buying a “racing stripe” paint job and an “air foil” extensions separately may each add individual looks or enhancements to speed, but together they gain a bonus, a synergy from the two.

Cons:

  • The product’s “extras” that are premium may not be accepted as “worth the money”, and a greater majority of people may consume the product’s free version, and then carry on with the next shiny thing that catches their eye.
  • The “extras” require specific development that extend beyond the core “Free” product scope.  More levels, more characters, etc, cost more time and money to create.
  • A buyer may be surprised to find “extras” that he or she, individually, naturally expects as part of the “Free” product, and complain or spread bad reviews complaining about “premiums that ought to be free”
  • An update to the free product that requires the previous purchase of a (very) popular extension should only be publicly released after making the popular extension become part of the core free product.  The Apple AppStore’s in-app purchases feature does not permit game logic upgrades, but does permit static data files like artwork (decals) and 3D models (new cars or whatever) and sound.  No new multi-player features, however there are ways around that…  So, realistically, an update to the free product that requires the previous purchase of an extension should not be an update to the free product, rather it should be a paid extension, perhaps one that absorbs and eliminates the previous popular extension

After this analysis, it seems the benefits are all mostly in favour of the end-user, and to a lesser extent, benefiting the company/developer.  The Cons are all weighing more heavily on the company/developer.  There are no major cons to the end-user except for those people who have personal expectations of extra free things, and the free-release of previously paid for products.

I like the idea of the fremium model, and may chose it but for now, the core game must completed.

Posted in Technology, Thoughts | Tagged , , , , , , , , , , , | Leave a comment

Progress in movement algorithm design

The album Bolero Gypsies: new flamenco is playing in my ears and the book iPhone cool projects from apress is spread open on my lap and I am typing a blog update on the iPod touch.

The chapter in the book I am reading is about the subject of networking. This may be getting ahead of myself, but it’s good to know for when the opportunity presents itself, and it will.

Yesterday, I created the movement of one of the principle game assets that will be moving constantly in the game. Let’s call this object a “bird” (it’s not in the game, neither is it in the air but it serves for this explanation).  The movement requires intelligence, not merely a simple movement like a rotation of some degrees on every game loop (eg. coins in Super Mario games or rings in Sonic the Hedgehog games). The game world may be large when the game is finished. The first draft for movement has become the following: a “home path” in the same style of the common footpath worn by animals in their native environments, when they travel the paths frequently. So it is with this movent. A path throughout any part of the game world.

This path is achieved by using a number of data variables.

The most important is the array of “checkpoints”. These are the same as checkpoints in a racetrack. In my first version of the movement system, the checkpoints are created as cubes in the world 3d model file, with special object names and serial number prefixes, such as “homepath001”. The name is searched and all the points are loaded into an array.

After the array, the most important variable is for storing the index serial number for the checkpoint the bird will be moving toward. Optionally, this destination may be interupted an replaced with any arbitrary location and the bird will move toward there instead.

Now, storing the index value of the next checkpoint allows us to determine the direction of movement whenever we want to. So now, this permits us to consider other random travel destinations.  If any arbitrary destination is chosen at anytime, another variable must store the yes/no status indicating if the object is moving toward the next checkpoint or not. So this possibility brings the necessity of another variable to store the actual destination location instead of only referencing the next checkpoint from the next-checkpoint-index. When the next checkpoint is reached, the index of the next checkpoint is updated, and the location for the new next-checkpoint is copied to this destination as the default destination.

This is just the tip of the iceberg.

Assuming movement toward some arbitrary location is part of the object’s life cycle, not only following a path forever, then it stands to reason to store not only the next destination separate to the next checkpoint as we’ve discussed, but also store the current home-path-location of the bird as soon as a new, arbitrary, destination is created. This will give the bird next-destination options to choose between, when it reaches the arbitrary location.  It may chose to return to it’s home path from where it left the path, somewhat tracing it’s path backward. Or, it may choose to continue to the next checkpoint directly.

In the case of the city pigeon bird, a home path might be the path around a large office building, and its checkpoints are lamp posts and parking lots (it may choose to walk in a parking lot).  When it is anywhere flying or walking, it may decide to go look at something away from its path, but it will try to keep its home building in sight, and if it decides to return, it can go onto wherever it wants, in the direction of its home!

Having more options is always a better thing than few options or only one.

Following this, I have implemented bezier curves for a smooth transition from approaching one checkpoint to the start of the next checkpoint’s path.  It doesn’t work well if a bird (or any other creature) is moving in a straight line and then instantly is moving in a separate direction..

Never before have I worked with bezier curve equations, so it’s brand new.  But I got the equation from Wikipedia’s page and plugged it in, and now it works.  This is a complicated issue, and not really part of algorithm design, but of implementation.

That will remain for some other day.

Posted in Technology | Tagged , , , , , , | Leave a comment

Late night development progress

Feb 17th, I have stayed awake almost all night. My iPhone app in development is coming along very well, in spite of the very painful lack of reference and learning materials for the main 3D engine.

There are many things to do yet, but today I have done more in a single day than I have any single day previously.

Posted in Activities & Adventures, Technology | Leave a comment

Today: learning and doing activities. Tomorrow: repeat.

Today has been a day of guitar-playing, reading, interpreting, typing, mouse-clicking, and dragging.

I built a 3D model of the Bahamas islands plus Turks & Caicos islands. I am using it as the sample of a 3D islands world in my iPhone game project. This is fun! Building in 3D is new to me, but things don’t go in one ear and out the other in this sort of subject. There are SO VERY MANY things to learn in Blender, because it’s not a user-friendly 3D modelling program. I didn’t know that when I started learning it, but now I do know it.

I got into using it from someone’s internet article of 3D and 2D graphic rendering engines for iPhone and iPod touch (and I suppose now, iPad). The person had SIO2 and Oolong as the top two ranked options, but in pros-and-cons, listed Blender as a con for SIO2.

But that’s probably because like me, that person had no experience with 3D art and modelling.  Anyway, I spent the latter half of January going through Blender 3D: Noob to Pro, and have progressed about 40-50% through it.  I should really finish, except SIO2 does not fully support everything, and I’m trying to catch up to the 40-50% full competency of SIO2  before going on with more complex Blender subjects (like animated armatures).

So anyway, going through the tutorials in the source code is what must be done, not reading step-by-step how-to documents.  This is a guerilla-style learning.  Gotta get my feet wet by jumping in 100% and swim with the sharks.  Not that anyone associated with SIO2 is a great white shark, more like very pleasant whale shark… that sounds really odd, but I’m tired now.

When complete, this project will be a lot of fun (I hope!).  A lot of ideas come to me when I’m in a lucid state, so immediately in the morning before I am out of bed and just sort of coming out of unconsiousness, I try to figure out what ideas are lurking.. at the edge of thought.  Collect them up!  They’re worth money in the right hands, my hands!

Posted in Activities & Adventures, Technology, Thoughts | Tagged , , , , , , , , , , , , | Leave a comment

Problems with iPhone device provisioning

Before working on the iPhone, I have only a day or two’s experience with secure certificates.  The SSL certificate I bought for a web site took a day to buy and another day to setup.

Now, I am dealing with these secure certificates again, and it’s a bit challenging because of my lack of experience!

The task at hand: get Xcode to properly upload one of my apps for testing on the iPod touch.  I think the provisioning is giving me troubles, or something else is.   When I tried to build an app I downloaded as a tutorial, it had someone else’s name in the configuration for the device, so I changed it to my own and also included the certificate, and it still kept telling me the other person’s name was in the configuration.

So I stopped trying to use that tutorial project, and went back to my own, and discovered that Xcode is bugged up.  I try to build and run, setting the device as the Active SDK.

And it uploads the app, but if I am running the debugger, and I click build and debug, it uploads, but gives me an error saying “Failed to upload the ___ app”

Then I look at the iPod touch, and the app is sitting there.  I delete the app, repeat the process, and it’s exactly the same as described.

I’ve checked every single Build-Debug configuration setting and they’re all proper, so I know the issue is not there.

I’ve just finished restarting the iPod touch, and the results:

Failure.

I’m really puzzled.  Possibly some other file is causing me grief.    I created a new test project to verify Xcode can still upload and debug, and yes, it can still upload and debug.  So, there’s something buggy with the app.  Cleaning the targets doesn’t help either.

And this issue has started since testing this other tutorial app out.

EDIT: I have solved this issue after checking google a second time.  First time searching for the problem did not give any success, but second time I found this: Failed to upload XXX.app

More information: The location of the name of this other person was in the Code signing area of the TARGET, but not of the PROJECT.    That’s solved now.

Posted in Technology | Tagged , , , , , , , , | Leave a comment

Documentation and 3D game engines

I am progressing slowly but surely. The 3d rendering engine for the iPhone that I am using is called SIO2. I am finding it slow going because the documentation is not what I call good. There are no step by step procedures in the tutorials and the reference doc materials are very raw, with no clean summaries,except those on the front page of the website that describe, in point form, the most general capabilities. Those capabilities are great,but after putting in the time to download the engine and try out the tutorials, I am coming to a question: is a pre-existing system better than developing a system in-house, if the existing system is so time consuming that the learning curve is almost as long as the development process?

Here I am frustrated with the lack of good quality and complete learning materials, but the existence of the system is still better than developing my own system. That is because in the time it would still take to devlelop my own thing, the existing thing may (or may not) improve, and in other cases I may find some help that I had not found at first. So the learning of an existing system is better.

This experience is going into the heaping pile of experiences of bad documentation for products I have seen. There is a wiki, and I might start contributing to it.

Going forward in the future, the skill to produce good documentation may become a very valuable transferable skill in the workplace. It seems very boring in some product contexts, ie. Vacuum cleaner manuals, software application manuals, inflatable air matress instruction sheets. Yet this world needs a better, universal, written method of learning that appeals to the reader. Something like google’s API doumentation, and apple’s iPhone and iPad documentation.

Posted in Technology | Tagged , , , , , , , , , , | Leave a comment