Tuesday, 13 September 2011

Sprite sheets and Maximum texture size


Sprite sheets are the “correct” way to add animation into your game/app with Corona SDK.

If you do use sprite sheets, you need to be aware of what's called the "Maximum Texture Size".
Each device has its maximum texture size, a maximum size for a single picture.

With Corona SDK you can get the device’s maximum texture size using the system.getInfo("maxTextureSize"), but that’s obviously for runtime, in order to make sure you are not trying to load an image larger than the maximum.

When creating a new sprite sheet you need to consider its size.
So how big a sprite sheets should be?

Let’s check the details:
(According to
http://www.glbenchmark.com/ 
and
http://developer.apple.com/library/ios/#documentation/3DDrawing/Conceptual/OpenGLES_ProgrammingGuide/OpenGLESPlatforms/OpenGLESPlatforms.html)

Device Maximum texture size
iPod Touch 1024 x 1024
iPod Touch (Second Generation) 1024 x 1024
iPod Touch (Third Generation) 2048 x 2048
iPod Touch (Fourth Generation) 2048 x 2048
iPhone 1024 x 1024
iPhone 3G 1024 x 1024
iPhone 3GS 2048 x 2048
iPhone 4 2048 x 2048
Samsung GT-i9100 Galaxy S2 4096 x 4096
Google Nexus S 2048 x 2048
HTC EVO 4G+ 4096 x 4096
HTC Vision (Desire Z) 4096 x 4096
LG P990 Optimus 2X 2048 x 2048
HTC G1 1024 x 1024
Barnes & Noble Nook color 2048 x 2048

As you can see from the table above, most devices built in the last two years are most likely to support 2048 x 2048 but older will require 1024 x 1024.

Conclusion:
As a guideline try to use 1024 x 1024 sheets only.
Use small amount of memory and target most of the mobile devices out there.
Go up to 2048 x 2048 if you don’t have other choice.
Jumping to 4096 x 4096 is not an option (currently not supported by Apple at all), unless you target your software for a specific device.

Saturday, 13 August 2011

Using math.random in Corona SDK


During my very first steps with Corona SDK (and Lua) I have tried to use the math.random method.
From my experience it wasn’t as simple as I thought, so I think it is worth a blog post.
Don’t get me wrong, using the math.random is not a big issue at all, but there are some details better known first.

When using the method you may notice that each time you’ll restart the application, you’lI get the exact same sequence of numbers back from it.
This is not a bug or a problem, it is a feature :)
So why do we get the same sequence and how to “solve” it out?

Let’s start with the why:
The math.random does not have any magic in it, it is an algorithm – a function.
As the same as with any other function, the expectation is to get the exact same B whenever you send the exact same A, and the math.random in not an exception to this rule.

So How to solve the “problem”:
The random algorithm needs a seed.
Different seeds will create different sequences; same seeds will create the same ones.
If you call the math.random without setting a seed first, the function will use the exact same default seed (and will create the exact same sequence).
So before calling math.random() you’ll need to set up a seed using math.randomseed().
math.randomseed(1) and math.randomseed(2) will produce different results after calling math.random().
In order to have a different and unique seed each time you start the application you can use the system's time as the seed: math.randomseed( os.time() ).

So far so good – but it is not all :)

There is also a bug with Lua.
Under some conditions the first number that returned back from math.random could be the exact number, each time you start.
To work around this issue you’ll have to call math.random couple of times before making a use to the returned result.

Bottom line: If you want to use math.random in Corona SDK (and in Lua in general) and to get a different sequence each time, set the random seed as the system’s current time, and call the random method twice before using it J.
Add the following lines to the head of your code:

math.randomseed(os.time())
math.random()
math.random()


C# (my main development language) is not very different in a sense that the concept of random and seeds is the same.
But Microsoft did our work easier. The default constructor of the random method will use the system's time as the seed, so you don't necessarily have to know anything about seeds (for good and bad). For more details check the MSDN page.
Arguably (and can really argue both sides) corona could have add the three lines of code from above to the framework, so it will bypass the Lua bug and use the system time as default.
It will make the development easier for new developers (the same decision Microsoft took with C#), but will produce unexpected result for Lua developers (and that’s why I can understand why they maybe should leave it as it).

This issue causes some pains to new Corona/Lua developers as you can understand from this forum thread.

Feel free to share your thoughts :)


