{
for (f=0; fmaxframe+1; f++)
destroy bitmap(sprite images[n][f]);
free(sprites[n]);
}
allegro exit();
return 0;
}
END OF MAIN()
9.2. Interrupt Handlers
This is a limited form ofmulti-threading in concept, although interrupt handlers do not run in parallel, but sequentially and based on the interval. Because no two interrupt handlers will ever be running at the same time, you don’t need to worry about corrupted data as you do with threading.
9.2.1 Creating an Interrupt Handler
After you have installed the timer using install timer, you can create one or more interrupt handlers using the install int function.
int install int(void (*proc)(), int speed)
9.2.1 The InterruptTest Program
Figure 9.2
The InterruptTest program demonstrates how you can use an interrupt callback function to calculate theframe rate.
Instead of using a ticks variable and the clock function to determine when to mark each second, this new program uses an interrupt handler that is set to 1,000 milliseconds to automatically tick off a second. To make things easier, I have modified the TimerTest program (which was quite lengthy) to use an interrupt instead of a simple timer;
Let’s get started on the changes, fewthat they are. The first change is up near the top of the program, where the counter, ticks, and framerate variables are declared. Add volatile to their definitions. This volatile key word tells the compiler that the variable should be protected because it is used by the interrupt.
/timer variables
volatile int counter;
volatile int ticks;
volatile int framerate;
Next, you need to add the interrupt handler callback function, timer1, to the
program. You can add this function right above main or up at the top of the
program, as long as it’s visible to main. Note how simple this function is; it
increments counter (for seconds), sets the framerate variable, and resets the ticks
variable.
//calculate framerate once per second
void timer1(void)
{
counter++;
framerate = ticks;
ticks=0;
}
END OF FUNCTION(timer1)
9.2.3 The TimedLoop Program
First, up near the top of the program, add another volatile variable.
//timer variables
volatile int counter;
volatile int ticks;
volatile int framerate;
volatile int resting, rested;
Scroll down to the timer1 interrupt callback function and add a line to it.
//calculate framerate every second
void timer1(void)
{
counter++;
framerate = ticks;
ticks=0;
rested = resting;
}
END OF FUNCTION(timer1)
Now you create the function that is called by rest callback. You can add this
function below timer1.
//do something while resting (?)
void rest1(void)
{
resting++;
}
The next change takes place in main, adding the code to call the rest callback
function, which is a call to rest1, just added. Note also the changes to the section
of code that displays the counter and frame rate. I have changed the last parameter of blit from 20 to 30 to erase the new line, which is also listed below,
highlighted in bold. This displays the number of ticks that transpired while the
program was waiting inside the rest1 callback function.
//update ticks
ticks++;
//slow the game down
resting=0;
rest callback(8, rest1);
//display framerate
Chia sẻ với bạn bè của bạn: