Complete program listing

A complete listing of the program discussed in this lesson is shown in Listing 10.

Listing 10. Source code for the project named DrawingPrimitives01.
//Project DrawingPrimitives01
//Draws a variety of shapes, lines, fills, etc., on a
// window.
//See Drawing Primitives at
//http://www.allegro.cc/manual/api/drawing-primitives/
#include <allegro.h>

int main(){
  //Do the preliminaries.
  allegro_init();
  install_keyboard();

  //Set the graphics mode to a window.
  //Note that not all possible combinations of width
  // and height can be used successfully in windowed
  // mode. I'm not certain how to determine which are
  // and which are not allowable short of
  // experimentation.
  set_gfx_mode(GFX_AUTODETECT_WINDOWED,400,400,0,0);

  //Set the background color of the window to light gray
  clear_to_color(screen,makecol(224,224,224));

  //Draw three lines of red, green, and blue pixels from
  // the lower left to the upper right of the window.
  for(int cnt = 0;cnt < 400;cnt += 3){
    putpixel(screen,cnt,400-cnt,makecol(255,0,0));
    putpixel(screen,cnt-1,400-cnt-1,makecol(0,255,0));
    putpixel(screen,cnt+1,400-cnt+1,makecol(0,0,255));
  }//end for loop
  
  //Inscribe a large red circle in the window.
  circle(screen,200,200,200,makecol(255,0,0));
  
  //Draw another red circle inside of that one.
  circle(screen,200,200,190,makecol(255,0,0));
  
  //Fill the area between the two circles with blue.
  floodfill(screen,5,200,makecol(0,0,255));
  
  //Draw a green filled circle.
  circlefill(screen,100,200,50,makecol(0,255,0));

  //Draw an empty blue rectangle.
  rect(screen,150,75,250,125,makecol(0,0,255));
  
  //Draw a filled rectangle where the color is turquoise
  rectfill(screen,175,250,225,350,makecol(0,255,255));

  //Draw black horizontal and vertical lines that
  // divide the window into quadrants of equal size.
  line(screen,0,200,400,200,makecol(0,0,0));
  line(screen,200,0,200,400,makecol(0,0,0));
  
  //Fill two of the quadrants with yellow.
  floodfill(screen,100,100,makecol(255,255,0));
  floodfill(screen,300,300,makecol(255,255,0));
  
  //Fill the other two quadrants with violet. //Must be
  // careful to avoid the lines of pixels when specifying
  // the point around which to fill.
  floodfill(screen,90,300,makecol(255,0,255));
  floodfill(screen,300,90,makecol(255,0,255));

  //Draw a blue filled triangle.
  triangle(screen,280,180,
                  320,180,
                  300,220,makecol(0,0,255));

  //Block and wait for the user to press any key.
  readkey();

  return 0;
}//end main function
END_OF_MAIN();