Friday, November 27, 2009

TextField update

There was a small bug inside the TextField code with regards to accessing the latest strings in textFieldUpdated method. The protocol has been adapted to reflect this.

Updates are available here.

Enjoy!

Wednesday, November 25, 2009

The Quest to implement a custom Text Field

To match the UI of the App I'm working on right now, I needed a text field with a custom look and feel that would nicely integrate with cocos2d.

I thought there must've been someone out there who has already accomplished / implemented something like this but after a while of google'ing and browing the cocos2d forums, I figured that there seems to be no final, working source code of something like this available.

With some help from this post by Jack and David (and some other stuff I don't seem to be able to remember), I eventually managed to code something myself which combines the rendering abilities of cocos2d (Sprites and BitmapFontAtlas) with the input capabilities of UITextField. This sure feels like a hack as I'm using an "invisible" (read: very tiny) UITextField to handle the text input while rendering the intercepted text using BitmapFontAtlas and some other Sprites but - hey - it works just fine.

I'll check with the people at cocos2d whether it would be worthwhile to include such a class in the main distribution. In the meantime, the files are available here.

Here's how you'd use the TextField class in your own projects:



Hope this gets you going much quicker than me! Now enjoy the code and let me know what you think!

Monday, November 23, 2009

Line-break code for BitmapFontAtlas available

I finally got around to submitting my cocos2d enhancement for BitmapFontAtlas to the cocos2d for iPhone issue tracker. The respective issue #637 contains the modified source code and is up for grabs if you can't wait to give this thing a try.

Here's a quick snippet of sample code for you to better understand how to use the added features:



Let me know in case you're having problems with the code or in case something's unclear. Enjoy!

Thursday, November 12, 2009

Code Snippets in Blogger posts

Finally got around to installing the awesome syntax highlighter on my blog thanks to the post by Carter Cole. I had to go hunt around for the Objective-C brush which you can copy and paste to a JS file on this page (the download link on the list of brushes did appear to point to a brush for Lua).

And this is what it looks like - pretty neat, huh?

Growl-based Hudson notifier

Since the Java-based notifier wasn't working properly for me, I quickly played around with a Cocoa-based notifier for Hudson. The result is a small piece of code that is neither properly refactored nor very clean.

It does its job, though, and that's why I felt like open source'ing it. Maybe somebody would like to help me flesh out the tool? The source code is hosted at Google Code:

http://code.google.com/p/hudson-on-cocoa/

Enhanced Fonts in cocos2d

One thing I came across in cocos2d was an issue with the font rendering. Since the fonts produced by Hiero bitmap font editor are very tightly packed on their containing images, rendering artefacts can appear when cocos2d displays the characters on screen.

Several people have reported these issues but I did not find a solution that fixes the problem in all cases. For that reason I have slightly amended the Hiero bitmap font editor: the modified editor outputs the fonts with more space between individual characters and amending the characters rectangles in the font definition files. This works very well and fixes the issue for me. I'll try to get in touch with Hiero's maintainer (who seems to be this guy) and will check whether there is any chance of having this feature integrated into the official version of the editor.

Another thing relating to fonts is the calculation of a string's width and, based on these calculations, a proper algorithm for line and page breaking. As I did not find such a facility within cocos2d source code, I enhanced the BitmapFontAtlas class to provide all of these. Basically, before calling [label setString:@""] to set the text for a label, you can assign a size to the label using

label.size = CGSizeMake(width, height);

If such a size has been assigned, a call to

[label setString:@"A very long string with reeeeeaaaaalllllllyyyyyyyyyyyyyyyyyyy long words and\nforced line breaks."];

will split the string according to the given size. The string will first be divided into lines (obeying the maximum width assigned earlier), then into pages (additionally obeying the maximum height). The first page will then be used as the string displayed by the label.

To choose a different page and figure out the amount of pages available within the label, you can then use the properties label.currentPage and label.pages respectively. If you assume that you want to switch to page 1 and this is within the amount of pages available, you can just assign label.page = 1 and be done.

If you don't assign a size before your call to [label setString:@""], the text is going to be rendered as with the unmodified version of BitmapFontAtlas. The only added benefit is that forced line-breaks using @"\n" are supported.

For this change, too, I'll try to contribute back to the cocos2d project. I will keep you updated!

Friday, November 6, 2009

Databases: Reserved Keywords

With technologies like Hibernate, deployment of database-driven application should've become a no-brainer: change the driver, change the DataSource's configuration in some XML file and off you go!

Reality is slightly different: a Grails-based web-application we're working on right now is configured for three different databases - depending on the system we're deploying to. During development, it's configured to use an in-memory HSQLDB. When working in development mode, things just work - there has not been a single reserver-keyword hickup so far.

For production, we have two different databases that we can test on: MS SQL Server 2005 and MySQL. For MS SQL Server 2005, first-time deployment has been a pain. During schema generation only the first error is that occurs is reported in the logs and thus we had to perform a couple of deploy -> bugfix -> deploy -> ... cycles as a couple of issues with reserved keywords popped up.

In MySQL, things have been going pretty well with no need to change mapping because of keywords. Just yesterday, however, I added a new field to one of our domain classes and ran into the first keyword issue on MySQL, too.

Now here's the list of keywords you need to avoid while working on the different databases. I'll try to keep this up-to-date as I encounter more such issues.

MS SQL Server 2005
  • "user" - Won't be accepted as table name in schema generation
  • "rule" - Won't be accepted as table name in schema generation
MySQL 5.1
  • "key" - Even though this keyword can be used as a column name in schema creation, adding to the table with an "insert into" will lead to an unhelpful 'you have a syntax error near ...' SQLGrammarException to be thrown

Which brings me to another question? Why does the Hibernate database dialect not automatically prevent these keywords to be used in mappings and provide replacement values? The dialect could, for example, prefix every known reserved keyword with a "_" character - deployment problem solved.

Sunday, November 1, 2009

My first barcamp

Yesterday I participated in my first barcamp. To be honest, I broke one of the most important rules about barcamps but more on that later.

The mobile devcamp Munich 2009 - or #mdc09 for short - took place at the Intel offices in Feldkirchen on Saturday. As I didn't know anything about barcamps until that day, here's a brief run down of what happened for anybody interested in visiting a barcamp one of these days.

Basically a barcamp is this event where loads of people show up, quickly introduce themselves (by the ritual, everyone's allowed to only give their name and three keywords or tags) and self-organize a day full of sessions on a broad range of topics. Whoever is ready to give a session enters the stage, briefly describes what they are going to talk about and asks for a show of hands indicating people's interest in the topic. In #mdc09's case, about 15 - 20 people gave a session and rooms were allocated based on the interest of the general public in these sessions. The sessions themselves are very casual and are not at all like the classic speeches that you'll know from conferences. Participation in the session is hugely encouraged - which leads to very fruitful discussions!

Besides all the sessions, the people who show up at a barcamp are really great! Everybody has a story and a lot of insights to share. People are very approachable and open to meet and greet.

Maybe I was a very lucky first-timer at a barcamp and things just went especially well. Still, if the next barcamp is only half as interesting it's going to be a big win! So in case you're interested, go visit a barcamp whenever you get the chance. You won't regret it!

Regarding the broken rule: there are a couple of rules encouraging participation in a barcamp. Amongst others, first-timers are supposed to give a session themselves during the first barcamp they visit. So ... I did not make it this far but am very much prepared to do so at the next barcamp I'll attend. To learn more about the other rules of barcamps, go visit @flobby's blog (German). For upcoming barcamps in your area, have a look at the official barcamp wiki.

Now, go, enjoy your first barcamp!