home *** CD-ROM | disk | FTP | other *** search
/ Game Programming - All in One (3rd Edition) / game_prog_all_in_one_3rd_ed.iso / sources / chapter06 / PlayMidi / main.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-09-13  |  1.4 KB  |  55 lines

  1. ////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Chapter 6 - PlayMidi program
  4. ////////////////////////////////////////////////////
  5.  
  6. #include <allegro.h>
  7.  
  8. #define MODE GFX_AUTODETECT_WINDOWED
  9. #define WIDTH 640
  10. #define HEIGHT 480
  11. #define WHITE makecol(255,255,255)
  12.  
  13. int main(void)
  14. {
  15.     MIDI *music;
  16.     int pos, length;
  17.  
  18.     //initialize the program
  19.     allegro_init();
  20.     install_keyboard(); 
  21.     install_timer();
  22.     set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0);
  23.     textout_ex(screen, font, "PlayMidi Program (Hit ESC to quit)", 0, 0, WHITE, -1);
  24.  
  25.    if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL) != 0) {
  26.       allegro_message("Error initializing sound system\n%s\n", allegro_error);
  27.       return 1;
  28.    }
  29.  
  30.     //load the midi file
  31.     music = load_midi("mach5.mid");
  32.     if (!music) {
  33.         allegro_message("Error loading Midi file");
  34.         return 1;
  35.     }
  36.     if (play_midi(music, 0) != 0) {
  37.         allegro_message("Error playing Midi\n%s", allegro_error);
  38.         return 1;
  39.     }
  40.  
  41.     length = get_midi_length(music);
  42.     do {
  43.          pos = midi_time;
  44.          textprintf_ex(screen, font, 0, 20, WHITE, -1, "Length:   %d:%02d", length / 60, length % 60);
  45.          textprintf_ex(screen, font, 0, 30, WHITE, -1, "Position: %d:%02d", pos / 60, pos % 60);
  46.          rest(20);
  47.       } while((pos <= length) && (!key[KEY_ESC]));
  48.  
  49.     stop_midi();
  50.     destroy_midi(music);
  51.     allegro_exit();
  52.     return 0;
  53. }
  54. END_OF_MAIN()
  55.