On Jan 19, 2010, at 6:10 AM, Per Bull Holmen wrote:
I have made one class called GameController, which is overridden by subclasses to control one specific type of game (PS: I will soon rename the class "Controller" to "MainController", and instance variable "controller" to "mainController"). The GameController base class also mediates between input from the main thread, and the game thread, with help from the class InputBuffer:
You've basically implemented coroutines, actually a simple form of an actor, using multiple threads. Each coroutine is running an event loop, and the game one blocks waiting for incoming events from the UI one.
(Ideally it's possible to implement this without using multiple threads; I tried to do that about two years ago using the low-level ucontext library, but it turns out that the stack manipulation it does is incompatible with Objective-C's exception handling system.)
This is a fine way to do things, except that you can't call AppKit from the game thread, and in general have to be careful about accessing shared state. More subtly, you've written your game code with the expectation that, each time it waits for user input, there is only one type of input the user can enter. For example, you wait for the user to enter a new note to play. If the game gets more complex, there may be multiple types of things the user can do (maybe deleting or editing notes) and then your code has to add something like a switch() statement to handle each possible action. In my experience, this quickly becomes unwieldy and results in spaghetti code, at which point the regular asynchronous event-loop mechanism becomes cleaner. (Historically this is a prime reason why GUI libraries use it in the first place.)
—Jens_______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com