#include "../../include/sharedIncludes.h"
#include "eventHandler.h"
#include "init.h"
#include "../CASI.h"


CASI scene;
Scalar rot[3];
Scalar dist = 100;


void drawAxes()
{
  glBegin(GL_LINES);
    glColor3f(1,0,1);
    glVertex3f(0,0,0);
    glVertex3f(1000,0,0);
    glColor3f(0,1,1);
    glVertex3f(0,0,0);
    glVertex3f(0,1000,0);
    glColor3f(1,1,0);
    glVertex3f(0,0,0);
    glVertex3f(0,0,1000);
  glEnd();
}

void renderScene(void)
{
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
  gluLookAt( 0,0,dist, 0,0,0, 0,1,0 );

  glPushMatrix();
    glRotatef(rot[0], 1, 0, 0);
    glRotatef(rot[1], 0, 1, 0);
    glRotatef(rot[2], 0, 0, 1);
    drawAxes();
    scene.testDraw();
  glPopMatrix();
  glFlush();
  SDL_GL_SwapBuffers( );
}

void wait(void)
{
  static Uint32 tic = 30;
  static Uint32 nextTime = 0;
  static Uint32 now;
  if( !nextTime )
    nextTime = tic + SDL_GetTicks();
  now = SDL_GetTicks();
  if( nextTime > now )
    SDL_Delay(nextTime - now);
  nextTime += tic;
}

void usage()
{
  printf("Usage: demo [--fastest] <ase file>\n");
  quitProg(0);
}

int main(int argc, char *argv[])
{
  if( !(argc == 2 || argc == 3) )
    usage();
  initSDL();
  initOpenGL();
  bool dowait = true;

  if( !strcmp(argv[1], "--fastest") && argc == 3 )
  {
    dowait = false;
    scene.import(argv[2]);
  }
  else
    scene.import(argv[1]);

  while( 1 )
  {
    processEvents( );
    renderScene( );
    if( dowait )
      wait();
  }
  return 0;
}

