Listing 6. Source code for the program named Mouse01.
/*Project Mouse01 Illustrates how to handle mouse presses and mouse drags. The program displays a black 440x500 graphics window. Pointing to the black area and pressing the left mouse button causes the mouse pointer to disappear and a red filled circle to be drawn at the location of the mouse pointer. Point to the black area and pressing the right mouse button causes the mouse pointer to disappear and a green filled circle to be drawn at the location of the mouse pointer. Dragging slowly with the right or left mouse button pressed produces a wide red or green line (depending on which button is pressed) that traces out the path of the mouse pointer. Dragging rapidly with a mouse button pressed may produce a non-uniform sequence of red or green filled circles that trace out the path of the mouse pointer. Releasing the mouse button causes the mouse pointer to reappear. Pressing the Esc key causes the program to terminate. ********************************************************/ #include <allegro.h> //LOOK! NO GLOBAL VARIABLES bool mouseInfo(int *xCoor,int *yCoor,int *color){ //Get current mouse coordinates and send back via // pointer variables. *xCoor = mouse_x; *yCoor = mouse_y; //Test for a mouse button pressed. Set drawing color // based on left or right mouse button. if(mouse_b & 1){//left mouse button *color = makecol(255,0,0);//red return true;//signal the requirement to draw }else if(mouse_b & 2){//right button *color = makecol(0,255,0);//green return true; }else{//no button return false;//no drawing required }//end else }//end mouseInfo //------------------------------------------------------// void draw(int xCoor,int yCoor,BITMAP* buf,int color){ show_mouse(NULL);//hide mouse pointer while drawing //Draw filled circle at mouse coor on off-screen buffer. circlefill(buf,xCoor,yCoor,5,color); //Copy the off-screen image to the screen. blit(buf,screen,0,0,0,0,440,500); show_mouse(screen);//show mouse when drawing finished }//end draw //------------------------------------------------------// int main(){ //NOTE THE USE OF LOCAL VARIABLES IN PLACE OF GLOBALS BITMAP* screenBuf = NULL; int xCoor = 0; int yCoor = 0; int color = 0; allegro_init(); install_mouse(); install_keyboard(); set_color_depth(32); set_gfx_mode( GFX_AUTODETECT_WINDOWED,440,500,0,0); screenBuf = create_bitmap(440,500); show_mouse(screen); while(!key[KEY_ESC]){ while(mouseInfo(&xCoor,&yCoor,&color)){ //Draw while mouse button is down. draw(xCoor,yCoor,screenBuf,color); }//end while }//end while return 0; }//end main END_OF_MAIN(); |