home *** CD-ROM | disk | FTP | other *** search
/ Game Programming - All in One (3rd Edition) / game_prog_all_in_one_3rd_ed.iso / sources / chapter03 / DoLines / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-09-10  |  1.9 KB  |  82 lines

  1. /////////////////////////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Chapter 3 - DoLines Program
  4. /////////////////////////////////////////////////////////////////////////
  5.  
  6. #include <allegro.h>
  7.  
  8. int stop = 0;
  9.  
  10. //doline is the callback function for do_line
  11. void doline(BITMAP *bmp, int x, int y, int color)
  12. {
  13.     if (!stop)
  14.     {
  15.         if (getpixel(bmp,x,y) == 0)
  16.         {
  17.             putpixel(bmp, x, y, color);
  18.             rest(5);
  19.         }
  20.         else
  21.         {
  22.             stop = 1;
  23.             circle(bmp, x, y, 5, 7);
  24.         }
  25.     }
  26. }
  27.  
  28. int main(void)
  29. {
  30.     int x1,y1,x2,y2;
  31.     int red,green,blue,color;
  32.     int ret;
  33.     
  34.     //initialize Allegro
  35.     allegro_init(); 
  36.     
  37.     install_timer();
  38.     srand(time(NULL));
  39.     
  40.     //initialize the keyboard
  41.     install_keyboard(); 
  42.  
  43.     //initialize video mode to 640x480
  44.     ret = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
  45.     if (ret != 0) {
  46.         allegro_message(allegro_error);
  47.         return 1;
  48.     }
  49.  
  50.     //display screen resolution
  51.     textprintf_ex(screen, font, 0, 0, 15, -1,
  52.         "DoLines Program - %dx%d - Press ESC to quit", 
  53.         SCREEN_W, SCREEN_H);
  54.  
  55.     //wait for keypress
  56.     while(!key[KEY_ESC])
  57.     {
  58.         //set a random location
  59.         x1 = 10 + rand() % (SCREEN_W-20);
  60.         y1 = 10 + rand() % (SCREEN_H-20);
  61.         x2 = 10 + rand() % (SCREEN_W-20);
  62.         y2 = 10 + rand() % (SCREEN_H-20);
  63.  
  64.         //set a random color
  65.         red = rand() % 255;
  66.         green = rand() % 255;
  67.         blue = rand() % 255;
  68.         color = makecol(red,green,blue);
  69.         
  70.         //draw the line using the callback function
  71.         stop = 0;
  72.         do_line(screen,x1,y1,x2,y2,color,*doline);
  73.         
  74.         rest(200);
  75.     }
  76.  
  77.     //end program
  78.     allegro_exit();
  79.     return 0;
  80. }
  81. END_OF_MAIN()
  82.