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

  1. /////////////////////////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Chapter 3 - FloodFill Program
  4. /////////////////////////////////////////////////////////////////////////
  5.  
  6. #include <allegro.h>
  7.  
  8. int main(void)
  9. {
  10.     int x = 100, y = 100;
  11.     int xdir = 10, ydir = 10;
  12.     int red,green,blue,color;
  13.     int radius = 50;
  14.     int ret;
  15.     
  16.     //initialize some things
  17.     allegro_init(); 
  18.     install_keyboard(); 
  19.     install_timer();
  20.  
  21.     //initialize video mode to 640x480
  22.     ret = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
  23.     if (ret != 0) {
  24.         allegro_message(allegro_error);
  25.         return 1;
  26.     }
  27.  
  28.     //display screen resolution
  29.     textprintf_ex(screen, font, 0, 0, 15, -1,
  30.         "FloodFill Program - %dx%d - Press ESC to quit", 
  31.         SCREEN_W, SCREEN_H);
  32.  
  33.     //wait for keypress
  34.     while(!key[KEY_ESC])
  35.     {
  36.         //update the x position, keep within screen
  37.         x += xdir;
  38.         if (x > SCREEN_W-radius)
  39.         {
  40.             xdir = -10;
  41.             radius = 10 + rand() % 40;
  42.             x = SCREEN_W-radius;
  43.         }
  44.         if (x < radius)
  45.         {
  46.             xdir = 10;
  47.             radius = 10 + rand() % 40;
  48.             x = radius;
  49.         }
  50.  
  51.         //update the y position, keep within screen
  52.         y += ydir;
  53.         if (y > SCREEN_H-radius)
  54.         {
  55.             ydir = -10;
  56.             radius = 10 + rand() % 40;
  57.             y = SCREEN_H-radius;
  58.         }
  59.         if (y < radius+20)
  60.         {
  61.             ydir = 10;
  62.             radius = 10 + rand() % 40;
  63.             y = radius+20;
  64.         }
  65.     
  66.         //set a random color
  67.         red = rand() % 255;
  68.         green = rand() % 255;
  69.         blue = rand() % 255;
  70.         color = makecol(red,green,blue);
  71.         
  72.         //draw the circle, pause, then erase it
  73.         circle(screen, x, y, radius, color);
  74.         floodfill(screen, x, y, color);
  75.         rest(20); // replaced sleep()
  76.         rectfill(screen, x-radius, y-radius, x+radius, y+radius, 0);
  77.     }
  78.  
  79.     //end program
  80.     allegro_exit();
  81.     return 0;
  82. }
  83. END_OF_MAIN()
  84.