/*
 *  hill_house.cpp
 *  
 *
 *  Created by Emmanuel Agu on Mon Sep 27 2003.
 *  Copyright (c) 2003 __MyCompanyName__. All rights reserved.
 *
 */

#include "hill_house.h"
#ifdef __APPLE_CC__
#include "gl.h"
#else
#include "GL/gl.h"
#include <GL/glu.h>
#endif
#include "minigl.h"
#include "miniglut.h"
#include <iostream.h>
#include <fstream.h>
#include <iomanip>
#include <stdlib.h>
#include "sdl.h"

Scene scn;

//--------------------------------------------------------------------------------
void hill_house::usage() {
  int i;
  // Tell the user what the keys do.
  cout << "\nHouse Usage:\n";
  cout << "\tKeys:\n";

  cout << "\t\tRight Arrow\t\tSlide camera 1 unit in the positive X direction\n";
  cout << "\t\tLeft Arrow\t\tSlide camera 1 unit in the negative X direction\n";
  cout << "\t\tUp Arrow\t\tSlide camera 1 unit in the positive Y direction\n";
  cout << "\t\tDown Arrow\t\tSlide camera 1 unit in the negative Y direction\n";
  cout << "\t\tShift Up Arrow\t\tSlide camera 1 unit in the positive Z (\"in\") direction\n";
  cout << "\t\tShift Down Arrow\tSlide camera 1 unit in the negative Z (\"out\") direction\n";
  cout << "\t\tControl Down Arrow\tChange camera pitch by 2 degrees\n";
  cout << "\t\tControl Up Arrow\tChange camera pitch by -2 degrees\n";
  cout << "\t\tControl Right Arrow\tChange camera yaw by 2 degrees\n";
  cout << "\t\tControl Left Arrow\tChange camera yaw by -2 degrees\n";
  cout << "\t\t<\t\t\tChange camera roll by 2 degrees\n";
  cout << "\t\t>\t\t\tChange camera roll by -2 degrees\n";
  cout << "\t\tESC\t\t\texit\n";

  cout << "\tMouse:\n";
  cout << "\t\tNone\n";
}

void hill_house::key    (minigl &mgl, miniglut &mglut, unsigned char k, int x, int y) {

  int mod = glutGetModifiers( ); // Get the state of SHIFT, CTRL, etc.

  if( mod & GLUT_ACTIVE_CTRL ) {
    cout << "Ctrl Held ";
  }
  if( mod & GLUT_ACTIVE_SHIFT ) {
    cout << "Shift Held ";
  }
  if( mod & GLUT_ACTIVE_ALT ) {
    cout << "Alt Held ";
  }

  // based on the key, update the camera.
  switch (k) {
    case 27:  /* Escape */
      std::exit(0);
      break;
    default:
      printf( "Key (%d) not implemented yet!\n", k );
      break;
  }

}

void hill_house::special (minigl &mgl, miniglut &mglut, int k, int x, int y) {

  int mod = glutGetModifiers( ); // Get the state of SHIFT, CTRL, etc.

  if( mod & GLUT_ACTIVE_CTRL ) {
    cout << "Ctrl Held ";
  }
  if( mod & GLUT_ACTIVE_SHIFT ) {
    cout << "Shift Held ";
  }
  if( mod & GLUT_ACTIVE_ALT ) {
    cout << "Alt Held ";
  }

  // based on the key, update the camera.
  switch (k) {
    default:
      printf( "Special key (%d) not implemented yet!\n", k );
      break;
  }
}

void hill_house::init(minigl &mgl, miniglut &mglut) {
  // init stuff
  mgl.mglShadeModel(minigl::MGL_SMOOTH);
  mgl.mglEnable(minigl::MGL_DEPTH_TEST);
  mgl.mglEnable(minigl::MGL_NORMALIZE);
  mgl.mglViewport(0,0, 640, 480);

  mgl.mglMatrixMode(minigl::MGL_PROJECTION);
  mgl.mglLoadIdentity( );
}

// redraw the screen
void hill_house::draw(minigl &mgl, miniglut &mglut) {
  scn.read("myScene1.dat"); // read the SDL file and build the objects
  mgl.mglEnable(minigl::MGL_LIGHTING);
  scn.makeLightsOpenGL( );

  double winHt = 1.0; // half-height of the window
  mgl.mglOrtho(-winHt*64/48.0, winHt*64/48.0, -winHt, winHt, 0.1, 100.0);
  mgl.mglMatrixMode(minigl::MGL_MODELVIEW);
  mgl.mglLoadIdentity( );
  mgl.mgluLookAt(2.3, 1.3, 2, 0, 0.25, 0, 0.0, 1.0, 0.0);
  mgl.mglClear(minigl::MGL_COLOR_BUFFER_BIT|minigl::MGL_DEPTH_BUFFER_BIT); // clear screen
  scn.drawSceneOpenGL( );
  mgl.mglFinish( );
  mglut.mglutSwapBuffers( );
}
