In the previous post we cobbled together an SDL2 project in Xcode 9. We have a very simple program that starts up, initialises the SDL2 framework and then shuts down. This gets us over the initial hurdle of setting up a game project but we are still along way off from having a working game. In this second part, I will be writing a simple game loop using SDL2. If you don’t know what a game loop is, or need a refresher, go ahead and read about it in Game Programming Patterns. The game loop is an important foundation of any game engine and its important to get right. I plan to develop this over two articles, in the first (This one) we’ll get the basic loop in place. Then in part 2, we’ll refactor the basic loop into something more maintainable. Anyway, without further ado, let’s get started.
I’ve put together a couple of side projects with SDL2 over the years. I like the library because of it’s simplicity and how it provides a quick way to throw together a cross platform project. This post is a reasonably comprehensive guide to how I set up an Xcode 9 project that links to the latest SDL2 framework. There are other ways to do it, but this is mine.
You often see apps with links, hashtags and usernames embedded in labels. I’m thinking of the many Twitter and Facebook clients out there in particular. I wanted to achieve the same effect in a client app I’m writing but there didn’t seem to be a nice way to do it with the native UIKit controls. Yes, I know it’s possible to get close to what I need with UITextView, but this isn’t easy to extend with new link types and introduces problems with interaction when its in a table cell. I tried a few open source UILabel replacements that I found and they all seemed a bit buggy or at least wouldn’t work how I expected them to.
I’ve ended up writing my own UILabel replacement, which you can find here on Github. KILabel is an extension of UILabel that adds the ability to automatically hi-light URLs, twitter style @usernames and #hashtags. It also provides a simple way to respond to users tapping on any of these using blocks. To use it just include the source code in your project and set the custom class of any labels in your nibs to KILabel.
KILabel uses ARC and TextKit so it’s iOS 7 only. It overrides UILabel’s text rendering to use a NSLayoutManager, NSTextContainer and NSTextStorage for rendering text. TextKit gives simple access to all sorts of text rendering operations which allow things like this to be implemented. There’s a useful TextKit primer here for those that are interested and my source code is quite heavily commented so it shouldn’t be too difficult to understand what’s going on.
I plan on extending KILabel as I need to, but if anyone else finds it useful and has any feature requests, please pop them in the comments.
Update: It’s been quite a while since I first release KILabel. Over that time I’ve had a few requests for help and new features. There’s now an update that you can read about here that addresses those issues and adds support for Swift and CocoaPods. – 1st May 2015