PROGRAM Sound; (* The sound example. *) USES allegro5, al5font, al5primitives, al5audio, al5acodec; (* Helper to test intialization. *) PROCEDURE MustInit (aTest: BOOLEAN; aDescription: STRING); BEGIN IF NOT aTest THEN BEGIN WriteLn ('couldn''t initialize ', aDescription, '.'); WriteLn ('Press [Enter] to close.'); ReadLn; Halt (1) END END; VAR Timer: ALLEGRO_TIMERptr; Queue: ALLEGRO_EVENT_QUEUEptr; Event: ALLEGRO_EVENT; Display: ALLEGRO_DISPLAYptr; Font: ALLEGRO_FONTptr; Redraw, EndProgram: BOOLEAN; Elephant: ALLEGRO_SAMPLEptr; Music: ALLEGRO_AUDIO_STREAMptr; BEGIN { Initialization. } MustInit (al_init, 'Allegro'); MustInit (al_install_keyboard, 'keyboard'); Timer := al_create_timer (1.0 / 30.0); MustInit (Assigned (Timer), 'timer'); { Create display. } al_set_new_display_option (ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); al_set_new_display_option (ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST); al_set_new_bitmap_flags (ALLEGRO_MIN_LINEAR OR ALLEGRO_MAG_LINEAR); Display := al_create_display (640, 480); MustInit (Assigned (Display), 'display'); { Init/load graphics. } Font := al_create_builtin_font; MustInit (Font <> NIL, 'font'); MustInit (al_init_primitives_addon, 'primitives'); { Init sound. } MustInit (al_install_audio, 'audio'); MustInit (al_init_acodec_addon, 'audio codecs'); MustInit (al_reserve_samples (16), 'reserve samples'); { Load sound. } Elephant := al_load_sample ('elephant.wav'); MustInit (Assigned (Elephant), 'elephant.wav'); { Music. } Music := al_load_audio_stream ('music.opus', 2, 2048); MustInit (Assigned (Music), 'music.opus'); al_set_audio_stream_playmode (Music, ALLEGRO_PLAYMODE_LOOP); al_attach_audio_stream_to_mixer (Music, al_get_default_mixer); { Create event queue. } Queue := al_create_event_queue; MustInit (Assigned (Queue), 'queue'); al_register_event_source (Queue, al_get_keyboard_event_source); al_register_event_source (Queue, al_get_display_event_source(Display)); al_register_event_source (Queue, al_get_timer_event_source (Timer)); { Initialize loop stuff. } Redraw := TRUE; EndProgram := FALSE; al_start_timer (Timer); { Main loop. } REPEAT al_wait_for_event (Queue, @Event); CASE Event.ftype OF ALLEGRO_EVENT_TIMER: { Game logic goes here. } Redraw := TRUE; ALLEGRO_EVENT_KEY_DOWN: BEGIN IF Event.keyboard.keycode = ALLEGRO_KEY_E THEN al_play_sample (Elephant, 1, 0, 1, ALLEGRO_PLAYMODE_ONCE, NIL) ELSE IF Event.keyboard.keycode = ALLEGRO_KEY_ESCAPE THEN EndProgram := TRUE END; ALLEGRO_EVENT_DISPLAY_CLOSE: EndProgram := TRUE; END; { If must redraw and there's nothing pending... } IF Redraw AND al_is_event_queue_empty (Queue) THEN BEGIN al_clear_to_color (al_map_rgb (0, 0, 0)); al_draw_text ( Font, al_map_rgb (255, 255, 255), 640 DIV 2, 480 DIV 2, ALLEGRO_ALIGN_CENTER, 'Knock knock, it''s Nelly' ); { Drawing was done in a backbuffer. } al_flip_display; { Updated. } Redraw := FALSE END UNTIL EndProgram; { Release resources. } al_destroy_audio_stream (Music); al_destroy_sample (Elephant); al_destroy_font (Font); al_destroy_display (Display); al_destroy_timer (Timer); al_destroy_event_queue (Queue) END.