#include "ZGo.h"



/*
  //  Create internal data
  im1 = new ZGoIMAGE(zgo, "image1.jpg", 512, 240);
  im2 = new ZGoIMAGE(zgo, "image2.jpg", 512, 240);
  alpha = new ZGoREAL(zgo);
  //  Create internal functions
  sine = new ZGoSine(zgo,
               (new ZGoPARAMS(zgo))->add("time", input->get("time")),
               (new ZGoPARAMS(zgo))->add("result", alpha)
             );
  fader = new ZGoFader(zgo,
                (new ZGoPARAMS(zgo))->add("image1", im1)->add("image2", im2)->add("alpha", alpha),
                (new ZGoPARAMS(zgo))->add("result", output->get("result"))
              );
*/


ZGoMain::ZGoMain(ZGo *zgo, ZGoPARAMS *input, ZGoPARAMS *output)
{
  this->zgo = zgo;
  this->input = input->setInput(this);
  this->output = output->setOutput(this);

  this->fnType = new ZGoNAME(zgo, "ZGoMain");

  //  Create internal data

  time = (ZGoREAL *)input->get("time");
  result = (ZGoIMAGE *)output->get("result");

  camera = new ZGoCAMERA(zgo);

  obj = new ZGo3DOBJECT(zgo);
  obj->setOutput(this);

  obj->pqtorus(1, 1, 0);

  m1 = new ZGoMATRIX(zgo);
  m2 = new ZGoMATRIX(zgo);
  m3 = new ZGoMATRIX(zgo);
  im = new ZGoIMAGE(zgo, 128, 128);

  env = new ZGoIMAGE(zgo, 256, 256);
  env->env();

  //  Create internal functions

  render = new ZGoRender(zgo,
                 (new ZGoPARAMS(zgo))->add("object", obj)->add("camera", camera)->add("env", env),
                 (new ZGoPARAMS(zgo))->add("result", im)
               );
}

void ZGoMain::process()
{
  time->process();

  //  Do object

  m1->rotateYMatrix(time->value*0.39786);
  m2->rotateXMatrix(time->value*0.17237);
  m2->transform(m1, m3);
  obj->transform(m3);
  im->process();

  double x = time->value*0.10325;
  double y = time->value*0.11235;

  x -= (int)x;
  y -= (int)y;

  //  Put tiles

//  result->clear(0x00336699);

  for(int j = -128; j <= result->h; j += 128)
    for(int i = -128; i <= result->w; i += 128)
      result->putImage(im, i + (int)(x*128), j + (int)(y*128));

//  result->copy(im);
}
