Skip to main content
gamedev.net
🔒 Locked

iPhone programming: Thinking while remaining responsive

Started by alvaro Jun 22, 2010 at 6:24 PM 2 replies 950+ views
Original Post
alvaro
alvaro
A couple of friends and I are considering writing small board-game apps for the iPhone (think "chess" or "backgammon"). I just started playing around with the iPhone SDK, I bought a couple of books and I think I understand basically how iPhone programs work.

Our programs will have one or more AI opponents, and we would like the program to be able to perform computations (alpha-beta or MCTS, most likely) while the program remains responsive to user input. The books I bought don't go deep enough into how things work, so I don't really know what my options are. I looked at Apple's online documentation, but I don't really like what it describes.

So ideally I would like to be able to call the event handler "by hand" from my AI code, so I can keep everything in a single thread, but I haven't found a way to do that. Is this even possible?

Alternatively, I could run the AI concurrently. Apple's documentation describes too many ways to do this (at least these: threads, operation objects, GCD, idle-time notifications, asynchronous functions).

Do you have experience with these options? Any recommendations or thoughts?

Palidine
Palidine
You can always just have the touches functions set flags or push input into a queue which you check for in your main loop. Standard single-threaded PC game design. That's what I do. Then your update loop just chugs away normally, you time-slice your AI calculations so that if more than time has passed, stop processing and wait until next frame.

Your main loop ends up looking algorithmically something like:
void tick(){    while (gameRunning)    {        if ( inputWaiting() )        {            processInput();        }        updateAI();        render();    }}void updateAI(){    timeRemainingThisFrame = frameBudget;    while ( timeRemainingThisFrame > 0 )    {        stepUpdate();        timeRemainingThisFrame -= amountOfTimeForThisIteration;    }}


-me
alvaro
alvaro
Quote:
Original post by Palidine
You can always just have the touches functions set flags or push input into a queue which you check for in your main loop. Standard single-threaded PC game design. That's what I do. Then your update loop just chugs away normally, you time-slice your AI calculations so that if more than time has passed, stop processing and wait until next frame.

Your main loop ends up looking algorithmically something like:
*** Source Snippet Removed ***

-me


Hmmm... That's basically what I do when implementing a UCI chess engine, so I should be able to make it work. Since I have very little experience writing GUIs I thought perhaps there was some better method that I was missing.

Thanks for your reply.

Palidine
Palidine
Quote:
Original post by alvaro
Hmmm... That's basically what I do when implementing a UCI chess engine, so I should be able to make it work. Since I have very little experience writing GUIs I thought perhaps there was some better method that I was missing.

Thanks for your reply.


Well, I mean it depends on what level you're implementing your UI. If you're just doing OpenGL it often makes sense just to roll your own in game UI. If you're layering Apple's UIKit stuff on top then you can just create your own delegates (Controllers for the various Views). In those controllers you can just implement the proper input override functions to execute your logic. They'll get called whenever the iPhone hands the Views the events. I prefer to just roll my own UI since it's more straight forward for me.

-me

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.