Allegro timer

From MWCSWiki

Jump to: navigation, search


Contents

Page Is Unavailable Due To Site Maintenance, Please Visit Reserve Copy Page


CLICK HERE


Put the function declaration and definition for the timer callback in your code and declare a global volatile int that will be used to poll the counter: <pre> volatile int count;

void timer() {

 count++;

} END_OF_FUNCTION(timer) </pre>

Any variable or memory region used by an Allegro timer must be locked, so lock any timers using the macros LOCK_FUNCTION and any variables the timer touches using LOCK_VARIABLE. This should be done in your init() function: <pre> void init() {

 /* ... */
 install_timer();
 install_keyboard();
 install_mouse();
 
 LOCK_VARIABLE(count);
 LOCK_FUNCTION(timer);

} </pre>

Finally, install the timer using install_int_ex(), which takes a pointer to the timer function you wish to install as its first parameter, and an integer specifying the number of ticks between timer intervals. Allegro provides the following macros to simplify converting from time units to hardware clock ticks: <pre> SECS_TO_TIMER(secs) - give the number of seconds between ticks MSEC_TO_TIMER(msec) - give the number of milliseconds between ticks BPS_TO_TIMER(bps) - give the number of ticks each second BPM_TO_TIMER(bpm) - give the number of ticks per minute </pre>

You can install a timer from your init() or from main(), for example, this would install a timer with a 3 second interval between ticks: <pre> install_int_ex(timer, SECS_TO_TIMER(3)); </pre>

Once you've done all this, polling the timer is as simple as checking the value of the global that the timer increments/decrements/etc: <pre> while(count<10) /* poll timer */ {

/* do something */

} </pre>

Example code

This simple code illustrates the usage of an allegro timer. <pre>

  1. include <allegro.h>

void init(); void deinit();

volatile int count;

void timer() {

     count++;

} END_OF_FUNCTION(timer)

int main() { init(); bool boom = false;

while (!key[KEY_ESC] & !boom) { textprintf_centre_ex(screen, font, SCREEN_W / 2, SCREEN_H / 2, 0xff0000, 0x0, "%d", count);

          vsync();                   

clear_bitmap(screen);

if(count > 10) /* poll timer */ {

                 /* do something */
                 textprintf_centre_ex(screen, font, SCREEN_W / 2, SCREEN_H / 2,
                                   0x0, 0xff0000, "KABOOM");
                 boom = true;
                 rest(3000);
           }

}

deinit(); return 0; } END_OF_MAIN()

void init() { int depth, res; allegro_init(); depth = desktop_color_depth(); if (depth == 0) depth = 32; set_color_depth(depth); res = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0); if (res != 0) { allegro_message(allegro_error); exit(-1); }

install_timer(); install_keyboard(); install_mouse();

LOCK_VARIABLE(count);

       LOCK_FUNCTION(timer);
       install_int_ex(timer, SECS_TO_TIMER(1));

}

void deinit() { clear_keybuf(); } </pre>

See also

Allegro Manual: Timer routines

Personal tools