Saturday, 25 June 2011

Motivation Injection

I am having a real fun with spending my free time developing my first mobile game.

Anyhow there is no doubt  that reading studies like this one are making me even more happy.
The trend is clear – people are spending less time on PCs and more time on mobile apps.

By far, gaming dominates the average mobile app user's time - 47% of mobile app usage in May was game-related…

Have nothing more to add J



Tuesday, 14 June 2011

Corona SDK

After watching this movie it was clear to me; I am going to develop a game !
If game development is really as simple as this video is, I will do it.

The next step was to understand more details about
pricing, licencing and conditions.
So far It is the first time I considered paying money for a framework.

What I like about Corona SDK?

Emulator
They have an emulator!
I can amend the code, save the file, and reload the emulator to see the effect (not very far from editing HTML and refreshing the browser to see the
immediate effect).
The emulator runs on both Windows and OSX, but iOS builds needs to be
made on a mac. This is great news for me, as I prefer to work on my Windows machine
(and will use a mac for iOS builds).

Multi platform support (currently iOS and android which is the main two ones I currently
care about)
Corona SDK gives an easy way to port the same lines of code to both iOS and Android !

I have never heard about Lua script before. The syntax of Lua and Corona SDK API
seems to be very clear and readable. Without knowing too much I could read some code
examples and to understand them.
The API features and methods looks great and the physics engine seems to be powerful
and easy to use.
Learning curves does not seems to be high.

Performance
With any abstraction layer there is always the trade off between simplicity and
performance.
You get a simple way to code but you pay the price with lower performances and less
flexible code (you will always have the dependency on how many features the abstraction
layer will give you, and how well it is implementing them).
In general, Lua script (after spent some time googling) seems to have good
performance.
Corona SDK works on top of openGL and openAL and some of the code samples can
demonstrate how good the performances are.
So the button line, it seems like the trade off is fair with corona SDK.

Good support for different screen sizes / proportions / resolutions
One of the points I was worry about (regardless what the develomet tools are) is how
to support different screens. With android it could be very hard as there are too many
flavours out there. Corona SDK has some solutions to solve the issue.

Lifetime trail version
You can try and play with it as long as you like.

What my concerns are with corona SDK  (and frameworks in general):
As I do not have experience with corona, I cannot give specific points yet, but I do have some general concerns.
  • Using a development framework will throw some obvious dependencies. You are always limited to the framework abilities. If Apple will come up tomorrow with a new API it will take time until the framework will follow (if they will). So you are always one step backward compare to a 'native' developer.
  • What if the framework will die tomorrow ? You will have to take the risk, hoping the framework has a good business plan, and they are going to stay here for a while.
  • Limited amount of knowledge, code examples, and forum members. I don't know how good the Corona SDK comunity is, and how good the support is.
    Let's say you have found a bug in your game, but the bug is not very urgent to Corona's guys. What will you do then ?
Again, all the above is true for any framework.

Decision:
I am going to try Corona SDK.
Will try to build a very simple game to see how it goes.

Monday, 14 March 2011

Titanium

I heard about Titanium before. It was mention somewhere and I remember I heard the name and checked the web site before.
So my next step was to revisit the site, register and download the tools.

What is Titanium ?
According to their website:
"Titanium translates your hard won web skills into native applications that perform and look just like they were written in Objective-C [iPhone and iPad] or Java [Android]. With over 300 APIs, a thriving developer community, and the support you need, you can build applications that are more social, local, media rich, interactive, and extensible"

Sounds good.
They also have a nice list of big names that used Titanium for their apps (eBay, Yahoo, Facebook and more).

