Re: Keeping program flow moving nicely



On Sat, Feb 10, 2001 at 12:03:26PM -0800, Timothy M. Shead wrote:
> Use gtk_timeout_add() to create a 'timeout' signal that gets called, 
> say, every 100 milliseconds or so.  In your handler for the timeout 
> event you decide whose turn it is and call process_move().  The timeout 
> event will get called frequently enough that there won't be any 
> noticeable delay, between moves but you won't be dragging down the CPU 
> (as you would if you used an idle handler).  At the same time, normal 
> events will continue to be processed without any intervention on your 
> behalf, so the human user can hit pause, cancel, or whatever.

Thanks :) That would certainly be a better solution.

In fact, I got what I think is the best soln yet going - adding a 
one-shot idle loop function which gets removed before it's needed 
again. That way we call the computer move function exactly as often 
as we need to, and outstanding events are handled. Thanks rambokid :)

It was pretty easy - the process_move() section above changes to 
gint process_move(gint move)
{
static int source_id=0;
...
    switch_players();
    if (is_computer(currentPlayer))
    {
        if (source_id !=0)
        {
            g_source_remove(sourceid);
        }
        g_idle_add(play_computer_move, NULL);
    }
    return 0;
}

where play_computer_move() returns FALSE, and consists of one line 
which is a call to process_move().

I'm not sdure if this is The Right Way (TM) to handle this kind of 
situation, but it worked :)
 
> Regards,
> Timothy M. Shead

Thanks,
Dave Neary.

-- 
  .------------------------------.
 /          David Neary,          \
|     E-Mail dneary eircom net     | 
 \     Phone +353-1-872-0654      /
  `------------------------------'




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]