#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()
{
    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]);

    sound.create("donnees/boing.wav");

    ORect_i dst;
    dst.set( (ECO_getViewPortW()/2)-128, (ECO_getViewPortH()/2)+16, 256,32);
/*
    // premiere methode
    label.create(dst, "appuyez sur f1", 5);
    label.setFonteSize(18); // recalcul la surface
*/
    // deuxieme methode (ideale pour plusieurs labels)
    int size = OFonte::P_getSize();
    OFonte::P_setSize(18);
    label.create(dst, "appuyez sur f1", 5);
    OFonte::P_setSize(size);
}

void Tscene::cycle()
{
    //
}


void Tscene::draw()
{
    //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);

	//
	// ... affichage
	//
	label.draw();

	ECO_swapBuffers();
}


void Tscene::keyDown(OKeyboard& keyboard)
{
    if (keyboard.getKeyDown(ECOK_escape))
    {
        m_running = false;
        return;
    }

    if (keyboard.getKeyDown(ECOK_f1))
    {
        sound.play();
        keyboard.clear(); // sinon repetition de la touche
        return;
    }
}


void Tscene::mouseDownL(int x, int y)
{
    //
}

void Tscene::mouseDownR(int x, int y)
{
    //
}

void Tscene::mouseMove(int x, int y)
{
    //
}

void Tscene::mouseUp()
{
    //
}

void Tscene::mouseWheel(int delta)
{
    //
}




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;

    //modEdit.resize();
    //modJeu.resize();

}




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
}

