PROGRAM Input; (* How to get user input. *) USES allegro5, al5font, al5primitives; CONST { Key statuses. } KEY_SEEN = 1; KEY_RELEASED = 2; (* 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; x, y: REAL; Key: ARRAY [0..ALLEGRO_KEY_MAX] OF BYTE; Ndx: INTEGER; 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 (Assigned (Font), 'font'); MustInit (al_init_primitives_addon, 'primitives'); { 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)); { Initial position. } x := 100; y := 100; { Key statuses. } FOR Ndx := 0 TO ALLEGRO_KEY_MAX DO Key[Ndx] := 0; { 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: BEGIN IF Key[ALLEGRO_KEY_UP] <> 0 THEN y := y - 1; IF Key[ALLEGRO_KEY_DOWN] <> 0 THEN y := y + 1; IF Key[ALLEGRO_KEY_LEFT] <> 0 THEN x := x - 1; IF Key[ALLEGRO_KEY_RIGHT] <> 0 THEN x := x + 1; IF Key[ALLEGRO_KEY_ESCAPE] <> 0 THEN EndProgram := TRUE; FOR Ndx := 0 TO ALLEGRO_KEY_MAX DO Key[Ndx] := Key[Ndx] AND KEY_SEEN; Redraw := TRUE END; ALLEGRO_EVENT_KEY_DOWN: Key[Event.keyboard.keycode] := KEY_SEEN OR KEY_RELEASED; ALLEGRO_EVENT_KEY_UP: Key[Event.keyboard.keycode] := Key[Event.keyboard.keycode] AND KEY_RELEASED; ALLEGRO_EVENT_DISPLAY_CLOSE: { Close the program. } 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_textf ( Font, al_map_rgb (255, 255, 255), 0, 0, 0, 'X: %.1f Y: %.1f', [x, y] ); al_draw_filled_rectangle (x, y, x + 10, y + 10, al_map_rgb (255, 0, 0)); { Drawing was done in a backbuffer. } al_flip_display; { Updated. } Redraw := FALSE END UNTIL EndProgram; al_destroy_font (Font); al_destroy_display (Display); al_destroy_timer (Timer); al_destroy_event_queue (Queue) END.