#include <iostream>
#ifdef _WIN32
#include <windows.h>
#endif
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include "cvec2t.h"
#include "cvec3t.h"

typedef CVec2T<float> Vec2f;
typedef CVec3T<float> Vec3f;

namespace Params { 
  const float WorldWidth = 1.33;
  const float WorldHeight = 1;
  const int TimerStep = 16; // millisec
};

namespace WindowParams {
  static int WindowWidth = 800;
  static int WindowHeight = 600;
  static int MainWindow; 
};


class Block { 
public:
  Block(float r, Vec2f c): _size(r), _center(c) {} 
  void draw() {
    Vec2f hdisp = Vec2f(0.5*_size,0);
    Vec2f vdisp = Vec2f(0,0.5*_size);
    glBegin(GL_QUADS);    
    glVertex2fv(_center-hdisp-vdisp);
    glVertex2fv(_center+hdisp-vdisp);
    glVertex2fv(_center+hdisp+vdisp);
    glVertex2fv(_center-hdisp+vdisp);
    glEnd();  
  }

  void update() {
    _center += Vec2f(0.01,0);  
  }

  float size() const { return _size; }
  Vec2f center() const { return _center; }

private:
  float _size;
  Vec2f _center;
};


void Reshape(int width, int height) {

  // glViewport defines part of the window we are going to use 
  WindowParams::WindowWidth = width;
  WindowParams::WindowHeight = height;
  glViewport(0,0,width,height);

  // initialize the viewing transformation (camera position) to identity; 
  // this corresponds to camera looking down negative z axis
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  // specify the projection transformation (camera parameters);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();

  // this defines a camera with orthogonal projection that captures the 
  // part of the virtual world inside the box from 
  // -WorldWidth/2 to WorldWidth/2 in the X direction 
  // -WorldHeight/2 to WorldHeight/2 in the Y direction 
  // and from 0 to 1 (default in Z direction 
  gluOrtho2D(-Params::WorldWidth/2, Params::WorldWidth/2,-Params::WorldHeight/2,Params::WorldHeight/2); 
}


void Keyboard( unsigned char key, int, int) { }

Block block(0.1,Vec2f(-1,0));


void Draw() {
  // set color to initialize the window to; the 4th component 
  // is the alpha value and is not used unless blending is enabled
  glClearColor( 0.5, 0.5,0.5,0.0);
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  block.draw(); 
  // all drawing is done in the back buffer; 
  // the next function swaps the back and front buffer
  glutSwapBuffers();
}


void Animate(int time) { 
  // update the state 
  block.update();
  // timer callback needs to be reinstalled each time 
  glutTimerFunc(Params::TimerStep,Animate,0);
  glutPostRedisplay();
}



int main(int argc, char* argv[]) {

  // initialize glut and parse command-line aguments that glut understands
  glutInit(&argc, argv);

  // initialize dislay mode: 4 color components, double buffer and depth buffer
  glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);

  glutInitWindowSize(WindowParams::WindowWidth,WindowParams::WindowHeight);


  WindowParams::MainWindow = glutCreateWindow("Moving square");

  // register all callbacks

  // gets called whenever the window needs to be redrawn
  glutDisplayFunc(Draw); 
  // gets called whenever the window changes shape
  glutReshapeFunc(Reshape);
  glutKeyboardFunc(Keyboard);
  // this insures that Animate is called the first time
  glutTimerFunc(Params::TimerStep,Animate,0);
  
  // this is an infinite loop get event - dispatch event which never returns
  glutMainLoop();
  return 0;
}











