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

  1. /////////////////////////////////////////////////////////////////////////
  2. // Game Programming All In One, Third Edition
  3. // Chapter 3 - Polygons Program
  4. /////////////////////////////////////////////////////////////////////////
  5.  
  6. #include <allegro.h>
  7.  
  8. int main(void)
  9. {
  10.     int vertices[8];
  11.     int red,green,blue,color;
  12.     int ret;
  13.     
  14.     //initialize everything
  15.     allegro_init(); 
  16.     install_keyboard(); 
  17.     install_timer();
  18.     srand(time(NULL));
  19.  
  20.     //initialize video mode to 640x480
  21.     ret = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
  22.     if (ret != 0) {
  23.         allegro_message(allegro_error);
  24.         return 1;
  25.     }
  26.  
  27.     //display screen resolution
  28.     textprintf_ex(screen, font, 0, 0, 15, -1,
  29.         "Polygons Program - %dx%d - Press ESC to quit", 
  30.         SCREEN_W, SCREEN_H);
  31.  
  32.     //wait for keypress
  33.     while(!key[KEY_ESC])
  34.     {
  35.         //set a random location
  36.         vertices[0] = 10 + rand() % (SCREEN_W-20);
  37.         vertices[1] = 10 + rand() % (SCREEN_H-20);
  38.         vertices[2] = vertices[0] + (rand() % 30)+50;
  39.         vertices[3] = vertices[1] + (rand() % 30)+50;
  40.         vertices[4] = vertices[2] + (rand() % 30)-100;
  41.         vertices[5] = vertices[3] + (rand() % 30)+50;
  42.         vertices[6] = vertices[4] + (rand() % 30);
  43.         vertices[7] = vertices[5] + (rand() % 30)-100;
  44.         
  45.         //set a random color
  46.         red = rand() % 255;
  47.         green = rand() % 255;
  48.         blue = rand() % 255;
  49.         color = makecol(red,green,blue);
  50.         
  51.         //draw the pixel
  52.         polygon(screen,4,vertices,color);
  53.         
  54.         rest(50); //replaced sleep()
  55.     }
  56.  
  57.     //end program
  58.     allegro_exit();
  59.     return 0;
  60. }
  61. END_OF_MAIN()
  62.