Show extimer.c syntax highlighted
/*
* Example program for the Allegro library, by Shawn Hargreaves.
*
* This program demonstrates how to use the timer routines.
* These can be a bit of a pain, because you have to be sure you lock
* all the memory that is used inside your interrupt handlers.
*/
#include "allegro.h"
/* these must be declared volatile so the optimiser doesn't mess up */
volatile int x = 0;
volatile int y = 0;
volatile int z = 0;
/* timer interrupt handler */
void inc_x(void)
{
x++;
}
AL_END_OF_FUNCTION(inc_x);
/* timer interrupt handler */
void inc_y(void)
{
y++;
}
AL_END_OF_FUNCTION(inc_y);
/* timer interrupt handler */
void inc_z(void)
{
z++;
}
AL_END_OF_FUNCTION(inc_z);
int main()
{
int c;
allegro_init();
al_install_keyboard();
al_install_timer();
if (al_set_gfx_mode(AL_GFX_SAFE, 320, 200, 0, 0) != 0) {
al_set_gfx_mode(AL_GFX_NONE, 0, 0, 0, 0);
al_show_message("Unable to set any graphic mode\n%s\n", al_error);
return 1;
}
al_set_palette(al_desktop_palette);
al_clear_to_color(al_screen, al_make_color(255, 255, 255));
al_text_mode(al_make_color(255, 255, 255));
al_printf_text_centre(al_screen, al_font_8x8, AL_SCREEN_W/2, 8, al_make_color(0, 0, 0),"Driver: %s", timer_driver->name);
/* use al_rest() to delay for a specified number of milliseconds */
al_printf_text_centre(al_screen, al_font_8x8, AL_SCREEN_W/2, 48, al_make_color(0, 0, 0),"Timing five seconds:");
for (c=1; c<=5; c++) {
al_printf_text_centre(al_screen, al_font_8x8, AL_SCREEN_W/2, 62+c*10, al_make_color(0, 0, 0),"%d", c);
al_rest(1000);
}
al_printf_text_centre(al_screen, al_font_8x8, AL_SCREEN_W/2, 142, al_make_color(0, 0, 0),"Press a al_key to set up interrupts");
al_read_key();
/* all variables and code used inside interrupt handlers must be locked */
LOCK_VARIABLE(x);
LOCK_VARIABLE(y);
LOCK_VARIABLE(z);
LOCK_FUNCTION(inc_x);
LOCK_FUNCTION(inc_y);
LOCK_FUNCTION(inc_z);
/* the speed can be specified in milliseconds (this is once a second) */
al_create_timer(inc_x, MSEC_TO_TIMER(1000));
/* or in beats per second (this is 10 ticks a second) */
al_create_timer(inc_y, BPS_TO_TIMER(10));
/* or in seconds (this is 10 ticks a second) */
al_create_timer(inc_z, SECS_TO_TIMER(10));
/* the interrupts are now active... */
while (!al_key_pressed())
al_printf_text_centre(al_screen, al_font_8x8, AL_SCREEN_W/2, 176, al_make_color(0, 0, 0),"x=%d, y=%d, z=%d", x, y, z);
return 0;
}
AL_END_OF_MAIN();
See more files for this project here