Purple Martians
Technical Code Descriptions

Events
Overview Processing the event queue Processing events
Overview When I converted my game from Allegro 4, I had to do a lot of things differently. Allegro 5 is event based. The event queue has to be processed regularily. All input comes from events, rather than being updated asyncronously in the background in Allegro 4. Processing the event queue The function 'mEventQueue.proc()' is called from whatever loop the program is currently in. It is called in the main loop when the game is running, and in the menu loop when the menu is running. It needs to be called any time the program is in a blocking loop that needs input.
void mwEventQueue::proc(int pch)
{
   mInput.key_pressed_ASCII = 0;
   while (!al_is_event_queue_empty(event_queue))
   {
      ALLEGRO_EVENT ev;
      al_get_next_event(event_queue, &ev);
      proc_events(ev);
   }
   if (pch) mInput.proc_keys_held();
}
Processing events
void mwEventQueue::proc_events(ALLEGRO_EVENT ev)
{
   if (ev.type == ALLEGRO_EVENT_DISPLAY_RESIZE) mDisplay.proc_display_change();
   if (ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) mMain.fast_exit();
   if (ev.type == ALLEGRO_EVENT_TIMER)
   {
      if (ev.timer.source == fps_timer) program_update = 1;
      if (ev.timer.source == sec_timer) program_update_1s = 1;
      if (ev.timer.source == mnu_timer) menu_update = 1;
      if (ev.timer.source == mou_timer)
      {
         if (mLoop.level_editor_running) al_set_timer_count(mEventQueue.mou_timer, 0);
         if (al_get_timer_count(mEventQueue.mou_timer) > 2) al_hide_mouse_cursor(mDisplay.display);
      }
   }
   mInput.proc_input_events(ev);  // send all other events to input handler
}
Display events are sent to their appropriate handler. Timer events set flags when they occur, or process the mouse hiding algorithm. See timers. Input events are processed by: 'mInput.proc_input_events(ev)' See input.
void mwInput::proc_input_events(ALLEGRO_EVENT ev)
{
   if (ev.type == ALLEGRO_EVENT_MOUSE_WARPED)
   {
      mouse_x = ev.mouse.x / mDisplay.display_transform_double;
      mouse_y = ev.mouse.y / mDisplay.display_transform_double;
   }
   if (ev.type == ALLEGRO_EVENT_MOUSE_AXES)
   {
      mouse_x = ev.mouse.x / mDisplay.display_transform_double;
      mouse_y = ev.mouse.y / mDisplay.display_transform_double;
      mouse_z = ev.mouse.z / mDisplay.display_transform_double;
      mouse_dx = ev.mouse.dx;
      mouse_dy = ev.mouse.dy;
      mouse_dz = ev.mouse.dz;
      al_show_mouse_cursor(mDisplay.display);
      al_set_timer_count(mEventQueue.mou_timer, 0);
   }

   if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN)
   {
      if (ev.mouse.button == 1) mouse_b[1][0] = true;
      if (ev.mouse.button == 2) mouse_b[2][0] = true;
      if (ev.mouse.button == 3) mouse_b[3][0] = true;
      if (ev.mouse.button == 4) mouse_b[4][0] = true;
      al_show_mouse_cursor(mDisplay.display);
      al_set_timer_count(mEventQueue.mou_timer, 0);
      key_or_mouse_pressed = 1;
   }
   if (ev.type == ALLEGRO_EVENT_MOUSE_BUTTON_UP)
   {
      if (ev.mouse.button == 1) mouse_b[1][0] = false;
      if (ev.mouse.button == 2) mouse_b[2][0] = false;
      if (ev.mouse.button == 3) mouse_b[3][0] = false;
      if (ev.mouse.button == 4) mouse_b[4][0] = false;
   }
   if (ev.type == ALLEGRO_EVENT_KEY_DOWN)
   {
      key_pressed = 1;
      key_or_mouse_pressed = 1;
      int k = ev.keyboard.keycode;
      key[k][0] = true;
   }
   if (ev.type == ALLEGRO_EVENT_KEY_UP)
   {
      int k = ev.keyboard.keycode;
      key[k][0] = false;
   }
   if (ev.type == ALLEGRO_EVENT_KEY_CHAR)
   {
      key_pressed_ASCII = ev.keyboard.unichar;
      serial_key_check(key_pressed_ASCII);
      mEventQueue.menu_update = 1;
      //printf("key_pressed_ASCII:%d\n", key_pressed_ASCII);
   }
   if (ev.type == ALLEGRO_EVENT_JOYSTICK_AXIS)
   {
      int jy = getJoystickNum(ev.joystick.id);
      int jo = 0; // offset
      if (jy == 0) jo = 0;
      if (jy == 1) jo = 20;
      int ax = ev.joystick.axis;
      float pos = ev.joystick.pos;
      if (ax == 0) // x axis
      {
         key[130+jo][0] = false;
         key[131+jo][0] = false;
         if (pos > 0) key[131+jo][0] = true;
         if (pos < 0) key[130+jo][0] = true;
      }
      if (ax == 1) // y axis
      {
         key[128+jo][0] = false;
         key[129+jo][0] = false;
         if (pos > 0) key[129+jo][0] = true;
         if (pos < 0) key[128+jo][0] = true;
      }
   }
   if (ev.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN)
   {
      int jy = getJoystickNum(ev.joystick.id);
      int sc = get_scan_code_from_joystick(jy, 1, ev.joystick.button);
      key[sc][0] = true;
   }
   if (ev.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_UP)
   {
      int jy = getJoystickNum(ev.joystick.id);
      int sc = get_scan_code_from_joystick(jy, 1, ev.joystick.button);
      key[sc][0] = false;
   }
}