#include <string.h>
#include <stdio.h>
#include <stdarg.h>

#include <GL/glut.h>

#define initial_width 400
#define initial_height 400

int main_window, sub_window;

// The following code permits drawing text on the screen 
// Bitmap character fonts must be pre-defined

void bitmap_output(int x, int y, int z, char *string, void *font)
{
  int len, i;

  glRasterPos3f(x, y, 0);
  len = (int) strlen(string);
  for (i = 0; i < len; i++) {
    glutBitmapCharacter(font, string[i]);
  }
}


// Draws a graph of a simple line.

DrawGraph()
{
  float x;
  glBegin(GL_POINTS);  			// Start drawing points
    glColor3f(0.0,0.0,0.0);		// Set drawing color to black
    for (x=-0.9; x<0.9; x+=0.01) 	// Step through x values
     {glVertex3f(x,x,0.0);		// Put a point at (x,x)
     }
  glEnd();
} 

// Draws a set of labeled axes

DrawAxes()
{
  glBegin(GL_LINES);
    glColor3f(1.0, 1.0, 0.0);
    glVertex3f(-0.9,0.0, 0.0);
    glVertex3f(0.9, 0.0, 0.0);
    glVertex3f(0.0, -0.9,0.0);
    glVertex3f(0.0, 0.9, 0.0);
  glEnd();
  glPushMatrix();
    glTranslatef(0.05, 0.85, 0);
    bitmap_output(0,0,0, "y", GLUT_BITMAP_TIMES_ROMAN_24);
  glPopMatrix();

  glPushMatrix();
    glTranslatef(0.8, -0.1, 0);
    bitmap_output(0,0,0, "x", GLUT_BITMAP_TIMES_ROMAN_24);
  glPopMatrix();
 
}

//Draws some titles on the screen in available Times Roman fonts

void DrawTitles()
{
  glColor3f(0.0, 0.0, 0.0);
  glPushMatrix();
  glTranslatef(-0.9, 0.8, 0);
  bitmap_output(0,0,0, "Example #1", GLUT_BITMAP_TIMES_ROMAN_24);
  glPopMatrix();

  glPushMatrix();
  glTranslatef(-0.5, -0.95, 0);
  bitmap_output(0,0,0, "The above graph is for the linear equation   y = x", 
                GLUT_BITMAP_TIMES_ROMAN_10);
  glPopMatrix();
} 
  
// Inner display window 

void display1(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
  glFlush();
  DrawAxes();
  DrawGraph();
  DrawTitles();
}
  				

// Main display loop

void display(void)
{
  glClear(GL_COLOR_BUFFER_BIT);
  glFlush();
}


// 
void reshape(int w, int h)
{
  int width = initial_width;
  int height = initial_height;
  glViewport(0, 0, w, h);

  // Give border, and take care of tiny image problems

  if (w > 50) {
      width = w - 20 ;
      } 
  else {
      width = 20;
      }
  if (h > 50) {
      height = h - 20;
      } 
  else {
      height = 20;
      }

  glutSetWindow(sub_window);
  glutPositionWindow(10,10);
  glutReshapeWindow(width, height);
}

int main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB);
  glutInitWindowSize(initial_width, initial_height);
  main_window = glutCreateWindow("My First Window ");
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glClearColor(1.0, 1.0, 1.0, 1.0);
  sub_window = glutCreateSubWindow(main_window, 10, 10, 
                                   initial_width, initial_height);
  glutDisplayFunc(display1);
  glClearColor(1.0, 0.0, 0.0, 1.0);
  glutMainLoop();
  return 0;             /* ANSI C requires main to return int. */
}
