/********************************************************************************************
03-A.cpp
	Jim Millard
	for CO311

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

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

void DrawCheckerboard(SimpleWindow& W, const int BlocksAcross, const int BlocksDown);
void BlockPosition(int row, int column, float blockHeight, float blockWidth,
            float& windowX, float& windowY);
color SetColor(int row, int column, color, color);

int ApiMain()
   {
   cout << "This program will draw a checkerboard with the dimensions you specify." << endl;


   int BlocksAcross, BlocksDown;
   cout << "Enter the number of blocks across: ";
   cin >> BlocksAcross;
   cout << "Enter the number of blocks down: ";
   cin >> BlocksDown;

   //create the primary window
   SimpleWindow W("Practical 3, Problem A");
   W.Open();

   DrawCheckerboard(W, BlocksAcross, BlocksDown);

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

   Terminate();
   return 0;
   }

//
//this function draws the checkerboard
//
void DrawCheckerboard(SimpleWindow& W, const int BlocksAcross, const int BlocksDown)
   {
   //A stack of useful constants that can later be replaced by function parameters
   const float height=1., //static height of all blocks
               width =1.; //static width of all blocks
   const color BaseColor = Green,  //static color of first block
               AltColor  = Yellow;   //static color of alternate block
  

   color currentColor; //the color we're drawing now
   float absX, absY; //the position of the block in the window

   //Draw by rows
   for(int row = 0; row < BlocksDown; row++)
       {
       //Draw the rectangles across the row
       for (int column = 0; column < BlocksAcross; column++)
           {
           //set the color based on its position
           currentColor = SetColor(row, column, BaseColor, AltColor);

           //calculate the position of the block in the display window
           BlockPosition(row, column, height, width, absX, absY);

           //create the block with relevant parameters
           RectangleShape Rxy(W, absX, absY, currentColor, width, height);

           //dray the block
           Rxy.Draw();

           } //inner for loop

       } //outer for loop

    return;
    } //DrawCheckerboard

//
//this function automates the discovery of the EXACT position where the block will be placed
//based on the row/column "general" placement, and the block's height and width.
//This routine assumes that all blocks will ultimately be of identical height and that
//the first row/column is (0,0) and the origin (0,0) is in the upper-left corner.
//
void BlockPosition(int row, int column, float blockHeight, float blockWidth,
            float& windowX, float& windowY)
    {
    windowX = column*blockWidth + blockWidth/2;
    windowY = row*blockHeight + blockHeight/2;
    }

//
//this function sets the color of the square based on it's position in the checkerboard
//it takes advantage of the coincicence between the sum of row & column for the block and
//whether that sum is even or odd.
//
color SetColor(int row, int column, color color_1, color color_2)
    {
    switch ((row + column)%2)
        {
        case 1:
            return color_2;
        default:
            return color_1;
        }
    }