Showing posts with label mac. Show all posts
Showing posts with label mac. Show all posts

Wednesday, 18 July 2012

Using UIKit with Cocos2d - iOS and Mac!

I was at the excellent iOS Dev UK conference last week and a few people talked about combining the power of cocos2d with the features of UIKit.  This made a lot of sense to me.  Cocos2d is great at graphics and such, but it's a real pig for certain basic behaviours.  For example, I've been putting off adding a feature to my latest game that allow you to edit the name of the players.  The thought of writing a text field control from scratch was a bit daunting.  I now know I can just use a UIKit edit box and avoid all the heavy lifting!

Adding UIKit controls is surprisingly easy.  The cocos2d app Director sets up the screen a regular UIView to display the Open GL stuff.  All you need to do is add a child to this view, then all the standard UIKit stuff can be used.  So easy!

I'm yet to see if there's any performance hits in using this approach, but since it's only going to be used in the menu part of the game, this won't be a problem.

After getting the UIKit working, I next turned to using the equivalent Cocoa controls for the Mac.  This turned out to be a little trickier.  Unlike the iOS Open GL view, the Mac version does NOT allow any child views to be added to it.  D'Oh!

After hunting around for ages, I eventually scraped together some code to do it.  Basically, it involves creating a new NSWindow with a NSView inside.  You then add this window as a child of the root window.    I place it at the top of the view stack and make it transparent, so the user doesn't realise what's going on.  All your Cocoa controls can then be added to the NSView.

There were a few gotchas with this.  First of all, if you add a Window without any chrome (window title, open/close etc),  OSX handily disables all edit functions!  That took me AGES to work out what was going on.  To get around this, all you need to do is sub-class the Window and add a single function that tells OSX to allow editing.  So bizarre!

I'm still working out a few kinks with this approach, but I'm now able to use standard OS controls for editing text, which is amazing. I'll post a complete project with examples on github once I get a nice clean project running.





Thursday, 26 April 2012

Mouse Scroll Wheel in Cocos2d

Today, I felt a bit dim.  For ages, I'd had a nice little slider on the game which was used to zoom the map in and out.  On the iPad version, I use a gesture handler to zoom in and out with a pinch.  I couldn't quite work out how to do the same thing on the Mac.

I was thinking I'd have to do some tricky gesture handling, or worse still, just use the ugly slider.

After peering into the CCEventDispatcher header, however, I discovered the delegate method ccScrollWheel. Derrr...  How better to handle scrolling than using the scroll wheel!  Five minutes later and the map is now zooming nicely using the scroll wheel.  Nice and neat!  Just read the deltaX and deltaY properties to see which direction the scroll was in.

It just goes to show how rewarding it can be to look at the header files now and then...

Tuesday, 17 April 2012

Welcome to the Experiment!

This blog is designed to track the highs and lows of the development of a new turn based strategy game for the iPad and Mac. The game is based on classic board games, such as Risk and Samurai Swords/Ikusa. The working title of the game is "Riskoid", but that will change once I get around to thinking up a better name!

At the moment, I have a working prototype of the game, which I'm going to start letting people play. Normally, I wouldn't dream of releasing a game that is this unfinished, but as the game's so broad is scope, I figured the sooner people start playing it, the better it will become. On the other hand, it could be as buggy as all hell and people might hate it. I'm hoping for a nice middle ground!

The game is being developed using v2 of the cocos2d framework. Whilst the game is not that graphically intensive, I love the fact that with a few small tweaks, I can pump out both a Mac and iOS version using the same code. Sweet!

As the game needs quite a large screen to be sensibly played, I've decided to produce an iPad and Mac version, but not an iPhone version. There's no technical reason for not having an iPhone version, but I just think of the small screen, it might be too fiddly to play properly. No small screens for you!

Anyway, please come back periodically and check what I'm been up to!

Cheers,

Dean.