I was very excited about Titanium but my experience was short after all, and honestly I don’t think I can say too much about how good or bad the framework is.
It was short mainly for two reasons:


  1. My Current mobile phone is iPhone 3GS and while testing it had no apple developer account, So I couldn't build any of the code examples to my device. Titanium does not have a kind of ‘emulator’ so I could not see an example of mobile code running. I was really curious to see how a ‘real’ program looks like. As I could not run any mobile app I have tried to run an example of Titanium Desktop instead (just to be able to see how the concept works).
    Unfortunately I could not find any documentation about creating desktop application and after deep search I have manage to find a single code example that was in ‘beta’ state and didn't work well for me.
  2. While playing with Titanium I have found a different framework that gave me a real passion to code :)

So eventually I have lost my interest with Titanium.
I do think it could work well for 'form' based apps, and if I will develop one I may check Titanium again.
But after finding my framework I knew I am not going to develop a 'form' base application - it is going to be a game!

So what the next framework is?
Next time :)

Thursday, 17 February 2011

Hello HTML 5

And so my journey starts, and it starts with a book.
“Building iPhone Apps with HTML, CSS, and JavaScript” it is exactly what I was looking for.

  1. I have a good knowledge and experience with HTML, CSS and JavaScript. So it looks like my learning curve will be very short (will have to learn some of the latest HTML 5 features and to polish my JavaScript, as I didn’t do any web project for the past four years).
  2. Although the book’s title talks about building an iPhone app, the same tools and technics applies for ANY mobile OS.
  3. No need to buy any product/license –just grab any text editor/IDE and hit the road.
  4. Can develop using my Windows PC. One of the annoying things with building an iPhone app is the fact you have to have a Mac. My wife owns a MacBook Pro, so worst case I can use her, but I would definitely prefer to work on my Windows PC, using my Visual Studio.

If I will try to summaries the book with my key points:

  1. Build a web app
  2. Give your web app the same look and feel as iPhone app, using HTML, CSS and JavaScript.
  3. Build a native package from the web app code using PhoneGap
  4. Send your application to the market. In case you had problems to approve your app, you still have a web app.

PhoneGap also have API that gives access to the device hardware, So if you want to write application that will make use of the device’s file system, Events, Contacts, Camera and so – it can be done with some limitations on some devices.
All sounds good, clear and simple.

My biggest problem at this point was the fact I didn’t have any idea of specific app to develop. As I cannot code without any clear target to achieve, I came up with a simple idea, I will develop a very simple math game. And so I started.

I was wondering how to manage a local Database using HTML, CSS and JavaScript. After some Googling I have learn about using local DB with HTML 5. Will answer most of my needs.
The next thing I did (following the book) was to install a web server (IIS) on my PC, so I will be able to browse and play with my web app from my iPhone, at my home wireless network.

Eventually didn't go very far with my development, as very quickly I felt a bit unsure about the whole thing.
I do know how to create nice user interface elements, using HTML, to mimic the iOS native controls, but it did not feel right. Too many lines of code and it does not feel like the ‘real’ thing. I felt I could not deliver the same 'native experience' with my app.
As I started to be a bit sceptic I have decided to stop my development and to go back to Google.

I was looking for some answers. What kind of web application people already made (not necessarily PhoneGap 'native' apps – but web apps in general) ? Just to get the impression of how powerful a web app could be.
What kind of games you can write using pure JavaScript ?

To answer my first question I have checked apple’s web app catalogue.
After checking some I could say that it was exactly what I thought. Some of the web applications are really nice and made with a lot of hard work, but still, for my view, nothing to compare to native apps.
As for the JavaScript games I have found few articles like this one. It was just like a self remind of what I didn't like with web development. I don't like all of this HTML inside JavaScript...
Some will say that JavaScript is very powerful and you can do amazing things with it. Even if I will agree, I don't think that it is easy.
The bottom line - I don't want to create a web app !
Few days ago, my good old friend Yossi sent me this link. I was very happy to read and to know, I am not alone.

To be fair I would say that I really think that PhoneGap is a very impressive tool and can easily see how it can be use for some scenarios, but I don't see how it will help me with having fun, coding on my free time.

OK - So bye bye HTML 5, and goodbye PhoneGap - so what's now :(  ?
Some Googling, and...
Should I try "Appcelerator" ?

- to be continue...