Listing 8. Source code for the program named MovableHelloWorld01.
//Project MovableHelloWorld01 //Move text around the screen by pressing the arrow keys. //Press Esc to terminate the program. //Note that this program is based on polling and not on //handling events. #include <allegro.h> int main(){ int x = 20,y = 30;//position coordinates for text allegro_init();//Allegro initialization install_keyboard();//Set up for keyboard input //Set the graphics mode to a 320x240-pixel window. set_gfx_mode(GFX_AUTODETECT_WINDOWED, 320,240,0,0); while(!key[KEY_ESC]){//loop until user presses Esc key clear_keybuf();//clear old stuff from the buffer //Draw black text on the existing text to erase it. textout_ex( screen,//Specify bitmap on the screen font,//Use a default font "HELLO WORLD",//Specify the text to display x,//x-coordinate for text y,//y-coordinate for text makecol(0,0,0),//Color text black -1 );//Transparent text background. //Modify coordinate variables if user presses arrow. // Note that diagonal motion resulting from holding // two keys down is not supported. if (key[KEY_UP]) --y; else if (key[KEY_DOWN]) ++y; else if (key[KEY_RIGHT]) ++x; else if (key[KEY_LEFT]) --x; //Draw fresh text on the window at the new coordinate // position. textout_ex( screen,//Specify bitmap on the screen font,//Use a default font "HELLO WORLD",//Specify the text to display x,//x-coordinate for text y,//y-coordinate for text makecol(255,255,0),//Color text yellow -1 );//Transparent text background. rest(10);//Give up the cpu for ten milliseconds. }//end while loop return 0;//Return 0 to indicate a successful run. }//end main function //Required macro at end of main. END_OF_MAIN() |