Skip to content

Example: al_extimer

Console
ts
/*
 *    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.  The first part of the example shows a basic use of
 *    timing using the blocking function rest(). The second part
 *    shows how to use three timers with different frequencies in
 *    a non blocking way.
 */

import "allegro-ts/global";
import { screen } from "allegro-ts";

/* these must be declared volatile so the optimiser doesn't mess up */
let x = 0;
let y = 0;
let z = 0;

/* timer interrupt handler */
function inc_x() {
  x++;
}
END_OF_FUNCTION(inc_x);

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

END_OF_FUNCTION(inc_y);

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

END_OF_FUNCTION(inc_z);

async function main() {
  let c: number;

  if (allegro_init() != 0) return 1;
  install_keyboard();
  install_timer();

  if (set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0) != 0) {
    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.value, makecol(255, 255, 255));

  textprintf_centre_ex(
    screen.value,
    font,
    SCREEN_W.value / 2,
    8,
    makecol(0, 0, 0),
    makecol(255, 255, 255),
    "Driver: %s",
    timer_driver.name,
  );

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

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

  textprintf_centre_ex(
    screen.value,
    font,
    SCREEN_W.value / 2,
    142,
    makecol(0, 0, 0),
    makecol(255, 255, 255),
    "Press a key to set up interrupts",
  );

  await 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 seconds a tick) */
  install_int_ex(inc_z, SECS_TO_TIMER(10));

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

    await rest(10);
  }

  return 0;
}

END_OF_MAIN();

// Start
export const run = () => {
  init_allegro_ts("canvas_id", main, {
    debug_element: "debug",
  });
};

View source on GitHub

Released under the MIT License.