Code Search for Developers
 
 
  

extimer.c from Allegro game programming library at Krugle


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

Allegro game programming library

Allegro is a cross-platform library intended for use in computer games and other types of multimedia programming.

Project homepage: http://sourceforge.net/projects/alleg
Programming language(s): Assembly,C,Shell Script
License: other

  allegro.pcx
  ex12bit.c
  ex3buf.c
  ex3d.c
  exaccel.c
  exalpha.c
  example.dat
  example.h
  examples.txt
  exbitmap.c
  exblend.c
  excamera.c
  excolmap.c
  excustom.c
  exdata.c
  exdbuf.c
  exdodgy.c
  exexedat.c
  exfixed.c
  exflame.c
  exflip.c
  exgui.c
  exhello.c
  exjoy.c
  exkeys.c
  exlights.c
  exmem.c
  exmidi.c
  exmouse.c
  expal.c
  expat.c
  exquat.c
  exrgbhsv.c
  exsample.c
  exscale.c
  exscn3d.c
  exscroll.c
  exshade.c
  exspline.c
  exsprite.c
  exstars.c
  exstream.c
  exswitch.c
  extimer.c
  extrans.c
  extruec.c
  exunicod.c
  exupdate.c
  exxfade.c
  exzbuf.c
  mysha.pcx
  planet.pcx
  running.dat
  running.h
  unifont.dat