#include <WFX/renderer/RenderTarget.h>

#include <WFX/renderer/Renderer.h>

using namespace WFX;
using namespace WFX::renderer;
using namespace std;

RenderTarget::RenderTarget(Int w, Int h, Int d) : Width(w), Height(h), Depth(d) {
}

void RenderTarget::addRenderJob(WFX::renderer::RenderJob* r) {
    //insert the RenderJob to the job list - ordered by priority
    list<RenderJob*>::iterator it;
    for (it=RenderJobs.begin(); it!=RenderJobs.end(); it++) {
        RenderJob* cmp = (*it);
        if (cmp->Priority > r->Priority) 
            break;
    }
    RenderJobs.insert(it,r);
}

void RenderTarget::render() {
    Renderer* r = Renderer::getInstancePtr();

    r->setRenderTarget(this);

    r->beginRenderOp();

    list<RenderJob*>::iterator it;
    for (it=RenderJobs.begin(); it!=RenderJobs.end(); it++) {
        RenderJob* job = *(it);
        if (job->isActive()) {
            //set render context
            r->setViewport(job->getViewport());
            r->setCamera(job->getCamera());
            job->getCamera()->updateFrustum();
            //render
            job->getRenderable()->render(r);
        }
    }

    r->endRenderOp();
}

void RenderTarget::printDebug() {
    LOG("RenderTarget::printDebug");
    LOG("   -> " << RenderJobs.size());
}

