Mail Archives: djgpp/1997/06/18/06:22:13
>
>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 -