File: extimer.c

package info (click to toggle)
allegro4 2%3A4.0.1-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 17,052 kB
  • ctags: 12,972
  • sloc: ansic: 109,525; asm: 16,672; cpp: 3,221; sh: 1,761; makefile: 556; pascal: 105; perl: 73
file content (104 lines) | stat: -rw-r--r-- 2,355 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 *    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++;
}

END_OF_FUNCTION(inc_x);



/* timer interrupt handler */
void inc_y(void)
{
   y++;
}

END_OF_FUNCTION(inc_y);



/* timer interrupt handler */
void inc_z(void)
{
   z++;
}

END_OF_FUNCTION(inc_z);



int main()
{
   int c;

   allegro_init();
   install_keyboard(); 
   install_timer();
   if (set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) != 0) {
      set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
      allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
      return 1;
   }
   set_palette(desktop_palette);
   clear_to_color(screen, makecol(255, 255, 255));
   text_mode(makecol(255, 255, 255));

   textprintf_centre(screen, font, SCREEN_W/2, 8, makecol(0, 0, 0),"Driver: %s", timer_driver->name);

   /* use rest() to delay for a specified number of milliseconds */
   textprintf_centre(screen, font, SCREEN_W/2, 48, makecol(0, 0, 0),"Timing five seconds:");

   for (c=1; c<=5; c++) {
      textprintf_centre(screen, font, SCREEN_W/2, 62+c*10, makecol(0, 0, 0),"%d", c);
      rest(1000);
   }

   textprintf_centre(screen, font, SCREEN_W/2, 142, makecol(0, 0, 0),"Press a key to set up interrupts");
   readkey();

   /* 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) */
   install_int(inc_x, 1000);

   /* or in beats per second (this is 10 ticks a second) */
   install_int_ex(inc_y, BPS_TO_TIMER(10));

   /* or in seconds (this is 10 ticks a second) */
   install_int_ex(inc_z, SECS_TO_TIMER(10));

   /* the interrupts are now active... */
   while (!keypressed())
      textprintf_centre(screen, font, SCREEN_W/2, 176, makecol(0, 0, 0),"x=%d, y=%d, z=%d", x, y, z);

   return 0;
}

END_OF_MAIN();