delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/18/06:22:13

Message-ID: <c=GB%a=_%p=Indigo_Active_Vi%l=CRIANLARICH-970618102600Z-25@crianlarich.Indigo>
From: Robert Humphris <r DOT humphris AT indigo-avs DOT com>
To: "'djgpp AT delorie DOT com'" <djgpp AT delorie DOT com>,
"'ao950 AT FreeNet DOT Carleton DOT CA'" <ao950 AT FreeNet DOT Carleton DOT CA>
Subject: RE: Callbacks
Date: Wed, 18 Jun 1997 11:26:00 +0100
Encoding: 74 TEXT

>
>Goretec3 (goretec3 AT aol DOT com) writes:
>> What are interrupt calbacks. I saw one when I was looking through some
>> allegro code. 
>
>Unless you are a wizard or some kind of guru... you don't want to know. ;-)

Sorry, but if thats your attitude then you are missing out on a
particularly effective method of
calling routines...

In C it is possible to have pointers not only to data, but to functions
as well, these are the pointers that are passed to interrupt handling
routines in Allegro and other such API's.  It works that when an
interrupt is processed, the function, which you registered the pointer
for is called.

You do not have to have these only for Interrupts, they are particularly
effective for state transition when you want to program objectively in
C....

/*	StateTransitions.c		*/
#include <stdlib.h>
#include <stdio.h>

/* were gonna model a fish object */
typedef int ( *FISH_STATE )( );

/* now we define the possible states */
int fishSwimming();
int fishEating();
int fishSleeping();
int fishProCreating();

FISH_STATE		pfnFishState;

/* the main function */
int main( void )
{
   int i;
   
   pfnFishState = fishSwimming;

   /* duty cycle */
   for( i=0; i<999; i++ )
   {
      /* call the function of the current state */
      pfnFishState();
   }
}

int fishSwimming()
{
   printf( "swimming\n" );
   /* is food near */
   if( food_is_near )
   {
      pfnFishState = fishEating;
   }

   if( time_for_bed )
   {
     pfnFishState = fishSleeping;
   }

   /* ..... and so on ..... */
}

Does this help at all?


Rob Humphris

>

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019