/* program to display a sequence of images
 *    each image has an outer blue border, a middle green region, 
 *       and an inner red section
 *    successive images the blue and green regions get 
 *       successively bigger, smaller, bigger, etc.
 *
 * This program and all MyroC software is licensed under the Creative Commons
 * Attribution-Noncommercial-Share Alike 3.0 United States License.
 * Details may be found at http://creativecommons.org/licenses/by-nc-sa/3.0/us/
 */

/* thus program only displays one image window, entitled "image window"
 */

/* utilize a MyroC-type package for rDisplayPicture
 */
#include "MyroC.h"
#include <stdio.h>
#include <unistd.h>   // needed for sleep function

/* approach outline based on two threads:
 * this [user] program runs in one thread
 *    as user thread, this program provides overall control of the robot, images, etc.
 * a second thread provides OpenGL control for the display of images
 *
 * overall algorithm
 *   utilize shared Picture struct variable 
 *   first thread 
 *      provides general user interface
 *      call to rDisplayPicture
 *         set shared Picture struct variable to desired Picture iamge
 *         if second thread not running, 
 *             create second thread
 *             initialize graphics environment
 *             establish shared Picture image as texture for image
 *             establish new_image procedure as glutIdleFunc
 *         if second thread already running
 *             first thread sends signal to activate processing in new_image procedure
 *             new_image uses glTexImage2D to establish shared Picture image
 *             use glutPostRedisplay to force display of new image
 */

/* create picture:
 *    blue outer border
 *    green middle section
 *    red inner section
 * borders get successively larger for indent 0, 1, 2, 3, 4
 *             successively smaller for indent 5, 6, 7, 8, 9
 */
void init_pic (Picture * pic, int indent)
{
  pic->height = 266;
  pic->width = 427;
  int row, col;
  for (row = 0; row < pic->height; row++)
    for (col = 0; col < pic->width; col++)
      { 
	indent = indent % 10;
	int border;
	if (indent < 5)
	  border = (indent + 1) * 5;
	else
	  border = (10 - indent) * 5;

	pic->pix_array[row][col].R = 0;
	pic->pix_array[row][col].G = 0;
	pic->pix_array[row][col].B = 0;
	if ((row < border) || (row >= pic->height-2*border) 
	    || (col < 2*border) || ( col >= pic->width-2*border))
	  {
	    pic->pix_array[row][col].B = 255;
	    // printf ("blue row: %d, col: %d\n", row, col);
	  }
	else if ((row < 2*border) || (row >= pic->height-4*border) 
		 || (col < 4*border) || ( col >= pic->width-4*border))
	  {
	    pic->pix_array[row][col].G = 255;
 	    //printf ("green  row: %d, col: %d\n", row, col);
	  }
	else
	  {
	    pic->pix_array[row][col].R = 255;
 	    //printf ("red row: %d, col: %d\n", row, col);
	  }
      }
}

/* command-line parameters might include -display, -geometry for X */
int main (int argc, char* argv [])
{
  Picture pic;

  /* printf statements added to example to report processing steps */
  printf ("example program to display\n");
  printf ("   a sequence of rectanges\n");
  printf ("   with varying sized borders.\n");

  int i;

  char * titles[13] = {"one", "two", "three", "four", "five", "six",
	    "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen"};

  // rDisplayImageProcessingErrors ();

  int delay = 1.0;

  //display 13 images
  for (i = 0; i < 13; i++)
    {
      printf ("test program: initializing image %d\n", i);
      init_pic (&pic, i);
      printf ("test program: displaying image\n");
      if (i % 3 == 0)
        { // blocking
         rDisplayPicture (&pic, 5.0, titles[i%9]);
        }
      else
        { // non-blocking
        rDisplayPicture (&pic, (double) -1* (i + 2.0), titles[i%9]);
        }
      printf ("test program: sleeping %d seconds\n", delay);
      sleep (delay);
    }

  printf ("test program: waiting until all timed, non-blocking images windows closed\n");  
  rWaitTimedImageDisplay ();

  printf ("program completed\n");
  return 0;
}
