// Modified 3/9/04 by Ben Pellow
// to include keyboard commands for changing the ball's position
// and velocity, as well as any vertex's position.

#include "cGame.h"

cGame::cGame()
{
    // Nothing to do
    lastX_ = 0;
    lastY_ = 0;
	frate_ = 0;
	tuos_ = 0;
}

void cGame::init()
{
    world_ = new cWorld();
}


void cGame::display()
{    
   // clear color and depth buffers
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    float delay = timer_.update();

	tuos_ += delay;

    world_->update(delay);
    world_->draw();

	++frate_;

	if (tuos_ > 1 )
	{
		cout<< "frames per second: " << frate_ << endl;
		frate_ = 0;
		tuos_ = 0;
	}
            
    // flush buffer
    glutSwapBuffers();
}

void cGame::idle()
{
    glutPostRedisplay();
}

void cGame::keyboard(unsigned char key, int x, int y) 
{
    //cout << "key=" << (int)key << endl;
    
    if(key == 45) world_->camera_.radius_ -= 4;
    if(key == 43) world_->camera_.radius_ += 4;
    
    if(key == 52) world_->ball_.launchAngle_ -= .1;
    if(key == 54) world_->ball_.launchAngle_ += .1;
    
    if(key == 53) world_->ball_.launchPower_ -= 5;
    if(key == 56) world_->ball_.launchPower_ += 5;
    
    if(key == ' ')  world_->ball_.launch(); 
}

void cGame::mouseClick(int button, int state, int x, int y) 
{
}

void cGame::mouseMove(int x, int y)
{
    if (lastX_>0  && abs(x-lastX_) < screenWidth_/6 && abs(y-lastY_) < screenHeight_/6)
    {	
        float theta = -.5 * 3.1415926535 * (float)(y-lastY_)/(float)screenHeight_;
        float phi = 3.1415926535 * (float)(x -lastX_)/(float)screenWidth_;
        world_->camera_.phi_ +=  phi;
        world_->camera_.theta_ += theta;
    }
    
    lastX_ = x;
    lastY_ = y;
     
}
