/*

end_picture

return:
  0 - doslo k chybe
  1 - este nezacal
  2 - prebehol v poriadku
  3 - uz skoncil
*/

#include <iostream>
#include "api3ds.h"
#include "efekt.h"
#include "efekt_18.h"
#include "syncs.h"

extern int sync_id;

int  efekt_18::init()
{
    cout << "Efekt 18 init ... ";
    start=TRUE;
    counter=ZACIATOK18*refresh;
//->init
    z=1.0;
    color=1.0;
    nxtcolor=1.0;
//<-
    cout << "ok" << endl;
    return 1;
}

int  efekt_18::load()
{
    cout << "Loading efekt18 ... ";
// -> load
    endtex=texture_library->GetOrCreate("END.JPG");
// <-
    cout << "ok!"<<endl;
    return 1;
}

int  efekt_18::free()  //vrati 3 ako ok, 0 ak doslo k chybe
{
    end=true;
    cout << "Efekt 18 free ... ";
// -> free
// <-
    cout << "ok!" << endl;
    return 3;
}

int  efekt_18::update()
{
    id=Sync[GetSyncNum(float(counter)/float(refresh))].id;
// -> update
    z-=0.0007;
    if (counter/refresh>210)
      {
      if (color>0.0) color-=0.0026;
                else color=0.0;
      nxtcolor=color-0.0026;
      if (nxtcolor<0.0) nxtcolor=0.0;
      }
// <-
    return 1;
}  

int efekt_18::go(double t)
{
if (t<ZACIATOK18) return 1;
if (end) return 3;
if (counter>=KONIEC18*refresh) return free();

if (!start) if (!init()) return 0;
int cur_frm=(int)(t*refresh);
if (cur_frm>KONIEC18*refresh) cur_frm=int(KONIEC18*refresh);
if (cur_frm>counter)
  while (counter<cur_frm)
    {
    counter++;
    if (counter<KONIEC18*refresh) update();
    }
if (counter>=KONIEC18*refresh) return free();
//tu sa kresli->

  glClearColor(nxtcolor,nxtcolor,nxtcolor,1.0);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  glEnable(GL_TEXTURE_2D);
  glDisable(GL_LIGHTING);
  glDisable(GL_CULL_FACE);
  glDisable(GL_BLEND);
  glDisable(GL_FOG);

  endtex->GL();


  float zz=1.0/z;
  glBegin(GL_QUADS);
    glColor3f(color,color,color);

    glTexCoord2f(1,1);
    glVertex2f(zz,zz);

    glTexCoord2f(0,1);
    glVertex2f(-zz,zz);

    glTexCoord2f(0,0);
    glVertex2f(-zz,-zz);

    glTexCoord2f(1,0);
    glVertex2f(zz,-zz); 
  glEnd();

//<-
return 2;
}