#include <iostream>
#include <vector>
#ifdef __APPLE__
#include "glui.h"  // i installed glui locally, see README file
#else
#include <GL/glui.h>  // linux and windows include line for glui
#endif
using namespace std;
int winheight=500, winwidth=500;
int sx,sy,ex,ey;
int spinnerx=0;
int maingfx;

void display();
void init();
void keyboard(unsigned char key, int x, int y);
void mouse(int button, int state, int x, int y);
void reshape(int w, int h);
void idle();
void passivemotion(int x, int y);
void activemotion(int x, int y);





void quit_fn(int ID)
{
exit(0);
}


int main(int argc, char *argv[])
{
  glutInit(&argc, argv);
  init();
  glutMainLoop();
}



//the initialization function
void init()
{
  //  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  glutInitWindowSize(winwidth,winheight);
  glutInitWindowPosition(0,0);
  maingfx = glutCreateWindow("Ramsey");
  glutDisplayFunc(display);
  glutKeyboardFunc(keyboard);
  glutMouseFunc(mouse);
  GLUI_Master.set_glutIdleFunc(idle);
//  glutIdleFunc(idle);
  GLUI *glui = GLUI_Master.create_glui("GLUI_WIN",0,550,0);
  glui->add_checkbox("sx",&sx);
  glui->add_button("QUIT",0,quit_fn);
  glui->add_spinner("X POS",GLUI_SPINNER_INT,&spinnerx);

  glui->set_main_gfx_window(maingfx);
  glutPassiveMotionFunc(passivemotion);
  glutMotionFunc(activemotion);
  glutReshapeFunc(reshape);

  //clear to black and fill to "greenish"
  
  glClearColor(0.0,0.0,0.0,0.0);
  glColor3f(0.1,1.0,0.1);

  //setup standard orthogoanl view iwth clipping box as cube of side 2
  //with center at the origin. This is the default view
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  //  gluOrtho2D(-2.0,2.0,-2.0,2.0);
  gluOrtho2D(0,winwidth,0,winheight);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

}




void reshape(int w, int h)
{
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  //  if(w <= h)
  //    gluOrtho2D(-2.0,2.0,-2.0*h/w, 2.0f* h/w);
  //  else
  //    gluOrtho2D(-2.0 * w / h, 2.0 * w / h, -2.0, 2.0);
  gluOrtho2D(0,w,0,h);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glViewport(0,0,w,h);
  winheight = h;
  winwidth = w;
}

//text?

void text(char *s, float x, float y)
{
  glRasterPos2d(x,y);
  unsigned int i = 0;
  for (;i < strlen(s); i++)
    {      
      glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,s[i]);
      
    }
}





//the display function
//whatever is in here is what will be displayed
#define NUM_PTS 100.0
void display()
{

  glClear(GL_COLOR_BUFFER_BIT);
  for(int i = 0; i < NUM_PTS; i ++)
    {
      glBegin(GL_POINTS);
        glColor3f(i/NUM_PTS,i/NUM_PTS,i/NUM_PTS);
	glVertex2f(400,200+250*i/(double)NUM_PTS);
      glEnd();
    }
  glBegin(GL_LINES);
  if(sx)
      glColor3f(1,1,1);
  else
	  glColor3f(0,1,1);
  glVertex2f(350,450);
  glColor3f(0,0,0);
  glVertex2f(350,200);
  glColor3ub(255,0,0);
  glVertex2f(sx*500,sy);
  glColor3ub(0,255,0);
  glVertex2f(spinnerx,ey+500);
  glEnd();

  glColor3f(1,1,0);
  text("Hello, Are We Having Fun Yet?", 2,2 );
  glColor3f(0,1,1);
  text("Hooooray", 50,50);
  glutSwapBuffers();//double buffer only
}


//the keyboard function
void keyboard(unsigned char key, int x, int y)
{
  if(key == 'q' || key == 'Q' || key == 3) //q or escape
    exit(0);

}


//the mouse does stuff
void mouse(int button, int state, int x, int y)
{

}


void activemotion(int x,int y)
{

}
void passivemotion(int x, int y)
{

}


void idle() 
{ 
	//always set window b4 redisplay
//	glutSetWindow(maingfx);
//	glutPostRedisplay();
}
