
#include "../include/WFXDisplay.h"


using namespace WFX;
using namespace WFX::lang;
using namespace WFX::scene;
using namespace WFX::kernel;
using namespace WFX::renderer;
using namespace WFX::input;
using namespace WFX::io;
using namespace WFX::math;
using namespace std;


WFXDisplay::WFXDisplay(WFX::Int argc, WFX::Char *argv[]) : WFXGUIApplication(argc, argv) {
    setup(800, 600, 16, "WFXDisplay");
    //scene...
    MainGraph = new ListSceneGraph();
    MainGraph->getRenderContext().Lighting = true;
    MainGraph->getRenderContext().TextureMapping = true;
    MainPort = new Viewport(0.0f, 0.0f, 1.0f, 1.0f);
    MainCamera = new FirstPersonCamera(Window);
    MainJob = new RenderJob(MainGraph, MainCamera, MainPort);
    Window->addRenderJob(MainJob);

    //which file?
    if (Params.getFileCount() < 1)
        THROW("no File Parameter given");

    File in(Params.getFile(0));

    MainGraph->add(GeometryLoader::load(in));

    //print
    //SceneGraphPrinter p;
    //MainGraph->accept(p);

    run();
}

WFXDisplay::~WFXDisplay() {
    delete MainGraph;
    delete MainCamera;
    delete MainJob;
    delete MainPort;
}

bool WFXDisplay::start() {
    return true;
}

void WFXDisplay::stop() {
}

void WFXDisplay::update() {
    Window->centerCursor();
    MainCamera->update();
    MainGraph->update();
    Window->render();
    Window->swapBuffers();
}

void WFXDisplay::keyPressed(WFX::input::KeyEvent k) {
    if (k.Key == WFXK_ESC) {
        exit();
    }
    if (k.Key == WFXK_p) {
        saveScreenShot();
    }
    if (k.Key == WFXK_o) {
        SceneGraphPrinter p;
        MainGraph->accept(p);
    }
    if (k.Key == WFXK_f) {
        LOG("FPS: " << TimeTask->FPS);
    }
    
}

void WFXDisplay::keyReleased(WFX::input::KeyEvent k) {
}

