#include "Tscene.h"

//static uint64_t FPS = 50;
//static uint64_t FPS_RATIO = 20; // 1000/50

Tscene::Tscene()
{
    m_running = false;
}

Tscene::~Tscene()
{
	//
}


void Tscene::close()
{
    exit(0);
}

void Tscene::create()
{
    s_texParam texParam;
    ORect_i dst,src;

    glEnable(GL_CULL_FACE);
    glCullFace(GL_BACK);
    glDepthFunc(GL_LESS);

    m_backColor[0] = 132.0f / 255.0f;
	m_backColor[1] = 208.0f / 255.0f;
	m_backColor[2] = 240.0f / 255.0f;
	m_backColor[3] = 1.0f;
	glClearColor(m_backColor[0], m_backColor[1], m_backColor[2], m_backColor[3]);

    // --- ecran d'attente
    OImage imgwait;
    OTextureList::P_setPath(PATH_INTERFACE);
    textureList.add("imgWait.png");
    texParam = textureList.getParam("imgWait.png");
    src.set(0,0,texParam.w, texParam.h);
    dst.set(0, ECO_getViewPortH(), ECO_getViewPortW(), ECO_getViewPortH());
    imgwait.create(dst, texParam, src);
    //
    //glClearColor(m_backColor[0], m_backColor[1], m_backColor[2], m_backColor[3]);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
	imgwait.draw();
	ECO_swapBuffers();

    // ---

    camera.create(0.0f,0.0f,1.5f, 345.0f,0.0f);
    m_proj3d.perspective(30.0f, (float)ECO_getViewPortW() / (float)ECO_getViewPortH(), 0.1f, 1000.0f);

    // ---

    // pour utilisation hors votre ordinateur
    // copiez-coller le repertoire (ECO/skyEarth > dans votre repertoire programme)
    // + enlevez les classes + indiquez le chemin
    skyEarth.P_setPath("donnees/skyEarth/");
    skyEarth.create(30.0f, ECO_getViewPortW(), ECO_getViewPortH() );
    //skyEarth.create(m_proj3d, 0); //catalogue d'etoiles
    //skyEarth.moon.setPhase(OSKYEARTH_PHASE_C2, 45.0f);
    //skyEarth.clouds.addFile("../ECO/skyEarth/clouds_200.png", 0.15f); // 0.15 = degradation de la valeur ambient (ici fort !!!)
    //skyEarth.clouds.setFile("clouds_200.png");
    //skyEarth.gleam.setVisible(true);
    // actualise avec skyEarth.setHour()

    // --- shaders

    dst.set(0,ECO_getViewPortH(),ECO_getViewPortW(),ECO_getViewPortH());

    defered_buffer.create(dst.w,dst.h, dst);

    dshGeo.create();

    dshPoint.create();
    dshPoint.enable();
		dshPoint.setTextureUnit();
        dshPoint.setScreenSize(dst.w, dst.h);
    dshPoint.disable();

    dshDir.create();
    dshDir.enable();
        dshDir.setTextureUnit();
        dshDir.setScreenSize(dst.w, dst.h);
        OMat4 mvp;
        mvp.loadIdentity();
        dshDir.setMVP(mvp);
    dshDir.disable();

    dshNull.create();

    //

    water_buffer.create(dst.w, dst.h);

    //
    dshMask.create();
    dshFreeze.create();
    //
    shLight_pbr.create();
    shLight.create();
    shParticle3d.create();
    shBillboard.create();
    shGlow.create();

    // --- post process

    ppAliasing.create();
        ppAliasing.enable();
        ppAliasing.setScreenSize(dst.w, dst.h);

    ppFog.create();
        ppFog.shFog.enable();
        ppFog.shFog.setScreenSize(dst.w, dst.h);
        // fog moyen loin
        //ppFog.shFog.setFogDensity(330.0f); // desactivez fog.update dans Mdraw et fog.start dans Mevent
        //ppFog.shFog.setFogArea(0.94f);

    ppRain.create();
        ppRain.shRain.enable();
        ppRain.shRain.setScreenSize(dst.w, dst.h);
        ppRain.P_setFile("donnees/image/pluie.wav");

    ppSnow.create();
        ppSnow.shSnow.enable();
        ppSnow.shSnow.setScreenSize(dst.w,dst.h);

    ppOutline.create();
        ppOutline.enable();
        ppOutline.setScreen_size(dst.w,dst.h);

    //s_fboBuffer param;
    //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, height, 0, GL_RGB, GL_FLOAT, NULL);
    //param.in_format = GL_RGB32F;
    //param.out_format = GL_RGB;
    //param.mag_filter = GL_NEAREST;
    //param.min_filter = GL_NEAREST;
    //param.type = GL_FLOAT;
    //fboPostProcess.create(dst, param);
    fboPostProcess.create(dst);

    // --- interface avant = prépare la liste d'animation des persos

    interface_create(textureList);

    // ---

    load();
}




void Tscene::resize(int width_old, int height_old, int width, int height)
{
    //int dx = 0, dy = 0;

    // modification composants / emplacements (après construction objets !!)
    if (!m_running) return;

    //décalage des objets
    //dx = 0;
    //dy = height - height_old;

    interface_shift();

}




void Tscene::run()
{
    ECO_event event;
    OKeyboard keyboard;
    uint64_t t0,t1;
    bool bascule = false;

    // ---

    create();
    ECO_clearEvent(&event);

    // ---

    m_running = true;
    t1 = ECO_getTicks();

    while(m_running)
	{
        //récupération des évènements
        ECO_peekEvent(&event);

        switch(event.message)
        {
        case WM_QUIT:
            m_running = false;
            break;
        case WM_KEYDOWN:
            keyboard.down(event.wParam);
            break;
        case WM_KEYUP:
            keyboard.up(event.wParam);
            break;
        case WM_MOUSEMOVE:
            mouseMove(LOWORD(event.lParam), ECO_getViewPortH() - HIWORD(event.lParam));
            break;
        case WM_LBUTTONDOWN:
            mouseDownL(LOWORD(event.lParam), ECO_getViewPortH() - HIWORD(event.lParam));
            break;
        //case WM_RBUTTONDOWN:
        //    mouseDownR(LOWORD(event.lParam), ECO_getViewPortH() - HIWORD(event.lParam));
        //    break;
        //case WM_LBUTTONUP:
        //    mouseUp();
        //    break;
        //case WM_MBUTTONDBLCLK
        //si style CS_DBLCLKS utilisé lors de la création du style de la fenêtre.
        case WM_MOUSEWHEEL:
            mouseWheel(event.wParam);
            break;
        }
        //keyDown(keyboard);

        // 1000 tickes par seconde
        // 50 frames par seconde = 1000 / 50 = 20
        t0 = ECO_getTicks();
        if (t0 > t1)
        {
            keyDown(keyboard); // ou avec draw()
            t1 = t0 + 10;
            bascule = !bascule;
            if (bascule) cycle(); else draw();
        }
    }

    ECO_delay(100); //ne ferme pas trop brusquement
}

