Much progress was to be had over the weekend, also much frustration… but my game code pretty much works on my iPod now! I’m so excited 😀
First of all, I’ve decided to create a new law. Let’s call this law Colin’s First Law of New Platform Development. It is thus: “When developing on a new platform and your code fails in a spectacular fashion, don’t assume it is the platform’s fault for being new and strange. It is in fact because you’re an idiot and didn’t initialize a variable properly.” I’ll just go ahead and leave this here.
In other news, I will now tell you how to make a fairly decent method to shuttle input from the main thread to your game thread when working on the iPhone OS. It took me a while to hunt it down, so I’m sure if this ever shows up in Teh Googles someone might find it useful.
First of all, I created a class which is basically a FIFO of “event” objects. It’s pretty basic, but it will do the trick.
Initially, I figured all I needed was a boolean variable in the FIFO to let the system know if the FIFO was busy. Boy howdy was I wrong. You see, I discovered that the variable itself was being accessed simultaneously by the view controller and the game code causing it to crash horribly. So I’m like… clearly people do this sort of thing all the time, I just need to keep searching until I find a mechanism that allows this sort of thing. After some searching I learned about atomic operations, or operations that are guaranteed to happen within one processor cycle. This guarantees that only one thread will be operating on the variable at one time, thus preventing Michael Bay levels of explosive action in your code.
So, where do we find these atomic operations? Thankfully Apple has thoughtfully included some in the OS. I’m sure there are more elsewhere, but for my code I decided to use the functions in the scarily located
<libkern/OSAtomic.h>. Really though, it’s not so scary! What we’re going to do is use a spinlock to resolve resource contention (the resource being our FIFO). Basically we give our game code and the view controller a pointer to an
OSSpinLock variable and they both use
OSSpinLockLock(OSSpinLock *); to fight it out for who gets first crack at the buffer. The reason I chose OSAtomic functions is because they are relatively lightweight, and this sort of resource sharing is fairly simple.
Anyway! To get this to work, you need to declare an
OSSpinLock variable (it’s basically an integer) somewhere and pass a pointer to it to your view controller and your game code. Also you have to do the same with your event queue object. Because your view controller will probably be trying to send touch events before your game code has fully initialized, it is best to have the view controller call the functions to init the spinlock and the event queue.
A word of warning, most of my game code is in C++, as I’m really not very comfortable with Obj-C atm, but C++ code will happily co-exist with your Obj-C code. The only major caveat is if you’re calling C++ inside your Obj-C objects, the implementation files have to be .mm, not just .m so they are compiled as Objective-C++. Otherwise the compiler will just sit there and stare at you funny. This spinlock method should work with straight Obj-C too though!
Okay so I’ve created a few simple functions (in C!) to initialize and pass around the spinlock and the event queue. They live in eventsync.cpp:
#include "eventsync.h" //This includes OSAtomic.h and the header for the event queue
static OSSpinLock *lock; //static so it never goes away
static CGEventQueue *uiEventQueue; //same here
uiEventQueue = new CGEventQueue;
lock = OS_SPINLOCK_INIT;
Now that that’s sorted out it’s pretty easy to get started. In your view’s interface add a pointer for an
OSSpinLock and your queue object. Then in the view’s init code (in my case
getEventQueueAndInit() to set up your view’s pointers (in this case,
eventQueue). In your game code you’ll want to call
WARNING: Stargate references abound. Now that you’ve spanned your two threads with the code equivalent of a wormhole, just like in Stargate, they can only work in one direction. So your spinlock is going to act like a DHD and will activate the wormhole and send SG-1 in the right direction, hopefully avoiding killing some hapless team member in the process.
Basically what we will do is use
OSSpinLockLock() to try and set the spinlock variable. If the spinlock is already locked, it is in use somewhere else, so
OSSpinLockLock() will loop until it is no longer locked. It is our responsibility to unlock the spinlock in each thread when we’re done with it or everything will stop working.
So before this post gets even longer, here’s the upshot. When we overload
touchesEnded (or other input methods) we’re going to do something like this:
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
//when the lock is free we can safely push events into the queue
TouchEvent *ev = new TouchEvent;
ev->type = CGE_TOUCHDOWN;
ev->x = touchLocation.x;
ev->y = touchLocation.y;
OSSpinLockUnlock(lock);//important to release the lock!
In our game code we’d have something like:
ev = static_cast<TouchEvent *>(eventQueue->topEvent());
//do stuff with the event
//Handle other events here
It’s as simple as that!
Til next time