/********************************************************************************************
Step5.cpp
	Jim Millard
	for CO311/Spring 2000

   Simple program that displays a 4x4 Blue/Red checkerboard
   Uses the ezwin API
********************************************************************************************/
#include <iostream>
#include <string>
using namespace std;

#include "ezwin.h"
#include "rect.h"

void DrawCheckerboard(SimpleWindow& W);  //required prototype

int ApiMain()
   {
   //create the primary window
   SimpleWindow W("Exercise 3.22, Step 5");
   W.Open();

   DrawCheckerboard(W);

   //wait for input prior to quitting
   char ch;
   cout << "Enter a character to continue";
   cin >> ch;

   Terminate();
   return 0;
   }
void DrawCheckerboard(SimpleWindow& W)
   {
   //A stack of useful constants that can later be replaced by function parameters
   const float height=2., //static height of all blocks
               width =2., //static width of all blocks
               xCenter =width/2., //horizontal center of each block
               yCenter =height/2.; //vertical center of each block
   const int BlocksAcross =4, //static width of checkerboard
             BlocksDown =4; //static height of checkerboard
   const color BaseColor = Blue,  //static color of first block
               AltColor  = Red;   //static color of alternate block

   bool doAltColor;  //keeps track of whether we should display in the alternate color
   color currentColor; //the color we're drawing now
   //Draw the rectangles
   int row; //declare here to avoid VC5 scope bug
   //draw even rows
   for(row = 0; row < BlocksDown; row+=2)
       {
       doAltColor = false; //start with the base color
       for (int column = 0; column < BlocksAcross; column++)
           {
           //set the color
           if (doAltColor)
               currentColor = AltColor;
           else
               currentColor = BaseColor;
           doAltColor = !doAltColor;

           RectangleShape Rxy(W, xCenter + width*column, yCenter + height*row, currentColor, width, height);
           Rxy.Draw();
           }//blocks across loop
       }//even row loop

   //draw odd rows
   for(row = 1; row < BlocksDown; row+=2)
       {
       doAltColor = true; //start with the alternate color
       for (int column = 0; column < BlocksAcross; column++)
           {
           //set the color
           if (doAltColor)
               currentColor = AltColor;
           else
               currentColor = BaseColor;
           doAltColor = !doAltColor;

           RectangleShape Rxy(W, xCenter + width*column, yCenter + height*row, currentColor, width, height);
           Rxy.Draw();
           }//blocks across loop
       }//odd row loop

   return;
   }//DrawCheckerboard