/*
MainControl.cpp
Written by Matthew Fisher

MainControl includes everything that changes often between applications, such as what meshes to load,
and also determines what is rendered each frame.

See the appropriate header file for a description of what this class does. 
*/

//All source files include Main.h
#include "Main.h"

const int ShapeCount = 2;
const int MovieWidth = 640;
const int MovieHeight = 480;

void MainControl::InitShapes(GraphicsDevice &GD)
{
    Shapes.Allocate(ShapeCount);    //allocate room for 7 meshes

    int i, i2;
    for(i=0;i<ShapeCount;i++)
        Shapes[i].SetGD(GD);        //associate the 7 meshes with the GD

    Shapes[0].CreatePlane(1.0f, 10, 10);
    Shapes[0].Rotate(Vec3(1.0f,1.0f,1.0f));
    Shapes[0].Translate(Vec3(0.0f,0.7f,0.0f));

    Shapes[1].CreatePlane(1.0f, 10, 10);
    Shapes[1].Rotate(Vec3(3.0f,0.0f,6.0f));
    Shapes[1].Translate(Vec3(0.0f,-0.7f,0.0f));

    for(i=0;i<ShapeCount;i++)
    {
        for(i2=0;i2<Shapes[i].VertexCount();i2++)
        {
            Shapes[i].Vertices()[i2].tx *= float(MovieWidth) / 1024.0f;
            Shapes[i].Vertices()[i2].ty *= float(MovieHeight) / 1024.0f;
        }
    }
}

void MainControl::ReInit(GraphicsDevice &GD, WindowManager &WM)
{
    GD.DisableWireframe();
    if(GD.GetFullScreen()) C.Mouse(0.0f);   //move the mouse to the middle of the screen

    MC.Perspective.PerspectiveFov(60.0f * PIf / 180.0f,                             //the field of view = 60 degrees
                                    float(WM.GetWidth()) / float(WM.GetHeight()),   //the aspect ratio
                                    0.1f,                                           //near Z-plane
                                    20.0f);                                         //far Z-plane

    VT.Restore();           //restore the video texture
}

void MainControl::Init(GraphicsDevice &GD, WindowManager &WM)
{
    VT.Init(&GD, "test.avi");   //load the video texture
    ReInit(GD, WM);

    //initalize the camera
    C.Reset(eX*3.0f,            //the eye vector
            eY,                 //the up vector
            Origin);            //the vector we're looking at

    MC.World.Identity();        //zero the world and view matrices
    MC.View.Identity();

    InitShapes(GD);             //initalize the shapes
    Time = 0.0f;                //reset the time to 0
}

void MainControl::Render(GraphicsDevice &GD, WindowManager &WM)
{
    Time += T.SPF();                                //advance the current time
    if(KeyCheckOnce(KEY_F)) GD.ToggleWireframe();   //toggle wireframe if the user presses "F"

    C.WindowKeyboard(1.0f,1.0f);                    //move based upon the keyboard
    if(GD.GetFullScreen()) C.Mouse(0.001f);         //move based upon the mouse, if we're in full-screen mode
    C.Update(MC, GD);                               //update the camera and load it into the current graphics device

    VT.Set();
    VT.Loop();

    int i;
    for(i=0;i<ShapeCount;i++)
    {
        Shapes[i].Render();                         //draw all the shapes
        Shapes[i].Rotate(Vec3(0.0f,T.SPF(),0.0f));  //rotate the shapes slightly
    }

    //alter the refinement based upon the keyboard input
    if(KeyCheckOnce(KEY_G))
    {
        VT.Seek(rnd());
    }
    GD.DrawVector("Camera - > ", C.VecEye, 0, 20);  //draw the camera position as text on the screen
    GD.DrawText("Press G to move to a random time in the movie.", " ", 0, 40);  //help
    GD.DrawDouble("Current Time (between 0 and 1) -> ", VT.GetPosition(), 0, 60);   //draw the current time
}

void MainControl::FreeMemory()
{

}